Merge pull request #10032 from filecoin-project/feat/cbor-cid

feat:shed:add cid to cbor serialization command
This commit is contained in:
Łukasz Magiera 2023-01-18 12:51:05 +01:00 committed by GitHub
commit 0934cf034d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
@ -12,6 +13,7 @@ import (
"github.com/ipld/go-car"
mh "github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-state-types/abi"
@ -27,6 +29,42 @@ var cidCmd = &cli.Command{
Subcommands: cli.Commands{
cidIdCmd,
inspectBundleCmd,
cborCid,
cidBytes,
},
}
var cidBytes = &cli.Command{
Name: "bytes",
Usage: "cid bytes",
ArgsUsage: "[cid]",
Action: func(cctx *cli.Context) error {
c, err := cid.Decode(cctx.Args().First())
if err != nil {
return err
}
// Add in the troublesome zero byte prefix
fmt.Printf("00%x\n", c.Bytes())
return nil
},
}
var cborCid = &cli.Command{
Name: "cbor",
Usage: "Serialize cid to cbor",
ArgsUsage: "[cid]",
Action: func(cctx *cli.Context) error {
c, err := cid.Decode(cctx.Args().First())
if err != nil {
return err
}
cbgc := cbg.CborCid(c)
buf := bytes.NewBuffer(make([]byte, 0))
if err := cbgc.MarshalCBOR(buf); err != nil {
return err
}
fmt.Printf("%x\n", buf.Bytes())
return nil
},
}