aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-04-06 16:54:38 +0800
committerJacob Evans <jacob@dekz.net>2018-04-06 17:03:35 +0800
commit0e8f5004d6a53929a4cc1de9231c43899c8b6eeb (patch)
tree793b58fa3935f2d6bd7446218e1a5a66908c3dec /packages/subproviders/src
parent524e4707d2274185f68f3ddfb82367a46429876d (diff)
downloaddexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar
dexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.gz
dexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.bz2
dexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.lz
dexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.xz
dexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.zst
dexon-sol-tools-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.zip
Add Mnemonic wallet subprovider
Diffstat (limited to 'packages/subproviders/src')
-rw-r--r--packages/subproviders/src/globals.d.ts2
-rw-r--r--packages/subproviders/src/index.ts1
-rw-r--r--packages/subproviders/src/subproviders/ledger.ts1
-rw-r--r--packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts123
-rw-r--r--packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts4
-rw-r--r--packages/subproviders/src/types.ts3
6 files changed, 130 insertions, 4 deletions
diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts
index c5ad26876..754d164e8 100644
--- a/packages/subproviders/src/globals.d.ts
+++ b/packages/subproviders/src/globals.d.ts
@@ -54,7 +54,9 @@ declare module '@ledgerhq/hw-transport-node-hid' {
// hdkey declarations
declare module 'hdkey' {
class HDNode {
+ public static fromMasterSeed(seed: Buffer): HDNode;
public publicKey: Buffer;
+ public privateKey: Buffer;
public chainCode: Buffer;
public constructor();
public derive(path: string): HDNode;
diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts
index dd553fde4..9c30c6ba1 100644
--- a/packages/subproviders/src/index.ts
+++ b/packages/subproviders/src/index.ts
@@ -13,6 +13,7 @@ export { GanacheSubprovider } from './subproviders/ganache';
export { Subprovider } from './subproviders/subprovider';
export { NonceTrackerSubprovider } from './subproviders/nonce_tracker';
export { PrivateKeyWalletSubprovider } from './subproviders/private_key_wallet_subprovider';
+export { MnemonicWalletSubprovider } from './subproviders/mnemonic_wallet_subprovider';
export {
Callback,
ErrorCallback,
diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts
index aa86bf6c0..23ed3c93e 100644
--- a/packages/subproviders/src/subproviders/ledger.ts
+++ b/packages/subproviders/src/subproviders/ledger.ts
@@ -1,5 +1,4 @@
import { assert } from '@0xproject/assert';
-import { JSONRPCRequestPayload } from '@0xproject/types';
import { addressUtils } from '@0xproject/utils';
import EthereumTx = require('ethereumjs-tx');
import ethUtil = require('ethereumjs-util');
diff --git a/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts b/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts
new file mode 100644
index 000000000..fdb497776
--- /dev/null
+++ b/packages/subproviders/src/subproviders/mnemonic_wallet_subprovider.ts
@@ -0,0 +1,123 @@
+import { assert } from '@0xproject/assert';
+import * as bip39 from 'bip39';
+import ethUtil = require('ethereumjs-util');
+import HDNode = require('hdkey');
+import * as _ from 'lodash';
+
+import { MnemonicSubproviderErrors, PartialTxParams } from '../types';
+
+import { BaseWalletSubprovider } from './base_wallet_subprovider';
+import { PrivateKeyWalletSubprovider } from './private_key_wallet_subprovider';
+
+const DEFAULT_DERIVATION_PATH = `44'/60'/0'`;
+const DEFAULT_NUM_ADDRESSES_TO_FETCH = 10;
+const DEFAULT_ADDRESS_SEARCH_LIMIT = 100;
+
+/**
+ * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface.
+ * This subprovider intercepts all account related RPC requests (e.g message/transaction signing, etc...) and handles
+ * all requests with accounts derived from the supplied mnemonic.
+ */
+export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
+ private _derivationPath: string;
+ private _hdKey: HDNode;
+ private _derivationPathIndex: number;
+ constructor(mnemonic: string, derivationPath: string = DEFAULT_DERIVATION_PATH) {
+ assert.isString('mnemonic', mnemonic);
+ super();
+ this._hdKey = HDNode.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
+ this._derivationPathIndex = 0;
+ this._derivationPath = derivationPath;
+ }
+ /**
+ * Retrieve the set derivation path
+ * @returns derivation path
+ */
+ public getPath(): string {
+ return this._derivationPath;
+ }
+ /**
+ * Set a desired derivation path when computing the available user addresses
+ * @param derivationPath The desired derivation path (e.g `44'/60'/0'`)
+ */
+ public setPath(derivationPath: string) {
+ this._derivationPath = derivationPath;
+ }
+ /**
+ * Set the final derivation path index. If a user wishes to sign a message with the
+ * 6th address in a derivation path, before calling `signPersonalMessageAsync`, you must
+ * call this method with pathIndex `6`.
+ * @param pathIndex Desired derivation path index
+ */
+ public setPathIndex(pathIndex: number) {
+ this._derivationPathIndex = pathIndex;
+ }
+ /**
+ * Retrieve the account associated with the supplied private key.
+ * This method is implicitly called when issuing a `eth_accounts` JSON RPC request
+ * via your providerEngine instance.
+ * @return An array of accounts
+ */
+ public async getAccountsAsync(numberOfAccounts: number = DEFAULT_NUM_ADDRESSES_TO_FETCH): Promise<string[]> {
+ const accounts: string[] = [];
+ for (let i = 0; i < numberOfAccounts; i++) {
+ const derivedHDNode = this._hdKey.derive(`m/${this._derivationPath}/${i + this._derivationPathIndex}`);
+ const derivedPublicKey = derivedHDNode.publicKey;
+ const shouldSanitizePublicKey = true;
+ const ethereumAddressUnprefixed = ethUtil
+ .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
+ .toString('hex');
+ const ethereumAddressPrefixed = ethUtil.addHexPrefix(ethereumAddressUnprefixed);
+ accounts.push(ethereumAddressPrefixed.toLowerCase());
+ }
+ return accounts;
+ }
+
+ /**
+ * Signs a transaction with the from account (if specificed in txParams) or the first account.
+ * If you've added this Subprovider to your app's provider, you can simply send
+ * an `eth_sendTransaction` 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 txParams Parameters of the transaction to sign
+ * @return Signed transaction hex string
+ */
+ public async signTransactionAsync(txParams: PartialTxParams): Promise<string> {
+ const accounts = await this.getAccountsAsync();
+ const hdKey = this._findHDKeyByPublicAddress(txParams.from || accounts[0]);
+ const privateKeyWallet = new PrivateKeyWalletSubprovider(hdKey.privateKey.toString('hex'));
+ const signedTx = privateKeyWallet.signTransactionAsync(txParams);
+ 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`
+ * 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
+ * @return Signature hex string (order: rsv)
+ */
+ public async signPersonalMessageAsync(data: string): Promise<string> {
+ const accounts = await this.getAccountsAsync();
+ const hdKey = this._findHDKeyByPublicAddress(accounts[0]);
+ const privateKeyWallet = new PrivateKeyWalletSubprovider(hdKey.privateKey.toString('hex'));
+ const sig = await privateKeyWallet.signPersonalMessageAsync(data);
+ return sig;
+ }
+
+ private _findHDKeyByPublicAddress(address: string, searchLimit: number = DEFAULT_ADDRESS_SEARCH_LIMIT): HDNode {
+ for (let i = 0; i < searchLimit; i++) {
+ const derivedHDNode = this._hdKey.derive(`m/${this._derivationPath}/${i + this._derivationPathIndex}`);
+ const derivedPublicKey = derivedHDNode.publicKey;
+ const shouldSanitizePublicKey = true;
+ const ethereumAddressUnprefixed = ethUtil
+ .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
+ .toString('hex');
+ const ethereumAddressPrefixed = ethUtil.addHexPrefix(ethereumAddressUnprefixed);
+ if (ethereumAddressPrefixed === address) {
+ return derivedHDNode;
+ }
+ }
+ throw new Error(MnemonicSubproviderErrors.AddressSearchExhausted);
+ }
+}
diff --git a/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts b/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts
index c3a53773a..0aa2fb590 100644
--- a/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts
+++ b/packages/subproviders/src/subproviders/private_key_wallet_subprovider.ts
@@ -1,13 +1,11 @@
import { assert } from '@0xproject/assert';
-import { JSONRPCRequestPayload } from '@0xproject/types';
import EthereumTx = require('ethereumjs-tx');
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';
-import { Callback, ErrorCallback, PartialTxParams, ResponseWithTxParams, WalletSubproviderErrors } from '../types';
+import { PartialTxParams, WalletSubproviderErrors } from '../types';
import { BaseWalletSubprovider } from './base_wallet_subprovider';
-import { Subprovider } from './subprovider';
/**
* This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface.
diff --git a/packages/subproviders/src/types.ts b/packages/subproviders/src/types.ts
index bacb7091b..de04499ce 100644
--- a/packages/subproviders/src/types.ts
+++ b/packages/subproviders/src/types.ts
@@ -95,6 +95,9 @@ export interface ResponseWithTxParams {
tx: PartialTxParams;
}
+export enum MnemonicSubproviderErrors {
+ AddressSearchExhausted = 'ADDRESS_SEARCH_EXHAUSTED',
+}
export enum WalletSubproviderErrors {
DataMissingForSignPersonalMessage = 'DATA_MISSING_FOR_SIGN_PERSONAL_MESSAGE',
SenderInvalidOrNotSupplied = 'SENDER_INVALID_OR_NOT_SUPPLIED',