aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src/walletUtils.ts
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-04-10 09:58:12 +0800
committerJacob Evans <jacob@dekz.net>2018-04-10 09:58:12 +0800
commit5b69cd4a22ce1720f4441aaa74c86f895015c0fd (patch)
tree2a38d521a8b4a944d3e7c3634a56583b3960b354 /packages/subproviders/src/walletUtils.ts
parent0e8f5004d6a53929a4cc1de9231c43899c8b6eeb (diff)
downloaddexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.tar
dexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.tar.gz
dexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.tar.bz2
dexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.tar.lz
dexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.tar.xz
dexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.tar.zst
dexon-sol-tools-5b69cd4a22ce1720f4441aaa74c86f895015c0fd.zip
Added walletUtils and address in signMessage
Diffstat (limited to 'packages/subproviders/src/walletUtils.ts')
-rw-r--r--packages/subproviders/src/walletUtils.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/packages/subproviders/src/walletUtils.ts b/packages/subproviders/src/walletUtils.ts
new file mode 100644
index 000000000..631636a71
--- /dev/null
+++ b/packages/subproviders/src/walletUtils.ts
@@ -0,0 +1,58 @@
+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;
+export const walletUtils = {
+ _calculateDerivedHDKeys(
+ initialHDKey: HDNode,
+ derivationPath: string,
+ searchLimit: number,
+ offset: number = DEFAULT_ADDRESS_SEARCH_OFFSET,
+ ): DerivedHDKey[] {
+ const derivedKeys: DerivedHDKey[] = [];
+ _.times(searchLimit, i => {
+ const path = `m/${derivationPath}/${i + offset}`;
+ 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: path,
+ hdKey,
+ address,
+ };
+ derivedKeys.push(derivedKey);
+ });
+ return derivedKeys;
+ },
+
+ _findDerivedKeyByAddress(
+ address: string,
+ initialHDKey: HDNode,
+ derivationPath: string,
+ searchLimit: number,
+ ): 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);
+ matchedKey = _.find(derivedKeys, derivedKey => derivedKey.address === address);
+ if (matchedKey) {
+ break;
+ }
+ }
+ return matchedKey;
+ },
+
+ _firstDerivedKey(initialHDKey: HDNode, derivationPath: string): DerivedHDKey {
+ const derivedKeys = walletUtils._calculateDerivedHDKeys(initialHDKey, derivationPath, 1);
+ const firstDerivedKey = derivedKeys[0];
+ return firstDerivedKey;
+ },
+};