Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat shed cid #5072

Merged
merged 4 commits into from
Dec 8, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions cmd/lotus-shed/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,67 @@ import (
"encoding/hex"
"fmt"

"github.com/urfave/cli/v2"

"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)

var cidCmd = &cli.Command{
Name: "cid",
Name: "cid",
Usage: "Cid command",
Subcommands: cli.Commands{
cidIdCmd,
},
}

var cidIdCmd = &cli.Command{
Name: "id",
Usage: "create identity CID from hex or base64 data",
Name: "id",
Usage: "Create identity CID from hex or base64 data",
ArgsUsage: "[data]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "encoding",
Value: "base64",
Usage: "specify input encoding to parse",
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must specify data")
}

dec, err := hex.DecodeString(cctx.Args().First())
if err != nil {
dec, err = base64.StdEncoding.DecodeString(cctx.Args().First())
var dec []byte
switch cctx.String("encoding") {
case "base64":
data, err := base64.StdEncoding.DecodeString(cctx.Args().First())
if err != nil {
return err
return xerrors.Errorf("decoding base64 value: %w", err)
}

dec = data
case "hex":
data, err := hex.DecodeString(cctx.Args().First())
if err != nil {
return xerrors.Errorf("decoding hex value: %w", err)
}
dec = data
default:
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
}

builder := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}

c, err := builder.Sum(dec)
rCid, err := builder.Sum(dec)
if err != nil {
return err
}
fmt.Printf("Raw Cid:%s\n", rCid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sometimes use this output in scripts, I'd print a single CID without a prefix like before, and add a flag to select a CID builder (--builder=[id,abi])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK
./lotus-shed cid id --encoding hex 313233343536
bafy2bzacebbwdptcaaoslxvsxwc7vm7emai27lsxkoicnwgtpvl7ixrjk4jhc
./lotus-shed cid id --encoding hex --codec raw 313233343536
bafkqabrrgiztinjw


fmt.Println(c)
aCid, err := abi.CidBuilder.Sum(dec)
if err != nil {
return err
}
fmt.Printf("Abi Cid:%s\n", aCid)
return nil
},
}