aboutsummaryrefslogtreecommitdiffstats
path: root/packages
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
parent524e4707d2274185f68f3ddfb82367a46429876d (diff)
downloaddexon-0x-contracts-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.gz
dexon-0x-contracts-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.tar.zst
dexon-0x-contracts-0e8f5004d6a53929a4cc1de9231c43899c8b6eeb.zip
Add Mnemonic wallet subprovider
Diffstat (limited to 'packages')
-rw-r--r--packages/subproviders/CHANGELOG.json4
-rw-r--r--packages/subproviders/package.json2
-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
-rw-r--r--packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts182
-rw-r--r--packages/subproviders/test/utils/fixture_data.ts2
10 files changed, 320 insertions, 4 deletions
diff --git a/packages/subproviders/CHANGELOG.json b/packages/subproviders/CHANGELOG.json
index f0702cd5d..ccf807695 100644
--- a/packages/subproviders/CHANGELOG.json
+++ b/packages/subproviders/CHANGELOG.json
@@ -5,6 +5,10 @@
{
"note": "Add private key subprovider and refactor shared functionality into a base wallet subprovider",
"pr": 506
+ },
+ {
+ "note": "Add mnemonic wallet subprovider, deprecating our truffle-hdwallet-provider fork",
+ "pr": 507
}
]
},
diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json
index cadbac0e8..d557eef29 100644
--- a/packages/subproviders/package.json
+++ b/packages/subproviders/package.json
@@ -56,9 +56,11 @@
"@0xproject/monorepo-scripts": "^0.1.16",
"@0xproject/tslint-config": "^0.4.14",
"@0xproject/utils": "^0.5.0",
+ "@types/bip39": "^2.4.0",
"@types/lodash": "4.14.104",
"@types/mocha": "^2.2.42",
"@types/node": "^8.0.53",
+ "bip39": "^2.5.0",
"chai": "^4.0.1",
"chai-as-promised": "^7.1.0",
"copyfiles": "^1.2.0",
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',
diff --git a/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts b/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts
new file mode 100644
index 000000000..e58461005
--- /dev/null
+++ b/packages/subproviders/test/unit/mnemonic_wallet_subprovider_test.ts
@@ -0,0 +1,182 @@
+import { JSONRPCResponsePayload } from '@0xproject/types';
+import * as chai from 'chai';
+import * as ethUtils from 'ethereumjs-util';
+import * as _ from 'lodash';
+import Web3ProviderEngine = require('web3-provider-engine');
+
+import { GanacheSubprovider, MnemonicWalletSubprovider } from '../../src/';
+import {
+ DoneCallback,
+ LedgerCommunicationClient,
+ LedgerSubproviderErrors,
+ MnemonicSubproviderErrors,
+ WalletSubproviderErrors,
+} from '../../src/types';
+import { chaiSetup } from '../chai_setup';
+import { fixtureData } from '../utils/fixture_data';
+import { reportCallbackErrors } from '../utils/report_callback_errors';
+
+chaiSetup.configure();
+const expect = chai.expect;
+
+describe('MnemonicWalletSubprovider', () => {
+ let subprovider: MnemonicWalletSubprovider;
+ before(async () => {
+ subprovider = new MnemonicWalletSubprovider(
+ fixtureData.TEST_RPC_MNEMONIC,
+ fixtureData.TEST_RPC_MNEMONIC_DERIVATION_PATH,
+ );
+ });
+ describe('direct method calls', () => {
+ describe('success cases', () => {
+ it('returns the account', async () => {
+ const accounts = await subprovider.getAccountsAsync();
+ expect(accounts[0]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_0);
+ expect(accounts.length).to.be.equal(10);
+ });
+ it('signs a personal message', async () => {
+ const data = ethUtils.bufferToHex(ethUtils.toBuffer(fixtureData.PERSONAL_MESSAGE_STRING));
+ const ecSignatureHex = await subprovider.signPersonalMessageAsync(data);
+ expect(ecSignatureHex).to.be.equal(fixtureData.PERSONAL_MESSAGE_SIGNED_RESULT);
+ });
+ it('signs a transaction', async () => {
+ const txHex = await subprovider.signTransactionAsync(fixtureData.TX_DATA);
+ expect(txHex).to.be.equal(fixtureData.TX_DATA_SIGNED_RESULT);
+ });
+ });
+ describe('failure cases', () => {
+ it('throws an error if account cannot be found', async () => {
+ const txData = { ...fixtureData.TX_DATA, from: '0x0' };
+ return expect(subprovider.signTransactionAsync(txData)).to.be.rejectedWith(
+ MnemonicSubproviderErrors.AddressSearchExhausted,
+ );
+ });
+ });
+ });
+ describe('calls through a provider', () => {
+ let provider: Web3ProviderEngine;
+ before(() => {
+ provider = new Web3ProviderEngine();
+ provider.addProvider(subprovider);
+ const ganacheSubprovider = new GanacheSubprovider({});
+ provider.addProvider(ganacheSubprovider);
+ provider.start();
+ });
+ describe('success cases', () => {
+ it('returns a list of accounts', (done: DoneCallback) => {
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'eth_accounts',
+ params: [],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.be.a('null');
+ expect(response.result[0]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_0);
+ expect(response.result.length).to.be.equal(10);
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ it('signs a personal message with eth_sign', (done: DoneCallback) => {
+ const messageHex = ethUtils.bufferToHex(ethUtils.toBuffer(fixtureData.PERSONAL_MESSAGE_STRING));
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'eth_sign',
+ params: ['0x0000000000000000000000000000000000000000', messageHex],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.be.a('null');
+ expect(response.result).to.be.equal(fixtureData.PERSONAL_MESSAGE_SIGNED_RESULT);
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ it('signs a personal message with personal_sign', (done: DoneCallback) => {
+ const messageHex = ethUtils.bufferToHex(ethUtils.toBuffer(fixtureData.PERSONAL_MESSAGE_STRING));
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'personal_sign',
+ params: [messageHex, '0x0000000000000000000000000000000000000000'],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.be.a('null');
+ expect(response.result).to.be.equal(fixtureData.PERSONAL_MESSAGE_SIGNED_RESULT);
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ });
+ describe('failure cases', () => {
+ it('should throw if `data` param not hex when calling eth_sign', (done: DoneCallback) => {
+ const nonHexMessage = 'hello world';
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'eth_sign',
+ params: ['0x0000000000000000000000000000000000000000', nonHexMessage],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.not.be.a('null');
+ expect(err.message).to.be.equal('Expected data to be of type HexString, encountered: hello world');
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ it('should throw if `data` param not hex when calling personal_sign', (done: DoneCallback) => {
+ const nonHexMessage = 'hello world';
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'personal_sign',
+ params: [nonHexMessage, '0x0000000000000000000000000000000000000000'],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.not.be.a('null');
+ expect(err.message).to.be.equal('Expected data to be of type HexString, encountered: hello world');
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ it('should throw if `from` param missing when calling eth_sendTransaction', (done: DoneCallback) => {
+ const tx = {
+ to: '0xafa3f8684e54059998bc3a7b0d2b0da075154d66',
+ value: '0xde0b6b3a7640000',
+ };
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'eth_sendTransaction',
+ params: [tx],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.not.be.a('null');
+ expect(err.message).to.be.equal(WalletSubproviderErrors.SenderInvalidOrNotSupplied);
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ it('should throw if `from` param invalid address when calling eth_sendTransaction', (done: DoneCallback) => {
+ const tx = {
+ to: '0xafa3f8684e54059998bc3a7b0d2b0da075154d66',
+ from: '0xIncorrectEthereumAddress',
+ value: '0xde0b6b3a7640000',
+ };
+ const payload = {
+ jsonrpc: '2.0',
+ method: 'eth_sendTransaction',
+ params: [tx],
+ id: 1,
+ };
+ const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => {
+ expect(err).to.not.be.a('null');
+ expect(err.message).to.be.equal(WalletSubproviderErrors.SenderInvalidOrNotSupplied);
+ done();
+ });
+ provider.sendAsync(payload, callback);
+ });
+ });
+ });
+});
diff --git a/packages/subproviders/test/utils/fixture_data.ts b/packages/subproviders/test/utils/fixture_data.ts
index 890573d0d..5ce3ff08f 100644
--- a/packages/subproviders/test/utils/fixture_data.ts
+++ b/packages/subproviders/test/utils/fixture_data.ts
@@ -3,6 +3,8 @@ const networkId = 42;
export const fixtureData = {
TEST_RPC_ACCOUNT_0,
TEST_RPC_ACCOUNT_0_ACCOUNT_PRIVATE_KEY: 'F2F48EE19680706196E2E339E5DA3491186E0C4C5030670656B0E0164837257D',
+ TEST_RPC_MNEMONIC: 'concert load couple harbor equip island argue ramp clarify fence smart topic',
+ TEST_RPC_MNEMONIC_DERIVATION_PATH: `44'/60'/0'/0`,
PERSONAL_MESSAGE_STRING: 'hello world',
PERSONAL_MESSAGE_SIGNED_RESULT:
'0x1b0ec5e2908e993d0c8ab6b46da46be2688fdf03c7ea6686075de37392e50a7d7fcc531446699132fbda915bd989882e0064d417018773a315fb8d43ed063c9b00',