Skip to content

Commit

Permalink
fix(go-client): use ethsecp256k1 instead of cosmos'
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pitasi committed Sep 9, 2024
1 parent 5a85986 commit 642975c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions go-client/tx_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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 := &ethsecp256k1.PrivKey{
Key: privKey.Serialize(),
}

Expand Down
23 changes: 23 additions & 0 deletions go-client/tx_identity_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 642975c

Please sign in to comment.