aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src/utils
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-04-10 12:39:43 +0800
committerJacob Evans <jacob@dekz.net>2018-04-10 12:39:43 +0800
commit84a4b7d1c16d008ffbf07ba5b22c61b025b5165f (patch)
tree54b6156824c31df39159f78cd306f0b8d2e26dc4 /packages/subproviders/src/utils
parent5b69cd4a22ce1720f4441aaa74c86f895015c0fd (diff)
downloaddexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.tar
dexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.tar.gz
dexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.tar.bz2
dexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.tar.lz
dexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.tar.xz
dexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.tar.zst
dexon-sol-tools-84a4b7d1c16d008ffbf07ba5b22c61b025b5165f.zip
Refactor ledger to support multiple accounts with from address
Diffstat (limited to 'packages/subproviders/src/utils')
-rw-r--r--packages/subproviders/src/utils/wallet_utils.ts76
1 files changed, 76 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..6c698a006
--- /dev/null
+++ b/packages/subproviders/src/utils/wallet_utils.ts
@@ -0,0 +1,76 @@
+import ethUtil = require('ethereumjs-util');
+import HDNode = require('hdkey');
+import * as _ from 'lodash';
+
+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,
+ ): DerivedHDKey[] {
+ const derivedKeys: DerivedHDKey[] = [];
+ _.times(searchLimit, i => {
+ const derivationIndex = offset + i;
+ const path = hardened ? `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);
+ const derivedKey: DerivedHDKey = {
+ derivationPath,
+ hdKey,
+ address,
+ derivationIndex,
+ };
+ derivedKeys.push(derivedKey);
+ });
+ return derivedKeys;
+ },
+
+ _findDerivedKeyByAddress(
+ address: string,
+ initialHDKey: HDNode,
+ derivationPath: string,
+ searchLimit: number,
+ hardened: 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,
+ hardened,
+ );
+ matchedKey = _.find(derivedKeys, derivedKey => derivedKey.address === address);
+ if (matchedKey) {
+ break;
+ }
+ }
+ return matchedKey;
+ },
+
+ _firstDerivedKey(initialHDKey: HDNode, derivationPath: string, hardened: boolean = false): DerivedHDKey {
+ const derivedKeys = walletUtils._calculateDerivedHDKeys(initialHDKey, derivationPath, 1, 0, hardened);
+ const firstDerivedKey = derivedKeys[0];
+ return firstDerivedKey;
+ },
+};