From bb4c748bf1e93d1f2c98514c574ba67678b0ed59 Mon Sep 17 00:00:00 2001 From: Cavan Date: Thu, 7 Jun 2018 09:42:50 -0600 Subject: Formatting and standards updates --- packages/subproviders/src/index.ts | 1 + .../src/subproviders/eth_lightwallet.ts | 88 ++++++++++ .../test/unit/eth_lightwallet_subprovider_test.ts | 183 +++++++++++++++++++++ .../types/eth-lightwallet/index.d.ts | 48 ++++++ 4 files changed, 320 insertions(+) create mode 100644 packages/subproviders/src/subproviders/eth_lightwallet.ts create mode 100644 packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts create mode 100644 packages/typescript-typings/types/eth-lightwallet/index.d.ts diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 6cc650a4d..0e1579706 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -15,6 +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 { Callback, ErrorCallback, diff --git a/packages/subproviders/src/subproviders/eth_lightwallet.ts b/packages/subproviders/src/subproviders/eth_lightwallet.ts new file mode 100644 index 000000000..af4ea77e9 --- /dev/null +++ b/packages/subproviders/src/subproviders/eth_lightwallet.ts @@ -0,0 +1,88 @@ +import { assert } from '@0xproject/assert'; +import { ECSignatureBuffer } from '@0xproject/types'; +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](https://github.com/MetaMask/provider-engine) subprovider interface. + * This subprovider intercepts all account related RPC requests (e.g message/transaction signing, etc...) and + * re-routes them to [eth-lightwallet](https://github.com/ConsenSys/eth-lightwallet). + */ +export class EthLightwalletSubprovider extends BaseWalletSubprovider { + private _signing: lightwallet.signing; + private _keystore: lightwallet.keystore; + private _pwDerivedKey: Uint8Array; + /** + * Instantiates a EthLightwalletSubprovider + * @param signing The lightwallet module containing signing functions + * @param keystore An instance of the lightwallet keystore + * @param pwDerivedKey The users password derived key + */ + 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 { + 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 { + 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); + + 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 EthLightwalletSubprovider 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 { + if (_.isUndefined(data)) { + throw new Error(WalletSubproviderErrors.DataMissingForSignPersonalMessage); + } + assert.isHexString('data', data); + assert.isETHAddressHex('address', address); + const result: ECSignatureBuffer = this._signing.signMsgHash(this._keystore, this._pwDerivedKey, data, address); + + const signature = this._signing.concatSig(result); + + return signature; + } +} diff --git a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts new file mode 100644 index 000000000..762163338 --- /dev/null +++ b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts @@ -0,0 +1,183 @@ +import { JSONRPCResponsePayload } from '@0xproject/types'; +import * as chai from 'chai'; +import * as lightwallet from 'eth-lightwallet'; +import Web3ProviderEngine = require('web3-provider-engine'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); + +import { EthLightwalletSubprovider } from '../../src'; +import { DoneCallback } from '../../src/types'; +import { chaiSetup } from '../chai_setup'; +import { ganacheSubprovider } from '../utils/ganache_subprovider'; +import { reportCallbackErrors } from '../utils/report_callback_errors'; + +chaiSetup.configure(); +const expect = chai.expect; +const FAKE_ADDRESS = '0x44be42fd88e22387c43ba9b75941aa3e680dae25'; +const NUM_GENERATED_ADDRESSES = 10; +const PASSWORD = 'supersecretpassword99'; +const SEED_PHRASE = 'dilemma hollow outer pony cube season start stereo surprise when edit blast'; +const SALT = 'kvODghzs7Ff1uqHyI0P3wI4Hso4w4iWT2e9qmrWz0y4'; +const HD_PATH_STRING = `m/44'/60'/0'`; + +describe('EthLightwalletSubprovider', () => { + let ethLightwalletSubprovider: EthLightwalletSubprovider; + before(async () => { + const options = { + password: PASSWORD, + seedPhrase: SEED_PHRASE, + salt: SALT, + hdPathString: HD_PATH_STRING, + }; + + const createVaultAsync = async (vaultOptions: lightwallet.VaultOptions) => { + return new Promise(resolve => { + // Create Vault + lightwallet.keystore.createVault(vaultOptions, (err: Error, vaultKeystore) => { + resolve(vaultKeystore); + }); + }); + }; + + const deriveKeyFromPasswordAsync = async (vaultKeystore: lightwallet.keystore) => { + return new Promise(resolve => { + vaultKeystore.keyFromPassword(PASSWORD, (err: Error, passwordDerivedKey: Uint8Array) => { + resolve(passwordDerivedKey); + }); + }); + }; + + const keystore: lightwallet.keystore = await createVaultAsync(options); + const pwDerivedKey: Uint8Array = await deriveKeyFromPasswordAsync(keystore); + + // Generate 10 addresses + keystore.generateNewAddress(pwDerivedKey, NUM_GENERATED_ADDRESSES); + + // Initialize Subprovider + ethLightwalletSubprovider = new EthLightwalletSubprovider(lightwallet.signing, keystore, pwDerivedKey); + }); + describe('direct method calls', () => { + describe('success cases', () => { + it('returns a list of accounts', async () => { + const accounts = await ethLightwalletSubprovider.getAccountsAsync(); + expect(accounts[0]).to.be.equal(FAKE_ADDRESS); + expect(accounts.length).to.be.equal(NUM_GENERATED_ADDRESSES); + }); + it('signs a personal message hash', async () => { + const signingAccount = (await ethLightwalletSubprovider.getAccountsAsync())[0]; + + // Keccak-256 hash of 'hello world' + const messageHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'; + const ecSignatureHex = await ethLightwalletSubprovider.signPersonalMessageAsync( + messageHash, + signingAccount, + ); + expect(ecSignatureHex).to.be.equal( + // tslint:disable-next-line:max-line-length + '0xa46b696c1aa8f91dbb33d1a66f6440bf3cf334c9dc45dc389668c1e60e2db31e259400b41f31632fa994837054c5345c88dc455c13931332489029adee6fd24d1b', + ); + }); + }); + }); + describe('calls through a provider', () => { + let provider: Web3ProviderEngine; + before(() => { + provider = new Web3ProviderEngine(); + provider.addProvider(ethLightwalletSubprovider); + 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.length).to.be.equal(NUM_GENERATED_ADDRESSES); + expect(response.result[0]).to.be.equal(FAKE_ADDRESS); + done(); + }); + provider.sendAsync(payload, callback); + }); + it('signs a personal message hash with eth_sign', (done: DoneCallback) => { + // Keccak-256 hash of 'hello world' + const messageHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'; + const payload = { + jsonrpc: '2.0', + method: 'eth_sign', + params: ['0x44be42fd88e22387c43ba9b75941aa3e680dae25', messageHash], + id: 1, + }; + const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { + expect(err).to.be.a('null'); + expect(response.result).to.be.equal( + // tslint:disable-next-line:max-line-length + '0xa46b696c1aa8f91dbb33d1a66f6440bf3cf334c9dc45dc389668c1e60e2db31e259400b41f31632fa994837054c5345c88dc455c13931332489029adee6fd24d1b', + ); + done(); + }); + provider.sendAsync(payload, callback); + }); + it('signs a transaction', (done: DoneCallback) => { + const tx = { + to: '0xafa3f8684e54059998bc3a7b0d2b0da075154d66', + value: '0x00', + gasPrice: '0x00', + nonce: '0x00', + gas: '0x00', + from: '0x44be42fd88e22387c43ba9b75941aa3e680dae25', + }; + const payload = { + jsonrpc: '2.0', + method: 'eth_signTransaction', + params: [tx], + id: 1, + }; + const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { + const expectedResponseLength = 192; + + expect(err).to.be.a('null'); + expect(response.result.raw.length).to.be.equal(expectedResponseLength); + expect(response.result.raw.substr(0, 2)).to.be.equal('0x'); + 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); + }); + }); + }); +}); diff --git a/packages/typescript-typings/types/eth-lightwallet/index.d.ts b/packages/typescript-typings/types/eth-lightwallet/index.d.ts new file mode 100644 index 000000000..d31e67fc4 --- /dev/null +++ b/packages/typescript-typings/types/eth-lightwallet/index.d.ts @@ -0,0 +1,48 @@ +// eth-lightwallet declarations +declare module 'eth-lightwallet' { + import { ECSignatureBuffer } from '@0xproject/types'; + + interface signing { + signTx(keystore: keystore, pwDerivedKey: Uint8Array, rawTx: string, signingAddress: string): string; + signMsg( + keystore: keystore, + pwDerivedKey: Uint8Array, + rawMsg: string, + signingAddress: string, + ): ECSignatureBuffer; + signMsgHash( + keystore: keystore, + pwDerivedKey: Uint8Array, + msgHash: string, + signingAddress: string, + ): ECSignatureBuffer; + concatSig(signature: any): string; + } + export const signing: signing; + + interface VaultOptions { + password: string; + seedPhrase: string; + salt?: string; + hdPathString: string; + } + export class keystore { + public static createVault( + options: VaultOptions, + 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[]; + } +} -- cgit v1.2.3 From 76f4d67f33af208e83eea5bcfa8802ce0e482901 Mon Sep 17 00:00:00 2001 From: Cavan Date: Wed, 6 Jun 2018 00:01:19 -0600 Subject: Remove timestamps and fix typings --- packages/subproviders/CHANGELOG.json | 8 ++++++++ packages/subproviders/src/subproviders/eth_lightwallet.ts | 14 ++++++++++++-- .../typescript-typings/types/eth-lightwallet/index.d.ts | 8 +++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/subproviders/CHANGELOG.json b/packages/subproviders/CHANGELOG.json index 65a74f7a8..c4dca2864 100644 --- a/packages/subproviders/CHANGELOG.json +++ b/packages/subproviders/CHANGELOG.json @@ -1,4 +1,12 @@ [ + { + "version": "0.11.0", + "changes": [ + { + "note": "Add `EthLightwalletSubprovider`" + } + ] + }, { "timestamp": 1529397769, "version": "0.10.4", diff --git a/packages/subproviders/src/subproviders/eth_lightwallet.ts b/packages/subproviders/src/subproviders/eth_lightwallet.ts index af4ea77e9..fedcc9578 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet.ts @@ -57,7 +57,12 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { const tx = new EthereumTx(txParams); const txHex = tx.serialize().toString('hex'); - let signedTxHex: string = this._signing.signTx(this._keystore, this._pwDerivedKey, txHex, txParams.from); + let signedTxHex: string = this._signing.signTx( + this._keystore, + this._pwDerivedKey, + txHex, + txParams.from, + ); signedTxHex = `0x${signedTxHex}`; @@ -79,7 +84,12 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { } assert.isHexString('data', data); assert.isETHAddressHex('address', address); - const result: ECSignatureBuffer = this._signing.signMsgHash(this._keystore, this._pwDerivedKey, data, address); + const result: ECSignatureBuffer = await this._signing.signMsgHash( + this._keystore, + this._pwDerivedKey, + data, + address, + ); const signature = this._signing.concatSig(result); diff --git a/packages/typescript-typings/types/eth-lightwallet/index.d.ts b/packages/typescript-typings/types/eth-lightwallet/index.d.ts index d31e67fc4..b871fb289 100644 --- a/packages/typescript-typings/types/eth-lightwallet/index.d.ts +++ b/packages/typescript-typings/types/eth-lightwallet/index.d.ts @@ -3,7 +3,12 @@ declare module 'eth-lightwallet' { import { ECSignatureBuffer } from '@0xproject/types'; interface signing { - signTx(keystore: keystore, pwDerivedKey: Uint8Array, rawTx: string, signingAddress: string): string; + signTx( + keystore: keystore, + pwDerivedKey: Uint8Array, + rawTx: string, + signingAddress: string, + ): string; signMsg( keystore: keystore, pwDerivedKey: Uint8Array, @@ -26,6 +31,7 @@ declare module 'eth-lightwallet' { salt?: string; hdPathString: string; } + export class keystore { public static createVault( options: VaultOptions, -- cgit v1.2.3 From db5880539b7a92a80d79692fbc70d46a326b1462 Mon Sep 17 00:00:00 2001 From: Cavan Date: Tue, 5 Jun 2018 17:31:36 -0600 Subject: Update changelogs --- packages/types/CHANGELOG.json | 8 ++++++++ packages/typescript-typings/CHANGELOG.json | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/types/CHANGELOG.json b/packages/types/CHANGELOG.json index 40197ab92..5e14faeb6 100644 --- a/packages/types/CHANGELOG.json +++ b/packages/types/CHANGELOG.json @@ -1,4 +1,12 @@ [ + { + "version": "0.8.2", + "changes": [ + { + "note": "Add `ECSignatureBuffer`" + } + ] + }, { "timestamp": 1529397769, "version": "0.8.1", diff --git a/packages/typescript-typings/CHANGELOG.json b/packages/typescript-typings/CHANGELOG.json index 5b5d5c31d..e5b9edc70 100644 --- a/packages/typescript-typings/CHANGELOG.json +++ b/packages/typescript-typings/CHANGELOG.json @@ -1,4 +1,12 @@ [ + { + "version": "0.4.2", + "changes": [ + { + "note": "Add types for `eth-lightwallet`" + } + ] + }, { "timestamp": 1529397769, "version": "0.4.1", -- cgit v1.2.3 From 235d78565e4d809d3ec1b91174fd0a831c126202 Mon Sep 17 00:00:00 2001 From: Cavan Date: Mon, 4 Jun 2018 16:24:37 -0600 Subject: Format subprovider using prettier --- .../src/subproviders/eth_lightwallet.ts | 2 ++ .../types/eth-lightwallet/index.d.ts | 24 ++++++---------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/packages/subproviders/src/subproviders/eth_lightwallet.ts b/packages/subproviders/src/subproviders/eth_lightwallet.ts index fedcc9578..970c5d981 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet.ts @@ -62,6 +62,7 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { this._pwDerivedKey, txHex, txParams.from, + this._keystore.hdPathString, ); signedTxHex = `0x${signedTxHex}`; @@ -89,6 +90,7 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { this._pwDerivedKey, data, address, + this._keystore.hdPathString, ); const signature = this._signing.concatSig(result); diff --git a/packages/typescript-typings/types/eth-lightwallet/index.d.ts b/packages/typescript-typings/types/eth-lightwallet/index.d.ts index b871fb289..a226d8049 100644 --- a/packages/typescript-typings/types/eth-lightwallet/index.d.ts +++ b/packages/typescript-typings/types/eth-lightwallet/index.d.ts @@ -2,41 +2,29 @@ declare module 'eth-lightwallet' { import { ECSignatureBuffer } from '@0xproject/types'; - interface signing { - signTx( + export class signing { + public static signTx( keystore: keystore, pwDerivedKey: Uint8Array, rawTx: string, signingAddress: string, ): string; - signMsg( + public static signMsg( keystore: keystore, pwDerivedKey: Uint8Array, rawMsg: string, signingAddress: string, ): ECSignatureBuffer; - signMsgHash( + public static signMsgHash( keystore: keystore, pwDerivedKey: Uint8Array, msgHash: string, signingAddress: string, ): ECSignatureBuffer; - concatSig(signature: any): string; + public static concatSig(signature: any): string; } - export const signing: signing; - - interface VaultOptions { - password: string; - seedPhrase: string; - salt?: string; - hdPathString: string; - } - export class keystore { - public static createVault( - options: VaultOptions, - callback?: (error: Error, keystore: keystore) => void, - ): 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; -- cgit v1.2.3 From 1821f60fb5ddd4a36f34cce94acabba32b4236c6 Mon Sep 17 00:00:00 2001 From: Cavan Date: Mon, 4 Jun 2018 16:04:25 -0600 Subject: Move eth-lightwallet declaration to typings --- packages/types/src/index.ts | 6 +++ .../types/eth-lightwallet/index.d.ts | 58 ++++++++-------------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index e5a0f41a9..390d2f50c 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -48,6 +48,12 @@ export interface ECSignature { s: string; } +export interface ECSignatureBuffer { + v: number; + r: Buffer; + s: Buffer; +} + /** * Validator signature components */ diff --git a/packages/typescript-typings/types/eth-lightwallet/index.d.ts b/packages/typescript-typings/types/eth-lightwallet/index.d.ts index a226d8049..76f910d9c 100644 --- a/packages/typescript-typings/types/eth-lightwallet/index.d.ts +++ b/packages/typescript-typings/types/eth-lightwallet/index.d.ts @@ -1,42 +1,24 @@ // eth-lightwallet declarations declare module 'eth-lightwallet' { - import { ECSignatureBuffer } from '@0xproject/types'; + import { ECSignatureBuffer } from "@0xproject/types"; - 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[]; - } + 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[]; + } } -- cgit v1.2.3 From 7ce1e9b18d30cb76dd61f3859164cb09d091a3dd Mon Sep 17 00:00:00 2001 From: Cavan Date: Mon, 4 Jun 2018 14:48:18 -0600 Subject: Add eth-lightwallet subprovider and tests --- packages/subproviders/package.json | 1 + packages/subproviders/src/globals.d.ts | 28 ++++++ packages/subproviders/src/index.ts | 2 +- .../subproviders/eth_lightwallet_subprovider.ts | 88 +++++++++++++++++ .../test/unit/eth_lightwallet_subprovider_test.ts | 6 +- yarn.lock | 104 +++++++++++++++++++-- 6 files changed, 218 insertions(+), 11 deletions(-) create mode 100644 packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index a04546bc1..c3e588718 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -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", diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 4b3ecdf3c..1e104053f 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -16,6 +16,11 @@ interface ECSignature { r: string; s: string; } +interface ECSignatureBuffer { + v: number; + r: Buffer; + s: Buffer; +} interface LedgerTransport { close(): Promise; @@ -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[]; + } +} diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 0e1579706..06d5871f9 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -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, diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts new file mode 100644 index 000000000..7507eeb49 --- /dev/null +++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts @@ -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 { + 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 { + 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 { + 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; + } +} diff --git a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts index 762163338..51308b6c2 100644 --- a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts @@ -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', diff --git a/yarn.lock b/yarn.lock index ff8f4aaf8..54334395d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" -- cgit v1.2.3 From 87d66ccf6cad43b58a3064dcc3a7e66eced84f66 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 22:58:36 +0200 Subject: Fix Tslint issues --- packages/contract-wrappers/src/utils/decorators.ts | 8 +++--- .../src/utils/exchange_transfer_simulator.ts | 3 +- .../contract-wrappers/src/utils/filter_utils.ts | 4 +-- .../test/ether_token_wrapper_test.ts | 4 +-- .../test/exchange_wrapper_test.ts | 4 +-- .../contract-wrappers/test/subscription_test.ts | 2 +- .../contract-wrappers/test/token_wrapper_test.ts | 4 +-- .../src/order_watcher/order_watcher.ts | 33 ++++++---------------- .../test/unit/eth_lightwallet_subprovider_test.ts | 10 +++++-- 9 files changed, 31 insertions(+), 41 deletions(-) diff --git a/packages/contract-wrappers/src/utils/decorators.ts b/packages/contract-wrappers/src/utils/decorators.ts index 494575e7b..ccb4c6e11 100644 --- a/packages/contract-wrappers/src/utils/decorators.ts +++ b/packages/contract-wrappers/src/utils/decorators.ts @@ -30,8 +30,8 @@ const schemaErrorTransformer = (error: Error) => { */ const asyncErrorHandlerFactory = (errorTransformer: ErrorTransformer) => { const asyncErrorHandlingDecorator = ( - target: object, - key: string | symbol, + _target: object, + _key: string | symbol, descriptor: TypedPropertyDescriptor, ) => { const originalMethod = descriptor.value as AsyncMethod; @@ -57,8 +57,8 @@ const asyncErrorHandlerFactory = (errorTransformer: ErrorTransformer) => { const syncErrorHandlerFactory = (errorTransformer: ErrorTransformer) => { const syncErrorHandlingDecorator = ( - target: object, - key: string | symbol, + _target: object, + _key: string | symbol, descriptor: TypedPropertyDescriptor, ) => { const originalMethod = descriptor.value as SyncMethod; diff --git a/packages/contract-wrappers/src/utils/exchange_transfer_simulator.ts b/packages/contract-wrappers/src/utils/exchange_transfer_simulator.ts index 527b8575d..279f2a796 100644 --- a/packages/contract-wrappers/src/utils/exchange_transfer_simulator.ts +++ b/packages/contract-wrappers/src/utils/exchange_transfer_simulator.ts @@ -1,8 +1,7 @@ -import { BlockParamLiteral, ExchangeContractErrs } from '@0xproject/types'; +import { ExchangeContractErrs } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { AbstractBalanceAndProxyAllowanceLazyStore } from '../abstract/abstract_balance_and_proxy_allowance_lazy_store'; -import { TokenWrapper } from '../contract_wrappers/token_wrapper'; import { TradeSide, TransferType } from '../types'; import { constants } from '../utils/constants'; diff --git a/packages/contract-wrappers/src/utils/filter_utils.ts b/packages/contract-wrappers/src/utils/filter_utils.ts index 5256d010f..f96dc573f 100644 --- a/packages/contract-wrappers/src/utils/filter_utils.ts +++ b/packages/contract-wrappers/src/utils/filter_utils.ts @@ -30,7 +30,7 @@ export const filterUtils = { blockRange?: BlockRange, ): FilterObject { const eventAbi = _.find(abi, { name: eventName }) as EventAbi; - const eventSignature = filterUtils.getEventSignatureFromAbiByName(eventAbi, eventName); + const eventSignature = filterUtils.getEventSignatureFromAbiByName(eventAbi); const topicForEventSignature = ethUtil.addHexPrefix(jsSHA3.keccak256(eventSignature)); const topicsForIndexedArgs = filterUtils.getTopicsForIndexedArgs(eventAbi, indexFilterValues); const topics = [topicForEventSignature, ...topicsForIndexedArgs]; @@ -46,7 +46,7 @@ export const filterUtils = { } return filter; }, - getEventSignatureFromAbiByName(eventAbi: EventAbi, eventName: ContractEvents): string { + getEventSignatureFromAbiByName(eventAbi: EventAbi): string { const types = _.map(eventAbi.inputs, 'type'); const signature = `${eventAbi.name}(${types.join(',')})`; return signature; diff --git a/packages/contract-wrappers/test/ether_token_wrapper_test.ts b/packages/contract-wrappers/test/ether_token_wrapper_test.ts index b13ac72bb..f401ef90a 100644 --- a/packages/contract-wrappers/test/ether_token_wrapper_test.ts +++ b/packages/contract-wrappers/test/ether_token_wrapper_test.ts @@ -277,7 +277,7 @@ describe('EtherTokenWrapper', () => { it('should cancel outstanding subscriptions when ZeroEx.setProvider is called', (done: DoneCallback) => { (async () => { const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)( - (logEvent: DecodedLogEvent) => { + (_logEvent: DecodedLogEvent) => { done(new Error('Expected this subscription to have been cancelled')); }, ); @@ -307,7 +307,7 @@ describe('EtherTokenWrapper', () => { it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => { (async () => { const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)( - (logEvent: DecodedLogEvent) => { + (_logEvent: DecodedLogEvent) => { done(new Error('Expected this subscription to have been cancelled')); }, ); diff --git a/packages/contract-wrappers/test/exchange_wrapper_test.ts b/packages/contract-wrappers/test/exchange_wrapper_test.ts index cf69d4813..c945b4c70 100644 --- a/packages/contract-wrappers/test/exchange_wrapper_test.ts +++ b/packages/contract-wrappers/test/exchange_wrapper_test.ts @@ -999,7 +999,7 @@ describe('ExchangeWrapper', () => { it('Outstanding subscriptions are cancelled when contractWrappers.setProvider called', (done: DoneCallback) => { (async () => { const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)( - (logEvent: DecodedLogEvent) => { + (_logEvent: DecodedLogEvent) => { done(new Error('Expected this subscription to have been cancelled')); }, ); @@ -1024,7 +1024,7 @@ describe('ExchangeWrapper', () => { it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => { (async () => { const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)( - (logEvent: DecodedLogEvent) => { + (_logEvent: DecodedLogEvent) => { done(new Error('Expected this subscription to have been cancelled')); }, ); diff --git a/packages/contract-wrappers/test/subscription_test.ts b/packages/contract-wrappers/test/subscription_test.ts index 4d638bf9b..b9417ca3d 100644 --- a/packages/contract-wrappers/test/subscription_test.ts +++ b/packages/contract-wrappers/test/subscription_test.ts @@ -80,7 +80,7 @@ describe('SubscriptionTest', () => { }); it('Should allow unsubscribeAll to be called successfully after an error', (done: DoneCallback) => { (async () => { - const callback = (err: Error | null, logEvent?: DecodedLogEvent) => _.noop; + const callback = (_err: Error | null, _logEvent?: DecodedLogEvent) => _.noop; contractWrappers.token.subscribe(tokenAddress, TokenEvents.Approval, indexFilterValues, callback); stubs = [ Sinon.stub((contractWrappers as any)._web3Wrapper, 'getBlockAsync').throws( diff --git a/packages/contract-wrappers/test/token_wrapper_test.ts b/packages/contract-wrappers/test/token_wrapper_test.ts index c9722c7b4..86b93b3f9 100644 --- a/packages/contract-wrappers/test/token_wrapper_test.ts +++ b/packages/contract-wrappers/test/token_wrapper_test.ts @@ -484,7 +484,7 @@ describe('TokenWrapper', () => { it('Outstanding subscriptions are cancelled when contractWrappers.setProvider called', (done: DoneCallback) => { (async () => { const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)( - (logEvent: DecodedLogEvent) => { + (_logEvent: DecodedLogEvent) => { done(new Error('Expected this subscription to have been cancelled')); }, ); @@ -508,7 +508,7 @@ describe('TokenWrapper', () => { it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => { (async () => { const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)( - (logEvent: DecodedLogEvent) => { + (_logEvent: DecodedLogEvent) => { done(new Error('Expected this subscription to have been cancelled')); }, ); diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index 140aa341b..d0acf2e6b 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -19,24 +19,9 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from '../artifacts'; -import { - DepositContractEventArgs, - EtherTokenContractEventArgs, - EtherTokenEvents, - WithdrawalContractEventArgs, -} from '../generated_contract_wrappers/ether_token'; -import { - ExchangeContractEventArgs, - ExchangeEvents, - LogCancelContractEventArgs, - LogFillContractEventArgs, -} from '../generated_contract_wrappers/exchange'; -import { - ApprovalContractEventArgs, - TokenContractEventArgs, - TokenEvents, - TransferContractEventArgs, -} from '../generated_contract_wrappers/token'; +import { EtherTokenContractEventArgs, EtherTokenEvents } from '../generated_contract_wrappers/ether_token'; +import { ExchangeContractEventArgs, ExchangeEvents } from '../generated_contract_wrappers/exchange'; +import { TokenContractEventArgs, TokenEvents } from '../generated_contract_wrappers/token'; import { OnOrderStateChangeCallback, OrderWatcherConfig, OrderWatcherError } from '../types'; import { assert } from '../utils/assert'; @@ -252,7 +237,7 @@ export class OrderWatcher { switch (decodedLog.event) { case TokenEvents.Approval: { // Invalidate cache - const args = decodedLog.args as ApprovalContractEventArgs; + const args = decodedLog.args; this._balanceAndProxyAllowanceLazyStore.deleteProxyAllowance(decodedLog.address, args._owner); // Revalidate orders makerToken = decodedLog.address; @@ -268,7 +253,7 @@ export class OrderWatcher { } case TokenEvents.Transfer: { // Invalidate cache - const args = decodedLog.args as TransferContractEventArgs; + const args = decodedLog.args; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._from); this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._to); // Revalidate orders @@ -285,7 +270,7 @@ export class OrderWatcher { } case EtherTokenEvents.Deposit: { // Invalidate cache - const args = decodedLog.args as DepositContractEventArgs; + const args = decodedLog.args; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._owner); // Revalidate orders makerToken = decodedLog.address; @@ -301,7 +286,7 @@ export class OrderWatcher { } case EtherTokenEvents.Withdrawal: { // Invalidate cache - const args = decodedLog.args as WithdrawalContractEventArgs; + const args = decodedLog.args; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._owner); // Revalidate orders makerToken = decodedLog.address; @@ -317,7 +302,7 @@ export class OrderWatcher { } case ExchangeEvents.LogFill: { // Invalidate cache - const args = decodedLog.args as LogFillContractEventArgs; + const args = decodedLog.args; this._orderFilledCancelledLazyStore.deleteFilledTakerAmount(args.orderHash); // Revalidate orders const orderHash = args.orderHash; @@ -329,7 +314,7 @@ export class OrderWatcher { } case ExchangeEvents.LogCancel: { // Invalidate cache - const args = decodedLog.args as LogCancelContractEventArgs; + const args = decodedLog.args; this._orderFilledCancelledLazyStore.deleteCancelledTakerAmount(args.orderHash); // Revalidate orders const orderHash = args.orderHash; diff --git a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts index 51308b6c2..77a1ac58e 100644 --- a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts @@ -33,6 +33,9 @@ describe('EthLightwalletSubprovider', () => { return new Promise(resolve => { // Create Vault lightwallet.keystore.createVault(vaultOptions, (err: Error, vaultKeystore) => { + if (err) { + throw new Error(`Failed to createVault: ${err}`); + } resolve(vaultKeystore); }); }); @@ -41,6 +44,9 @@ describe('EthLightwalletSubprovider', () => { const deriveKeyFromPasswordAsync = async (vaultKeystore: lightwallet.keystore) => { return new Promise(resolve => { vaultKeystore.keyFromPassword(PASSWORD, (err: Error, passwordDerivedKey: Uint8Array) => { + if (err) { + throw new Error(`Failed to get key from password: ${err}`); + } resolve(passwordDerivedKey); }); }); @@ -154,7 +160,7 @@ describe('EthLightwalletSubprovider', () => { params: ['0x0000000000000000000000000000000000000000', nonHexMessage], id: 1, }; - const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { + 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(); @@ -169,7 +175,7 @@ describe('EthLightwalletSubprovider', () => { params: [nonHexMessage, '0x0000000000000000000000000000000000000000'], id: 1, }; - const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { + 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(); -- cgit v1.2.3 From 1dd9ec4d5af3de37a9f7defd11265795d04c9604 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 22:58:50 +0200 Subject: Remove duplicate type --- packages/subproviders/src/globals.d.ts | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 1e104053f..287d5e0fe 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -62,26 +62,3 @@ 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[]; - } -} -- cgit v1.2.3 From 699de9174e242a81c469a14319c2384b94e47356 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 22:59:04 +0200 Subject: Format type --- .../types/eth-lightwallet/index.d.ts | 67 +++++++++++++++------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/packages/typescript-typings/types/eth-lightwallet/index.d.ts b/packages/typescript-typings/types/eth-lightwallet/index.d.ts index 76f910d9c..f6d0cd6d8 100644 --- a/packages/typescript-typings/types/eth-lightwallet/index.d.ts +++ b/packages/typescript-typings/types/eth-lightwallet/index.d.ts @@ -1,24 +1,51 @@ // eth-lightwallet declarations declare module 'eth-lightwallet' { - import { ECSignatureBuffer } from "@0xproject/types"; + import { ECSignatureBuffer } from '@0xproject/types'; - 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[]; - } + // tslint:disable-next-line:class-name + 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; + } + // tslint:disable-next-line:class-name + 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[]; + } + + interface VaultOptions { + password: string; + seedPhrase: string; + salt?: string; + hdPathString: string; + } } -- cgit v1.2.3 From b2e32aaf588196b21a453fe7f7e38eb6eb154c22 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 22:59:24 +0200 Subject: Remove legacy named subprovider --- .../src/subproviders/eth_lightwallet.ts | 100 --------------------- 1 file changed, 100 deletions(-) delete mode 100644 packages/subproviders/src/subproviders/eth_lightwallet.ts diff --git a/packages/subproviders/src/subproviders/eth_lightwallet.ts b/packages/subproviders/src/subproviders/eth_lightwallet.ts deleted file mode 100644 index 970c5d981..000000000 --- a/packages/subproviders/src/subproviders/eth_lightwallet.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { assert } from '@0xproject/assert'; -import { ECSignatureBuffer } from '@0xproject/types'; -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](https://github.com/MetaMask/provider-engine) subprovider interface. - * This subprovider intercepts all account related RPC requests (e.g message/transaction signing, etc...) and - * re-routes them to [eth-lightwallet](https://github.com/ConsenSys/eth-lightwallet). - */ -export class EthLightwalletSubprovider extends BaseWalletSubprovider { - private _signing: lightwallet.signing; - private _keystore: lightwallet.keystore; - private _pwDerivedKey: Uint8Array; - /** - * Instantiates a EthLightwalletSubprovider - * @param signing The lightwallet module containing signing functions - * @param keystore An instance of the lightwallet keystore - * @param pwDerivedKey The users password derived key - */ - 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 { - 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 { - 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 EthLightwalletSubprovider 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 { - 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; - } -} -- cgit v1.2.3 From 31e3b9ff8baac145be3e00eedc6d0ae065474b8d Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 22:59:41 +0200 Subject: Fix and imprrove ethLightWalletSubprovider --- packages/subproviders/package.json | 2 +- .../src/subproviders/eth_lightwallet_subprovider.ts | 19 ++++++++++--------- .../test/unit/eth_lightwallet_subprovider_test.ts | 11 ++++++----- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index c3e588718..76b88cee0 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -41,7 +41,7 @@ }, "dependencies": { "@0xproject/assert": "^0.2.12", - "@0xproject/types": "^0.8.1", + "@0xproject/types": "^1.0.0", "@0xproject/typescript-typings": "^0.4.1", "@0xproject/utils": "^0.7.1", "@ledgerhq/hw-app-eth": "^4.3.0", diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts index 7507eeb49..a908ab5fc 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts @@ -15,14 +15,12 @@ import { BaseWalletSubprovider } from './base_wallet_subprovider'; * Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js */ export class EthLightwalletSubprovider extends BaseWalletSubprovider { - private _signing: any; - private _keystore: any; + private _keystore: lightwallet.keystore; private _pwDerivedKey: Uint8Array; - constructor(signing: lightwallet.signing, keystore: lightwallet.keystore, pwDerivedKey: Uint8Array) { + constructor(keystore: lightwallet.keystore, pwDerivedKey: Uint8Array) { super(); - this._signing = signing; this._keystore = keystore; this._pwDerivedKey = pwDerivedKey; } @@ -54,8 +52,7 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { 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); + let signedTxHex: string = lightwallet.signing.signTx(this._keystore, this._pwDerivedKey, txHex, txParams.from); signedTxHex = `0x${signedTxHex}`; @@ -78,10 +75,14 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { } 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 result: ECSignatureBuffer = lightwallet.signing.signMsgHash( + this._keystore, + this._pwDerivedKey, + data, + address, + ); - const signature = this._signing.concatSig(result); + const signature = lightwallet.signing.concatSig(result); return signature; } diff --git a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts index 77a1ac58e..b99dbff2f 100644 --- a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts @@ -1,8 +1,7 @@ -import { JSONRPCResponsePayload } from '@0xproject/types'; import * as chai from 'chai'; import * as lightwallet from 'eth-lightwallet'; +import { JSONRPCResponsePayload } from 'ethereum-types'; import Web3ProviderEngine = require('web3-provider-engine'); -import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); import { EthLightwalletSubprovider } from '../../src'; import { DoneCallback } from '../../src/types'; @@ -59,7 +58,7 @@ describe('EthLightwalletSubprovider', () => { keystore.generateNewAddress(pwDerivedKey, NUM_GENERATED_ADDRESSES); // Initialize Subprovider - ethLightwalletSubprovider = new EthLightwalletSubprovider(lightwallet.signing, keystore, pwDerivedKey); + ethLightwalletSubprovider = new EthLightwalletSubprovider(keystore, pwDerivedKey); }); describe('direct method calls', () => { describe('success cases', () => { @@ -73,8 +72,10 @@ 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', -- cgit v1.2.3 From 3d56817da14091a2d966dba5e108a6b104e919e3 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 23:00:19 +0200 Subject: Update yarn.lock --- yarn.lock | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/yarn.lock b/yarn.lock index 54334395d..1a45d135e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -64,17 +64,6 @@ "@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" @@ -321,6 +310,10 @@ version "2.2.48" resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.48.tgz#3523b126a0b049482e1c3c11877460f76622ffab" +"@types/mocha@^5.2.2": + version "5.2.3" + resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.3.tgz#11f3a5629d67cd444fa6c94536576244e6a52ea9" + "@types/nock@^9.1.2": version "9.1.3" resolved "https://registry.yarnpkg.com/@types/nock/-/nock-9.1.3.tgz#1d445679375b9e25afd449dc56585f81729454e8" @@ -2924,6 +2917,17 @@ copyfiles@^1.2.0: noms "0.0.0" through2 "^2.0.1" +copyfiles@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/copyfiles/-/copyfiles-2.0.0.tgz#bbd78bb78e8fd6db5c67adf54249317b24560f2a" + dependencies: + glob "^7.0.5" + minimatch "^3.0.3" + mkdirp "^0.5.1" + noms "0.0.0" + through2 "^2.0.1" + yargs "^11.0.0" + core-js@^1.0.0: version "1.2.7" resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636" @@ -4112,6 +4116,13 @@ ethereum-common@^0.0.18: version "0.0.18" resolved "https://registry.yarnpkg.com/ethereum-common/-/ethereum-common-0.0.18.tgz#2fdc3576f232903358976eb39da783213ff9523f" +ethereum-types@^0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/ethereum-types/-/ethereum-types-0.0.2.tgz#6ef6faf46a24697cbf66b6c8a0ecf2095ce58c38" + dependencies: + "@types/node" "^8.0.53" + bignumber.js "~4.1.0" + ethereumjs-abi@0.6.5, ethereumjs-abi@^0.6.4, "ethereumjs-abi@git+https://github.com/ethereumjs/ethereumjs-abi.git": version "0.6.5" resolved "git+https://github.com/ethereumjs/ethereumjs-abi.git#4ea2fdfed09e8f99117d9362d17c6b01b64a2bcf" @@ -10240,6 +10251,10 @@ run-queue@^1.0.0, run-queue@^1.0.3: dependencies: aproba "^1.1.1" +run-s@^0.0.0: + version "0.0.0" + resolved "https://registry.yarnpkg.com/run-s/-/run-s-0.0.0.tgz#599912be20c00ba7698655c9936d075d31b71754" + rustbn.js@~0.1.1: version "0.1.2" resolved "https://registry.yarnpkg.com/rustbn.js/-/rustbn.js-0.1.2.tgz#979fa0f9562216dd667c9d2cd179ae5d13830eff" @@ -13207,6 +13222,23 @@ yargs@^10.0.3: y18n "^3.2.1" yargs-parser "^8.1.0" +yargs@^11.0.0: + version "11.0.0" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-11.0.0.tgz#c052931006c5eee74610e5fc0354bedfd08a201b" + dependencies: + cliui "^4.0.0" + decamelize "^1.1.1" + find-up "^2.1.0" + get-caller-file "^1.0.1" + os-locale "^2.0.0" + require-directory "^2.1.1" + require-main-filename "^1.0.1" + set-blocking "^2.0.0" + string-width "^2.0.0" + which-module "^2.0.0" + y18n "^3.2.1" + yargs-parser "^9.0.2" + yargs@^4.7.1: version "4.8.1" resolved "https://registry.yarnpkg.com/yargs/-/yargs-4.8.1.tgz#c0c42924ca4aaa6b0e6da1739dfb216439f9ddc0" -- cgit v1.2.3 From 6bb2203f790923a83f6f0665d47d29a90817b371 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Mon, 25 Jun 2018 23:07:27 +0200 Subject: Small stylistic tweaks --- .../subproviders/src/subproviders/eth_lightwallet_subprovider.ts | 6 ------ packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts | 5 +---- packages/typescript-typings/types/eth-lightwallet/index.d.ts | 1 - 3 files changed, 1 insertion(+), 11 deletions(-) diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts index a908ab5fc..64d984996 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts @@ -17,14 +17,11 @@ import { BaseWalletSubprovider } from './base_wallet_subprovider'; export class EthLightwalletSubprovider extends BaseWalletSubprovider { private _keystore: lightwallet.keystore; private _pwDerivedKey: Uint8Array; - constructor(keystore: lightwallet.keystore, pwDerivedKey: Uint8Array) { super(); - 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 @@ -36,7 +33,6 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { 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 @@ -58,7 +54,6 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { return signedTxHex; } - /** * Sign a personal Ethereum signed message. The signing account will be the account * associated with the provided address. @@ -83,7 +78,6 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { ); const signature = lightwallet.signing.concatSig(result); - return signature; } } diff --git a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts index b99dbff2f..c0adb9225 100644 --- a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts @@ -11,6 +11,7 @@ import { reportCallbackErrors } from '../utils/report_callback_errors'; chaiSetup.configure(); const expect = chai.expect; + const FAKE_ADDRESS = '0x44be42fd88e22387c43ba9b75941aa3e680dae25'; const NUM_GENERATED_ADDRESSES = 10; const PASSWORD = 'supersecretpassword99'; @@ -30,7 +31,6 @@ describe('EthLightwalletSubprovider', () => { const createVaultAsync = async (vaultOptions: lightwallet.VaultOptions) => { return new Promise(resolve => { - // Create Vault lightwallet.keystore.createVault(vaultOptions, (err: Error, vaultKeystore) => { if (err) { throw new Error(`Failed to createVault: ${err}`); @@ -39,7 +39,6 @@ describe('EthLightwalletSubprovider', () => { }); }); }; - const deriveKeyFromPasswordAsync = async (vaultKeystore: lightwallet.keystore) => { return new Promise(resolve => { vaultKeystore.keyFromPassword(PASSWORD, (err: Error, passwordDerivedKey: Uint8Array) => { @@ -50,14 +49,12 @@ describe('EthLightwalletSubprovider', () => { }); }); }; - const keystore: lightwallet.keystore = await createVaultAsync(options); const pwDerivedKey: Uint8Array = await deriveKeyFromPasswordAsync(keystore); // Generate 10 addresses keystore.generateNewAddress(pwDerivedKey, NUM_GENERATED_ADDRESSES); - // Initialize Subprovider ethLightwalletSubprovider = new EthLightwalletSubprovider(keystore, pwDerivedKey); }); describe('direct method calls', () => { diff --git a/packages/typescript-typings/types/eth-lightwallet/index.d.ts b/packages/typescript-typings/types/eth-lightwallet/index.d.ts index f6d0cd6d8..58096e9f4 100644 --- a/packages/typescript-typings/types/eth-lightwallet/index.d.ts +++ b/packages/typescript-typings/types/eth-lightwallet/index.d.ts @@ -41,7 +41,6 @@ declare module 'eth-lightwallet' { public exportPrivateKey(address: string, pwDerivedKey: Uint8Array): string; public getAddresses(): string[]; } - interface VaultOptions { password: string; seedPhrase: string; -- cgit v1.2.3 From b9165c03af40983d885af2b18e729f11746de91d Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 2 Jul 2018 11:07:51 +1000 Subject: Use PrivateKeySubprovider inside eth lightwallet There's a loss of information when hex encoding and passing to eth light wallet (chain id is lost). This results in a different signature. While it may work on testnets it is not sufficient for our test cases. We can export the private key and use it in our PrivateKeyWalletSubprovider --- .../subproviders/eth_lightwallet_subprovider.ts | 40 +++++------- .../test/unit/eth_lightwallet_subprovider_test.ts | 74 ++++++++-------------- .../unit/private_key_wallet_subprovider_test.ts | 14 ++++ 3 files changed, 57 insertions(+), 71 deletions(-) diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts index 64d984996..b594ffb24 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts @@ -2,11 +2,13 @@ import { assert } from '@0xproject/assert'; import { addressUtils } from '@0xproject/utils'; import * as lightwallet from 'eth-lightwallet'; import EthereumTx = require('ethereumjs-tx'); +import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; import { PartialTxParams, WalletSubproviderErrors } from '../types'; import { BaseWalletSubprovider } from './base_wallet_subprovider'; +import { PrivateKeyWalletSubprovider } from './private_key_wallet'; /* * This class implements the web3-provider-engine subprovider interface and forwards @@ -42,17 +44,14 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { * @return Signed transaction hex string */ public async signTransactionAsync(txParams: PartialTxParams): Promise { - 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 = lightwallet.signing.signTx(this._keystore, this._pwDerivedKey, txHex, txParams.from); - - signedTxHex = `0x${signedTxHex}`; - - return signedTxHex; + // Lightwallet loses the chain id information when hex encoding the transaction + // this results in a different signature on certain networks. PrivateKeyWallet + // respects this as it uses the parameters passed in + let privKey = this._keystore.exportPrivateKey(txParams.from, this._pwDerivedKey); + const privKeyWallet = new PrivateKeyWalletSubprovider(privKey); + const privKeySignature = await privKeyWallet.signTransactionAsync(txParams); + privKey = ''; + return privKeySignature; } /** * Sign a personal Ethereum signed message. The signing account will be the account @@ -65,19 +64,10 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { * @return Signature hex string (order: rsv) */ public async signPersonalMessageAsync(data: string, address: string): Promise { - if (_.isUndefined(data)) { - throw new Error(WalletSubproviderErrors.DataMissingForSignPersonalMessage); - } - assert.isHexString('data', data); - assert.isETHAddressHex('address', address); - const result: ECSignatureBuffer = lightwallet.signing.signMsgHash( - this._keystore, - this._pwDerivedKey, - data, - address, - ); - - const signature = lightwallet.signing.concatSig(result); - return signature; + let privKey = this._keystore.exportPrivateKey(address, this._pwDerivedKey); + const privKeyWallet = new PrivateKeyWalletSubprovider(privKey); + privKey = ''; + const result = privKeyWallet.signPersonalMessageAsync(data, address); + return result; } } diff --git a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts index c0adb9225..f17c21f02 100644 --- a/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/eth_lightwallet_subprovider_test.ts @@ -1,34 +1,32 @@ import * as chai from 'chai'; import * as lightwallet from 'eth-lightwallet'; import { JSONRPCResponsePayload } from 'ethereum-types'; +import * as ethUtils from 'ethereumjs-util'; import Web3ProviderEngine = require('web3-provider-engine'); import { EthLightwalletSubprovider } from '../../src'; import { DoneCallback } from '../../src/types'; import { chaiSetup } from '../chai_setup'; +import { fixtureData } from '../utils/fixture_data'; import { ganacheSubprovider } from '../utils/ganache_subprovider'; import { reportCallbackErrors } from '../utils/report_callback_errors'; chaiSetup.configure(); const expect = chai.expect; -const FAKE_ADDRESS = '0x44be42fd88e22387c43ba9b75941aa3e680dae25'; -const NUM_GENERATED_ADDRESSES = 10; +const DEFAULT_NUM_ACCOUNTS = 10; const PASSWORD = 'supersecretpassword99'; -const SEED_PHRASE = 'dilemma hollow outer pony cube season start stereo surprise when edit blast'; const SALT = 'kvODghzs7Ff1uqHyI0P3wI4Hso4w4iWT2e9qmrWz0y4'; -const HD_PATH_STRING = `m/44'/60'/0'`; describe('EthLightwalletSubprovider', () => { let ethLightwalletSubprovider: EthLightwalletSubprovider; before(async () => { const options = { password: PASSWORD, - seedPhrase: SEED_PHRASE, + seedPhrase: fixtureData.TEST_RPC_MNEMONIC, salt: SALT, - hdPathString: HD_PATH_STRING, + hdPathString: fixtureData.TESTRPC_BASE_DERIVATION_PATH, }; - const createVaultAsync = async (vaultOptions: lightwallet.VaultOptions) => { return new Promise(resolve => { lightwallet.keystore.createVault(vaultOptions, (err: Error, vaultKeystore) => { @@ -53,7 +51,7 @@ describe('EthLightwalletSubprovider', () => { const pwDerivedKey: Uint8Array = await deriveKeyFromPasswordAsync(keystore); // Generate 10 addresses - keystore.generateNewAddress(pwDerivedKey, NUM_GENERATED_ADDRESSES); + keystore.generateNewAddress(pwDerivedKey, DEFAULT_NUM_ACCOUNTS); ethLightwalletSubprovider = new EthLightwalletSubprovider(keystore, pwDerivedKey); }); @@ -61,22 +59,20 @@ describe('EthLightwalletSubprovider', () => { describe('success cases', () => { it('returns a list of accounts', async () => { const accounts = await ethLightwalletSubprovider.getAccountsAsync(); - expect(accounts[0]).to.be.equal(FAKE_ADDRESS); - expect(accounts.length).to.be.equal(NUM_GENERATED_ADDRESSES); + expect(accounts[0]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_0); + expect(accounts[1]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_1); + expect(accounts.length).to.be.equal(DEFAULT_NUM_ACCOUNTS); }); it('signs a personal message hash', async () => { - const signingAccount = (await ethLightwalletSubprovider.getAccountsAsync())[0]; - - // Keccak-256 hash of 'hello world' - const messageHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'; - const ecSignatureHex = await ethLightwalletSubprovider.signPersonalMessageAsync( - messageHash, - signingAccount, - ); - expect(ecSignatureHex).to.be.equal( - // tslint:disable-next-line:max-line-length - '0xa46b696c1aa8f91dbb33d1a66f6440bf3cf334c9dc45dc389668c1e60e2db31e259400b41f31632fa994837054c5345c88dc455c13931332489029adee6fd24d1b', - ); + const accounts = await ethLightwalletSubprovider.getAccountsAsync(); + const signingAccount = accounts[0]; + const data = ethUtils.bufferToHex(ethUtils.toBuffer(fixtureData.PERSONAL_MESSAGE_STRING)); + const ecSignatureHex = await ethLightwalletSubprovider.signPersonalMessageAsync(data, signingAccount); + expect(ecSignatureHex).to.be.equal(fixtureData.PERSONAL_MESSAGE_SIGNED_RESULT); + }); + it('signs a transaction', async () => { + const txHex = await ethLightwalletSubprovider.signTransactionAsync(fixtureData.TX_DATA); + expect(txHex).to.be.equal(fixtureData.TX_DATA_SIGNED_RESULT); }); }); }); @@ -98,52 +94,38 @@ describe('EthLightwalletSubprovider', () => { }; const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { expect(err).to.be.a('null'); - expect(response.result.length).to.be.equal(NUM_GENERATED_ADDRESSES); - expect(response.result[0]).to.be.equal(FAKE_ADDRESS); + expect(response.result[0]).to.be.equal(fixtureData.TEST_RPC_ACCOUNT_0); + expect(response.result.length).to.be.equal(DEFAULT_NUM_ACCOUNTS); done(); }); provider.sendAsync(payload, callback); }); it('signs a personal message hash with eth_sign', (done: DoneCallback) => { - // Keccak-256 hash of 'hello world' - const messageHash = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'; + const data = ethUtils.bufferToHex(ethUtils.toBuffer(fixtureData.PERSONAL_MESSAGE_STRING)); + const account = fixtureData.TEST_RPC_ACCOUNT_0; const payload = { jsonrpc: '2.0', method: 'eth_sign', - params: ['0x44be42fd88e22387c43ba9b75941aa3e680dae25', messageHash], + params: [account, data], id: 1, }; const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { expect(err).to.be.a('null'); - expect(response.result).to.be.equal( - // tslint:disable-next-line:max-line-length - '0xa46b696c1aa8f91dbb33d1a66f6440bf3cf334c9dc45dc389668c1e60e2db31e259400b41f31632fa994837054c5345c88dc455c13931332489029adee6fd24d1b', - ); + expect(response.result).to.be.equal(fixtureData.PERSONAL_MESSAGE_SIGNED_RESULT); done(); }); provider.sendAsync(payload, callback); }); it('signs a transaction', (done: DoneCallback) => { - const tx = { - to: '0xafa3f8684e54059998bc3a7b0d2b0da075154d66', - value: '0x00', - gasPrice: '0x00', - nonce: '0x00', - gas: '0x00', - from: '0x44be42fd88e22387c43ba9b75941aa3e680dae25', - }; const payload = { jsonrpc: '2.0', method: 'eth_signTransaction', - params: [tx], + params: [fixtureData.TX_DATA], id: 1, }; const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { - const expectedResponseLength = 192; - expect(err).to.be.a('null'); - expect(response.result.raw.length).to.be.equal(expectedResponseLength); - expect(response.result.raw.substr(0, 2)).to.be.equal('0x'); + expect(response.result.raw).to.be.equal(fixtureData.TX_DATA_SIGNED_RESULT); done(); }); provider.sendAsync(payload, callback); @@ -155,7 +137,7 @@ describe('EthLightwalletSubprovider', () => { const payload = { jsonrpc: '2.0', method: 'eth_sign', - params: ['0x0000000000000000000000000000000000000000', nonHexMessage], + params: [fixtureData.TEST_RPC_ACCOUNT_0, nonHexMessage], id: 1, }; const callback = reportCallbackErrors(done)((err: Error, _response: JSONRPCResponsePayload) => { @@ -170,7 +152,7 @@ describe('EthLightwalletSubprovider', () => { const payload = { jsonrpc: '2.0', method: 'personal_sign', - params: [nonHexMessage, '0x0000000000000000000000000000000000000000'], + params: [nonHexMessage, fixtureData.TEST_RPC_ACCOUNT_0], id: 1, }; const callback = reportCallbackErrors(done)((err: Error, _response: JSONRPCResponsePayload) => { diff --git a/packages/subproviders/test/unit/private_key_wallet_subprovider_test.ts b/packages/subproviders/test/unit/private_key_wallet_subprovider_test.ts index a41ad7790..ab321bcff 100644 --- a/packages/subproviders/test/unit/private_key_wallet_subprovider_test.ts +++ b/packages/subproviders/test/unit/private_key_wallet_subprovider_test.ts @@ -60,6 +60,20 @@ describe('PrivateKeyWalletSubprovider', () => { }); provider.sendAsync(payload, callback); }); + it('signs a transaction', (done: DoneCallback) => { + const payload = { + jsonrpc: '2.0', + method: 'eth_signTransaction', + params: [fixtureData.TX_DATA], + id: 1, + }; + const callback = reportCallbackErrors(done)((err: Error, response: JSONRPCResponsePayload) => { + expect(err).to.be.a('null'); + expect(response.result.raw).to.be.equal(fixtureData.TX_DATA_SIGNED_RESULT); + 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 = { -- cgit v1.2.3 From 687802394a6959ca5058ae1a4445d513d04062c6 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 2 Jul 2018 18:09:05 +1000 Subject: Remove duplicate type definitions. Clear private key earlier Fix changelog in typescript-typings from bad merge --- packages/subproviders/src/globals.d.ts | 58 ---------------------- .../subproviders/eth_lightwallet_subprovider.ts | 2 +- packages/typescript-typings/CHANGELOG.json | 1 + 3 files changed, 2 insertions(+), 59 deletions(-) diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 287d5e0fe..94e63a32d 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -1,61 +1,3 @@ -// tslint:disable:max-classes-per-file -// tslint:disable:class-name -// tslint:disable:async-suffix -// tslint:disable:completed-docs - -// Ethereumjs-tx declarations - -// Ledgerco declarations -interface ECSignatureString { - v: string; - r: string; - s: string; -} -interface ECSignature { - v: number; - r: string; - s: string; -} -interface ECSignatureBuffer { - v: number; - r: Buffer; - s: Buffer; -} - -interface LedgerTransport { - close(): Promise; -} - -declare module '@ledgerhq/hw-app-eth' { - class Eth { - public transport: LedgerTransport; - constructor(transport: LedgerTransport); - public getAddress( - path: string, - boolDisplay?: boolean, - boolChaincode?: boolean, - ): Promise<{ publicKey: string; address: string; chainCode: string }>; - public signTransaction(path: string, rawTxHex: string): Promise; - public getAppConfiguration(): Promise<{ arbitraryDataEnabled: number; version: string }>; - public signPersonalMessage(path: string, messageHex: string): Promise; - } - export default Eth; -} - -declare module '@ledgerhq/hw-transport-u2f' { - export default class TransportU2F implements LedgerTransport { - public static create(): Promise; - public close(): Promise; - } -} - -declare module '@ledgerhq/hw-transport-node-hid' { - export default class TransportNodeHid implements LedgerTransport { - public static create(): Promise; - public close(): Promise; - } -} - declare module '*.json' { const json: any; /* tslint:disable */ diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts index b594ffb24..3cd94dac3 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts @@ -49,8 +49,8 @@ export class EthLightwalletSubprovider extends BaseWalletSubprovider { // respects this as it uses the parameters passed in let privKey = this._keystore.exportPrivateKey(txParams.from, this._pwDerivedKey); const privKeyWallet = new PrivateKeyWalletSubprovider(privKey); - const privKeySignature = await privKeyWallet.signTransactionAsync(txParams); privKey = ''; + const privKeySignature = await privKeyWallet.signTransactionAsync(txParams); return privKeySignature; } /** diff --git a/packages/typescript-typings/CHANGELOG.json b/packages/typescript-typings/CHANGELOG.json index 2e4b8dfdf..1d4230783 100644 --- a/packages/typescript-typings/CHANGELOG.json +++ b/packages/typescript-typings/CHANGELOG.json @@ -6,6 +6,7 @@ "note": "Add types for `eth-lightwallet`", "pr": 775 }, + { "note": "Improve 'web3-provider-engine' typings", "pr": 768 }, -- cgit v1.2.3 From d6f40a9281ef3db8f6daffc17712008a2509a7f0 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 2 Jul 2018 18:14:19 +1000 Subject: Remove duplicated typescript-typings in package.json --- packages/subproviders/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index 5fd7c3c2d..e5c87ee45 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -42,7 +42,6 @@ "dependencies": { "@0xproject/assert": "^0.2.12", "@0xproject/types": "^1.0.0", - "@0xproject/typescript-typings": "^0.4.1", "@0xproject/utils": "^0.7.1", "@ledgerhq/hw-app-eth": "^4.3.0", "@ledgerhq/hw-transport-u2f": "^4.3.0", -- cgit v1.2.3 From 1a901554cceab03ead6129a1f41d87349fc3fe07 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 3 Jul 2018 15:39:15 +1000 Subject: compiler and ts lint are confused about ContractEventArgs --- .../src/order_watcher/order_watcher.ts | 31 ++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index d0acf2e6b..3418b4ee9 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -1,7 +1,10 @@ import { BalanceAndProxyAllowanceLazyStore, ContractWrappers, + LogCancelContractEventArgs, + LogFillContractEventArgs, OrderFilledCancelledLazyStore, + WithdrawalContractEventArgs, } from '@0xproject/contract-wrappers'; import { schemas } from '@0xproject/json-schemas'; import { getOrderHashHex, OrderStateUtils } from '@0xproject/order-utils'; @@ -19,9 +22,18 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as _ from 'lodash'; import { artifacts } from '../artifacts'; -import { EtherTokenContractEventArgs, EtherTokenEvents } from '../generated_contract_wrappers/ether_token'; +import { + DepositContractEventArgs, + EtherTokenContractEventArgs, + EtherTokenEvents, +} from '../generated_contract_wrappers/ether_token'; import { ExchangeContractEventArgs, ExchangeEvents } from '../generated_contract_wrappers/exchange'; -import { TokenContractEventArgs, TokenEvents } from '../generated_contract_wrappers/token'; +import { + ApprovalContractEventArgs, + TokenContractEventArgs, + TokenEvents, + TransferContractEventArgs, +} from '../generated_contract_wrappers/token'; import { OnOrderStateChangeCallback, OrderWatcherConfig, OrderWatcherError } from '../types'; import { assert } from '../utils/assert'; @@ -237,7 +249,7 @@ export class OrderWatcher { switch (decodedLog.event) { case TokenEvents.Approval: { // Invalidate cache - const args = decodedLog.args; + const args = decodedLog.args as ApprovalContractEventArgs; this._balanceAndProxyAllowanceLazyStore.deleteProxyAllowance(decodedLog.address, args._owner); // Revalidate orders makerToken = decodedLog.address; @@ -253,7 +265,7 @@ export class OrderWatcher { } case TokenEvents.Transfer: { // Invalidate cache - const args = decodedLog.args; + const args = decodedLog.args as TransferContractEventArgs; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._from); this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._to); // Revalidate orders @@ -270,7 +282,7 @@ export class OrderWatcher { } case EtherTokenEvents.Deposit: { // Invalidate cache - const args = decodedLog.args; + const args = decodedLog.args as DepositContractEventArgs; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._owner); // Revalidate orders makerToken = decodedLog.address; @@ -286,7 +298,8 @@ export class OrderWatcher { } case EtherTokenEvents.Withdrawal: { // Invalidate cache - const args = decodedLog.args; + // tslint:disable-next-line + const args = decodedLog.args as WithdrawalContractEventArgs; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._owner); // Revalidate orders makerToken = decodedLog.address; @@ -302,7 +315,8 @@ export class OrderWatcher { } case ExchangeEvents.LogFill: { // Invalidate cache - const args = decodedLog.args; + // tslint:disable-next-line + const args = decodedLog.args as LogFillContractEventArgs; this._orderFilledCancelledLazyStore.deleteFilledTakerAmount(args.orderHash); // Revalidate orders const orderHash = args.orderHash; @@ -314,7 +328,8 @@ export class OrderWatcher { } case ExchangeEvents.LogCancel: { // Invalidate cache - const args = decodedLog.args; + // tslint:disable-next-line + const args = decodedLog.args as LogCancelContractEventArgs; this._orderFilledCancelledLazyStore.deleteCancelledTakerAmount(args.orderHash); // Revalidate orders const orderHash = args.orderHash; -- cgit v1.2.3 From 27c03cffe2c1fe7f6c70cab286594840ef37d6e2 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Tue, 3 Jul 2018 17:01:08 +1000 Subject: Fix unused imports --- .../subproviders/src/subproviders/eth_lightwallet_subprovider.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts index 3cd94dac3..a9ebbb790 100644 --- a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts @@ -1,11 +1,6 @@ -import { assert } from '@0xproject/assert'; -import { addressUtils } from '@0xproject/utils'; import * as lightwallet from 'eth-lightwallet'; -import EthereumTx = require('ethereumjs-tx'); -import * as ethUtil from 'ethereumjs-util'; -import * as _ from 'lodash'; -import { PartialTxParams, WalletSubproviderErrors } from '../types'; +import { PartialTxParams } from '../types'; import { BaseWalletSubprovider } from './base_wallet_subprovider'; import { PrivateKeyWalletSubprovider } from './private_key_wallet'; -- cgit v1.2.3 From 9e0f06d06055022fff80bbf7a8b6444f72f898ec Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Wed, 4 Jul 2018 20:06:10 +1000 Subject: Specify the lint rule to disable --- packages/order-watcher/src/order_watcher/order_watcher.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/order-watcher/src/order_watcher/order_watcher.ts b/packages/order-watcher/src/order_watcher/order_watcher.ts index 3418b4ee9..0ee56592e 100644 --- a/packages/order-watcher/src/order_watcher/order_watcher.ts +++ b/packages/order-watcher/src/order_watcher/order_watcher.ts @@ -298,7 +298,7 @@ export class OrderWatcher { } case EtherTokenEvents.Withdrawal: { // Invalidate cache - // tslint:disable-next-line + // tslint:disable-next-line:no-unnecessary-type-assertion const args = decodedLog.args as WithdrawalContractEventArgs; this._balanceAndProxyAllowanceLazyStore.deleteBalance(decodedLog.address, args._owner); // Revalidate orders @@ -315,7 +315,7 @@ export class OrderWatcher { } case ExchangeEvents.LogFill: { // Invalidate cache - // tslint:disable-next-line + // tslint:disable-next-line:no-unnecessary-type-assertion const args = decodedLog.args as LogFillContractEventArgs; this._orderFilledCancelledLazyStore.deleteFilledTakerAmount(args.orderHash); // Revalidate orders @@ -328,7 +328,7 @@ export class OrderWatcher { } case ExchangeEvents.LogCancel: { // Invalidate cache - // tslint:disable-next-line + // tslint:disable-next-line:no-unnecessary-type-assertion const args = decodedLog.args as LogCancelContractEventArgs; this._orderFilledCancelledLazyStore.deleteCancelledTakerAmount(args.orderHash); // Revalidate orders -- cgit v1.2.3