Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth: Improve cli tx decoding with fallback to direct txBytes protobuf unmarshaling #23611

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions x/auth/client/cli/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
)

const flagHex = "hex"
Expand All @@ -32,12 +33,7 @@
return err
}

tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
if err != nil {
return err
}

json, err := clientCtx.TxConfig.TxJSONEncoder()(tx)
json, err := decodeTxAndGetJSON(clientCtx, txBytes)
if err != nil {
return err
}
Expand All @@ -52,3 +48,19 @@

return cmd
}

func decodeTxAndGetJSON(clientCtx client.Context, txBytes []byte) ([]byte, error) {
// First try decoding with TxDecoder
tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
if err == nil {
return clientCtx.TxConfig.TxJSONEncoder()(tx)
}

// Fallback to direct unmarshaling
var sdkTx sdktx.Tx
if err := clientCtx.Codec.Unmarshal(txBytes, &sdkTx); err != nil {
return nil, fmt.Errorf("failed to directly unmarshal txBytes: %w", err)

Check failure on line 62 in x/auth/client/cli/decode.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: fmt

Check failure on line 62 in x/auth/client/cli/decode.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: fmt (typecheck)

Check failure on line 62 in x/auth/client/cli/decode.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: fmt) (typecheck)

Check failure on line 62 in x/auth/client/cli/decode.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: fmt) (typecheck)

Check failure on line 62 in x/auth/client/cli/decode.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: fmt

Check failure on line 62 in x/auth/client/cli/decode.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: fmt
}

return clientCtx.Codec.MarshalJSON(&sdkTx)
}
Comment on lines +52 to +66
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add missing fmt package import.

The fmt package is required for error formatting but is not imported, causing build failures.

Add this import to fix the build:

 import (
 	"encoding/base64"
 	"encoding/hex"
+	"fmt"
 
 	"github.com/spf13/cobra"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func decodeTxAndGetJSON(clientCtx client.Context, txBytes []byte) ([]byte, error) {
// First try decoding with TxDecoder
tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
if err == nil {
return clientCtx.TxConfig.TxJSONEncoder()(tx)
}
// Fallback to direct unmarshaling
var sdkTx sdktx.Tx
if err := clientCtx.Codec.Unmarshal(txBytes, &sdkTx); err != nil {
return nil, fmt.Errorf("failed to directly unmarshal txBytes: %w", err)
}
return clientCtx.Codec.MarshalJSON(&sdkTx)
}
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
// ... other imports if any
)
🧰 Tools
🪛 GitHub Check: tests (03)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: tests (02)

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: dependency-review

[failure] 62-62:
undefined: fmt

🪛 GitHub Check: golangci-lint

[failure] 62-62:
undefined: fmt (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)


[failure] 62-62:
undefined: fmt) (typecheck)

🪛 GitHub Actions: Dependency Review

[error] 62-62: undefined: fmt

🪛 GitHub Actions: Lint

[error] 62-62: undefined: fmt (typecheck)

🪛 GitHub Actions: Build SimApp

[error] 62-62: undefined: fmt

🪛 GitHub Actions: v2 core Tests

[error] 62-62: undefined: fmt

Loading