From f235fe29fa5de194de6fd76a25f963061619fb2e Mon Sep 17 00:00:00 2001 From: Unique Divine <51418232+Unique-Divine@users.noreply.github.com> Date: Fri, 29 Dec 2023 16:51:33 -0600 Subject: [PATCH] refactor(decode-base64): clean code improvements and fn docs (#1754) --- CHANGELOG.md | 1 + cmd/nibid/cmd/decode_base64.go | 84 ++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91129e1fe..13fec3c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1723](https://github.com/NibiruChain/nibiru/pull/1723) - ci(e2e-wasm.yml): rm unused workflow * [#1728](https://github.com/NibiruChain/nibiru/pull/1728) - test(devgas-cli): CLI tests for devgas txs * [#1735](https://github.com/NibiruChain/nibiru/pull/1735) - test(sim): fix simulation tests +* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs ### Dependencies - Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750)) diff --git a/cmd/nibid/cmd/decode_base64.go b/cmd/nibid/cmd/decode_base64.go index 968f07679..3de3706f2 100644 --- a/cmd/nibid/cmd/decode_base64.go +++ b/cmd/nibid/cmd/decode_base64.go @@ -15,18 +15,39 @@ import ( wasmvm "github.com/CosmWasm/wasmvm/types" ) -// YieldStargateMsgs parses the JSON and sends wasmvm.StargateMsg objects to a channel -func YieldStargateMsgs(jsonBz []byte) ([]wasmvm.StargateMsg, error) { +// YieldStargateMsgs takes a byte slice of JSON data and converts it into a slice +// of wasmvm.StargateMsg objects. This function is essential for processing +// JSON-formatted messages that contain base64-encoded protobuf messages. +// +// Args: +// - jsonBz []byte: A byte slice containing the JSON data to be parsed. +// +// Returns: +// - sgMsgs []wasmvm.StargateMsg: A slice of wasmvm.StargateMsg objects parsed +// from the provided JSON data. +// - err error: An error object, which is nil if the operation is successful. +func YieldStargateMsgs(jsonBz []byte) (sgMsgs []wasmvm.StargateMsg, err error) { var data interface{} if err := json.Unmarshal(jsonBz, &data); err != nil { - return nil, err + return sgMsgs, err } - var msgs []wasmvm.StargateMsg - parseStargateMsgs(data, &msgs) - return msgs, nil + parseStargateMsgs(data, &sgMsgs) + return sgMsgs, nil } +// parseStargateMsgs is a recursive function used by YieldStargateMsgs to +// traverse the JSON data, filter for any protobuf.Any messages in the +// "WasmVM.StargateMsg" format and decode them from base64 back to human-readable +// form as JSON objects. +// +// Args: +// - jsonData any: JSON data to parse. According to the JSON specification, +// possible value types are: +// Null, Bool, Number(f64), String, Array, or Object(Map) +// - msgs *[]wasmvm.StargateMsg: Mutable reference to a slice of protobuf +// messages. These are potentially altered in place if the value is an +// encoded base 64 string. func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) { switch v := jsonData.(type) { case map[string]interface{}: @@ -48,33 +69,51 @@ func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) { } } +// StargateMsgDecoded is a struct designed to hold a decoded version of a +// "wasmvm.StargateMsg". type StargateMsgDecoded struct { TypeURL string `json:"type_url"` Value string `json:"value"` } +// DecodeBase64StargateMsgs decodes a series of base64-encoded +// wasmvm.StargateMsg objects from the provided JSON byte slice (jsonBz). +// This function is vital for extracting and interpreting the contents of these +// protobuf messages. +// +// Args: +// - jsonBz []byte: JSON data containing potential base64-encoded messages. +// - clientCtx client.Context: Context for the `nibid` CLI. +// +// Returns: +// - newSgMsgs []StargateMsgDecoded: The decoded stargate messages. +// - err error: An error object, which is nil if the operation is successful. func DecodeBase64StargateMsgs( - jsonBz []byte, context client.Context, + jsonBz []byte, clientCtx client.Context, ) (newSgMsgs []StargateMsgDecoded, err error) { - codec := context.Codec + codec := clientCtx.Codec var data interface{} if err := json.Unmarshal(jsonBz, &data); err != nil { - return []StargateMsgDecoded{}, err + return newSgMsgs, fmt.Errorf( + "failed to decode stargate msgs due to invalid JSON: %w", err) } sgMsgs, err := YieldStargateMsgs(jsonBz) if err != nil { - return + return newSgMsgs, err } for _, sgMsg := range sgMsgs { valueStr := string(sgMsg.Value) - value := strings.Replace(string(sgMsg.Value), `\"`, `"`, -1) - value = strings.Replace(value, `"{`, `{`, -1) - value = strings.Replace(value, `}"`, `}`, -1) + replacer := strings.NewReplacer( + `\"`, `"`, // old, new + `"{`, `{`, + `}"`, `}`, + ) + value := replacer.Replace(string(sgMsg.Value)) if _, err := base64.StdEncoding.DecodeString(valueStr); err == nil { - protoMsg, err := context.InterfaceRegistry.Resolve(sgMsg.TypeURL) + protoMsg, err := clientCtx.InterfaceRegistry.Resolve(sgMsg.TypeURL) if err != nil { return newSgMsgs, err } @@ -92,9 +131,13 @@ func DecodeBase64StargateMsgs( return newSgMsgs, err } - newSgMsgs = append(newSgMsgs, StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)}) + newSgMsgs = append(newSgMsgs, + StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)}, + ) } else if _, err := json.Marshal(value); err == nil { - newSgMsgs = append(newSgMsgs, StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)}) + newSgMsgs = append(newSgMsgs, + StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)}, + ) } else { return newSgMsgs, fmt.Errorf( "parse error: encountered wasmvm.StargateMsg with unexpected format: %s", sgMsg) @@ -103,7 +146,9 @@ func DecodeBase64StargateMsgs( return newSgMsgs, nil } -// DecodeBase64Cmd creates a cobra command for base64 decoding. +// DecodeBase64Cmd creates a Cobra command used to decode base64-encoded protobuf +// messages from a JSON input. This function enables users to input arbitrary +// JSON strings and parse the contents of base-64 encoded protobuf.Any messages. func DecodeBase64Cmd(defaultNodeHome string) *cobra.Command { cmd := &cobra.Command{ Use: "base64-decode", @@ -114,8 +159,9 @@ The input should be a JSON object with 'type_url' and 'value' fields.`, clientCtx := client.GetClientContextFromCmd(cmd) outMessage, err := DecodeBase64StargateMsgs([]byte(args[0]), clientCtx) - fmt.Println(outMessage) - + if err == nil { + fmt.Println(outMessage) + } return err }, }