Skip to content

Commit

Permalink
Enable and support Protobuf payloads in Ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
austinchandra committed Aug 4, 2022
1 parent de7b2b0 commit 80b38c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
11 changes: 1 addition & 10 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -235,20 +234,12 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err

if clientCtx.From == "" || flagSet.Changed(flags.FlagFrom) {
from, _ := flagSet.GetString(flags.FlagFrom)
fromAddr, fromName, keyType, err := GetFromFields(clientCtx.Keyring, from, clientCtx.GenerateOnly)
fromAddr, fromName, _, err := GetFromFields(clientCtx.Keyring, from, clientCtx.GenerateOnly)
if err != nil {
return clientCtx, err
}

clientCtx = clientCtx.WithFrom(from).WithFromAddress(fromAddr).WithFromName(fromName)

// If the `from` signer account is a ledger key, we need to use
// SIGN_MODE_AMINO_JSON, because ledger doesn't support proto yet.
// ref: https://github.com/cosmos/cosmos-sdk/issues/8109
if keyType == keyring.TypeLedger && clientCtx.SignModeStr != flags.SignModeLegacyAminoJSON {
fmt.Println("Default sign-mode 'direct' not supported by Ledger, using sign-mode 'amino-json'.")
clientCtx = clientCtx.WithSignModeStr(flags.SignModeLegacyAminoJSON)
}
}
return clientCtx, nil
}
Expand Down
21 changes: 20 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig boo
}

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

// Parse Chain ID as UInt
chainID, err := etherminttypes.ParseChainID(txf.chainID)
if err != nil {
return fmt.Errorf("Error parsing chain id: %v\n", err)
Expand All @@ -423,8 +425,25 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig boo
TypedDataChainID: chainID.Uint64(),
FeePayerSig: sigBytes,
})

extensionBuilder.SetExtensionOptions(option)

// Set blank signature data with Amino Sign Type
// (Regardless of input signMode, Evmos requires an Amino payload for Ledger)
sigData = signing.SingleSignatureData{
SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON,
Signature: nil,
}
sig = signing.SignatureV2{
PubKey: pubKey,
Data: &sigData,
Sequence: txf.Sequence(),
}

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

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/ledger/ledger_secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func sign(device SECP256K1, pkl PrivKeyLedgerSecp256k1, msg []byte) ([]byte, err
func getPubKeyUnsafe(device SECP256K1, path hd.BIP44Params) (types.PubKey, error) {
publicKey, err := device.GetPublicKeySECP256K1(path.LedgerDerivationPath())
if err != nil {
return nil, fmt.Errorf("please open Cosmos app on the Ledger device - error: %v", err)
return nil, fmt.Errorf("please open the Ethereum app on the Ledger device - error: %v", err)
}

publicKey = append([]byte{byte(4)}, publicKey...)
Expand Down

0 comments on commit 80b38c3

Please sign in to comment.