This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ReformatLedgerTx to reformat EIP-712 payloads
- Loading branch information
1 parent
f11bc35
commit d897959
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package eip712 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
cosmoskr "github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" | ||
"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 { | ||
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") | ||
} | ||
|
||
// Get signatures from TxBuilder | ||
sigs, err := txBuilder.GetTx().GetSignaturesV2() | ||
if err != nil { | ||
return fmt.Errorf("Could not get signatures with error: %w", err) | ||
} | ||
|
||
// Verify single-signer | ||
if len(sigs) != 1 { | ||
return fmt.Errorf("Invalid number of signatures, expected 1 and got %v", len(sigs)) | ||
} | ||
|
||
signature := sigs[0] | ||
sigBytes := signature.Data.(*signing.SingleSignatureData).Signature | ||
|
||
// Parse Chain ID as big.Int | ||
chainIDInt, err := types.ParseChainID(chainID) | ||
if err != nil { | ||
return fmt.Errorf("Error parsing chain id: %v\n", err) | ||
} | ||
|
||
// Add ExtensionOptionsWeb3Tx extension with signature | ||
var option *codectypes.Any | ||
option, err = codectypes.NewAnyWithValue(&types.ExtensionOptionsWeb3Tx{ | ||
FeePayer: signature.PubKey.Address().String(), | ||
TypedDataChainID: chainIDInt.Uint64(), | ||
FeePayerSig: sigBytes, | ||
}) | ||
extensionBuilder.SetExtensionOptions(option) | ||
|
||
// Set signature data with to indicate 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, | ||
Signature: nil, | ||
} | ||
sig := signing.SignatureV2{ | ||
PubKey: signature.PubKey, | ||
Data: &blankSig, | ||
Sequence: signature.Sequence, | ||
} | ||
|
||
err = txBuilder.SetSignatures(sig) | ||
if err != nil { | ||
return fmt.Errorf("Unable to set signatures on payload: %v\n", err) | ||
} | ||
|
||
return nil | ||
} |