Add Mnemonic wallet subprovider
This commit is contained in:
@@ -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
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -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",
|
||||
|
||||
2
packages/subproviders/src/globals.d.ts
vendored
2
packages/subproviders/src/globals.d.ts
vendored
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user