From 138c7d758a5661d2916c40cdb79fa53335782f58 Mon Sep 17 00:00:00 2001 From: Giuseppe Natale <12249307+giunatale@users.noreply.github.com> Date: Fri, 26 Apr 2024 00:26:06 +0200 Subject: [PATCH] remove tip when null --- build.sh | 1 + cmd/cosmos-signer/cmd/root.go | 16 ++++- x/signer/client/cli/register_msgs.go | 2 +- x/signer/client/cli/tx_sign.go | 5 +- x/signer/client/cli/utils.go | 94 ++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 6 deletions(-) create mode 100755 build.sh create mode 100644 x/signer/client/cli/utils.go diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..c8e8744 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +go build -C cmd/cosmos-signer -o ../../build/cosmos-signer \ No newline at end of file diff --git a/cmd/cosmos-signer/cmd/root.go b/cmd/cosmos-signer/cmd/root.go index faf7676..4fb7e8c 100644 --- a/cmd/cosmos-signer/cmd/root.go +++ b/cmd/cosmos-signer/cmd/root.go @@ -26,6 +26,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/atomone-hub/cosmos-signer/app" + signercli "github.com/atomone-hub/cosmos-signer/x/signer/client/cli" ) const ( @@ -49,7 +50,7 @@ func NewRootCmd() *cobra.Command { SilenceErrors: true, PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { // set the default command outputs - cmd.SetOut(cmd.OutOrStdout()) + cmd.SetOut(signercli.NewFilterNullValJSON(cmd.OutOrStdout())) cmd.SetErr(cmd.ErrOrStderr()) clientCtx = clientCtx.WithCmdContext(cmd.Context()) @@ -116,6 +117,19 @@ func NewRootCmd() *cobra.Command { return err } + return nil + }, + PersistentPostRunE: func(cmd *cobra.Command, _ []string) error { + txCmd, _, err := cmd.Find([]string{"tx"}) + if err != nil { + return err + } + outputDoc, err := txCmd.Flags().GetString(flags.FlagOutputDocument) + if err != nil { + return err + } + signercli.FilterNullJSONKeysFromFile(outputDoc) + return nil }, } diff --git a/x/signer/client/cli/register_msgs.go b/x/signer/client/cli/register_msgs.go index 1732cc0..03a4e16 100644 --- a/x/signer/client/cli/register_msgs.go +++ b/x/signer/client/cli/register_msgs.go @@ -12,7 +12,7 @@ import ( cdctypes "github.com/cosmos/cosmos-sdk/codec/types" ) -func RegisterSdkMsgsDynamic(ctx client.Context, pluginsDir string, unregisteredTypes map[string]struct{}) error { +func RegisterTypes(ctx client.Context, pluginsDir string, unregisteredTypes map[string]struct{}) error { legacyAminoCodec := ctx.LegacyAmino registry := ctx.Codec.InterfaceRegistry() files, err := filepath.Glob(filepath.Join(pluginsDir, "*.so")) diff --git a/x/signer/client/cli/tx_sign.go b/x/signer/client/cli/tx_sign.go index feefa74..6038a57 100644 --- a/x/signer/client/cli/tx_sign.go +++ b/x/signer/client/cli/tx_sign.go @@ -52,7 +52,6 @@ func preSignCmd(cmd *cobra.Command, _ []string) { func makeSignCmd(origMakeSignCmd func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) (err error) { var clientCtx client.Context - clientCtx, err = client.GetClientTxContext(cmd) if err != nil { return err @@ -82,12 +81,10 @@ func makeSignCmd(origMakeSignCmd func(cmd *cobra.Command, args []string) error) if err != nil { return err } - err = RegisterSdkMsgsDynamic(clientCtx, pluginsDir, unregisteredTypes) + err = RegisterTypes(clientCtx, pluginsDir, unregisteredTypes) if err != nil { return err } - - client.SetCmdClientContext(cmd, clientCtx) // not sure if this is needed } return origMakeSignCmd(cmd, args) diff --git a/x/signer/client/cli/utils.go b/x/signer/client/cli/utils.go new file mode 100644 index 0000000..fd38775 --- /dev/null +++ b/x/signer/client/cli/utils.go @@ -0,0 +1,94 @@ +package cli + +import ( + "encoding/json" + "io" + "os" +) + +var ( + nullKeys = []string{"tip"} +) + +// FilterNullValJSON implements the io.Writer interface +type FilterNullValJSON struct { + Output io.Writer + NullKeys []string +} + +func NewFilterNullValJSON(output io.Writer) *FilterNullValJSON { + return &FilterNullValJSON{ + Output: output, + NullKeys: nullKeys, + } +} + +func (w *FilterNullValJSON) Write(p []byte) (n int, err error) { + var data interface{} + if err := json.Unmarshal(p, &data); err != nil { + return 0, err + } + filteredData := FilterNullJSONKeys(data) + filteredBytes, err := json.Marshal(filteredData) + if err != nil { + return 0, err + } + return w.Output.Write(filteredBytes) +} + +// FilterNullJSONKeys recursively filters out null values from JSON for specified keys +func FilterNullJSONKeys(data interface{}) interface{} { + switch v := data.(type) { + case map[string]interface{}: + for key, val := range v { + if shouldCheckKey(key) { + if val == nil { + delete(v, key) + } else { + v[key] = FilterNullJSONKeys(val) + } + } else { + v[key] = FilterNullJSONKeys(val) // Recursively process all keys + } + } + return v + case []interface{}: + for i := range v { + v[i] = FilterNullJSONKeys(v[i]) // Recursively process array elements + } + return v + default: + return v + } +} + +// shouldCheckKey checks if the key is one of the specified keys to check +func shouldCheckKey(key string) bool { + for _, k := range nullKeys { + if key == k { + return true + } + } + return false +} + +func FilterNullJSONKeysFromFile(outputDoc string) { + if outputDoc != "" { + content, err := os.ReadFile(outputDoc) + if err != nil { + panic(err) + } + var data interface{} + if err := json.Unmarshal(content, &data); err != nil { + panic(err) + } + filteredData := FilterNullJSONKeys(data) + filteredBytes, err := json.Marshal(filteredData) + if err != nil { + panic(err) + } + if err := os.WriteFile(outputDoc, filteredBytes, 0644); err != nil { + panic(err) + } + } +}