Skip to content

Commit

Permalink
fix NewSeed
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jul 8, 2018
1 parent a26571a commit 9b10528
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
32 changes: 32 additions & 0 deletions example/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"log"

"github.com/miguelmota/go-ethereum-hdwallet"
)

func main() {
seed, _ := hdwallet.NewSeed()
wallet, err := hdwallet.NewFromSeed(seed)
if err != nil {
log.Fatal(err)
}

path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0")
account, err := wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}

fmt.Println(account.Address.Hex()) // 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947

path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1")
account, err = wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}

fmt.Println(account.Address.Hex()) // 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
}
20 changes: 12 additions & 8 deletions hdwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hdwallet

import (
"crypto/ecdsa"
"crypto/rand"
"errors"
"fmt"
"math/big"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"
)

Expand Down Expand Up @@ -60,6 +60,10 @@ func NewFromMnemonic(mnemonic string) (*Wallet, error) {
return nil, errors.New("mnemonic is required")
}

if !bip39.IsMnemonicValid(mnemonic) {
return nil, errors.New("mnemonic is invalid")
}

seed, err := NewSeedFromMnemonic(mnemonic)
if err != nil {
return nil, err
Expand Down Expand Up @@ -377,9 +381,9 @@ func MustParseDerivationPath(path string) accounts.DerivationPath {
return parsed
}

// NewMnemonic returns a randomly generated BIP-39 mnemonic.
func NewMnemonic() (string, error) {
entropy, err := bip39.NewEntropy(128)
// NewMnemonic returns a randomly generated BIP-39 mnemonic using 128-256 bits of entropy.
func NewMnemonic(bits int) (string, error) {
entropy, err := bip39.NewEntropy(bits)
if err != nil {
return "", err
}
Expand All @@ -388,7 +392,9 @@ func NewMnemonic() (string, error) {

// NewSeed returns a randomly generated BIP-39 seed.
func NewSeed() ([]byte, error) {
return bip32.NewSeed()
b := make([]byte, 64)
_, err := rand.Read(b)
return b, err
}

// NewSeedFromMnemonic returns a BIP-39 seed based on a BIP-39 mnemonic.
Expand All @@ -397,9 +403,7 @@ func NewSeedFromMnemonic(mnemonic string) ([]byte, error) {
return nil, errors.New("mnemonic is required")
}

seed := bip39.NewSeed(mnemonic, "")

return seed, nil
return bip39.NewSeedWithErrorChecking(mnemonic, "")
}

// DerivePrivateKey derives the private key of the derivation path.
Expand Down
6 changes: 3 additions & 3 deletions hdwallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ func TestWallet(t *testing.T) {
t.Error(err)
}

if len(seed) != 256 {
t.Error("expected size of 256")
if len(seed) != 64 {
t.Error("expected size of 64")
}

seed, err = NewSeedFromMnemonic(mnemonic)
Expand All @@ -221,7 +221,7 @@ func TestWallet(t *testing.T) {
t.Error("expected size of 64")
}

mnemonic, err = NewMnemonic()
mnemonic, err = NewMnemonic(128)
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 9b10528

Please sign in to comment.