lotus/cmd/lotus-shed/base64.go

74 lines
1.2 KiB
Go
Raw Normal View History

2021-03-24 03:32:56 +00:00
package main
import (
"encoding/base64"
"fmt"
"io"
"os"
"strings"
2022-06-14 15:00:51 +00:00
"github.com/urfave/cli/v2"
2021-03-24 03:32:56 +00:00
"github.com/filecoin-project/go-address"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/go-state-types/abi"
2021-03-24 03:32:56 +00:00
)
var base64Cmd = &cli.Command{
Name: "base64",
Description: "multiformats base64",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "decodeAddr",
Value: false,
Usage: "Decode a base64 addr",
},
&cli.BoolFlag{
Name: "decodeBig",
Value: false,
Usage: "Decode a base64 big",
},
},
Action: func(cctx *cli.Context) error {
var input io.Reader
2022-09-14 18:33:29 +00:00
if cctx.NArg() == 0 {
2021-03-24 03:32:56 +00:00
input = os.Stdin
} else {
input = strings.NewReader(cctx.Args().First())
}
bytes, err := io.ReadAll(input)
2021-03-24 03:32:56 +00:00
if err != nil {
return nil
}
decoded, err := base64.RawStdEncoding.DecodeString(strings.TrimSpace(string(bytes)))
if err != nil {
return err
}
if cctx.Bool("decodeAddr") {
addr, err := address.NewFromBytes(decoded)
if err != nil {
return err
}
fmt.Println(addr)
return nil
}
if cctx.Bool("decodeBig") {
var val abi.TokenAmount
err = val.UnmarshalBinary(decoded)
if err != nil {
return err
}
fmt.Println(val)
}
return nil
},
}