From 7f92912d82c1790d43e2eba2bb229fbde61c707a Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Tue, 26 Mar 2019 16:27:24 -0300 Subject: [PATCH 1/7] add issmartcontract code --- src/util.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/util.ts b/src/util.ts index 46d9c33a5cb..1472c98d3db 100644 --- a/src/util.ts +++ b/src/util.ts @@ -251,6 +251,20 @@ export function validateTypedSignMessageDataV3(messageData: TypedMessageParams, } } +/** + * Returns wether the given code corresponds to a smart contract + * + * @returns {string} - Corresponding code to review + */ +export function isSmartContractCode(code: string) { + if (!code) { + return false; + } + // Geth will return '0x', and ganache-core v2.2.1 will return '0x0' + const smartContractCode = code !== '0x' && code !== '0x0'; + return smartContractCode; +} + export default { BNToHex, fractionBN, @@ -258,6 +272,7 @@ export default { handleFetch, hexToBN, hexToText, + isSmartContractCode, normalizeTransaction, safelyExecute, validateTransaction, From 45b9c923cc739122da19fc09e3bbc7d15355b42d Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Tue, 26 Mar 2019 16:27:46 -0300 Subject: [PATCH 2/7] check if recipient is smart contract when fetching tx --- src/TransactionController.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/TransactionController.ts b/src/TransactionController.ts index 8a50369c9d1..339a13d2c64 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -1,7 +1,15 @@ import { EventEmitter } from 'events'; import BaseController, { BaseConfig, BaseState } from './BaseController'; import NetworkController from './NetworkController'; -import { BNToHex, fractionBN, hexToBN, normalizeTransaction, safelyExecute, validateTransaction } from './util'; +import { + BNToHex, + fractionBN, + hexToBN, + normalizeTransaction, + safelyExecute, + validateTransaction, + isSmartContractCode +} from './util'; const EthQuery = require('eth-query'); const Transaction = require('ethereumjs-tx'); @@ -55,7 +63,8 @@ export interface Transaction { * @property origin - Origin this transaction was sent from * @property rawTransaction - Hex representation of the underlying transaction * @property status - String status of this transaction - * @property time - Timestamp associated with this transaction + * @property toSmartContract - Whether transaction receipt is a smart contract + * @property status - String status of this transaction * @property transaction - Underlying Transaction object * @property transactionHash - Hash of a successful transaction * @property blockNumber - Number of the block where the transaction has been included @@ -71,6 +80,7 @@ export interface TransactionMeta { rawTransaction?: string; status: string; time: number; + toSmartContract?: boolean; transaction: Transaction; transactionHash?: string; blockNumber?: string; @@ -552,7 +562,7 @@ export class TransactionController extends BaseController (/* istanbul ignore next */ a.time < b.time ? -1 : 1)); let latestIncomingTxBlockNumber: string | undefined; - allTxs.forEach((tx) => { + allTxs.forEach(async (tx) => { /* istanbul ignore next */ if ( tx.networkID === currentNetworkID && @@ -567,6 +577,12 @@ export class TransactionController extends BaseController Date: Tue, 26 Mar 2019 17:00:32 -0300 Subject: [PATCH 3/7] add tests for changes --- src/TransactionController.test.ts | 21 +++++++++++++++++++-- src/TransactionController.ts | 13 ++++++++----- src/util.test.ts | 11 +++++++++++ src/util.ts | 1 + 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/TransactionController.test.ts b/src/TransactionController.test.ts index 22bd54decd0..24d46ee8894 100644 --- a/src/TransactionController.test.ts +++ b/src/TransactionController.test.ts @@ -96,6 +96,24 @@ const MOCK_FETCH_TX_HISTORY_DATA_OK = { transactionIndex: '12', txreceipt_status: '1', value: '50000000000000000' + }, + { + blockNumber: '4535105', + confirmations: '4', + contractAddress: '', + cumulativeGasUsed: '693910', + from: '0x6bf137f335ea1b8f193b8f6ea92561a60d23a207', + gas: '335208', + gasPrice: '20000000000', + gasUsed: '21000', + hash: '0x342e9d73e10004af41d04973339fc7219dbadcbb5629730cfe65e9f9cb15ff91', + input: '0x', + isError: '0', + nonce: '1', + timeStamp: '1543596356', + transactionIndex: '13', + txreceipt_status: '1', + value: '50000000000000000' } ], status: '1' @@ -373,7 +391,7 @@ describe('TransactionController', () => { const from = '0x6bf137f335ea1b8f193b8f6ea92561a60d23a207'; const latestBlock = await controller.fetchAll(from); - expect(controller.state.transactions.length).toBe(2); + expect(controller.state.transactions.length).toBe(3); expect(latestBlock).toBe('4535101'); expect(controller.state.transactions[0].transaction.to).toBe(from); }); @@ -387,7 +405,6 @@ describe('TransactionController', () => { } as any; controller.onComposed(); expect(controller.state.transactions.length).toBe(0); - const from = '0x6bf137f335ea1b8f193b8f6ea92561a60d23a207'; const result = await controller.fetchAll(from); expect(controller.state.transactions.length).toBe(0); diff --git a/src/TransactionController.ts b/src/TransactionController.ts index 339a13d2c64..6c8adfc064d 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -577,11 +577,14 @@ export class TransactionController extends BaseController { ) ).not.toThrow(); }); + + it('should not throw if data is correct', () => { + const toSmartContract1 = util.isSmartContractCode(''); + const toSmartContract2 = util.isSmartContractCode('0x'); + const toSmartContract3 = util.isSmartContractCode('0x0'); + const toSmartContract4 = util.isSmartContractCode('0x01234'); + expect(toSmartContract1).toBe(false); + expect(toSmartContract2).toBe(false); + expect(toSmartContract3).toBe(false); + expect(toSmartContract4).toBe(true); + }); }); }); diff --git a/src/util.ts b/src/util.ts index 1472c98d3db..3c53cb5bbba 100644 --- a/src/util.ts +++ b/src/util.ts @@ -257,6 +257,7 @@ export function validateTypedSignMessageDataV3(messageData: TypedMessageParams, * @returns {string} - Corresponding code to review */ export function isSmartContractCode(code: string) { + /* istanbul ignore if */ if (!code) { return false; } From 31c547c08cd85b2945de79aa1e39790e4026d800 Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Tue, 26 Mar 2019 17:10:05 -0300 Subject: [PATCH 4/7] remove duplicated doc --- src/TransactionController.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TransactionController.ts b/src/TransactionController.ts index 6c8adfc064d..1b45ad3cbec 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -64,7 +64,6 @@ export interface Transaction { * @property rawTransaction - Hex representation of the underlying transaction * @property status - String status of this transaction * @property toSmartContract - Whether transaction receipt is a smart contract - * @property status - String status of this transaction * @property transaction - Underlying Transaction object * @property transactionHash - Hash of a successful transaction * @property blockNumber - Number of the block where the transaction has been included From 572da9a232fb98f86cad22e9063e9284a0c54dac Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Tue, 26 Mar 2019 22:23:16 -0300 Subject: [PATCH 5/7] dont request code if empty data --- src/TransactionController.test.ts | 2 +- src/TransactionController.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/TransactionController.test.ts b/src/TransactionController.test.ts index 24d46ee8894..d538145318a 100644 --- a/src/TransactionController.test.ts +++ b/src/TransactionController.test.ts @@ -107,7 +107,7 @@ const MOCK_FETCH_TX_HISTORY_DATA_OK = { gasPrice: '20000000000', gasUsed: '21000', hash: '0x342e9d73e10004af41d04973339fc7219dbadcbb5629730cfe65e9f9cb15ff91', - input: '0x', + input: '0x1', isError: '0', nonce: '1', timeStamp: '1543596356', diff --git a/src/TransactionController.ts b/src/TransactionController.ts index 1b45ad3cbec..532c99d9c8c 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -578,7 +578,8 @@ export class TransactionController extends BaseController Date: Tue, 26 Mar 2019 22:25:27 -0300 Subject: [PATCH 6/7] fix typo --- src/TransactionController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TransactionController.ts b/src/TransactionController.ts index 532c99d9c8c..d1831d38f7e 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -63,7 +63,7 @@ export interface Transaction { * @property origin - Origin this transaction was sent from * @property rawTransaction - Hex representation of the underlying transaction * @property status - String status of this transaction - * @property toSmartContract - Whether transaction receipt is a smart contract + * @property toSmartContract - Whether transaction recipient is a smart contract * @property transaction - Underlying Transaction object * @property transactionHash - Hash of a successful transaction * @property blockNumber - Number of the block where the transaction has been included From ad2449013dc7819344b3e59a54aab041442a9960 Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Wed, 27 Mar 2019 12:25:31 -0300 Subject: [PATCH 7/7] fix condition --- src/TransactionController.test.ts | 2 +- src/TransactionController.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TransactionController.test.ts b/src/TransactionController.test.ts index d538145318a..24d46ee8894 100644 --- a/src/TransactionController.test.ts +++ b/src/TransactionController.test.ts @@ -107,7 +107,7 @@ const MOCK_FETCH_TX_HISTORY_DATA_OK = { gasPrice: '20000000000', gasUsed: '21000', hash: '0x342e9d73e10004af41d04973339fc7219dbadcbb5629730cfe65e9f9cb15ff91', - input: '0x1', + input: '0x', isError: '0', nonce: '1', timeStamp: '1543596356', diff --git a/src/TransactionController.ts b/src/TransactionController.ts index d1831d38f7e..56806cfc030 100644 --- a/src/TransactionController.ts +++ b/src/TransactionController.ts @@ -579,7 +579,7 @@ export class TransactionController extends BaseController