2020-04-15 17:50:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-01 11:36:01 +00:00
|
|
|
"encoding/base64"
|
2020-04-18 00:09:09 +00:00
|
|
|
"encoding/hex"
|
2020-04-15 17:50:46 +00:00
|
|
|
"fmt"
|
2020-04-18 00:09:09 +00:00
|
|
|
|
2020-04-15 17:50:46 +00:00
|
|
|
commcid "github.com/filecoin-project/go-fil-commcid"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-12-01 11:36:01 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-04-15 17:50:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var commpToCidCmd = &cli.Command{
|
|
|
|
Name: "commp-to-cid",
|
2020-12-01 11:36:01 +00:00
|
|
|
Usage: "Convert commP to Cid",
|
2020-04-15 17:50:46 +00:00
|
|
|
Description: "Convert a raw commP to a piece-Cid",
|
2020-12-01 11:36:01 +00:00
|
|
|
ArgsUsage: "[data]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "encoding",
|
|
|
|
Value: "base64",
|
|
|
|
Usage: "specify input encoding to parse",
|
|
|
|
},
|
|
|
|
},
|
2020-04-15 17:50:46 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-04-18 00:09:09 +00:00
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must specify commP to convert")
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:36:01 +00:00
|
|
|
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"))
|
2020-04-18 00:09:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 11:36:01 +00:00
|
|
|
cid, err := commcid.PieceCommitmentV1ToCID(dec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(cid)
|
2020-04-15 17:50:46 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|