Add a command to encode JSON params

This commit is contained in:
Aayush Rajasekaran 2021-01-08 01:25:24 -05:00
parent 02f33e2b8e
commit b33bf16f70
2 changed files with 88 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path"
"reflect"
"sort"
"strconv"
"strings"
@ -56,6 +57,7 @@ var chainCmd = &cli.Command{
chainGasPriceCmd,
chainInspectUsage,
chainDecodeCmd,
chainEncodeCmd,
},
}
@ -1311,3 +1313,86 @@ var chainDecodeParamsCmd = &cli.Command{
return nil
},
}
var chainEncodeCmd = &cli.Command{
Name: "encode",
Usage: "encode various types",
Subcommands: []*cli.Command{
chainEncodeParamsCmd,
},
}
var chainEncodeParamsCmd = &cli.Command{
Name: "params",
Usage: "Encodes the given JSON params",
ArgsUsage: "[toAddr method params]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
},
&cli.StringFlag{
Name: "encoding",
Value: "base64",
Usage: "specify input encoding to parse",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
if cctx.Args().Len() != 3 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
}
to, err := address.NewFromString(cctx.Args().First())
if err != nil {
return xerrors.Errorf("parsing toAddr: %w", err)
}
method, err := strconv.ParseInt(cctx.Args().Get(1), 10, 64)
if err != nil {
return xerrors.Errorf("parsing method id: %w", err)
}
ts, err := LoadTipSet(ctx, cctx, api)
if err != nil {
return err
}
act, err := api.StateGetActor(ctx, to, ts.Key())
if err != nil {
return xerrors.Errorf("getting actor: %w", err)
}
methodMeta, found := stmgr.MethodsMap[act.Code][abi.MethodNum(method)]
if !found {
return fmt.Errorf("method %d not found on actor %s", method, act.Code)
}
p := reflect.New(methodMeta.Params.Elem()).Interface().(cbg.CBORMarshaler)
if err := json.Unmarshal([]byte(cctx.Args().Get(2)), p); err != nil {
return fmt.Errorf("unmarshaling input into params type: %w", err)
}
buf := new(bytes.Buffer)
if err := p.MarshalCBOR(buf); err != nil {
return err
}
switch cctx.String("encoding") {
case "base64":
fmt.Println(base64.StdEncoding.EncodeToString(buf.Bytes()))
case "hex":
fmt.Println(hex.EncodeToString(buf.Bytes()))
default:
return xerrors.Errorf("unrecognized encoding: %s", cctx.String("encoding"))
}
return nil
},
}

View File

@ -670,7 +670,7 @@ var actorSetOwnerCmd = &cli.Command{
return err
}
fromAddrId, err := api.StateLookupID(ctx, na, types.EmptyTSK)
fromAddrId, err := api.StateLookupID(ctx, fa, types.EmptyTSK)
if err != nil {
return err
}
@ -717,6 +717,8 @@ var actorSetOwnerCmd = &cli.Command{
if wait.Receipt.ExitCode != 0 {
fmt.Println("owner change failed!")
return err
} else {
fmt.Println("message succeeded!")
}
return nil