From 94e8153a76e61afae1bae6ad4717bdf955f17a54 Mon Sep 17 00:00:00 2001 From: fudongbai <296179868@qq.com> Date: Mon, 22 Jul 2019 14:21:48 +0800 Subject: [PATCH] bip44 to derive many address from same seed phase --- keys/hdpath.go | 3 ++- keys/keys.go | 14 +++++++++++--- keys/keys_test.go | 11 +++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/keys/hdpath.go b/keys/hdpath.go index aa31d534..021899ae 100644 --- a/keys/hdpath.go +++ b/keys/hdpath.go @@ -19,7 +19,8 @@ const ( BIPCoinType = 714 BIPChange = false BIP44Prefix = "44'/714'/" - FullFundraiserPath = BIP44Prefix + "0'/0/0" + PartialFundraiserPath = "0'/0/0" + FullFundraiserPath = BIP44Prefix + PartialFundraiserPath ) // BIP44Params wraps BIP 44 params (5 level BIP 32 path). diff --git a/keys/keys.go b/keys/keys.go index beae2012..29d2e520 100644 --- a/keys/keys.go +++ b/keys/keys.go @@ -39,7 +39,15 @@ type KeyManager interface { func NewMnemonicKeyManager(mnemonic string) (KeyManager, error) { k := keyManager{} - err := k.recoveryFromKMnemonic(mnemonic) + err := k.recoveryFromMnemonic(mnemonic, FullFundraiserPath) + return &k, err +} + +// The full fundraiser path is "purpose' / coin_type' / account' / change / address_index". +// "purpose' / coin_type'" is fixed as "44'/714'/", user can customize the rest part. +func NewMnemonicPathKeyManager(mnemonic, keyPath string) (KeyManager, error) { + k := keyManager{} + err := k.recoveryFromMnemonic(mnemonic, BIP44Prefix+keyPath) return &k, err } @@ -98,7 +106,7 @@ func NewKeyManager() (KeyManager, error) { return NewMnemonicKeyManager(mnemonic) } -func (m *keyManager) recoveryFromKMnemonic(mnemonic string) error { +func (m *keyManager) recoveryFromMnemonic(mnemonic, keyPath string) error { words := strings.Split(mnemonic, " ") if len(words) != 12 && len(words) != 24 { return fmt.Errorf("mnemonic length should either be 12 or 24") @@ -109,7 +117,7 @@ func (m *keyManager) recoveryFromKMnemonic(mnemonic string) error { } // create master key and derive first key: masterPriv, ch := ComputeMastersFromSeed(seed) - derivedPriv, err := DerivePrivateKeyForPath(masterPriv, ch, FullFundraiserPath) + derivedPriv, err := DerivePrivateKeyForPath(masterPriv, ch, keyPath) if err != nil { return err } diff --git a/keys/keys_test.go b/keys/keys_test.go index 3684fe50..212ac324 100644 --- a/keys/keys_test.go +++ b/keys/keys_test.go @@ -21,12 +21,11 @@ func TestRecoveryFromKeyWordsNoError(t *testing.T) { assert.NoError(t, err) acc := keyManger.GetAddr() key := keyManger.GetPrivKey() - if acc.String() != "bnb1ddt3ls9fjcd8mh69ujdg3fxc89qle2a7km33aa" { - t.Fatalf("RecoveryFromKeyWords get unstable account") - } - if key == nil { - t.Fatalf("Failed to recover private key") - } + assert.Equal(t,"bnb1ddt3ls9fjcd8mh69ujdg3fxc89qle2a7km33aa",acc.String()) + assert.NotNil(t,key) + customPathKey, err:= NewMnemonicPathKeyManager(mnemonic,"1'/1/1") + assert.NoError(t, err) + assert.Equal(t,"bnb1c67nwp7u5adl7gw0ffn3d47kttcm4crjy9mrye",customPathKey.GetAddr().String()) } func TestRecoveryFromKeyBaseNoError(t *testing.T) {