Add a util to decode base64 inputs
This commit is contained in:
parent
c41777dcd2
commit
fdb69fdd71
75
cmd/lotus-shed/base64.go
Normal file
75
cmd/lotus-shed/base64.go
Normal file
@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/filecoin-project/go-state-types/abi"
|
||||
|
||||
"github.com/filecoin-project/go-address"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
if cctx.Args().Len() == 0 {
|
||||
input = os.Stdin
|
||||
} else {
|
||||
input = strings.NewReader(cctx.Args().First())
|
||||
}
|
||||
|
||||
bytes, err := ioutil.ReadAll(input)
|
||||
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
|
||||
},
|
||||
}
|
@ -16,6 +16,7 @@ func main() {
|
||||
logging.SetLogLevel("*", "INFO")
|
||||
|
||||
local := []*cli.Command{
|
||||
base64Cmd,
|
||||
base32Cmd,
|
||||
base16Cmd,
|
||||
bitFieldCmd,
|
||||
|
Loading…
Reference in New Issue
Block a user