2020-11-04 15:20:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2022-05-26 19:14:29 +00:00
|
|
|
"os"
|
2022-06-23 20:31:15 +00:00
|
|
|
"text/tabwriter"
|
2022-05-26 19:14:29 +00:00
|
|
|
|
2020-11-04 15:20:57 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-06-23 20:31:15 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
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"
|
2022-06-23 20:31:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
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,
|
2022-06-23 20:31:15 +00:00
|
|
|
inspectBundleCmd,
|
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",
|
2020-12-02 00:41:58 +00:00
|
|
|
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)
|
2020-12-02 00:41:58 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
2022-05-26 19:14:29 +00:00
|
|
|
|
2022-06-23 20:31:15 +00:00
|
|
|
var inspectBundleCmd = &cli.Command{
|
|
|
|
Name: "inspect-bundle",
|
|
|
|
Usage: "Get the manifest CID from a car file, as well as the actor code CIDs",
|
2022-05-26 19:14:29 +00:00
|
|
|
ArgsUsage: "[path]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-06-08 17:18:50 +00:00
|
|
|
ctx := cctx.Context
|
2022-05-26 19:14:29 +00:00
|
|
|
|
|
|
|
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-23 20:31:15 +00:00
|
|
|
wrapBs := adt.WrapStore(ctx, cbor.NewCborStore(bs))
|
2022-05-26 19:14:29 +00:00
|
|
|
|
2022-06-02 02:12:50 +00:00
|
|
|
hdr, err := car.LoadCar(ctx, bs, f)
|
2022-05-26 19:14:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error loading car file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestCid := hdr.Roots[0]
|
|
|
|
|
|
|
|
fmt.Printf("Manifest CID: %s\n", manifestCid.String())
|
|
|
|
|
2022-06-23 20:31:15 +00:00
|
|
|
entries, err := actors.ReadManifest(ctx, wrapBs, manifestCid)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error loading manifest: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tw := tabwriter.NewWriter(os.Stdout, 2, 4, 2, ' ', 0)
|
|
|
|
_, _ = fmt.Fprintln(tw, "\nActor\tCID\t")
|
|
|
|
|
|
|
|
for name, cid := range entries {
|
|
|
|
_, _ = fmt.Fprintf(tw, "%v\t%v\n", name, cid)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return tw.Flush()
|
2022-05-26 19:14:29 +00:00
|
|
|
},
|
|
|
|
}
|