diff --git a/cli/chain.go b/cli/chain.go index 984d55828..f4fcab5aa 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -19,6 +19,7 @@ var chainCmd = &cli.Command{ chainHeadCmd, chainGetBlock, chainReadObjCmd, + chainGetMsgCmd, }, } @@ -160,3 +161,46 @@ var chainReadObjCmd = &cli.Command{ return nil }, } + +var chainGetMsgCmd = &cli.Command{ + Name: "getmessage", + Usage: "Get and print a message by its cid", + Action: func(cctx *cli.Context) error { + api, closer, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + ctx := ReqContext(cctx) + + c, err := cid.Decode(cctx.Args().First()) + if err != nil { + return xerrors.Errorf("failed to parse cid input: %w", err) + } + + mb, err := api.ChainReadObj(ctx, c) + if err != nil { + return xerrors.Errorf("failed to read object: %w", err) + } + + var i interface{} + m, err := types.DecodeMessage(mb) + if err != nil { + sm, err := types.DecodeSignedMessage(mb) + if err != nil { + return xerrors.Errorf("failed to decode object as a message: %w", err) + } + i = sm + } else { + i = m + } + + enc, err := json.MarshalIndent(i, "", " ") + if err != nil { + return err + } + + fmt.Println(string(enc)) + return nil + }, +}