-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
address.go
64 lines (56 loc) · 1.22 KB
/
address.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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
)
var addressCmd = &cli.Command{
Name: "addr",
Usage: "interact with filecoin addresses",
Subcommands: cli.Commands{
addrDecode,
addrEncode,
},
}
var addrDecode = &cli.Command{
Name: "decode",
Usage: "decode hex bytes into address",
Action: func(cctx *cli.Context) error {
addrHex := cctx.Args().First()
bs, err := hex.DecodeString(addrHex)
if err != nil {
return err
}
// first try cbor
var a address.Address
err = a.UnmarshalCBOR((bytes.NewReader(bs)))
if err != nil {
fmt.Printf("failed to unmarshal as CBOR, trying raw\n")
} else {
fmt.Printf("%s\n", a)
return nil
}
// next try raw payload
a, err = address.NewFromBytes(bs)
if err != nil {
return xerrors.New("could not decode as CBOR or raw payload, failing")
}
fmt.Printf("%s\n", a)
return nil
},
}
var addrEncode = &cli.Command{
Name: "encode",
Usage: "encode address to hex bytes",
Action: func(cctx *cli.Context) error {
addr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
fmt.Printf("%x\n", addr.Bytes())
return nil
},
}