Skip to content

Commit

Permalink
bip44 to derive many address from same seed phase
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Jul 22, 2019
1 parent a208ba1 commit dd015d2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion keys/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
14 changes: 11 additions & 3 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
Expand All @@ -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
}
Expand Down
11 changes: 5 additions & 6 deletions keys/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dd015d2

Please sign in to comment.