Add eth-lightwallet subprovider and tests

This commit is contained in:
Cavan 2018-06-04 14:48:18 -06:00 committed by Fabio Berger
parent 1821f60fb5
commit 7ce1e9b18d
6 changed files with 218 additions and 11 deletions

View File

@ -49,6 +49,7 @@
"ethereum-types": "^0.0.1",
"bip39": "^2.5.0",
"bn.js": "^4.11.8",
"eth-lightwallet": "^3.0.1",
"ethereumjs-tx": "^1.3.3",
"ethereumjs-util": "^5.1.1",
"ganache-core": "0xProject/ganache-core",

View File

@ -16,6 +16,11 @@ interface ECSignature {
r: string;
s: string;
}
interface ECSignatureBuffer {
v: number;
r: Buffer;
s: Buffer;
}
interface LedgerTransport {
close(): Promise<void>;
@ -57,3 +62,26 @@ declare module '*.json' {
export default json;
/* tslint:enable */
}
// eth-lightwallet declarations
declare module 'eth-lightwallet' {
export class signing {
public static signTx(keystore: keystore, pwDerivedKey: Uint8Array, rawTx: string, signingAddress: string): string;
public static signMsg(keystore: keystore, pwDerivedKey: Uint8Array, rawMsg: string, signingAddress: string): ECSignatureBuffer;
public static signMsgHash(keystore: keystore, pwDerivedKey: Uint8Array, msgHash: string, signingAddress: string): ECSignatureBuffer;
public static concatSig(signature: any): string;
}
export class keystore {
public static createVault(options: any, callback?: (error: Error, keystore: keystore) => void): keystore;
public static generateRandomSeed(): string;
public static isSeedValid(seed: string): boolean;
public static deserialize(keystore: string): keystore;
public serialize(): string;
public keyFromPassword(password: string, callback?: (error: Error, pwDerivedKey: Uint8Array) => void): Uint8Array;
public isDerivedKeyCorrect(pwDerivedKey: Uint8Array): boolean;
public generateNewAddress(pwDerivedKey: Uint8Array, numberOfAddresses: number): void;
public getSeed(pwDerivedKey: Uint8Array): string;
public exportPrivateKey(address: string, pwDerivedKey: Uint8Array): string;
public getAddresses(): string[];
}
}

View File

@ -15,7 +15,7 @@ export { Subprovider } from './subproviders/subprovider';
export { NonceTrackerSubprovider } from './subproviders/nonce_tracker';
export { PrivateKeyWalletSubprovider } from './subproviders/private_key_wallet';
export { MnemonicWalletSubprovider } from './subproviders/mnemonic_wallet';
export { EthLightwalletSubprovider } from './subproviders/eth_lightwallet';
export { EthLightwalletSubprovider } from './subproviders/eth_lightwallet_subprovider';
export {
Callback,
ErrorCallback,

View File

@ -0,0 +1,88 @@
import { assert } from '@0xproject/assert';
import { addressUtils } from '@0xproject/utils';
import * as lightwallet from 'eth-lightwallet';
import EthereumTx = require('ethereumjs-tx');
import * as _ from 'lodash';
import { PartialTxParams, WalletSubproviderErrors } from '../types';
import { BaseWalletSubprovider } from './base_wallet_subprovider';
/*
* This class implements the web3-provider-engine subprovider interface and forwards
* requests involving user accounts and signing operations to eth-lightwallet
*
* Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js
*/
export class EthLightwalletSubprovider extends BaseWalletSubprovider {
private _signing: any;
private _keystore: any;
private _pwDerivedKey: Uint8Array;
constructor(signing: lightwallet.signing, keystore: lightwallet.keystore, pwDerivedKey: Uint8Array) {
super();
this._signing = signing;
this._keystore = keystore;
this._pwDerivedKey = pwDerivedKey;
}
/**
* Retrieve the accounts associated with the eth-lightwallet instance.
* 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(): Promise<string[]> {
const accounts = this._keystore.getAddresses();
return accounts;
}
/**
* Signs a transaction with the account specificed by the `from` field in txParams.
* 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> {
if (_.isUndefined(txParams.from) || !addressUtils.isAddress(txParams.from)) {
throw new Error(WalletSubproviderErrors.FromAddressMissingOrInvalid);
}
const tx = new EthereumTx(txParams);
const txHex = tx.serialize().toString('hex');
let signedTxHex: string = this._signing.signTx(
this._keystore, this._pwDerivedKey, txHex, txParams.from, this._keystore.hdPathString);
signedTxHex = `0x${signedTxHex}`;
return signedTxHex;
}
/**
* Sign a personal Ethereum signed message. The signing account will be the account
* associated with the provided address.
* 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 Hex string message to sign
* @param address Address of the account to sign with
* @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 result: ECSignatureBuffer = await this._signing.signMsgHash(
this._keystore, this._pwDerivedKey, data, address, this._keystore.hdPathString);
const signature = this._signing.concatSig(result);
return signature;
}
}

View File

@ -67,10 +67,8 @@ describe('EthLightwalletSubprovider', () => {
// Keccak-256 hash of 'hello world'
const messageHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad';
const ecSignatureHex = await ethLightwalletSubprovider.signPersonalMessageAsync(
messageHash,
signingAccount,
);
const ecSignatureHex =
await ethLightwalletSubprovider.signPersonalMessageAsync(messageHash, signingAccount);
expect(ecSignatureHex).to.be.equal(
// tslint:disable-next-line:max-line-length
'0xa46b696c1aa8f91dbb33d1a66f6440bf3cf334c9dc45dc389668c1e60e2db31e259400b41f31632fa994837054c5345c88dc455c13931332489029adee6fd24d1b',

104
yarn.lock
View File

@ -64,6 +64,17 @@
"@0xproject/types" "^0.5.0"
bignumber.js "~4.1.0"
"@0xproject/web3-wrapper@^0.6.4":
version "0.6.4"
resolved "https://registry.yarnpkg.com/@0xproject/web3-wrapper/-/web3-wrapper-0.6.4.tgz#fb15b71cdf4e5001c2b2e0d316b0de485a2be5f8"
dependencies:
"@0xproject/types" "^0.7.0"
"@0xproject/typescript-typings" "^0.3.2"
"@0xproject/utils" "^0.6.2"
ethers "^3.0.15"
lodash "^4.17.4"
web3 "^0.20.0"
"@ledgerhq/hw-app-eth@^4.3.0":
version "4.7.3"
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-app-eth/-/hw-app-eth-4.7.3.tgz#d352e19658ae296532e522c53c8ec2a1a77b64e5"
@ -1582,6 +1593,12 @@ base-x@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/base-x/-/base-x-1.1.0.tgz#42d3d717474f9ea02207f6d1aa1f426913eeb7ac"
base-x@^3.0.2:
version "3.0.4"
resolved "https://registry.yarnpkg.com/base-x/-/base-x-3.0.4.tgz#94c1788736da065edb1d68808869e357c977fa77"
dependencies:
safe-buffer "^5.0.1"
base64-js@0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-0.0.8.tgz#1101e9544f4a76b1bc3b26d452ca96d7a35e7978"
@ -1734,6 +1751,24 @@ bip66@^1.1.3:
dependencies:
safe-buffer "^5.0.1"
bitcore-lib@^0.15.0:
version "0.15.0"
resolved "https://registry.yarnpkg.com/bitcore-lib/-/bitcore-lib-0.15.0.tgz#f924be13869f2aab7e04aeec5642ad3359b6cec2"
dependencies:
bn.js "=4.11.8"
bs58 "=4.0.1"
buffer-compare "=1.1.1"
elliptic "=6.4.0"
inherits "=2.0.1"
lodash "=4.17.4"
bitcore-mnemonic@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bitcore-mnemonic/-/bitcore-mnemonic-1.5.0.tgz#c7e785beb6bf0616ed4992785dc3658670425a39"
dependencies:
bitcore-lib "^0.15.0"
unorm "^1.3.3"
bl@^1.0.0:
version "1.2.2"
resolved "https://registry.yarnpkg.com/bl/-/bl-1.2.2.tgz#a160911717103c07410cef63ef51b397c025af9c"
@ -1779,10 +1814,14 @@ bn.js@4.11.7:
version "4.11.7"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.7.tgz#ddb048e50d9482790094c13eb3fcfc833ce7ab46"
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.10.0, bn.js@^4.11.0, bn.js@^4.11.3, bn.js@^4.11.6, bn.js@^4.11.8, bn.js@^4.4.0, bn.js@^4.8.0:
bn.js@=4.11.8, bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.10.0, bn.js@^4.11.0, bn.js@^4.11.3, bn.js@^4.11.6, bn.js@^4.11.8, bn.js@^4.4.0, bn.js@^4.8.0:
version "4.11.8"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
bn.js@^2.0.3:
version "2.2.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-2.2.0.tgz#12162bc2ae71fc40a5626c33438f3a875cd37625"
body-parser@1.18.2, body-parser@^1.16.0, body-parser@^1.17.1:
version "1.18.2"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.2.tgz#87678a19d84b47d859b83199bd59bce222b10454"
@ -1953,6 +1992,12 @@ browserslist@^2.1.2:
caniuse-lite "^1.0.30000792"
electron-to-chromium "^1.3.30"
bs58@=4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/bs58/-/bs58-4.0.1.tgz#be161e76c354f6f788ae4071f63f34e8c4f0a42a"
dependencies:
base-x "^3.0.2"
bs58@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/bs58/-/bs58-2.0.1.tgz#55908d58f1982aba2008fa1bed8f91998a29bf8d"
@ -1974,6 +2019,10 @@ btoa@1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/btoa/-/btoa-1.1.2.tgz#3e40b81663f81d2dd6596a4cb714a8dc16cfabe0"
buffer-compare@=1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-compare/-/buffer-compare-1.1.1.tgz#5be7be853af89198d1f4ddc090d1d66a48aef596"
buffer-crc32@~0.2.3:
version "0.2.13"
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
@ -2010,7 +2059,7 @@ buffer@^3.0.1:
ieee754 "^1.1.4"
isarray "^1.0.0"
buffer@^4.3.0:
buffer@^4.3.0, buffer@^4.9.0:
version "4.9.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-4.9.1.tgz#6d1bb601b07a4efced97094132093027c95bc298"
dependencies:
@ -3021,7 +3070,7 @@ crypto-js@3.1.9-1, crypto-js@^3.1.9-1:
version "3.1.9-1"
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-3.1.9-1.tgz#fda19e761fc077e01ffbfdc6e9fdfc59e8806cd8"
crypto-js@^3.1.4:
crypto-js@^3.1.4, crypto-js@^3.1.5:
version "3.1.8"
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-3.1.8.tgz#715f070bf6014f2ae992a98b3929258b713f08d5"
@ -3683,7 +3732,7 @@ elliptic@6.3.3:
hash.js "^1.0.0"
inherits "^2.0.1"
elliptic@^6.0.0, elliptic@^6.2.3, elliptic@^6.4.0:
elliptic@=6.4.0, elliptic@^6.0.0, elliptic@^6.2.3, elliptic@^6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.4.0.tgz#cac9af8762c85836187003c8dfe193e5e2eae5df"
dependencies:
@ -3695,6 +3744,15 @@ elliptic@^6.0.0, elliptic@^6.2.3, elliptic@^6.4.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.0"
elliptic@^3.1.0:
version "3.1.0"
resolved "http://registry.npmjs.org/elliptic/-/elliptic-3.1.0.tgz#c21682ef762769b56a74201609105da11d5f60cc"
dependencies:
bn.js "^2.0.3"
brorand "^1.0.1"
hash.js "^1.0.0"
inherits "^2.0.1"
emojis-list@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389"
@ -3994,6 +4052,22 @@ eth-lib@0.2.7:
elliptic "^6.4.0"
xhr-request-promise "^0.1.2"
eth-lightwallet@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/eth-lightwallet/-/eth-lightwallet-3.0.1.tgz#297022932aa568f4e4eb0873bff257f5e5b78709"
dependencies:
bitcore-lib "^0.15.0"
bitcore-mnemonic "^1.5.0"
buffer "^4.9.0"
crypto-js "^3.1.5"
elliptic "^3.1.0"
ethereumjs-tx "^1.3.3"
ethereumjs-util "^5.1.1"
rlp "^2.0.0"
scrypt-async "^1.2.0"
tweetnacl "0.13.2"
web3 "0.20.2"
eth-query@^2.0.2, eth-query@^2.1.0, eth-query@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/eth-query/-/eth-query-2.1.2.tgz#d6741d9000106b51510c72db92d6365456a6da5e"
@ -5815,7 +5889,7 @@ inherits@2, inherits@2.0.3, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, i
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
inherits@2.0.1:
inherits@2.0.1, inherits@=2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"
@ -7160,7 +7234,7 @@ lodash@4.17.2:
version "4.17.2"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
lodash@4.17.4:
lodash@4.17.4, lodash@=4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
@ -10232,6 +10306,10 @@ scoped-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/scoped-regex/-/scoped-regex-1.0.0.tgz#a346bb1acd4207ae70bd7c0c7ca9e566b6baddb8"
scrypt-async@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/scrypt-async/-/scrypt-async-1.3.1.tgz#a11fd6fac981b4b823ee01dee0221169500ddae9"
scrypt-js@2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/scrypt-js/-/scrypt-js-2.0.3.tgz#bb0040be03043da9a012a2cea9fc9f852cfc87d4"
@ -11684,6 +11762,10 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"
tweetnacl@0.13.2:
version "0.13.2"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.13.2.tgz#453161770469d45cd266c36404e2bc99a8fa9944"
tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"
@ -12610,6 +12692,16 @@ web3-utils@1.0.0-beta.34:
underscore "1.8.3"
utf8 "2.1.1"
web3@0.20.2:
version "0.20.2"
resolved "https://registry.yarnpkg.com/web3/-/web3-0.20.2.tgz#c54dac5fc0e377399c04c1a6ecbb12e4513278d6"
dependencies:
bignumber.js "git+https://github.com/frozeman/bignumber.js-nolookahead.git"
crypto-js "^3.1.4"
utf8 "^2.1.1"
xhr2 "*"
xmlhttprequest "*"
web3@^0.18.0:
version "0.18.4"
resolved "https://registry.yarnpkg.com/web3/-/web3-0.18.4.tgz#81ec1784145491f2eaa8955b31c06049e07c5e7d"