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"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
|
|
|
"github.com/ipld/go-car"
|
2020-11-04 15:20:57 +00:00
|
|
|
|
2020-12-01 09:16:41 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-11-04 15:20:57 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
2020-12-01 08:50:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
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-05-26 19:14:29 +00:00
|
|
|
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",
|
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
|
|
|
|
|
|
|
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 {
|
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-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())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|