From 4e4842a62f6b2d728e2d9986acf5116032f763f2 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 10 Apr 2018 16:01:34 +1000 Subject: Improve Documentation for functions and constructors --- .../src/subproviders/mnemonic_wallet_subprovider.ts | 18 ++++++++++++++---- .../src/subproviders/private_key_wallet_subprovider.ts | 12 ++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'packages') diff --git a/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts b/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts index 3ff437659..02d06e2cd 100644 --- a/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts @@ -23,6 +23,14 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider { private _addressSearchLimit: number; private _derivationPath: string; private _hdKey: HDNode; + /** + * 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 derivationPath The derivation path, defaults to `44'/60'/0'/0` + * @param addressSearchLimit The limit on address search attempts before raising `WalletSubproviderErrors.AddressNotFound` + * @return MnemonicWalletSubprovider instance + */ constructor( mnemonic: string, derivationPath: string = DEFAULT_DERIVATION_PATH, @@ -32,7 +40,8 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider { assert.isString('derivationPath', derivationPath); assert.isNumber('addressSearchLimit', addressSearchLimit); super(); - this._hdKey = HDNode.fromMasterSeed(bip39.mnemonicToSeed(mnemonic)); + const seed = bip39.mnemonicToSeed(mnemonic); + this._hdKey = HDNode.fromMasterSeed(seed); this._derivationPath = derivationPath; this._addressSearchLimit = addressSearchLimit; } @@ -54,6 +63,7 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider { * Retrieve the accounts associated with the mnemonic. * This method is implicitly called when issuing a `eth_accounts` JSON RPC request * via your providerEngine instance. + * @param numberOfAccounts Number of accounts to retrieve (default: 10) * @return An array of accounts */ public async getAccountsAsync(numberOfAccounts: number = DEFAULT_NUM_ADDRESSES_TO_FETCH): Promise { @@ -79,9 +89,9 @@ export class MnemonicWalletSubprovider extends BaseWalletSubprovider { return signedTx; } /** - * Sign a personal Ethereum signed message. The signing address will be - * derived from the set path. - * If you've added the PKWalletSubprovider to your app's provider, you can simply send an `eth_sign` + * Sign a personal Ethereum signed message. The signing address used will be + * address provided or the first address derived from the set path. + * If you've added the MnemonicWalletSubprovider 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. * @param data Message to sign diff --git a/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts b/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts index f6906bab6..005d36f93 100644 --- a/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts @@ -15,6 +15,11 @@ import { BaseWalletSubprovider } from './base_wallet_subprovider'; export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider { private _address: string; private _privateKeyBuffer: Buffer; + /** + * Instantiates a PrivateKeyWalletSubprovider. + * @param privateKey The private key of the ethereum address + * @return PrivateKeyWalletSubprovider instance + */ constructor(privateKey: string) { assert.isString('privateKey', privateKey); super(); @@ -40,6 +45,9 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider { */ public async signTransactionAsync(txParams: PartialTxParams): Promise { PrivateKeyWalletSubprovider._validateTxParams(txParams); + if (!_.isUndefined(txParams.from) && txParams.from !== this._address) { + throw new Error(`${WalletSubproviderErrors.AddressNotFound}: ${txParams.from}`); + } const tx = new EthereumTx(txParams); tx.sign(this._privateKeyBuffer); const rawTx = `0x${tx.serialize().toString('hex')}`; @@ -47,8 +55,8 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider { } /** * Sign a personal Ethereum signed message. The signing address will be - * calculated from the private key. - * If you've added the PKWalletSubprovider to your app's provider, you can simply send an `eth_sign` + * 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. * @param data Message to sign -- cgit v1.2.3