aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src/utils/wallet_utils.ts
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-04-11 10:22:41 +0800
committerJacob Evans <jacob@dekz.net>2018-04-11 10:22:41 +0800
commit9169913a2cbe9d421629db2e8169c0806044e3fc (patch)
treee2366700c2ab72ed3051a899816b74d4b103dcd6 /packages/subproviders/src/utils/wallet_utils.ts
parent4e4842a62f6b2d728e2d9986acf5116032f763f2 (diff)
downloaddexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.tar
dexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.tar.gz
dexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.tar.bz2
dexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.tar.lz
dexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.tar.xz
dexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.tar.zst
dexon-sol-tools-9169913a2cbe9d421629db2e8169c0806044e3fc.zip
Add isChildKey to derived key
Diffstat (limited to 'packages/subproviders/src/utils/wallet_utils.ts')
-rw-r--r--packages/subproviders/src/utils/wallet_utils.ts59
1 files changed, 28 insertions, 31 deletions
diff --git a/packages/subproviders/src/utils/wallet_utils.ts b/packages/subproviders/src/utils/wallet_utils.ts
index 48d475559..0c3e895b6 100644
--- a/packages/subproviders/src/utils/wallet_utils.ts
+++ b/packages/subproviders/src/utils/wallet_utils.ts
@@ -8,54 +8,51 @@ const DEFAULT_ADDRESS_SEARCH_OFFSET = 0;
const BATCH_SIZE = 10;
export const walletUtils = {
- _calculateDerivedHDKeys(
- initialHDKey: HDNode,
- derivationPath: string,
+ DEFAULT_NUM_ADDRESSES_TO_FETCH: 10,
+ DEFAULT_ADDRESS_SEARCH_LIMIT: 1000,
+ calculateDerivedHDKeys(
+ initialDerivedKey: DerivedHDKey,
searchLimit: number,
offset: number = DEFAULT_ADDRESS_SEARCH_OFFSET,
- isChildKey: boolean = false,
): DerivedHDKey[] {
const derivedKeys: DerivedHDKey[] = [];
_.times(searchLimit, i => {
+ const derivationPath = initialDerivedKey.derivationPath;
const derivationIndex = offset + i;
- // 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;
- const ethereumAddressUnprefixed = ethUtil
- .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
- .toString('hex');
- const address = ethUtil.addHexPrefix(ethereumAddressUnprefixed);
+ // If the DerivedHDKey is a child then we walk relative, if not we walk the full derivation path
+ const path = initialDerivedKey.isChildKey
+ ? `m/${derivationIndex}`
+ : `m/${derivationPath}/${derivationIndex}`;
+ const hdKey = initialDerivedKey.hdKey.derive(path);
+ const address = walletUtils.addressOfHDKey(hdKey);
const derivedKey: DerivedHDKey = {
- derivationPath,
- hdKey,
address,
+ hdKey,
+ derivationPath,
derivationIndex,
+ isChildKey: initialDerivedKey.isChildKey,
};
derivedKeys.push(derivedKey);
});
return derivedKeys;
},
-
- _findDerivedKeyByAddress(
+ addressOfHDKey(hdKey: HDNode): string {
+ const shouldSanitizePublicKey = true;
+ const derivedPublicKey = hdKey.publicKey;
+ const ethereumAddressUnprefixed = ethUtil
+ .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
+ .toString('hex');
+ const address = ethUtil.addHexPrefix(ethereumAddressUnprefixed);
+ return address;
+ },
+ findDerivedKeyByAddress(
address: string,
- initialHDKey: HDNode,
- derivationPath: string,
+ initialDerivedKey: DerivedHDKey,
searchLimit: number,
- isChild: 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,
- isChild,
- );
+ const derivedKeys = walletUtils.calculateDerivedHDKeys(initialDerivedKey, BATCH_SIZE, index);
matchedKey = _.find(derivedKeys, derivedKey => derivedKey.address === address);
if (matchedKey) {
break;
@@ -64,8 +61,8 @@ export const walletUtils = {
return matchedKey;
},
- _firstDerivedKey(initialHDKey: HDNode, derivationPath: string, isChild: boolean = false): DerivedHDKey {
- const derivedKeys = walletUtils._calculateDerivedHDKeys(initialHDKey, derivationPath, 1, 0, isChild);
+ _firstDerivedKey(initialDerivedKey: DerivedHDKey): DerivedHDKey {
+ const derivedKeys = walletUtils.calculateDerivedHDKeys(initialDerivedKey, 1, 0);
const firstDerivedKey = derivedKeys[0];
return firstDerivedKey;
},