aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/subproviders/src')
-rw-r--r--packages/subproviders/src/subproviders/ledger.ts73
-rw-r--r--packages/subproviders/src/subproviders/mnemonic_wallet.ts91
-rw-r--r--packages/subproviders/src/subproviders/private_key_wallet.ts21
-rw-r--r--packages/subproviders/src/types.ts18
-rw-r--r--packages/subproviders/src/utils/wallet_utils.ts40
5 files changed, 135 insertions, 108 deletions
diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts
index 9b2d9d7d0..33aa5c1bf 100644
--- a/packages/subproviders/src/subproviders/ledger.ts
+++ b/packages/subproviders/src/subproviders/ledger.ts
@@ -8,7 +8,7 @@ import { Lock } from 'semaphore-async-await';
import {
Callback,
- DerivedHDKey,
+ DerivedHDKeyInfo,
LedgerEthereumClient,
LedgerEthereumClientFactoryAsync,
LedgerSubproviderConfigs,
@@ -21,9 +21,11 @@ import { walletUtils } from '../utils/wallet_utils';
import { BaseWalletSubprovider } from './base_wallet_subprovider';
-const DEFAULT_DERIVATION_PATH = `44'/60'/0'`;
+const DEFAULT_BASE_DERIVATION_PATH = `44'/60'/0'`;
const ASK_FOR_ON_DEVICE_CONFIRMATION = false;
const SHOULD_GET_CHAIN_CODE = true;
+const DEFAULT_NUM_ADDRESSES_TO_FETCH = 10;
+const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;
/**
* Subprovider for interfacing with a user's [Ledger Nano S](https://www.ledgerwallet.com/products/ledger-nano-s).
@@ -34,7 +36,7 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
private _nonceLock = new Lock();
private _connectionLock = new Lock();
private _networkId: number;
- private _derivationBasePath: string;
+ private _baseDerivationPath: string;
private _ledgerEthereumClientFactoryAsync: LedgerEthereumClientFactoryAsync;
private _ledgerClientIfExists?: LedgerEthereumClient;
private _shouldAlwaysAskForConfirmation: boolean;
@@ -49,7 +51,7 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
super();
this._networkId = config.networkId;
this._ledgerEthereumClientFactoryAsync = config.ledgerEthereumClientFactoryAsync;
- this._derivationBasePath = config.derivationPath || DEFAULT_DERIVATION_PATH;
+ this._baseDerivationPath = config.baseDerivationPath || DEFAULT_BASE_DERIVATION_PATH;
this._shouldAlwaysAskForConfirmation =
!_.isUndefined(config.accountFetchingConfigs) &&
!_.isUndefined(config.accountFetchingConfigs.shouldAskForOnDeviceConfirmation)
@@ -59,21 +61,21 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
!_.isUndefined(config.accountFetchingConfigs) &&
!_.isUndefined(config.accountFetchingConfigs.addressSearchLimit)
? config.accountFetchingConfigs.addressSearchLimit
- : walletUtils.DEFAULT_ADDRESS_SEARCH_LIMIT;
+ : DEFAULT_ADDRESS_SEARCH_LIMIT;
}
/**
* Retrieve the set derivation path
* @returns derivation path
*/
public getPath(): string {
- return this._derivationBasePath;
+ return this._baseDerivationPath;
}
/**
* Set a desired derivation path when computing the available user addresses
- * @param derivationPath The desired derivation path (e.g `44'/60'/0'`)
+ * @param basDerivationPath The desired derivation path (e.g `44'/60'/0'`)
*/
- public setPath(derivationPath: string) {
- this._derivationBasePath = derivationPath;
+ public setPath(basDerivationPath: string) {
+ this._baseDerivationPath = basDerivationPath;
}
/**
* Retrieve a users Ledger accounts. The accounts are derived from the derivationPath,
@@ -84,12 +86,10 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
* @param numberOfAccounts Number of accounts to retrieve (default: 10)
* @return An array of accounts
*/
- public async getAccountsAsync(
- numberOfAccounts: number = walletUtils.DEFAULT_NUM_ADDRESSES_TO_FETCH,
- ): Promise<string[]> {
- const initialDerivedKey = await this._initialDerivedKeyAsync();
- const derivedKeys = walletUtils.calculateDerivedHDKeys(initialDerivedKey, numberOfAccounts);
- const accounts = _.map(derivedKeys, 'address');
+ public async getAccountsAsync(numberOfAccounts: number = DEFAULT_NUM_ADDRESSES_TO_FETCH): Promise<string[]> {
+ const initialDerivedKeyInfo = await this._initialDerivedKeyInfoAsync();
+ const derivedKeyInfos = walletUtils.calculateDerivedHDKeyInfos(initialDerivedKeyInfo, numberOfAccounts);
+ const accounts = _.map(derivedKeyInfos, k => k.address);
return accounts;
}
/**
@@ -102,8 +102,11 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
*/
public async signTransactionAsync(txParams: PartialTxParams): Promise<string> {
LedgerSubprovider._validateTxParams(txParams);
- const initialDerivedKey = await this._initialDerivedKeyAsync();
- const derivedKey = this._findDerivedKeyByPublicAddress(initialDerivedKey, txParams.from);
+ if (_.isUndefined(txParams.from) || !addressUtils.isAddress(txParams.from)) {
+ throw new Error(WalletSubproviderErrors.FromAddressMissingOrInvalid);
+ }
+ const initialDerivedKeyInfo = await this._initialDerivedKeyInfoAsync();
+ const derivedKeyInfo = this._findDerivedKeyInfoForAddress(initialDerivedKeyInfo, txParams.from);
const ledgerClient = await this._createLedgerClientAsync();
@@ -116,7 +119,7 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
const txHex = tx.serialize().toString('hex');
try {
- const fullDerivationPath = derivedKey.derivationPath;
+ const fullDerivationPath = derivedKeyInfo.derivationPath;
const result = await ledgerClient.signTransaction(fullDerivationPath, txHex);
// Store signature in transaction
tx.r = Buffer.from(result.r, 'hex');
@@ -155,12 +158,13 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
throw new Error(WalletSubproviderErrors.DataMissingForSignPersonalMessage);
}
assert.isHexString('data', data);
- const initialDerivedKey = await this._initialDerivedKeyAsync();
- const derivedKey = this._findDerivedKeyByPublicAddress(initialDerivedKey, address);
+ assert.isETHAddressHex('address', address);
+ const initialDerivedKeyInfo = await this._initialDerivedKeyInfoAsync();
+ const derivedKeyInfo = this._findDerivedKeyInfoForAddress(initialDerivedKeyInfo, address);
const ledgerClient = await this._createLedgerClientAsync();
try {
- const fullDerivationPath = derivedKey.derivationPath;
+ const fullDerivationPath = derivedKeyInfo.derivationPath;
const result = await ledgerClient.signPersonalMessage(fullDerivationPath, ethUtil.stripHexPrefix(data));
const v = result.v - 27;
let vHex = v.toString(16);
@@ -196,13 +200,13 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
this._ledgerClientIfExists = undefined;
this._connectionLock.release();
}
- private async _initialDerivedKeyAsync(): Promise<DerivedHDKey> {
+ private async _initialDerivedKeyInfoAsync(): Promise<DerivedHDKeyInfo> {
const ledgerClient = await this._createLedgerClientAsync();
let ledgerResponse;
try {
ledgerResponse = await ledgerClient.getAddress(
- this._derivationBasePath,
+ this._baseDerivationPath,
this._shouldAlwaysAskForConfirmation,
SHOULD_GET_CHAIN_CODE,
);
@@ -212,23 +216,26 @@ export class LedgerSubprovider extends BaseWalletSubprovider {
const hdKey = new HDNode();
hdKey.publicKey = new Buffer(ledgerResponse.publicKey, 'hex');
hdKey.chainCode = new Buffer(ledgerResponse.chainCode, 'hex');
+ const derivationPath = `${this._baseDerivationPath}/0`;
+ const derivationIndex = 0;
return {
hdKey,
address: ledgerResponse.address,
isChildKey: true,
- derivationBasePath: this._derivationBasePath,
- derivationPath: `${this._derivationBasePath}/0`,
- derivationIndex: 0,
+ baseDerivationPath: this._baseDerivationPath,
+ derivationPath,
+ derivationIndex,
};
}
- private _findDerivedKeyByPublicAddress(initalHDKey: DerivedHDKey, address: string): DerivedHDKey {
- if (_.isUndefined(address) || !addressUtils.isAddress(address)) {
- throw new Error(WalletSubproviderErrors.FromAddressMissingOrInvalid);
- }
- const matchedDerivedKey = walletUtils.findDerivedKeyByAddress(address, initalHDKey, this._addressSearchLimit);
- if (_.isUndefined(matchedDerivedKey)) {
+ private _findDerivedKeyInfoForAddress(initalHDKey: DerivedHDKeyInfo, address: string): DerivedHDKeyInfo {
+ const matchedDerivedKeyInfo = walletUtils.findDerivedKeyInfoForAddressIfExists(
+ address,
+ initalHDKey,
+ this._addressSearchLimit,
+ );
+ if (_.isUndefined(matchedDerivedKeyInfo)) {
throw new Error(`${WalletSubproviderErrors.AddressNotFound}: ${address}`);
}
- return matchedDerivedKey;
+ return matchedDerivedKeyInfo;
}
}
diff --git a/packages/subproviders/src/subproviders/mnemonic_wallet.ts b/packages/subproviders/src/subproviders/mnemonic_wallet.ts
index de34a9d11..9a627b1ec 100644
--- a/packages/subproviders/src/subproviders/mnemonic_wallet.ts
+++ b/packages/subproviders/src/subproviders/mnemonic_wallet.ts
@@ -5,13 +5,17 @@ import ethUtil = require('ethereumjs-util');
import HDNode = require('hdkey');
import * as _ from 'lodash';
-import { DerivedHDKey, PartialTxParams, WalletSubproviderErrors } from '../types';
+import { DerivedHDKeyInfo, MnemonicWalletSubproviderConfigs, PartialTxParams, WalletSubproviderErrors } from '../types';
import { walletUtils } from '../utils/wallet_utils';
import { BaseWalletSubprovider } from './base_wallet_subprovider';
import { PrivateKeyWalletSubprovider } from './private_key_wallet';
-const DEFAULT_DERIVATION_PATH = `44'/60'/0'/0`;
+const DEFAULT_BASE_DERIVATION_PATH = `44'/60'/0'/0`;
+const DEFAULT_NUM_ADDRESSES_TO_FETCH = 10;
+const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;
+const ROOT_KEY_DERIVATION_PATH = 'm/';
+const ROOT_KEY_DERIVATION_INDEX = 0;
/**
* This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface.
@@ -20,35 +24,33 @@ const DEFAULT_DERIVATION_PATH = `44'/60'/0'/0`;
*/
export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
private _addressSearchLimit: number;
- private _derivationBasePath: string;
- private _derivedKey: DerivedHDKey;
+ private _baseDerivationPath: string;
+ private _derivedKeyInfo: DerivedHDKeyInfo;
/**
- * Instantiates a MnemonicWalletSubprovider. Defaults to derivationPath set to `44'/60'/0'/0`.
- * This is the default in TestRPC/Ganache, this can be overridden if desired.
- * @param mnemonic The mnemonic seed
- * @param derivationBasePath The derivation path, defaults to `44'/60'/0'/0`
- * @param addressSearchLimit The limit on address search attempts before raising `WalletSubproviderErrors.AddressNotFound`
+ * Instantiates a MnemonicWalletSubprovider. Defaults to baseDerivationPath set to `44'/60'/0'/0`.
+ * This is the default in TestRPC/Ganache, it can be overridden if desired.
+ * @param config Several available configurations
* @return MnemonicWalletSubprovider instance
*/
- constructor(
- mnemonic: string,
- derivationBasePath: string = DEFAULT_DERIVATION_PATH,
- addressSearchLimit: number = walletUtils.DEFAULT_ADDRESS_SEARCH_LIMIT,
- ) {
+ constructor(config: MnemonicWalletSubproviderConfigs) {
+ const mnemonic = config.mnemonic;
assert.isString('mnemonic', mnemonic);
- assert.isString('derivationPath', derivationBasePath);
+ const baseDerivationPath = config.baseDerivationPath || DEFAULT_BASE_DERIVATION_PATH;
+ assert.isString('baseDerivationPath', baseDerivationPath);
+ const addressSearchLimit = config.addressSearchLimit || DEFAULT_ADDRESS_SEARCH_LIMIT;
assert.isNumber('addressSearchLimit', addressSearchLimit);
super();
+
const seed = bip39.mnemonicToSeed(mnemonic);
const hdKey = HDNode.fromMasterSeed(seed);
- this._derivationBasePath = derivationBasePath;
- this._derivedKey = {
+ this._baseDerivationPath = baseDerivationPath;
+ this._derivedKeyInfo = {
address: walletUtils.addressOfHDKey(hdKey),
// HACK this isn't the base path for this root key, but is is the base path
- // we want all of the derived children to spawn from
- derivationBasePath: this._derivationBasePath,
- derivationPath: 'm/0',
- derivationIndex: 0,
+ // we want all of the derived children to branched from
+ baseDerivationPath: this._baseDerivationPath,
+ derivationPath: ROOT_KEY_DERIVATION_PATH,
+ derivationIndex: ROOT_KEY_DERIVATION_INDEX,
hdKey,
isChildKey: false,
};
@@ -59,17 +61,17 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
* @returns derivation path
*/
public getPath(): string {
- return this._derivationBasePath;
+ return this._baseDerivationPath;
}
/**
* Set a desired derivation path when computing the available user addresses
- * @param derivationPath The desired derivation path (e.g `44'/60'/0'`)
+ * @param baseDerivationPath The desired derivation path (e.g `44'/60'/0'`)
*/
- public setPath(derivationPath: string) {
- this._derivationBasePath = derivationPath;
- this._derivedKey = {
- ...this._derivedKey,
- derivationBasePath: this._derivationBasePath,
+ public setPath(baseDerivationPath: string) {
+ this._baseDerivationPath = baseDerivationPath;
+ this._derivedKeyInfo = {
+ ...this._derivedKeyInfo,
+ baseDerivationPath,
};
}
/**
@@ -79,11 +81,9 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
* @param numberOfAccounts Number of accounts to retrieve (default: 10)
* @return An array of accounts
*/
- public async getAccountsAsync(
- numberOfAccounts: number = walletUtils.DEFAULT_NUM_ADDRESSES_TO_FETCH,
- ): Promise<string[]> {
- const derivedKeys = walletUtils.calculateDerivedHDKeys(this._derivedKey, numberOfAccounts);
- const accounts = _.map(derivedKeys, 'address');
+ public async getAccountsAsync(numberOfAccounts: number = DEFAULT_NUM_ADDRESSES_TO_FETCH): Promise<string[]> {
+ const derivedKeys = walletUtils.calculateDerivedHDKeyInfos(this._derivedKeyInfo, numberOfAccounts);
+ const accounts = _.map(derivedKeys, k => k.address);
return accounts;
}
@@ -96,6 +96,9 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
* @return Signed transaction hex string
*/
public async signTransactionAsync(txParams: PartialTxParams): Promise<string> {
+ if (_.isUndefined(txParams.from) || !addressUtils.isAddress(txParams.from)) {
+ throw new Error(WalletSubproviderErrors.FromAddressMissingOrInvalid);
+ }
const privateKeyWallet = this._privateKeyWalletForAddress(txParams.from);
const signedTx = privateKeyWallet.signTransactionAsync(txParams);
return signedTx;
@@ -111,28 +114,30 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
* @return Signature hex string (order: rsv)
*/
public async signPersonalMessageAsync(data: string, address: string): Promise<string> {
+ if (_.isUndefined(data)) {
+ throw new Error(WalletSubproviderErrors.DataMissingForSignPersonalMessage);
+ }
+ assert.isHexString('data', data);
+ assert.isETHAddressHex('address', address);
const privateKeyWallet = this._privateKeyWalletForAddress(address);
const sig = await privateKeyWallet.signPersonalMessageAsync(data, address);
return sig;
}
private _privateKeyWalletForAddress(address: string): PrivateKeyWalletSubprovider {
- const derivedKey = this._findDerivedKeyByPublicAddress(address);
- const privateKeyHex = derivedKey.hdKey.privateKey.toString('hex');
+ const derivedKeyInfo = this._findDerivedKeyInfoForAddress(address);
+ const privateKeyHex = derivedKeyInfo.hdKey.privateKey.toString('hex');
const privateKeyWallet = new PrivateKeyWalletSubprovider(privateKeyHex);
return privateKeyWallet;
}
- private _findDerivedKeyByPublicAddress(address: string): DerivedHDKey {
- if (_.isUndefined(address) || !addressUtils.isAddress(address)) {
- throw new Error(WalletSubproviderErrors.FromAddressMissingOrInvalid);
- }
- const matchedDerivedKey = walletUtils.findDerivedKeyByAddress(
+ private _findDerivedKeyInfoForAddress(address: string): DerivedHDKeyInfo {
+ const matchedDerivedKeyInfo = walletUtils.findDerivedKeyInfoForAddressIfExists(
address,
- this._derivedKey,
+ this._derivedKeyInfo,
this._addressSearchLimit,
);
- if (_.isUndefined(matchedDerivedKey)) {
+ if (_.isUndefined(matchedDerivedKeyInfo)) {
throw new Error(`${WalletSubproviderErrors.AddressNotFound}: ${address}`);
}
- return matchedDerivedKey;
+ return matchedDerivedKeyInfo;
}
}
diff --git a/packages/subproviders/src/subproviders/private_key_wallet.ts b/packages/subproviders/src/subproviders/private_key_wallet.ts
index f8afab722..f3727039c 100644
--- a/packages/subproviders/src/subproviders/private_key_wallet.ts
+++ b/packages/subproviders/src/subproviders/private_key_wallet.ts
@@ -17,7 +17,7 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider {
private _privateKeyBuffer: Buffer;
/**
* Instantiates a PrivateKeyWalletSubprovider.
- * @param privateKey The private key of the ethereum address
+ * @param privateKey The corresponding private key to an Ethereum address
* @return PrivateKeyWalletSubprovider instance
*/
constructor(privateKey: string) {
@@ -46,7 +46,11 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider {
public async signTransactionAsync(txParams: PartialTxParams): Promise<string> {
PrivateKeyWalletSubprovider._validateTxParams(txParams);
if (!_.isUndefined(txParams.from) && txParams.from !== this._address) {
- throw new Error(`${WalletSubproviderErrors.AddressNotFound}: ${txParams.from}`);
+ throw new Error(
+ `Requested to sign transaction with address: ${txParams.from}, instantiated with address: ${
+ this._address
+ }`,
+ );
}
const tx = new EthereumTx(txParams);
tx.sign(this._privateKeyBuffer);
@@ -54,8 +58,8 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider {
return rawTx;
}
/**
- * Sign a personal Ethereum signed message. The signing address will be
- * calculated from the private key, if an address is provided it must match the address calculated from the private key.
+ * Sign a personal Ethereum signed message. The signing address will be calculated from the private key.
+ * if an address is provided it must match the address calculated from the private key.
* If you've added this Subprovider to your app's provider, you can simply send an `eth_sign`
* or `personal_sign` JSON RPC request, and this method will be called auto-magically.
* If you are not using this via a ProviderEngine instance, you can call it directly.
@@ -67,10 +71,13 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider {
if (_.isUndefined(data)) {
throw new Error(WalletSubproviderErrors.DataMissingForSignPersonalMessage);
}
- if (_.isUndefined(address) || address !== this._address) {
- throw new Error(`${WalletSubproviderErrors.FromAddressMissingOrInvalid}: ${address}`);
- }
assert.isHexString('data', data);
+ assert.isETHAddressHex('address', address);
+ if (address !== this._address) {
+ throw new Error(
+ `Requested to sign message with address: ${address}, instantiated with address: ${this._address}`,
+ );
+ }
const dataBuff = ethUtil.toBuffer(data);
const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
const sig = ethUtil.ecsign(msgHashBuff, this._privateKeyBuffer);
diff --git a/packages/subproviders/src/types.ts b/packages/subproviders/src/types.ts
index 121992278..b9d9d08ee 100644
--- a/packages/subproviders/src/types.ts
+++ b/packages/subproviders/src/types.ts
@@ -41,11 +41,12 @@ export type LedgerEthereumClientFactoryAsync = () => Promise<LedgerEthereumClien
export interface LedgerSubproviderConfigs {
networkId: number;
ledgerEthereumClientFactoryAsync: LedgerEthereumClientFactoryAsync;
- derivationPath?: string;
+ baseDerivationPath?: string;
accountFetchingConfigs?: AccountFetchingConfigs;
}
/*
+ * addressSearchLimit: The maximum number of addresses to search through, defaults to 1000
* numAddressesToReturn: Number of addresses to return from 'eth_accounts' call
* shouldAskForOnDeviceConfirmation: Whether you wish to prompt the user on their Ledger
* before fetching their addresses
@@ -56,6 +57,17 @@ export interface AccountFetchingConfigs {
shouldAskForOnDeviceConfirmation?: boolean;
}
+/*
+ * mnemonic: The string mnemonic seed
+ * addressSearchLimit: The maximum number of addresses to search through, defaults to 1000
+ * baseDerivationPath: The base derivation path (e.g 44'/60'/0'/0)
+ */
+export interface MnemonicWalletSubproviderConfigs {
+ mnemonic: string;
+ addressSearchLimit?: number;
+ baseDerivationPath?: string;
+}
+
export interface SignatureData {
hash: string;
r: string;
@@ -106,10 +118,10 @@ export enum NonceSubproviderErrors {
EmptyParametersFound = 'EMPTY_PARAMETERS_FOUND',
CannotDetermineAddressFromPayload = 'CANNOT_DETERMINE_ADDRESS_FROM_PAYLOAD',
}
-export interface DerivedHDKey {
+export interface DerivedHDKeyInfo {
address: string;
derivationIndex: number;
- derivationBasePath: string;
+ baseDerivationPath: string;
derivationPath: string;
hdKey: HDNode;
isChildKey: boolean;
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;