-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cid.go
85 lines (78 loc) · 1.89 KB
/
cid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)
var cidCmd = &cli.Command{
Name: "cid",
Usage: "Cid command",
Subcommands: cli.Commands{
cidIdCmd,
},
}
var cidIdCmd = &cli.Command{
Name: "id",
Usage: "Create identity CID from hex or base64 data",
ArgsUsage: "[data]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "encoding",
Aliases: []string{"e"},
Value: "base64",
Usage: "specify input encoding to parse",
},
&cli.StringFlag{
Name: "codec",
Value: "id",
Usage: "multicodec-packed content types: abi or id",
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return fmt.Errorf("must specify data")
}
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", "x":
data, err := hex.DecodeString(cctx.Args().First())
if err != nil {
return xerrors.Errorf("decoding hex value: %w", err)
}
dec = data
case "raw", "r":
dec = []byte(cctx.Args().First())
default:
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
}
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":
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"))
}
return nil
},
}