lotus/cmd/lotus-shed/cid.go

120 lines
2.6 KiB
Go
Raw Normal View History

2020-11-04 15:20:57 +00:00
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"
2020-11-04 15:20:57 +00:00
"github.com/ipfs/go-cid"
2022-06-14 15:00:51 +00:00
"github.com/ipld/go-car"
2020-11-04 15:20:57 +00:00
mh "github.com/multiformats/go-multihash"
2020-12-01 08:50:53 +00:00
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/blockstore"
2020-11-04 15:20:57 +00:00
)
var cidCmd = &cli.Command{
2020-12-01 08:50:53 +00:00
Name: "cid",
Usage: "Cid command",
2020-11-04 15:20:57 +00:00
Subcommands: cli.Commands{
cidIdCmd,
cidFromCarCmd,
2020-11-04 15:20:57 +00:00
},
}
var cidIdCmd = &cli.Command{
2020-12-01 08:50:53 +00:00
Name: "id",
Usage: "Create identity CID from hex or base64 data",
ArgsUsage: "[data]",
Flags: []cli.Flag{
&cli.StringFlag{
2021-08-10 14:47:55 +00:00
Name: "encoding",
Aliases: []string{"e"},
Value: "base64",
Usage: "specify input encoding to parse",
2020-12-01 08:50:53 +00:00
},
2020-12-01 14:53:24 +00:00
&cli.StringFlag{
Name: "codec",
Value: "id",
Usage: "multicodec-packed content types: abi or id",
2020-12-01 14:53:24 +00:00
},
2020-12-01 08:50:53 +00:00
},
2020-11-04 15:20:57 +00:00
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must specify data")
}
2020-12-01 08:50:53 +00:00
var dec []byte
switch cctx.String("encoding") {
case "base64":
data, err := base64.StdEncoding.DecodeString(cctx.Args().First())
2020-11-04 15:20:57 +00:00
if err != nil {
2020-12-01 08:50:53 +00:00
return xerrors.Errorf("decoding base64 value: %w", err)
2020-11-04 15:20:57 +00:00
}
2020-12-01 08:50:53 +00:00
dec = data
2021-08-10 14:47:55 +00:00
case "hex", "x":
2020-12-01 08:50:53 +00:00
data, err := hex.DecodeString(cctx.Args().First())
if err != nil {
return xerrors.Errorf("decoding hex value: %w", err)
}
dec = data
2021-08-10 14:47:55 +00:00
case "raw", "r":
dec = []byte(cctx.Args().First())
2020-12-01 08:50:53 +00:00
default:
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
2020-11-04 15:20:57 +00:00
}
2020-12-01 14:53:24 +00:00
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":
2020-12-01 14:53:24 +00:00
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"))
2020-11-04 15:20:57 +00:00
}
return nil
},
}
var cidFromCarCmd = &cli.Command{
Name: "manifest-cid-from-car",
Usage: "Get the manifest CID from a car file",
ArgsUsage: "[path]",
Action: func(cctx *cli.Context) error {
ctx := cctx.Context
cf := cctx.Args().Get(0)
f, err := os.OpenFile(cf, os.O_RDONLY, 0664)
if err != nil {
return xerrors.Errorf("opening the car file: %w", err)
}
bs := blockstore.NewMemory()
2022-06-02 02:12:50 +00:00
hdr, err := car.LoadCar(ctx, bs, f)
if err != nil {
return xerrors.Errorf("error loading car file: %w", err)
}
manifestCid := hdr.Roots[0]
fmt.Printf("Manifest CID: %s\n", manifestCid.String())
return nil
},
}