diff options
author | Jacob Evans <jacob@dekz.net> | 2018-04-11 16:48:46 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2018-04-11 17:08:28 +0800 |
commit | 916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9 (patch) | |
tree | 663308bd8318c36867d4b216ea3ebcb15c6aa7cf /packages/subproviders/src/utils | |
parent | f44ef7ce59cd5c811a92662d3fb095f21d80f665 (diff) | |
download | dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.tar dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.tar.gz dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.tar.bz2 dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.tar.lz dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.tar.xz dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.tar.zst dexon-sol-tools-916b4d3a26e6189c77634b0d2cde4d20bb4cb9a9.zip |
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
Diffstat (limited to 'packages/subproviders/src/utils')
-rw-r--r-- | packages/subproviders/src/utils/wallet_utils.ts | 40 |
1 files changed, 18 insertions, 22 deletions
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<DerivedHDKey> { - private _initialDerivedKey: DerivedHDKey; +class DerivedHDKeyInfoIterator implements IterableIterator<DerivedHDKeyInfo> { + 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<DerivedHDKey> { - const derivationBasePath = this._initialDerivedKey.derivationBasePath; + public next(): IteratorResult<DerivedHDKeyInfo> { + 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<DerivedHDKey> { }; } - public [Symbol.iterator](): IterableIterator<DerivedHDKey> { + public [Symbol.iterator](): IterableIterator<DerivedHDKeyInfo> { 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; |