aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src/utils/wallet_utils.ts
diff options
context:
space:
mode:
authorJacob Evans <dekz@dekz.net>2018-04-12 15:35:32 +0800
committerGitHub <noreply@github.com>2018-04-12 15:35:32 +0800
commited0c64fdcf5c180107f54ad916c11c4901a4c01c (patch)
tree89d71313275a970121bc8241ad20045160197505 /packages/subproviders/src/utils/wallet_utils.ts
parent364d8824af412790e3cd4d82ff7f98c2f2a75fa1 (diff)
parent9a91e39b3f9419287ba0c508b86d519db4e72dd7 (diff)
downloaddexon-sol-tools-ed0c64fdcf5c180107f54ad916c11c4901a4c01c.tar.gz
dexon-sol-tools-ed0c64fdcf5c180107f54ad916c11c4901a4c01c.tar.zst
dexon-sol-tools-ed0c64fdcf5c180107f54ad916c11c4901a4c01c.zip
Merge pull request #507 from 0xProject/feature/subproviders/mnemonic-wallet-subprovider
Mnemonic wallet subprovider and multiple accounts in ledger
Diffstat (limited to 'packages/subproviders/src/utils/wallet_utils.ts')
-rw-r--r--packages/subproviders/src/utils/wallet_utils.ts79
1 files changed, 79 insertions, 0 deletions
diff --git a/packages/subproviders/src/utils/wallet_utils.ts b/packages/subproviders/src/utils/wallet_utils.ts
new file mode 100644
index 000000000..cd5cd9ba0
--- /dev/null
+++ b/packages/subproviders/src/utils/wallet_utils.ts
@@ -0,0 +1,79 @@
+import ethUtil = require('ethereumjs-util');
+import HDNode = require('hdkey');
+import * as _ from 'lodash';
+
+import { DerivedHDKeyInfo, WalletSubproviderErrors } from '../types';
+
+const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;
+
+class DerivedHDKeyInfoIterator implements IterableIterator<DerivedHDKeyInfo> {
+ private _parentDerivedKeyInfo: DerivedHDKeyInfo;
+ private _searchLimit: number;
+ private _index: number;
+
+ constructor(initialDerivedKey: DerivedHDKeyInfo, searchLimit: number = DEFAULT_ADDRESS_SEARCH_LIMIT) {
+ this._searchLimit = searchLimit;
+ this._parentDerivedKeyInfo = initialDerivedKey;
+ this._index = 0;
+ }
+
+ public next(): IteratorResult<DerivedHDKeyInfo> {
+ const baseDerivationPath = this._parentDerivedKeyInfo.baseDerivationPath;
+ const derivationIndex = this._index;
+ const fullDerivationPath = `m/${baseDerivationPath}/${derivationIndex}`;
+ const path = `m/${derivationIndex}`;
+ const hdKey = this._parentDerivedKeyInfo.hdKey.derive(path);
+ const address = walletUtils.addressOfHDKey(hdKey);
+ const derivedKey: DerivedHDKeyInfo = {
+ address,
+ hdKey,
+ baseDerivationPath,
+ derivationPath: fullDerivationPath,
+ };
+ const done = this._index === this._searchLimit;
+ this._index++;
+ return {
+ done,
+ value: derivedKey,
+ };
+ }
+
+ public [Symbol.iterator](): IterableIterator<DerivedHDKeyInfo> {
+ return this;
+ }
+}
+
+export const walletUtils = {
+ calculateDerivedHDKeyInfos(parentDerivedKeyInfo: DerivedHDKeyInfo, numberOfKeys: number): DerivedHDKeyInfo[] {
+ const derivedKeys: DerivedHDKeyInfo[] = [];
+ const derivedKeyIterator = new DerivedHDKeyInfoIterator(parentDerivedKeyInfo, numberOfKeys);
+ for (const key of derivedKeyIterator) {
+ derivedKeys.push(key);
+ }
+ return derivedKeys;
+ },
+ findDerivedKeyInfoForAddressIfExists(
+ address: string,
+ parentDerivedKeyInfo: DerivedHDKeyInfo,
+ searchLimit: number,
+ ): DerivedHDKeyInfo | undefined {
+ let matchedKey: DerivedHDKeyInfo | undefined;
+ const derivedKeyIterator = new DerivedHDKeyInfoIterator(parentDerivedKeyInfo, searchLimit);
+ for (const key of derivedKeyIterator) {
+ if (key.address === address) {
+ matchedKey = key;
+ break;
+ }
+ }
+ return matchedKey;
+ },
+ addressOfHDKey(hdKey: HDNode): string {
+ const shouldSanitizePublicKey = true;
+ const derivedPublicKey = hdKey.publicKey;
+ const ethereumAddressUnprefixed = ethUtil
+ .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
+ .toString('hex');
+ const address = ethUtil.addHexPrefix(ethereumAddressUnprefixed).toLowerCase();
+ return address;
+ },
+};