lotus/cmd/lotus-shed/address.go

65 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-07-20 15:57:00 +00:00
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/urfave/cli/v2"
2022-07-20 16:27:34 +00:00
"golang.org/x/xerrors"
2022-07-20 15:57:00 +00:00
"github.com/filecoin-project/go-address"
)
var addressCmd = &cli.Command{
Name: "addr",
2023-01-24 07:18:04 +00:00
Usage: "interact with filecoin addresses",
Subcommands: cli.Commands{
addrDecode,
addrEncode,
},
}
var addrDecode = &cli.Command{
Name: "decode",
2022-07-20 15:57:00 +00:00
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 {
2022-07-20 16:27:34 +00:00
return xerrors.New("could not decode as CBOR or raw payload, failing")
2022-07-20 15:57:00 +00:00
}
fmt.Printf("%s\n", a)
return nil
},
}
2023-01-24 07:18:04 +00:00
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
},
}