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 3 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
66 changes: 50 additions & 16 deletions cmd/lotus-shed/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,78 @@ 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",
},
&cli.StringFlag{
Name: "codec",
Value: "abi",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should keep old default behavior to not break existing scripts

Copy link
Contributor Author

@zgfzgf zgfzgf Dec 2, 2020

Choose a reason for hiding this comment

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

./lotus-shed cid id --encoding hex 313233343536
bafkqabrrgiztinjw

Usage: "multicodec-packed content types: abi or raw",
},
},
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)
if err != nil {
return err
switch cctx.String("codec") {
case "abi":
aCid, err := abi.CidBuilder.Sum(dec)
if err != nil {
return xerrors.Errorf("cidBuilder abi: %w", err)
}
fmt.Println(aCid)
case "raw":
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be id or identity. Raw actually has a slightly different meaning for IPLD data (also, this command is called id)

builder := cid.V1Builder{Codec: cid.Raw, MhType: mh.IDENTITY}
rCid, err := builder.Sum(dec)
if err != nil {
return xerrors.Errorf("cidBuilder raw: %w", err)
}
fmt.Println(rCid)
default:
return xerrors.Errorf("unrecognized codec: %s", cctx.String("codec"))
}

fmt.Println(c)
return nil
},
}