diff --git a/src/app/ui/universal-dapp-ui.js b/src/app/ui/universal-dapp-ui.js index 1e3a70c6a4..e795225bb1 100644 --- a/src/app/ui/universal-dapp-ui.js +++ b/src/app/ui/universal-dapp-ui.js @@ -166,7 +166,6 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address contractName: contractName, contractABI: contractABI } - let calldata = calldataInput.value const amount = document.querySelector('#value').value if (amount !== '0') { // check for numeric and receive/fallback @@ -176,16 +175,17 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address setLLIError("In order to receive Ether transfer the contract should have either 'receive' or payable 'fallback' function") } } + let calldata = calldataInput.value if (calldata) { - if (calldata.length < 2) setLLIError('the calldata should be a valid hexadecimal value with size of at least one byte.') - if (calldata.length < 4 && calldata.substr(0, 2) === '0x') { - setLLIError('The calldata should be a valid hexadecimal value with size of at least one byte.') - } else if (calldata.substr(0, 2) === '0x') { - if (!helper.isHexadecimal(calldata.substr(2, calldata.length))) { - setLLIError('The calldata should be a valid hexadecimal value.') + if (calldata.length < 2 || calldata.length < 4 && helper.is0XPrefixed(calldata)) { + setLLIError('the calldata should be a valid hexadecimal value with size of at least one byte.') + } else { + if (helper.is0XPrefixed(calldata)) { + calldata = calldata.substr(2, calldata.length) + } + if (!helper.isHexadecimal(calldata)) { + setLLIError('the calldata should be a valid hexadecimal value with size of at least one byte.') } - } else if (!helper.isHexadecimal(calldata)) { - setLLIError('The calldata should be a valid hexadecimal value.') } if (!fallback) { setLLIError("'Fallback' function is not defined") @@ -200,9 +200,9 @@ UniversalDAppUI.prototype.renderInstanceFromABI = function (contractABI, address if (receive && !calldata) args.funABI = receive else if (fallback) args.funABI = fallback - if (!args.funABI) setLLIError(`Please define at least a 'Fallback' with/without sending calldata or a 'Receive' without sending calldata`) + if (!args.funABI) setLLIError(`Please define a 'Fallback' function to send calldata and a either 'Receive' or payable 'Fallback' to send ethers`) - if (!error) self.runTransaction(false, args, null, calldata, null) + if (!error) self.runTransaction(false, args, null, calldataInput.value, null) } contractActionsWrapper.appendChild(lowLevelInteracions) diff --git a/src/lib/helper.js b/src/lib/helper.js index 93f688e4f9..cda3bfe519 100644 --- a/src/lib/helper.js +++ b/src/lib/helper.js @@ -45,7 +45,10 @@ module.exports = { return name.match(/[:*?"<>\\'|]/) != null }, isHexadecimal (value) { - return /^[0-9a-fA-F]+$/.test(value) + return /^[0-9a-fA-F]+$/.test(value) && (value.length % 2 === 0) + }, + is0XPrefixed (value) { + return value.substr(0, 2) === '0x' }, isNumeric (value) { return /^\+?(0|[1-9]\d*)$/.test(value) diff --git a/test-browser/tests/specialFunctions.js b/test-browser/tests/specialFunctions.js index 0ef58c1bab..fa773dcd85 100644 --- a/test-browser/tests/specialFunctions.js +++ b/test-browser/tests/specialFunctions.js @@ -34,7 +34,7 @@ module.exports = { browser.sendLowLevelTx(address, '0', '0xa') .pause(1000) .waitForElementVisible(`#instance${address} label[id="deployAndRunLLTxError"]`) - .assert.containsText(`#instance${address} label[id="deployAndRunLLTxError"]`, `size of at least one byte`) + .assert.containsText(`#instance${address} label[id="deployAndRunLLTxError"]`, `the calldata should be a valid hexadecimal value with size of at least one byte.`) .perform(done) }) }) @@ -164,6 +164,18 @@ module.exports = { }) }) }, + 'Use special functions receive/fallback - receive and fallback are declared and payable, sending wei': function (browser) { + browser.perform((done) => { + browser.getAddressAtPosition(4, (address) => { + browser.sendLowLevelTx(address, '1', '') + .pause(1000) + .journalLastChildIncludes('to:CheckSpecials.(receive)') + .journalLastChildIncludes('value:1 wei') + .journalLastChildIncludes('data:0x') + .perform(done) + }) + }) + }, 'Use special functions receive/fallback - receive and fallback are not declared, sending nothing': function (browser) { browser.waitForElementVisible('#icon-panel', 10000) .testContracts('notSpecial.sol', sources[5]['browser/notSpecial.sol'], ['CheckSpecials']) @@ -180,7 +192,7 @@ module.exports = { browser.sendLowLevelTx(address, '0', '') .pause(1000) .waitForElementVisible(`#instance${address} label[id="deployAndRunLLTxError"]`) - .assert.containsText(`#instance${address} label[id="deployAndRunLLTxError"]`, `Both 'receive' and 'fallback' functions are not defined`) + .assert.containsText(`#instance${address} label[id="deployAndRunLLTxError"]`, `Please define a 'Fallback' function to send calldata and a either 'Receive' or payable 'Fallback' to send ethers`) .perform(done) }) })