Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Fix issue with public key; minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
0a1c committed Aug 31, 2022
1 parent 032bcfc commit 6dea312
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import (
"github.com/evmos/ethermint/types"
)

// ReformatLedgerTx reformats Ledger-signed Cosmos transactions to match the fork expected by Evmos
// by including the signature in a Web3Tx extension.
func ReformatLedgerTx(chainID string, keyType cosmoskr.KeyType, txBuilder client.TxBuilder) error {
// PreprocessLedgerTx reformats Ledger-signed Cosmos transactions to match the fork expected by Ethermint
// by including the signature in a Web3Tx extension and sending a blank signature in the body.
func PreprocessLedgerTx(chainID string, keyType cosmoskr.KeyType, txBuilder client.TxBuilder) error {
// Only process Ledger transactions
if keyType != cosmoskr.TypeLedger {
// Only process Ledger transactions
return nil
}

// Init extension builder to set Web3 extension
extensionBuilder, ok := txBuilder.(authtx.ExtensionOptionsTxBuilder)
if !ok {
return fmt.Errorf("Error setting extension options: cannot cast to ExtensionOptionsTxBuilder")
return fmt.Errorf("cannot cast TxBuilder to ExtensionOptionsTxBuilder")
}

// Get signatures from TxBuilder
sigs, err := txBuilder.GetTx().GetSignaturesV2()
if err != nil {
return fmt.Errorf("Could not get signatures with error: %w", err)
return fmt.Errorf("could not get signatures: %w", err)
}

// Verify single-signer
if len(sigs) != 1 {
return fmt.Errorf("Invalid number of signatures, expected 1 and got %v", len(sigs))
return fmt.Errorf("invalid number of signatures, expected 1 and got %v", len(sigs))
}

signature := sigs[0]
Expand All @@ -42,19 +42,19 @@ func ReformatLedgerTx(chainID string, keyType cosmoskr.KeyType, txBuilder client
// Parse Chain ID as big.Int
chainIDInt, err := types.ParseChainID(chainID)
if err != nil {
return fmt.Errorf("Error parsing chain id: %v\n", err)
return fmt.Errorf("could not parse chain id: %v", err)
}

// Add ExtensionOptionsWeb3Tx extension with signature
var option *codectypes.Any
option, err = codectypes.NewAnyWithValue(&types.ExtensionOptionsWeb3Tx{
FeePayer: signature.PubKey.Address().String(),
FeePayer: txBuilder.GetTx().FeePayer().String(),
TypedDataChainID: chainIDInt.Uint64(),
FeePayerSig: sigBytes,
})
extensionBuilder.SetExtensionOptions(option)

// Set signature data with to indicate Amino Sign Type
// Set blank signature with Amino Sign Type
// (Regardless of input signMode, Evmos requires Amino signature type for Ledger)
blankSig := signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON,
Expand All @@ -68,7 +68,7 @@ func ReformatLedgerTx(chainID string, keyType cosmoskr.KeyType, txBuilder client

err = txBuilder.SetSignatures(sig)
if err != nil {
return fmt.Errorf("Unable to set signatures on payload: %v\n", err)
return fmt.Errorf("unable to set signatures on payload: %v", err)
}

return nil
Expand Down

0 comments on commit 6dea312

Please sign in to comment.