From 02c51c83cd5d6b1802cd1b276f9a184ef8ca41be Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Wed, 8 Mar 2017 13:21:39 +0100 Subject: [PATCH] Better logic for contract deployments (#4821) --- .../ui/MethodDecoding/methodDecodingStore.js | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/js/src/ui/MethodDecoding/methodDecodingStore.js b/js/src/ui/MethodDecoding/methodDecodingStore.js index da23f916c..05a8f8546 100644 --- a/js/src/ui/MethodDecoding/methodDecodingStore.js +++ b/js/src/ui/MethodDecoding/methodDecodingStore.js @@ -148,7 +148,19 @@ export default class MethodDecodingStore { // Contract deployment if (!signature || signature === CONTRACT_CREATE || transaction.creates) { - return this.decodeContractCreation(result, contractAddress || transaction.creates); + const address = contractAddress || transaction.creates; + + return this.isContractCreation(input, address) + .then((isContractCreation) => { + if (!isContractCreation) { + result.contract = false; + result.deploy = false; + + return result; + } + + return this.decodeContractCreation(result, address); + }); } return this @@ -204,7 +216,7 @@ export default class MethodDecodingStore { const { input } = data; const abi = this._contractsAbi[contractAddress]; - if (!input || !abi || !abi.constructors || abi.constructors.length === 0) { + if (!abi || !abi.constructors || abi.constructors.length === 0) { return Promise.resolve(result); } @@ -306,6 +318,30 @@ export default class MethodDecodingStore { return Promise.resolve(this._isContract[contractAddress]); } + /** + * Check if the input resulted in a contract creation + * by checking that the contract address code contains + * a part of the input, or vice-versa + */ + isContractCreation (input, contractAddress) { + return this.api.eth + .getCode(contractAddress) + .then((code) => { + if (/^(0x)?0*$/.test(code)) { + return false; + } + + const strippedCode = code.replace(/^0x/, ''); + const strippedInput = input.replace(/^0x/, ''); + + return strippedInput.indexOf(strippedInput) >= 0 || strippedCode.indexOf(strippedInput) >= 0; + }) + .catch((error) => { + console.error(error); + return false; + }); + } + getCode (contractAddress) { // If zero address, resolve to '0x' if (!contractAddress || /^(0x)?0*$/.test(contractAddress)) {