add base64 decode

This commit is contained in:
zgfzgf 2020-12-01 19:36:01 +08:00
parent 3e143cac4b
commit ec08e27af2

View File

@ -1,27 +1,55 @@
package main package main
import ( import (
"encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
commcid "github.com/filecoin-project/go-fil-commcid" commcid "github.com/filecoin-project/go-fil-commcid"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/xerrors"
) )
var commpToCidCmd = &cli.Command{ var commpToCidCmd = &cli.Command{
Name: "commp-to-cid", Name: "commp-to-cid",
Usage: "Convert commP to Cid",
Description: "Convert a raw commP to a piece-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 { Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() { if !cctx.Args().Present() {
return fmt.Errorf("must specify commP to convert") return fmt.Errorf("must specify commP to convert")
} }
dec, err := hex.DecodeString(cctx.Args().First()) var dec []byte
if err != nil { switch cctx.String("encoding") {
return fmt.Errorf("failed to decode input as hex string: %w", err) 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 return nil
}, },
} }