From 3d5739715b22521f1416726be307698ab8d1c42c Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 15 Aug 2018 14:11:49 -0400 Subject: [PATCH] Payload requires 'contract' and 'address' --- README.md | 5 +++-- gas-relayer/src/message-processor.js | 21 ++++++++++++------- gas-relayer/src/strategy/IdentityStrategy.js | 6 +++--- gas-relayer/src/strategy/SNTStrategy.js | 4 ++-- .../components/approveandcallgasrelayed.js | 5 +++-- test-dapp/app/components/callgasrelayed.js | 5 +++-- test-dapp/app/components/execute.js | 4 ++-- test-dapp/app/components/transfersnt.js | 4 ++-- 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6491e92..77c7309 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,9 @@ const msgObj = { powTime: 20, topic: "0x4964656e", payload: web3.utils.toHex({ - 'address': "0x692a70d2e424a56d2c6c27aa97d1a86395877b3a", - 'encodedFunctionCall': funCall + 'contract': "0x692a70d2e424a56d2c6c27aa97d1a86395877b3a", + 'encodedFunctionCall': funCall, + 'address': web3.eth.defaultAccount }) }; diff --git a/gas-relayer/src/message-processor.js b/gas-relayer/src/message-processor.js index 53abe83..1d19c6b 100644 --- a/gas-relayer/src/message-processor.js +++ b/gas-relayer/src/message-processor.js @@ -23,7 +23,7 @@ class MessageProcessor { } async _validateInput(message){ - console.info("Processing request to: %s, %s", message.input.address, message.input.functionName); + console.info("Processing request to: %s, %s", message.input.contract, message.input.functionName); const contract = this.settings.getContractByTopic(message.topic); @@ -37,24 +37,31 @@ class MessageProcessor { return false; } - // Get code from address and compare it against the contract code + // Get code from contract and compare it against the contract code if(!contract.isIdentity){ - const code = this.web3.utils.soliditySha3(await this.web3.eth.getCode(message.input.address)); + const code = this.web3.utils.soliditySha3(await this.web3.eth.getCode(message.input.contract)); if(code != contract.code){ this._reply('Invalid contract code', message); return false; } } else { - if(!(/^0x[0-9a-f]{40}$/i).test(message.input.address)){ - this._reply('Invalid address', message); + if(!(/^0x[0-9a-f]{40}$/i).test(message.input.contract)){ + this._reply('Invalid contract address', message); return false; } } + + if(message.input.address && !(/^0x[0-9a-f]{40}$/i).test(message.input.address)){ + this._reply('Invalid address', message); + return false; + } + return true; } _extractInput(message){ let obj = { + contract: null, address: null, functionName: null, functionParameters: null, @@ -64,8 +71,8 @@ class MessageProcessor { try { const msg = this.web3.utils.toAscii(message.payload); let parsedObj = JSON.parse(msg); + obj.contract = parsedObj.contract; obj.address = parsedObj.address; - obj.wallet = parsedObj.wallet; obj.functionName = parsedObj.encodedFunctionCall.slice(0, 10); obj.functionParameters = "0x" + parsedObj.encodedFunctionCall.slice(10); obj.payload = parsedObj.encodedFunctionCall; @@ -103,7 +110,7 @@ class MessageProcessor { let p = { from: this.config.node.blockchain.account, - to: message.input.address, + to: message.input.contract, value: 0, data: message.input.payload, gasPrice: this.config.gasPrice diff --git a/gas-relayer/src/strategy/IdentityStrategy.js b/gas-relayer/src/strategy/IdentityStrategy.js index 749079e..c9c7b6d 100644 --- a/gas-relayer/src/strategy/IdentityStrategy.js +++ b/gas-relayer/src/strategy/IdentityStrategy.js @@ -4,7 +4,7 @@ const erc20ABI = require('../../abi/ERC20Token.json'); class IdentityStrategy extends Strategy { async _validateInstance(message){ - const instanceCodeHash = this.web3.utils.soliditySha3(await this.web3.eth.getCode(message.input.address)); + const instanceCodeHash = this.web3.utils.soliditySha3(await this.web3.eth.getCode(message.input.contract)); const kernelVerifSignature = this.web3.utils.soliditySha3(this.contract.kernelVerification).slice(0, 10); if(instanceCodeHash === null) return false; @@ -35,14 +35,14 @@ class IdentityStrategy extends Strategy { if(this.contract.allowedFunctions[message.input.functionName].isToken){ const Token = new this.web3.eth.Contract(erc20ABI.abi); Token.options.address = params('_baseToken'); - const tokenBalance = new this.web3.utils.BN(await Token.methods.balanceOf(message.input.address).call()); + const tokenBalance = new this.web3.utils.BN(await Token.methods.balanceOf(message.input.contract).call()); if(tokenBalance.lt(this.web3.utils.toBN(params('_value')))){ return {success: false, message: "Identity has not enough balance for specified value"}; } } // gasPrice * limit calculation - const balance = await this.getBalance(message.input.address, token); + const balance = await this.getBalance(message.input.contract, token); if(balance.lt(this.web3.utils.toBN(gasPrice.mul(gasLimit)))) { return {success: false, message: "Identity has not enough tokens for gasPrice*gasLimit"}; } diff --git a/gas-relayer/src/strategy/SNTStrategy.js b/gas-relayer/src/strategy/SNTStrategy.js index d7345ad..53c945c 100644 --- a/gas-relayer/src/strategy/SNTStrategy.js +++ b/gas-relayer/src/strategy/SNTStrategy.js @@ -13,13 +13,13 @@ class SNTStrategy extends Strategy { const token = this.settings.getTokenBySymbol("SNT"); if(token == undefined) return {success: false, message: "Token not allowed"}; - const balance = await this.getBalance(message.input.wallet, token); + const balance = await this.getBalance(message.input.address, token); if(message.input.functionName == TransferSNT){ const estimatedGas = await this.web3.eth.estimateGas({ data: message.input.payload, from: this.config.node.blockchain.account, - to: message.input.address + to: message.input.contract }); const gas = this.web3.utils.toBN(estimatedGas); diff --git a/test-dapp/app/components/approveandcallgasrelayed.js b/test-dapp/app/components/approveandcallgasrelayed.js index 1ef3a9c..0ef01f2 100644 --- a/test-dapp/app/components/approveandcallgasrelayed.js +++ b/test-dapp/app/components/approveandcallgasrelayed.js @@ -126,8 +126,9 @@ class ApproveAndCallGasRelayed extends Component { topic: this.state.topic, symKeyID: skid, payload: web3.utils.toHex({ - 'address': this.props.identityAddress, - 'encodedFunctionCall': funCall + 'contract': this.props.identityAddress, + 'encodedFunctionCall': funCall, + 'address': web3.eth.defaultAccount }) }; diff --git a/test-dapp/app/components/callgasrelayed.js b/test-dapp/app/components/callgasrelayed.js index fd51d32..efd11ba 100644 --- a/test-dapp/app/components/callgasrelayed.js +++ b/test-dapp/app/components/callgasrelayed.js @@ -128,8 +128,9 @@ class CallGasRelayed extends Component { topic: this.state.topic, symKeyID: skid, payload: web3.utils.toHex({ - 'address': this.props.identityAddress, - 'encodedFunctionCall': funCall + 'contract': this.props.identityAddress, + 'encodedFunctionCall': funCall, + 'address': web3.eth.defaultAccount }) }; diff --git a/test-dapp/app/components/execute.js b/test-dapp/app/components/execute.js index 432dc7f..87ca0b5 100644 --- a/test-dapp/app/components/execute.js +++ b/test-dapp/app/components/execute.js @@ -131,8 +131,8 @@ class Execute extends Component { topic: this.state.topic, symKeyID: skid, payload: web3.utils.toHex({ - 'address': SNTController.options.address, - 'wallet': accounts[2], + 'contract': SNTController.options.address, + 'address': accounts[2], 'encodedFunctionCall': funCall }) }; diff --git a/test-dapp/app/components/transfersnt.js b/test-dapp/app/components/transfersnt.js index a32b5a4..9c91aa1 100644 --- a/test-dapp/app/components/transfersnt.js +++ b/test-dapp/app/components/transfersnt.js @@ -128,8 +128,8 @@ class TransferSNT extends Component { topic: this.state.topic, symKeyID: skid, payload: web3.utils.toHex({ - 'address': SNTController.options.address, - 'wallet': accounts[2], + 'contract': SNTController.options.address, + 'address': accounts[2], 'encodedFunctionCall': funCall }) };