From a34c9095c3a5d217d50318471660a5508922bcc2 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 10 Apr 2018 15:50:08 +1000 Subject: Rename to IS_CHILD_KEY --- packages/subproviders/src/subproviders/ledger.ts | 8 ++++---- packages/subproviders/src/utils/wallet_utils.ts | 22 +++++++++------------- .../test/unit/mnemonic_wallet_subprovider_test.ts | 2 -- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts index a7b79c128..fabff88cd 100644 --- a/packages/subproviders/src/subproviders/ledger.ts +++ b/packages/subproviders/src/subproviders/ledger.ts @@ -25,6 +25,7 @@ const DEFAULT_DERIVATION_PATH = `44'/60'/0'`; const DEFAULT_NUM_ADDRESSES_TO_FETCH = 10; const ASK_FOR_ON_DEVICE_CONFIRMATION = false; const SHOULD_GET_CHAIN_CODE = true; +const IS_CHILD_KEY = true; /** * Subprovider for interfacing with a user's [Ledger Nano S](https://www.ledgerwallet.com/products/ledger-nano-s). @@ -40,7 +41,6 @@ export class LedgerSubprovider extends BaseWalletSubprovider { private _ledgerClientIfExists?: LedgerEthereumClient; private _shouldAlwaysAskForConfirmation: boolean; private _addressSearchLimit: number; - private _hardenedKey: boolean = true; /** * Instantiates a LedgerSubprovider. Defaults to derivationPath set to `44'/60'/0'`. * TestRPC/Ganache defaults to `m/44'/60'/0'/0`, so set this in the configs if desired. @@ -110,7 +110,7 @@ export class LedgerSubprovider extends BaseWalletSubprovider { LedgerSubprovider._validateTxParams(txParams); const initialHDKey = await this._initialHDKeyAsync(); const derivedKey = _.isUndefined(txParams.from) - ? walletUtils._firstDerivedKey(initialHDKey, this._derivationPath, this._hardenedKey) + ? walletUtils._firstDerivedKey(initialHDKey, this._derivationPath, IS_CHILD_KEY) : this._findDerivedKeyByPublicAddress(initialHDKey, txParams.from); this._ledgerClientIfExists = await this._createLedgerClientAsync(); @@ -165,7 +165,7 @@ export class LedgerSubprovider extends BaseWalletSubprovider { assert.isHexString('data', data); const initialHDKey = await this._initialHDKeyAsync(); const derivedKey = _.isUndefined(address) - ? walletUtils._firstDerivedKey(initialHDKey, this._derivationPath, this._hardenedKey) + ? walletUtils._firstDerivedKey(initialHDKey, this._derivationPath, IS_CHILD_KEY) : this._findDerivedKeyByPublicAddress(initialHDKey, address); this._ledgerClientIfExists = await this._createLedgerClientAsync(); @@ -232,7 +232,7 @@ export class LedgerSubprovider extends BaseWalletSubprovider { initalHDKey, this._derivationPath, this._addressSearchLimit, - this._hardenedKey, + IS_CHILD_KEY, ); if (_.isUndefined(matchedDerivedKey)) { throw new Error(`${WalletSubproviderErrors.AddressNotFound}: ${address}`); diff --git a/packages/subproviders/src/utils/wallet_utils.ts b/packages/subproviders/src/utils/wallet_utils.ts index 6c698a006..48d475559 100644 --- a/packages/subproviders/src/utils/wallet_utils.ts +++ b/packages/subproviders/src/utils/wallet_utils.ts @@ -7,25 +7,21 @@ import { DerivedHDKey, WalletSubproviderErrors } from '../types'; const DEFAULT_ADDRESS_SEARCH_OFFSET = 0; const BATCH_SIZE = 10; -// Derivation Paths -// BIP44 m / purpose' / coin_type' / account' / change / address_index -// m/44'/60'/0'/0 -// m/44'/60'/0'/0/0 -// m/44'/60'/0'/0/{account_index} - testrpc -// m/44'/60'/0' - ledger - export const walletUtils = { _calculateDerivedHDKeys( initialHDKey: HDNode, derivationPath: string, searchLimit: number, offset: number = DEFAULT_ADDRESS_SEARCH_OFFSET, - hardened: boolean = false, + isChildKey: boolean = false, ): DerivedHDKey[] { const derivedKeys: DerivedHDKey[] = []; _.times(searchLimit, i => { const derivationIndex = offset + i; - const path = hardened ? `m/${derivationIndex}` : `m/${derivationPath}/${derivationIndex}`; + // 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; @@ -49,7 +45,7 @@ export const walletUtils = { initialHDKey: HDNode, derivationPath: string, searchLimit: number, - hardened: boolean = false, + isChild: boolean = false, ): DerivedHDKey | undefined { let matchedKey: DerivedHDKey | undefined; for (let index = 0; index < searchLimit; index = index + BATCH_SIZE) { @@ -58,7 +54,7 @@ export const walletUtils = { derivationPath, BATCH_SIZE, index, - hardened, + isChild, ); matchedKey = _.find(derivedKeys, derivedKey => derivedKey.address === address); if (matchedKey) { @@ -68,8 +64,8 @@ export const walletUtils = { return matchedKey; }, - _firstDerivedKey(initialHDKey: HDNode, derivationPath: string, hardened: boolean = false): DerivedHDKey { - const derivedKeys = walletUtils._calculateDerivedHDKeys(initialHDKey, derivationPath, 1, 0, hardened); + _firstDerivedKey(initialHDKey: HDNode, derivationPath: string, isChild: boolean = false): DerivedHDKey { + const derivedKeys = walletUtils._calculateDerivedHDKeys(initialHDKey, derivationPath, 1, 0, isChild); const firstDerivedKey = derivedKeys[0]; return firstDerivedKey; }, diff --git a/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts b/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts index 16981cf86..77e5d35ae 100644 --- a/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts @@ -30,8 +30,6 @@ describe('MnemonicWalletSubprovider', () => { describe('success cases', () => { it('returns the accounts', async () => { const accounts = await subprovider.getAccountsAsync(); - // tslint:disable-next-line:no-console - console.log(accounts); expect(accounts[0]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_0); expect(accounts[1]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_1); expect(accounts.length).to.be.equal(10); -- cgit v1.2.3