Skip to content

Commit

Permalink
refactor(decode-base64): clean code improvements and fn docs (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Dec 29, 2023
1 parent d8641fb commit f235fe2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
84 changes: 65 additions & 19 deletions cmd/nibid/cmd/decode_base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Value>)
// - 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{}:
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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",
Expand All @@ -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
},
}
Expand Down

0 comments on commit f235fe2

Please sign in to comment.