From 9169913a2cbe9d421629db2e8169c0806044e3fc Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 11 Apr 2018 12:22:41 +1000 Subject: Add isChildKey to derived key --- packages/subproviders/src/utils/wallet_utils.ts | 59 ++++++++++++------------- 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'packages/subproviders/src/utils') diff --git a/packages/subproviders/src/utils/wallet_utils.ts b/packages/subproviders/src/utils/wallet_utils.ts index 48d475559..0c3e895b6 100644 --- a/packages/subproviders/src/utils/wallet_utils.ts +++ b/packages/subproviders/src/utils/wallet_utils.ts @@ -8,54 +8,51 @@ const DEFAULT_ADDRESS_SEARCH_OFFSET = 0; const BATCH_SIZE = 10; export const walletUtils = { - _calculateDerivedHDKeys( - initialHDKey: HDNode, - derivationPath: string, + DEFAULT_NUM_ADDRESSES_TO_FETCH: 10, + DEFAULT_ADDRESS_SEARCH_LIMIT: 1000, + calculateDerivedHDKeys( + initialDerivedKey: DerivedHDKey, searchLimit: number, offset: number = DEFAULT_ADDRESS_SEARCH_OFFSET, - isChildKey: boolean = false, ): DerivedHDKey[] { const derivedKeys: DerivedHDKey[] = []; _.times(searchLimit, i => { + const derivationPath = initialDerivedKey.derivationPath; const derivationIndex = offset + i; - // Normally we need to set the full derivation path to walk the tree from the root - // as the initial key is at the root. - // But with ledger the initial key is a child so we walk the tree relative to that child - const path = isChildKey ? `m/${derivationIndex}` : `m/${derivationPath}/${derivationIndex}`; - const hdKey = initialHDKey.derive(path); - const derivedPublicKey = hdKey.publicKey; - const shouldSanitizePublicKey = true; - const ethereumAddressUnprefixed = ethUtil - .publicToAddress(derivedPublicKey, shouldSanitizePublicKey) - .toString('hex'); - const address = ethUtil.addHexPrefix(ethereumAddressUnprefixed); + // If the DerivedHDKey is a child then we walk relative, if not we walk the full derivation path + const path = initialDerivedKey.isChildKey + ? `m/${derivationIndex}` + : `m/${derivationPath}/${derivationIndex}`; + const hdKey = initialDerivedKey.hdKey.derive(path); + const address = walletUtils.addressOfHDKey(hdKey); const derivedKey: DerivedHDKey = { - derivationPath, - hdKey, address, + hdKey, + derivationPath, derivationIndex, + isChildKey: initialDerivedKey.isChildKey, }; derivedKeys.push(derivedKey); }); return derivedKeys; }, - - _findDerivedKeyByAddress( + addressOfHDKey(hdKey: HDNode): string { + const shouldSanitizePublicKey = true; + const derivedPublicKey = hdKey.publicKey; + const ethereumAddressUnprefixed = ethUtil + .publicToAddress(derivedPublicKey, shouldSanitizePublicKey) + .toString('hex'); + const address = ethUtil.addHexPrefix(ethereumAddressUnprefixed); + return address; + }, + findDerivedKeyByAddress( address: string, - initialHDKey: HDNode, - derivationPath: string, + initialDerivedKey: DerivedHDKey, searchLimit: number, - isChild: boolean = false, ): DerivedHDKey | undefined { let matchedKey: DerivedHDKey | undefined; for (let index = 0; index < searchLimit; index = index + BATCH_SIZE) { - const derivedKeys = walletUtils._calculateDerivedHDKeys( - initialHDKey, - derivationPath, - BATCH_SIZE, - index, - isChild, - ); + const derivedKeys = walletUtils.calculateDerivedHDKeys(initialDerivedKey, BATCH_SIZE, index); matchedKey = _.find(derivedKeys, derivedKey => derivedKey.address === address); if (matchedKey) { break; @@ -64,8 +61,8 @@ export const walletUtils = { return matchedKey; }, - _firstDerivedKey(initialHDKey: HDNode, derivationPath: string, isChild: boolean = false): DerivedHDKey { - const derivedKeys = walletUtils._calculateDerivedHDKeys(initialHDKey, derivationPath, 1, 0, isChild); + _firstDerivedKey(initialDerivedKey: DerivedHDKey): DerivedHDKey { + const derivedKeys = walletUtils.calculateDerivedHDKeys(initialDerivedKey, 1, 0); const firstDerivedKey = derivedKeys[0]; return firstDerivedKey; }, -- cgit v1.2.3