From 916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 11 Apr 2018 18:48:46 +1000 Subject: Renamed DerivedHDKey to DerivedHDKeyInfo Added assertions on addresses for public methods Throw a helpful error message when signer address is not instantiated address in PrivateKeyWalletSubprovider Update changelog and rename derivationBasePath to baseDerivationPath When returning undefined use pattern of IfExists Added configuration object for MnemonicWallet Put constants back into each individual wallet rather than in walletUtils Delete accidental package-lock.json --- packages/subproviders/src/utils/wallet_utils.ts | 40 +++++++++++-------------- 1 file changed, 18 insertions(+), 22 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 d5ebf5ce6..a37597dff 100644 --- a/packages/subproviders/src/utils/wallet_utils.ts +++ b/packages/subproviders/src/utils/wallet_utils.ts @@ -2,37 +2,35 @@ import ethUtil = require('ethereumjs-util'); import HDNode = require('hdkey'); import * as _ from 'lodash'; -import { DerivedHDKey, WalletSubproviderErrors } from '../types'; +import { DerivedHDKeyInfo, WalletSubproviderErrors } from '../types'; -const DEFAULT_ADDRESS_SEARCH_OFFSET = 0; -const BATCH_SIZE = 10; const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000; -class DerivedHDKeyIterator implements IterableIterator { - private _initialDerivedKey: DerivedHDKey; +class DerivedHDKeyInfoIterator implements IterableIterator { + private _initialDerivedKey: DerivedHDKeyInfo; private _searchLimit: number; private _index: number; - constructor(initialDerivedKey: DerivedHDKey, searchLimit: number = DEFAULT_ADDRESS_SEARCH_OFFSET) { + constructor(initialDerivedKey: DerivedHDKeyInfo, searchLimit: number = DEFAULT_ADDRESS_SEARCH_LIMIT) { this._searchLimit = searchLimit; this._initialDerivedKey = initialDerivedKey; this._index = 0; } - public next(): IteratorResult { - const derivationBasePath = this._initialDerivedKey.derivationBasePath; + public next(): IteratorResult { + const baseDerivationPath = this._initialDerivedKey.baseDerivationPath; const derivationIndex = this._index; const isChildKey = this._initialDerivedKey.isChildKey; // If the DerivedHDKey is a child then we walk relative, if not we walk the full derivation path - const fullDerivationPath = `m/${derivationBasePath}/${derivationIndex}`; + const fullDerivationPath = `m/${baseDerivationPath}/${derivationIndex}`; const relativeDerivationPath = `m/${derivationIndex}`; const path = isChildKey ? relativeDerivationPath : fullDerivationPath; const hdKey = this._initialDerivedKey.hdKey.derive(path); const address = walletUtils.addressOfHDKey(hdKey); - const derivedKey: DerivedHDKey = { + const derivedKey: DerivedHDKeyInfo = { address, hdKey, - derivationBasePath, + baseDerivationPath, derivationIndex, derivationPath: fullDerivationPath, isChildKey, @@ -45,29 +43,27 @@ class DerivedHDKeyIterator implements IterableIterator { }; } - public [Symbol.iterator](): IterableIterator { + public [Symbol.iterator](): IterableIterator { return this; } } export const walletUtils = { - DEFAULT_ADDRESS_SEARCH_LIMIT, - DEFAULT_NUM_ADDRESSES_TO_FETCH: 10, - calculateDerivedHDKeys(initialDerivedKey: DerivedHDKey, numberOfKeys: number): DerivedHDKey[] { - const derivedKeys: DerivedHDKey[] = []; - const derivedKeyIterator = new DerivedHDKeyIterator(initialDerivedKey, numberOfKeys); + calculateDerivedHDKeyInfos(initialDerivedKey: DerivedHDKeyInfo, numberOfKeys: number): DerivedHDKeyInfo[] { + const derivedKeys: DerivedHDKeyInfo[] = []; + const derivedKeyIterator = new DerivedHDKeyInfoIterator(initialDerivedKey, numberOfKeys); for (const key of derivedKeyIterator) { derivedKeys.push(key); } return derivedKeys; }, - findDerivedKeyByAddress( + findDerivedKeyInfoForAddressIfExists( address: string, - initialDerivedKey: DerivedHDKey, + initialDerivedKey: DerivedHDKeyInfo, searchLimit: number, - ): DerivedHDKey | undefined { - let matchedKey: DerivedHDKey | undefined; - const derivedKeyIterator = new DerivedHDKeyIterator(initialDerivedKey, searchLimit); + ): DerivedHDKeyInfo | undefined { + let matchedKey: DerivedHDKeyInfo | undefined; + const derivedKeyIterator = new DerivedHDKeyInfoIterator(initialDerivedKey, searchLimit); for (const key of derivedKeyIterator) { if (key.address === address) { matchedKey = key; -- cgit v1.2.3