Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R4R] support bip44 to derive many address from same seed phase #61

Merged
merged 4 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
IMPROVEMENTS
* [\#53](https://github.com/binance-chain/go-sdk/pull/53) [SOURCE] change the default source into 0
* [\#56](https://github.com/binance-chain/go-sdk/pull/56) [RPC] add reconnect strategy when timeout to receive response
* [\#61](https://github.com/binance-chain/go-sdk/pull/61) [KEY] support bip44 to derive many address from same seed phase

## BREAKING
* [\#57](https://github.com/binance-chain/go-sdk/pull/57) [API] add query option to getTokens api
3 changes: 3 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ NewKeyManager() (KeyManager, error)

NewMnemonicKeyManager(mnemonic string) (KeyManager, error)

NewMnemonicPathKeyManager(mnemonic, keyPath string) (KeyManager, error)

NewKeyStoreKeyManager(file string, auth string) (KeyManager, error)

NewPrivateKeyManager(priKey string) (KeyManager, error)
Expand All @@ -62,6 +64,7 @@ NewLedgerKeyManager(path ledger.DerivationPath) (KeyManager, error)
```
- NewKeyManager. You will get a new private key without provide anything, you can export and save this `KeyManager`.
- NewMnemonicKeyManager. You should provide your mnemonic, usually is a string of 24 words.
- NewMnemonicPathKeyManager. The difference between `NewMnemonicKeyManager` is that you can use custom keypath to generate different `keyManager` while using the same mnemonic. 5 levels in BIP44 path: "purpose' / coin_type' / account' / change / address_index", "purpose' / coin_type'" is fixed as "44'/714'/", you can customize the rest part.
- NewKeyStoreKeyManager. You should provide a keybase json file and you password, you can download the key base json file when your create a wallet account.
- NewPrivateKeyManager. You should provide a Hex encoded string of your private key.
- NewLedgerKeyManager. You must have a ledger device with binance ledger app and connect it to your machine.
Expand Down
11 changes: 6 additions & 5 deletions keys/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (

// BIP44Prefix is the parts of the BIP32 HD path that are fixed by what we used during the fundraiser.
const (
BIPPurpose = 44
BIPCoinType = 714
BIPChange = false
BIP44Prefix = "44'/714'/"
FullFundraiserPath = BIP44Prefix + "0'/0/0"
BIPPurpose = 44
BIPCoinType = 714
BIPChange = false
BIP44Prefix = "44'/714'/"
PartialPath = "0'/0/0"
FullPath = BIP44Prefix + PartialPath
)

// 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, FullPath)
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