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 all 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: "id",
Usage: "multicodec-packed content types: abi or id",
},
},
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 "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
},
}