From 2347ba6f39e37d1523cd36ddb80bad7d747bef67 Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:04:28 -0700 Subject: [PATCH 1/7] print "client"-command CIDs as base32-encoded strs Fixes #916 --- cli/client.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/cli/client.go b/cli/client.go index 3bfc5de70..5c8e7a957 100644 --- a/cli/client.go +++ b/cli/client.go @@ -9,6 +9,7 @@ import ( "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" + "github.com/multiformats/go-multibase" "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" @@ -67,7 +68,14 @@ var clientImportCmd = &cli.Command{ if err != nil { return err } - fmt.Println(c.String()) + + encoded, err := c.StringOfBase(multibase.Base32) + if err != nil { + return err + } + + fmt.Println(encoded) + return nil }, } @@ -150,7 +158,12 @@ var clientLocalCmd = &cli.Command{ return err } for _, v := range list { - fmt.Printf("%s %s %d %s\n", v.Key, v.FilePath, v.Size, v.Status) + encoded, err := v.Key.StringOfBase(multibase.Base32) + if err != nil { + return err + } + + fmt.Printf("%s %s %d %s\n", encoded, v.FilePath, v.Size, v.Status) } return nil }, From b3eef76cddc53005b0e4c36eb05feab7a42b7a96 Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:13:16 -0700 Subject: [PATCH 2/7] client commP outputs Base32-encoded string --- cli/client.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cli/client.go b/cli/client.go index 5c8e7a957..1750aa7bb 100644 --- a/cli/client.go +++ b/cli/client.go @@ -102,11 +102,16 @@ var clientCommPCmd = &cli.Command{ } ret, err := api.ClientCalcCommP(ctx, cctx.Args().Get(0), miner) - if err != nil { return err } - fmt.Println("CID: ", ret.Root) + + encoded, err := ret.Root.StringOfBase(multibase.Base32) + if err != nil { + return err + } + + fmt.Println("CID: ", encoded) fmt.Println("Piece size: ", ret.Size) return nil }, From 55a9c9ae0a119991c5e70333540ad216177408b2 Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:13:33 -0700 Subject: [PATCH 3/7] client deal command outputs Base32-encoded CID --- cli/client.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cli/client.go b/cli/client.go index 1750aa7bb..afd133523 100644 --- a/cli/client.go +++ b/cli/client.go @@ -281,7 +281,13 @@ var clientDealCmd = &cli.Command{ return err } - fmt.Println(proposal) + encoded, err := proposal.StringOfBase(multibase.Base32) + if err != nil { + return err + } + + fmt.Println(encoded) + return nil }, } From 28e454fd4aeaf14d1d586bc8b668defaa8b1baba Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:13:53 -0700 Subject: [PATCH 4/7] go mod tidy --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 89818fce5..d83e6a845 100644 --- a/go.mod +++ b/go.mod @@ -95,6 +95,7 @@ require ( github.com/multiformats/go-multiaddr v0.2.2 github.com/multiformats/go-multiaddr-dns v0.2.0 github.com/multiformats/go-multiaddr-net v0.1.5 + github.com/multiformats/go-multibase v0.0.2 github.com/multiformats/go-multihash v0.0.13 github.com/opentracing/opentracing-go v1.1.0 github.com/stretchr/objx v0.2.0 // indirect From b167e73acb0c0aeb0f88e90b21f8747ae432dd7d Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:33:45 -0700 Subject: [PATCH 5/7] allow CLI consumer to specify CID encoding for output --- cli/client.go | 26 +++++++++++++------------- cli/cmd.go | 21 +++++++++++++++++++++ cmd/lotus/main.go | 7 +++++++ go.mod | 1 + go.sum | 2 ++ 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/cli/client.go b/cli/client.go index afd133523..49002d67e 100644 --- a/cli/client.go +++ b/cli/client.go @@ -9,7 +9,6 @@ import ( "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" - "github.com/multiformats/go-multibase" "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" @@ -69,12 +68,12 @@ var clientImportCmd = &cli.Command{ return err } - encoded, err := c.StringOfBase(multibase.Base32) + encoder, err := GetCidEncoder(cctx) if err != nil { return err } - fmt.Println(encoded) + fmt.Println(encoder.Encode(c)) return nil }, @@ -106,12 +105,12 @@ var clientCommPCmd = &cli.Command{ return err } - encoded, err := ret.Root.StringOfBase(multibase.Base32) + encoder, err := GetCidEncoder(cctx) if err != nil { return err } - fmt.Println("CID: ", encoded) + fmt.Println("CID: ", encoder.Encode(ret.Root)) fmt.Println("Piece size: ", ret.Size) return nil }, @@ -162,13 +161,14 @@ var clientLocalCmd = &cli.Command{ if err != nil { return err } - for _, v := range list { - encoded, err := v.Key.StringOfBase(multibase.Base32) - if err != nil { - return err - } - fmt.Printf("%s %s %d %s\n", encoded, v.FilePath, v.Size, v.Status) + encoder, err := GetCidEncoder(cctx) + if err != nil { + return err + } + + for _, v := range list { + fmt.Printf("%s %s %d %s\n", encoder.Encode(v.Key), v.FilePath, v.Size, v.Status) } return nil }, @@ -281,12 +281,12 @@ var clientDealCmd = &cli.Command{ return err } - encoded, err := proposal.StringOfBase(multibase.Base32) + encoder, err := GetCidEncoder(cctx) if err != nil { return err } - fmt.Println(encoded) + fmt.Println(encoder.Encode(*proposal)) return nil }, diff --git a/cli/cmd.go b/cli/cmd.go index fd0ab1e78..2d4ff7a75 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -9,6 +9,9 @@ import ( "strings" "syscall" + "github.com/multiformats/go-multibase" + + cidenc "github.com/ipfs/go-cidutil/cidenc" logging "github.com/ipfs/go-log/v2" "github.com/mitchellh/go-homedir" "github.com/multiformats/go-multiaddr" @@ -193,6 +196,24 @@ func DaemonContext(cctx *cli.Context) context.Context { return context.Background() } +// GetCidEncoder returns an encoder using the cid-base flag if provided, or +// the default (Base32) encoder if not. +func GetCidEncoder(cctx *cli.Context) (cidenc.Encoder, error) { + val := cctx.String("cid-base") + + e := cidenc.Default() + + if val != "" { + var err error + e.Base, err = multibase.EncoderByName(val) + if err != nil { + return e, err + } + } + + return e, nil +} + // ReqContext returns context for cli execution. Calling it for the first time // installs SIGTERM handler that will close returned context. // Not safe for concurrent execution. diff --git a/cmd/lotus/main.go b/cmd/lotus/main.go index 33ff56ac3..22d121364 100644 --- a/cmd/lotus/main.go +++ b/cmd/lotus/main.go @@ -61,6 +61,13 @@ func main() { Hidden: true, Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME }, + &cli.StringFlag{ + Name: "cid-base", + Hidden: true, + Value: "base32", + Usage: "Multibase encoding used for version 1 CIDs in output.", + DefaultText: "base32", + }, }, Commands: append(local, lcli.Commands...), diff --git a/go.mod b/go.mod index d83e6a845..a41fcec7d 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,7 @@ require ( github.com/ipfs/go-block-format v0.0.2 github.com/ipfs/go-blockservice v0.1.3 github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00 + github.com/ipfs/go-cidutil v0.0.2 github.com/ipfs/go-datastore v0.4.4 github.com/ipfs/go-ds-badger2 v0.1.0 github.com/ipfs/go-filestore v1.0.0 diff --git a/go.sum b/go.sum index 4c4ddf12f..f78d58bd7 100644 --- a/go.sum +++ b/go.sum @@ -345,6 +345,8 @@ github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00 h1:QN88Q0kT2QiDaLxpR/SDsqOBtNIEF/F3n96gSDUimkA= github.com/ipfs/go-cid v0.0.6-0.20200501230655-7c82f3b81c00/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= +github.com/ipfs/go-cidutil v0.0.2 h1:CNOboQf1t7Qp0nuNh8QMmhJs0+Q//bRL1axtCnIB1Yo= +github.com/ipfs/go-cidutil v0.0.2/go.mod h1:ewllrvrxG6AMYStla3GD7Cqn+XYSLqjK0vc+086tB6s= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.0.5/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= From 963e4dd2fc2da19f01ec55681711a16e8720e83e Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 13:52:36 -0700 Subject: [PATCH 6/7] set a Base32 default explicitly --- cli/cmd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/cmd.go b/cli/cmd.go index 2d4ff7a75..82b75801c 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -196,12 +196,12 @@ func DaemonContext(cctx *cli.Context) context.Context { return context.Background() } -// GetCidEncoder returns an encoder using the cid-base flag if provided, or +// GetCidEncoder returns an encoder using the `cid-base` flag if provided, or // the default (Base32) encoder if not. func GetCidEncoder(cctx *cli.Context) (cidenc.Encoder, error) { val := cctx.String("cid-base") - e := cidenc.Default() + e := cidenc.Encoder{Base: multibase.MustNewEncoder(multibase.Base32)} if val != "" { var err error From a80dce2e3ba990cad549b9a6fb7511f50b4e353d Mon Sep 17 00:00:00 2001 From: laser Date: Fri, 5 Jun 2020 14:22:29 -0700 Subject: [PATCH 7/7] move to client command --- cli/client.go | 36 ++++++++++++++++++++++++++++++++++++ cli/cmd.go | 21 --------------------- cmd/lotus/main.go | 7 ------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/cli/client.go b/cli/client.go index 49002d67e..9787df37e 100644 --- a/cli/client.go +++ b/cli/client.go @@ -8,7 +8,9 @@ import ( "text/tabwriter" "github.com/ipfs/go-cid" + "github.com/ipfs/go-cidutil/cidenc" "github.com/libp2p/go-libp2p-core/peer" + "github.com/multiformats/go-multibase" "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" @@ -21,6 +23,32 @@ import ( "github.com/filecoin-project/lotus/chain/types" ) +var CidBaseFlag = cli.StringFlag{ + Name: "cid-base", + Hidden: true, + Value: "base32", + Usage: "Multibase encoding used for version 1 CIDs in output.", + DefaultText: "base32", +} + +// GetCidEncoder returns an encoder using the `cid-base` flag if provided, or +// the default (Base32) encoder if not. +func GetCidEncoder(cctx *cli.Context) (cidenc.Encoder, error) { + val := cctx.String("cid-base") + + e := cidenc.Encoder{Base: multibase.MustNewEncoder(multibase.Base32)} + + if val != "" { + var err error + e.Base, err = multibase.EncoderByName(val) + if err != nil { + return e, err + } + } + + return e, nil +} + var clientCmd = &cli.Command{ Name: "client", Usage: "Make deals, store data, retrieve data", @@ -46,6 +74,7 @@ var clientImportCmd = &cli.Command{ Name: "car", Usage: "import from a car file instead of a regular file", }, + &CidBaseFlag, }, Action: func(cctx *cli.Context) error { api, closer, err := GetFullNodeAPI(cctx) @@ -83,6 +112,9 @@ var clientCommPCmd = &cli.Command{ Name: "commP", Usage: "calculate the piece-cid (commP) of a CAR file", ArgsUsage: "[inputFile minerAddress]", + Flags: []cli.Flag{ + &CidBaseFlag, + }, Action: func(cctx *cli.Context) error { api, closer, err := GetFullNodeAPI(cctx) if err != nil { @@ -149,6 +181,9 @@ var clientCarGenCmd = &cli.Command{ var clientLocalCmd = &cli.Command{ Name: "local", Usage: "List locally imported data", + Flags: []cli.Flag{ + &CidBaseFlag, + }, Action: func(cctx *cli.Context) error { api, closer, err := GetFullNodeAPI(cctx) if err != nil { @@ -196,6 +231,7 @@ var clientDealCmd = &cli.Command{ Usage: "specify the epoch that the deal should start at", Value: -1, }, + &CidBaseFlag, }, Action: func(cctx *cli.Context) error { api, closer, err := GetFullNodeAPI(cctx) diff --git a/cli/cmd.go b/cli/cmd.go index 82b75801c..fd0ab1e78 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -9,9 +9,6 @@ import ( "strings" "syscall" - "github.com/multiformats/go-multibase" - - cidenc "github.com/ipfs/go-cidutil/cidenc" logging "github.com/ipfs/go-log/v2" "github.com/mitchellh/go-homedir" "github.com/multiformats/go-multiaddr" @@ -196,24 +193,6 @@ func DaemonContext(cctx *cli.Context) context.Context { return context.Background() } -// GetCidEncoder returns an encoder using the `cid-base` flag if provided, or -// the default (Base32) encoder if not. -func GetCidEncoder(cctx *cli.Context) (cidenc.Encoder, error) { - val := cctx.String("cid-base") - - e := cidenc.Encoder{Base: multibase.MustNewEncoder(multibase.Base32)} - - if val != "" { - var err error - e.Base, err = multibase.EncoderByName(val) - if err != nil { - return e, err - } - } - - return e, nil -} - // ReqContext returns context for cli execution. Calling it for the first time // installs SIGTERM handler that will close returned context. // Not safe for concurrent execution. diff --git a/cmd/lotus/main.go b/cmd/lotus/main.go index 22d121364..33ff56ac3 100644 --- a/cmd/lotus/main.go +++ b/cmd/lotus/main.go @@ -61,13 +61,6 @@ func main() { Hidden: true, Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME }, - &cli.StringFlag{ - Name: "cid-base", - Hidden: true, - Value: "base32", - Usage: "Multibase encoding used for version 1 CIDs in output.", - DefaultText: "base32", - }, }, Commands: append(local, lcli.Commands...),