From ec08e27af21de399fc2f772e78947e2ee7a57879 Mon Sep 17 00:00:00 2001 From: zgfzgf <1901989065@qq.com> Date: Tue, 1 Dec 2020 19:36:01 +0800 Subject: [PATCH] add base64 decode --- cmd/lotus-shed/commp.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/cmd/lotus-shed/commp.go b/cmd/lotus-shed/commp.go index 9b0cab75d..6f7923c24 100644 --- a/cmd/lotus-shed/commp.go +++ b/cmd/lotus-shed/commp.go @@ -1,27 +1,55 @@ package main import ( + "encoding/base64" "encoding/hex" "fmt" commcid "github.com/filecoin-project/go-fil-commcid" "github.com/urfave/cli/v2" + "golang.org/x/xerrors" ) var commpToCidCmd = &cli.Command{ Name: "commp-to-cid", + Usage: "Convert commP to Cid", Description: "Convert a raw commP to a piece-Cid", + 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 commP to convert") } - dec, err := hex.DecodeString(cctx.Args().First()) - if err != nil { - return fmt.Errorf("failed to decode input as hex string: %w", err) + var dec []byte + switch cctx.String("encoding") { + case "base64": + data, err := base64.StdEncoding.DecodeString(cctx.Args().First()) + if err != nil { + 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")) } - fmt.Println(commcid.PieceCommitmentV1ToCID(dec)) + cid, err := commcid.PieceCommitmentV1ToCID(dec) + if err != nil { + return err + } + fmt.Println(cid) return nil }, }