Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Dec 21, 2023
1 parent d33dfe9 commit de4dc1e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/nibid/cmd/decode_base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

Check failure on line 1 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/NibiruChain/nibiru/cmd/nibid/cmd [github.com/NibiruChain/nibiru/cmd/nibid/cmd.test]

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)

// DecodeBase64Cmd creates a cobra command for base64 decoding.
func DecodeBase64Cmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "base64-decode",
Short: "Decode a base64-encoded protobuf message",
Long: `Decode a base64-encoded protobuf message from JSON input.
The input should be a JSON object with 'type_url' and 'value' fields.`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

protoMsg := &anypb.Any{}
if err := proto.Unmarshal(protoMsgBytes, protoMsg); err != nil {

Check failure on line 22 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

undefined: protoMsgBytes

Check failure on line 22 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

undefined: protoMsgBytes

Check failure on line 22 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

undefined: protoMsgBytes

Check failure on line 22 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / build

undefined: protoMsgBytes

Check failure on line 22 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / integration-tests

undefined: protoMsgBytes

Check failure on line 22 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / unit-tests

undefined: protoMsgBytes
return err
}

clientCtx.PrintProto(protoMsg)
},

Check failure on line 27 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

missing return) (typecheck)

Check failure on line 27 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

missing return (typecheck)

Check failure on line 27 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / lint

missing return) (typecheck)

Check failure on line 27 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / build

missing return

Check failure on line 27 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / integration-tests

missing return

Check failure on line 27 in cmd/nibid/cmd/decode_base64.go

View workflow job for this annotation

GitHub Actions / unit-tests

missing return
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}
81 changes: 81 additions & 0 deletions cmd/nibid/cmd/decode_base64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cmd_test

import (
"context"
"testing"

"github.com/NibiruChain/nibiru/app"

"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"

nibid "github.com/NibiruChain/nibiru/cmd/nibid/cmd"

Check failure on line 16 in cmd/nibid/cmd/decode_base64_test.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/NibiruChain/nibiru/cmd/nibid/cmd (-: # github.com/NibiruChain/nibiru/cmd/nibid/cmd [github.com/NibiruChain/nibiru/cmd/nibid/cmd.test]
)

func TestBase64Decode(t *testing.T) {
type TestCase struct {
name string
json_message string
expectError bool
}

executeTest := func(t *testing.T, testCase TestCase) {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
home := t.TempDir()
logger := log.NewNopLogger()
cfg, err := genutiltest.CreateDefaultTendermintConfig(home)
require.NoError(t, err)

appCodec := app.MakeEncodingConfig().Marshaler
err = genutiltest.ExecInitCmd(
testModuleBasicManager, home, appCodec)
require.NoError(t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)

cmd := nibid.DecodeBase64Cmd(home)
cmd.SetArgs([]string{
tc.json_message,
})

if tc.expectError {
require.Error(t, cmd.ExecuteContext(ctx))
} else {
require.NoError(t, cmd.ExecuteContext(ctx))
}
})
}

testCases := []TestCase{
// {
// name: "empty message",
// json_message: "",
// expectError: true,
// },
{
name: "valid message",
json_message: `
{
"stargate": {
"type_url": "/cosmos.staking.v1beta1.MsgUndelegate",
"value": "Cj9uaWJpMTdwOXJ6d25uZnhjanAzMnVuOXVnN3loaHpndGtodmw5amZrc3p0Z3c1dWg2OXdhYzJwZ3N5bjcwbmoSMm5pYml2YWxvcGVyMXdqNWtma25qa3BjNmpkMzByeHRtOHRweGZqZjd4cWx3eDM4YzdwGgwKBXVuaWJpEgMxMTE="
}
}`,
expectError: false,
},
}

for _, testCase := range testCases {
executeTest(t, testCase)
}
}
1 change: 1 addition & 0 deletions cmd/nibid/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) {
rootCmd.AddCommand(
InitCmd(app.ModuleBasics, app.DefaultNodeHome),
AddGenesisAccountCmd(app.DefaultNodeHome),
DecodeBase64Cmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
testnetCmd(app.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
Expand Down

0 comments on commit de4dc1e

Please sign in to comment.