From 642975c87c35e256d57c01b9ee6ecf827c06cbfa Mon Sep 17 00:00:00 2001 From: Antonio Pitasi Date: Mon, 9 Sep 2024 10:16:11 +0200 Subject: [PATCH] fix(go-client): use ethsecp256k1 instead of cosmos' This fixes the derivation of a seed phrase into a private key. It also makes the derivation path non-configurable, since it only makes sense when used with the Ethereum derivation path. --- go-client/tx_identity.go | 10 ++++++---- go-client/tx_identity_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 go-client/tx_identity_test.go diff --git a/go-client/tx_identity.go b/go-client/tx_identity.go index f4d0ef0a0..77767fedf 100644 --- a/go-client/tx_identity.go +++ b/go-client/tx_identity.go @@ -5,12 +5,14 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdktypes "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/go-bip39" + "github.com/evmos/evmos/v18/crypto/ethsecp256k1" ) +var DefaultDerivationPath = "m/44'/60'/0'/0/0" + // Identity represents an account on the Warden Protocol. It can be used to sign // transactions. type Identity struct { @@ -20,7 +22,7 @@ type Identity struct { // NewIdentityFromSeed returns a Identity from a seed phrase. // This is useful in a mock environment or during testing but should not be used in production. -func NewIdentityFromSeed(derivationPath, seedPhrase string) (Identity, error) { +func NewIdentityFromSeed(seedPhrase string) (Identity, error) { // Convert the seed phrase to a seed seedBytes, err := bip39.NewSeedWithErrorChecking(seedPhrase, "") if err != nil { @@ -29,7 +31,7 @@ func NewIdentityFromSeed(derivationPath, seedPhrase string) (Identity, error) { // Create a master key and derive the desired key masterKey, ch := hd.ComputeMastersFromSeed(seedBytes) - derivedKey, err := hd.DerivePrivateKeyForPath(masterKey, ch, derivationPath) + derivedKey, err := hd.DerivePrivateKeyForPath(masterKey, ch, DefaultDerivationPath) if err != nil { return Identity{}, fmt.Errorf("failed to derive private key: %w", err) } @@ -38,7 +40,7 @@ func NewIdentityFromSeed(derivationPath, seedPhrase string) (Identity, error) { privKey, _ := btcec.PrivKeyFromBytes(derivedKey) // Convert the public key to a Cosmos secp256k1.PublicKey - cosmosPrivKey := &secp256k1.PrivKey{ + cosmosPrivKey := ðsecp256k1.PrivKey{ Key: privKey.Serialize(), } diff --git a/go-client/tx_identity_test.go b/go-client/tx_identity_test.go new file mode 100644 index 000000000..20b19b7ec --- /dev/null +++ b/go-client/tx_identity_test.go @@ -0,0 +1,23 @@ +package client + +import ( + "encoding/base64" + "fmt" + "testing" +) + +func TestNewIdentityFromSeed(t *testing.T) { + seed := "exclude try nephew main caught favorite tone degree lottery device tissue tent ugly mouse pelican gasp lava flush pen river noise remind balcony emerge" + + id, err := NewIdentityFromSeed(seed) + if err != nil { + t.Fatal(err) + } + + fmt.Printf("address: %s\n", id.Address.String()) + fmt.Printf("private key: %s\n", base64.StdEncoding.EncodeToString(id.PrivKey.Bytes())) + + if id.Address.String() != "warden1d652c9nngq5cneak2whyaqa4g9ehr8pstxj0r5" { + t.Fatalf("unexpected address: %s", id.Address.String()) + } +}