diff --git a/package-lock.json b/package-lock.json index 7c9f3fffd63..a4cbc316777 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2570,8 +2570,34 @@ "resolved": "https://registry.npmjs.org/eth-sig-util/-/eth-sig-util-1.4.2.tgz", "integrity": "sha1-jZWCAsftuq6Dlwf7pvCf8ydgYhA=", "requires": { - "ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git", + "ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git#572d4bafe08a8a231137e1f9daeb0f8a23f197d2", "ethereumjs-util": "^5.1.1" + }, + "dependencies": { + "ethereumjs-abi": { + "version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#572d4bafe08a8a231137e1f9daeb0f8a23f197d2", + "from": "git+https://github.com/ethereumjs/ethereumjs-abi.git", + "requires": { + "bn.js": "^4.11.8", + "ethereumjs-util": "^6.0.0" + }, + "dependencies": { + "ethereumjs-util": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-6.1.0.tgz", + "integrity": "sha512-URESKMFbDeJxnAxPppnk2fN6Y3BIatn9fwn76Lm8bQlt+s52TpG8dN9M66MLPuRAiAOIqL3dfwqWJf0sd0fL0Q==", + "requires": { + "bn.js": "^4.11.0", + "create-hash": "^1.1.2", + "ethjs-util": "0.1.6", + "keccak": "^1.0.2", + "rlp": "^2.0.0", + "safe-buffer": "^5.1.1", + "secp256k1": "^3.0.1" + } + } + } + } } }, "ethereumjs-abi": { @@ -2600,6 +2626,14 @@ } } }, + "eth-method-registry": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/eth-method-registry/-/eth-method-registry-1.1.0.tgz", + "integrity": "sha512-jGbbGYd19XJCtoGFtUD2qJYWefKCCbFcu7F/AQ5sJXvqTIVAHnFn3paaV2zhN5t7iyKYp1qxc+ugOky+72xcbg==", + "requires": { + "ethjs": "^0.3.0" + } + }, "eth-phishing-detect": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/eth-phishing-detect/-/eth-phishing-detect-1.1.13.tgz", @@ -2874,6 +2908,123 @@ } } }, + "ethjs": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/ethjs/-/ethjs-0.3.9.tgz", + "integrity": "sha512-gOQzA3tDUjoLpNONSOALJ/rUFtHi5tXl2mholHasF1cvXhoddqi06yU4OJFJu9AGd6n9v9ywzHlYeIKg1t1hdw==", + "requires": { + "bn.js": "4.11.6", + "ethjs-abi": "0.2.1", + "ethjs-contract": "0.2.2", + "ethjs-filter": "0.1.8", + "ethjs-provider-http": "0.1.6", + "ethjs-query": "0.3.7", + "ethjs-unit": "0.1.6", + "ethjs-util": "0.1.3", + "js-sha3": "0.5.5", + "number-to-bn": "1.7.0" + }, + "dependencies": { + "bn.js": { + "version": "4.11.6", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.6.tgz", + "integrity": "sha1-UzRK2xRhehP26N0s4okF0cC6MhU=" + }, + "ethjs-query": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/ethjs-query/-/ethjs-query-0.3.7.tgz", + "integrity": "sha512-TZnKUwfkWjy0SowFdPLtmsytCorHi0i4vvkQn7Jg8rZt33cRzKhuzOwKr/G3vdigCc+ePXOhUGMcJSAPlOG44A==", + "requires": { + "ethjs-format": "0.2.7", + "ethjs-rpc": "0.2.0", + "promise-to-callback": "^1.0.0" + } + }, + "ethjs-util": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/ethjs-util/-/ethjs-util-0.1.3.tgz", + "integrity": "sha1-39XqSkANxeQhqInK9H4IGtp4u1U=", + "requires": { + "is-hex-prefixed": "1.0.0", + "strip-hex-prefix": "1.0.0" + } + }, + "js-sha3": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.5.5.tgz", + "integrity": "sha1-uvDA6MVK1ZA0R9+Wreekobynmko=" + } + } + }, + "ethjs-abi": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/ethjs-abi/-/ethjs-abi-0.2.1.tgz", + "integrity": "sha1-4KepOn6BFjqUR3utVu3lJKtt5TM=", + "requires": { + "bn.js": "4.11.6", + "js-sha3": "0.5.5", + "number-to-bn": "1.7.0" + }, + "dependencies": { + "bn.js": { + "version": "4.11.6", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.6.tgz", + "integrity": "sha1-UzRK2xRhehP26N0s4okF0cC6MhU=" + }, + "js-sha3": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.5.5.tgz", + "integrity": "sha1-uvDA6MVK1ZA0R9+Wreekobynmko=" + } + } + }, + "ethjs-contract": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/ethjs-contract/-/ethjs-contract-0.2.2.tgz", + "integrity": "sha512-xxPqEjsULQ/QNWuvX6Ako0PGs5RxALA8N/H3+boLvnaXDFZVGpD7H63H1gBCRTZyYqCldPpVlVHuw/rD45vazw==", + "requires": { + "ethjs-abi": "0.2.0", + "ethjs-filter": "0.1.8", + "ethjs-util": "0.1.3", + "js-sha3": "0.5.5" + }, + "dependencies": { + "bn.js": { + "version": "4.11.6", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.6.tgz", + "integrity": "sha1-UzRK2xRhehP26N0s4okF0cC6MhU=" + }, + "ethjs-abi": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/ethjs-abi/-/ethjs-abi-0.2.0.tgz", + "integrity": "sha1-0+LCIQEVIPxJm3FoIDbBT8wvWyU=", + "requires": { + "bn.js": "4.11.6", + "js-sha3": "0.5.5", + "number-to-bn": "1.7.0" + } + }, + "ethjs-util": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/ethjs-util/-/ethjs-util-0.1.3.tgz", + "integrity": "sha1-39XqSkANxeQhqInK9H4IGtp4u1U=", + "requires": { + "is-hex-prefixed": "1.0.0", + "strip-hex-prefix": "1.0.0" + } + }, + "js-sha3": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.5.5.tgz", + "integrity": "sha1-uvDA6MVK1ZA0R9+Wreekobynmko=" + } + } + }, + "ethjs-filter": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/ethjs-filter/-/ethjs-filter-0.1.8.tgz", + "integrity": "sha512-qTDPskDL2UadHwjvM8A+WG9HwM4/FoSY3p3rMJORkHltYcAuiQZd2otzOYKcL5w2Q3sbAkW/E3yt/FPFL/AVXA==" + }, "ethjs-format": { "version": "0.2.7", "resolved": "https://registry.npmjs.org/ethjs-format/-/ethjs-format-0.2.7.tgz", @@ -2907,7 +3058,6 @@ "version": "0.1.6", "resolved": "https://registry.npmjs.org/ethjs-provider-http/-/ethjs-provider-http-0.1.6.tgz", "integrity": "sha1-HsXZtL4lfvHValALIqdBmF6IlCA=", - "dev": true, "requires": { "xhr2": "0.1.3" } @@ -2936,6 +3086,22 @@ "resolved": "https://registry.npmjs.org/ethjs-schema/-/ethjs-schema-0.2.1.tgz", "integrity": "sha512-DXd8lwNrhT9sjsh/Vd2Z+4pfyGxhc0POVnLBUfwk5udtdoBzADyq+sK39dcb48+ZU+2VgtwHxtGWnLnCfmfW5g==" }, + "ethjs-unit": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/ethjs-unit/-/ethjs-unit-0.1.6.tgz", + "integrity": "sha1-xmWSHkduh7ziqdWIpv4EBbLEFpk=", + "requires": { + "bn.js": "4.11.6", + "number-to-bn": "1.7.0" + }, + "dependencies": { + "bn.js": { + "version": "4.11.6", + "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.6.tgz", + "integrity": "sha1-UzRK2xRhehP26N0s4okF0cC6MhU=" + } + } + }, "ethjs-util": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/ethjs-util/-/ethjs-util-0.1.6.tgz", @@ -3282,8 +3448,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -3304,14 +3469,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3326,20 +3489,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -3456,8 +3616,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -3469,7 +3628,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3484,7 +3642,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3492,14 +3649,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -3518,7 +3673,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -3599,8 +3753,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -3612,7 +3765,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -3698,8 +3850,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -3735,7 +3886,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3755,7 +3905,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3799,14 +3948,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -8830,7 +8977,7 @@ "resolved": "https://registry.npmjs.org/web3/-/web3-0.20.7.tgz", "integrity": "sha512-VU6/DSUX93d1fCzBz7WP/SGCQizO1rKZi4Px9j/3yRyfssHyFcZamMw2/sj4E8TlfMXONvZLoforR8B4bRoyTQ==", "requires": { - "bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git", + "bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git#57692b3ecfc98bbdd6b3a516cb2353652ea49934", "crypto-js": "^3.1.4", "utf8": "^2.1.1", "xhr2-cookies": "^1.1.0", @@ -8868,8 +9015,34 @@ "resolved": "https://registry.npmjs.org/eth-sig-util/-/eth-sig-util-1.4.2.tgz", "integrity": "sha1-jZWCAsftuq6Dlwf7pvCf8ydgYhA=", "requires": { - "ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git", + "ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git#572d4bafe08a8a231137e1f9daeb0f8a23f197d2", "ethereumjs-util": "^5.1.1" + }, + "dependencies": { + "ethereumjs-abi": { + "version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#572d4bafe08a8a231137e1f9daeb0f8a23f197d2", + "from": "git+https://github.com/ethereumjs/ethereumjs-abi.git", + "requires": { + "bn.js": "^4.11.8", + "ethereumjs-util": "^6.0.0" + }, + "dependencies": { + "ethereumjs-util": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-6.1.0.tgz", + "integrity": "sha512-URESKMFbDeJxnAxPppnk2fN6Y3BIatn9fwn76Lm8bQlt+s52TpG8dN9M66MLPuRAiAOIqL3dfwqWJf0sd0fL0Q==", + "requires": { + "bn.js": "^4.11.0", + "create-hash": "^1.1.2", + "ethjs-util": "0.1.6", + "keccak": "^1.0.2", + "rlp": "^2.0.0", + "safe-buffer": "^5.1.1", + "secp256k1": "^3.0.1" + } + } + } + } } }, "ethereumjs-abi": { @@ -9026,8 +9199,7 @@ "xhr2": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/xhr2/-/xhr2-0.1.3.tgz", - "integrity": "sha1-y/xHWaabSoiOeM9PILBRA4dXvRE=", - "dev": true + "integrity": "sha1-y/xHWaabSoiOeM9PILBRA4dXvRE=" }, "xhr2-cookies": { "version": "1.1.0", diff --git a/package.json b/package.json index 973c26f643c..fa241169fb5 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "eth-contract-metadata": "github:MetaMask/eth-contract-metadata#faa4f56fb17b3ae8579df68708be59d617732f31", "eth-json-rpc-infura": "^3.1.2", "eth-keyring-controller": "^4.0.0", + "eth-method-registry": "1.1.0", "eth-phishing-detect": "^1.1.13", "eth-query": "^2.1.2", "eth-sig-util": "^2.1.0", diff --git a/src/TransactionController.test.ts b/src/TransactionController.test.ts index b9195b4ba67..6738530e26b 100644 --- a/src/TransactionController.test.ts +++ b/src/TransactionController.test.ts @@ -48,6 +48,7 @@ function mockFetch(data: any) { const HttpProvider = require('ethjs-provider-http'); const MOCK_PRFERENCES = { state: { selectedAddress: 'foo' } }; const PROVIDER = new HttpProvider('https://ropsten.infura.io'); +const MAINNET_PROVIDER = new HttpProvider('https://mainnet.infura.io'); const MOCK_NETWORK = { provider: PROVIDER, state: { network: '3', provider: { type: 'ropsten' } }, @@ -55,6 +56,13 @@ const MOCK_NETWORK = { /* eslint-disable-line no-empty */ } }; +const MOCK_MAINNET_NETWORK = { + provider: MAINNET_PROVIDER, + state: { network: '1', provider: { type: 'mainnet' } }, + subscribe: () => { + /* eslint-disable-line no-empty */ + } +}; const MOCK_FETCH_TX_HISTORY_DATA_OK = { message: 'OK', @@ -151,7 +159,7 @@ describe('TransactionController', () => { it('should set default state', () => { const controller = new TransactionController(); - expect(controller.state).toEqual({ transactions: [] }); + expect(controller.state).toEqual({ methodData: {}, transactions: [] }); }); it('should set default config', () => { @@ -370,7 +378,6 @@ describe('TransactionController', () => { controller.approveTransaction(controller.state.transactions[0].id); }); }); - it('should query transaction statuses', () => { return new Promise((resolve) => { const controller = new TransactionController({ @@ -429,4 +436,34 @@ describe('TransactionController', () => { expect(controller.state.transactions.length).toBe(0); expect(result).toBe(undefined); }); + + it('should handle new method data', async () => { + const controller = new TransactionController({ provider: MOCK_MAINNET_NETWORK }); + controller.context = { + NetworkController: MOCK_MAINNET_NETWORK + } as any; + controller.onComposed(); + const registry = await controller.handleMethodData('0xf39b5b9b'); + expect(registry.parsedRegistryMethod).toEqual({ + args: [{ type: 'uint256' }, { type: 'uint256' }], + name: 'Eth To Token Swap Input' + }); + expect(registry.registryMethod).toEqual('ethToTokenSwapInput(uint256,uint256)'); + }); + + it('should handle known method data', async () => { + const controller = new TransactionController({ provider: MOCK_MAINNET_NETWORK }); + controller.context = { + NetworkController: MOCK_MAINNET_NETWORK + } as any; + controller.onComposed(); + const registry = await controller.handleMethodData('0xf39b5b9b'); + expect(registry.parsedRegistryMethod).toEqual({ + args: [{ type: 'uint256' }, { type: 'uint256' }], + name: 'Eth To Token Swap Input' + }); + const registryLookup = stub(controller, 'registryLookup' as any); + await controller.handleMethodData('0xf39b5b9b'); + expect(registryLookup.called).toBe(false); + }); }); diff --git a/src/TransactionController.ts b/src/TransactionController.ts index 56cfe5a5968..fdbadb1b4d9 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -10,11 +10,12 @@ import { validateTransaction, isSmartContractCode } from './util'; - +const MethodRegistry = require('eth-method-registry'); const EthQuery = require('eth-query'); const Transaction = require('ethereumjs-tx'); const random = require('uuid/v1'); const { addHexPrefix, bufferToHex, BN } = require('ethereumjs-util'); +const Mutex = require('await-semaphore').Mutex; /** * @type Result @@ -143,15 +144,30 @@ export interface TransactionConfig extends BaseConfig { sign?: (transaction: Transaction, from: string) => Promise; } +/** + * @type MethodData + * + * Method data registry object + * + * @property registryMethod - Registry method raw string + * @property parsedRegistryMethod - Registry method object, containing name and method arguments + */ +export interface MethodData { + registryMethod: string; + parsedRegistryMethod: object; +} + /** * @type TransactionState * * Transaction controller state * * @property transactions - A list of TransactionMeta objects + * @property methodData - Object containing all known method data information */ export interface TransactionState extends BaseState { transactions: TransactionMeta[]; + methodData: { [key: string]: MethodData }; } /** @@ -159,7 +175,9 @@ export interface TransactionState extends BaseState { */ export class TransactionController extends BaseController { private ethQuery: any; + private registry: any; private handle?: NodeJS.Timer; + private mutex = new Mutex(); private failTransaction(transactionMeta: TransactionMeta, error: Error) { transactionMeta.status = 'failed'; @@ -183,6 +201,12 @@ export class TransactionController extends BaseController { + const registryMethod = await this.registry.lookup(fourBytePrefix); + const parsedRegistryMethod = this.registry.parse(registryMethod); + return { registryMethod, parsedRegistryMethod }; + } + /** * Normalizes the transaction information from etherscan * to be compatible with the TransactionMeta interface @@ -246,7 +270,10 @@ export class TransactionController extends BaseController { + const releaseLock = await this.mutex.acquire(); + const { methodData } = this.state; + const knownMethod = Object.keys(methodData).find( + (knownFourBytePrefix) => fourBytePrefix === knownFourBytePrefix + ); + if (knownMethod) { + releaseLock(); + return methodData[fourBytePrefix]; + } + const registry = await this.registryLookup(fourBytePrefix); + this.update({ methodData: { ...methodData, ...{ [fourBytePrefix]: registry } } }); + releaseLock(); + return registry; + } + /** * Add a new unapproved transaction to state. Parameters will be validated, a * unique transaction id will be generated, and gas and gasPrice will be calculated @@ -434,6 +483,9 @@ export class TransactionController extends BaseController { this.ethQuery = network.provider ? new EthQuery(network.provider) : /* istanbul ignore next */ null; + this.registry = network.provider + ? new MethodRegistry({ provider: network.provider }) /* istanbul ignore next */ + : null; }; onProviderUpdate(); network.subscribe(onProviderUpdate);