From 69e8167499974ee7a72df9f0c87032bfeec4f016 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Sat, 1 Sep 2018 00:45:28 -0400 Subject: [PATCH] Adding logic to obtain signatures --- test-dapp/app/components/callgasrelayed.js | 28 +++++-------- test-dapp/app/status-gas-relayer.js | 46 +++++++++++++++++----- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/test-dapp/app/components/callgasrelayed.js b/test-dapp/app/components/callgasrelayed.js index ee246b2..f508042 100644 --- a/test-dapp/app/components/callgasrelayed.js +++ b/test-dapp/app/components/callgasrelayed.js @@ -70,7 +70,7 @@ class CallGasRelayed extends Component { }); }; - sign = (event) => { + sign = async (event) => { if(event) event.preventDefault(); this.setState({ @@ -78,25 +78,15 @@ class CallGasRelayed extends Component { transactionError: '' }); - IdentityGasRelay.options.address = this.props.identityAddress; - try { - IdentityGasRelay.methods.callGasRelayHash( - this.state.to, - this.state.value, - web3.utils.soliditySha3({t: 'bytes', v: this.state.data}), - this.props.nonce, - this.state.gasPrice, - this.state.gasLimit, - this.state.gasToken - ) - .call() - .then(message => { - return web3.eth.sign(message, web3.eth.defaultAccount); - }) - .then(signature => { - this.setState({signature}); - }); + + const s = new StatusGasRelayer.Identity(this.props.identityAddress, web3.eth.defaultAccount) + .setTransaction(this.state.to, this.state.value, this.state.data) + .setGas(this.state.gasToken, this.state.gasPrice, this.state.gasLimit); + + const signature = await s.sign(web3); + + this.setState({signature}); } catch(error){ this.setState({transactionError: error.message}); } diff --git a/test-dapp/app/status-gas-relayer.js b/test-dapp/app/status-gas-relayer.js index ae82941..7dac953 100644 --- a/test-dapp/app/status-gas-relayer.js +++ b/test-dapp/app/status-gas-relayer.js @@ -8,12 +8,12 @@ export const Functions = { 'call': 'callGasRelayed', 'approveAndCall': 'approveAndCallGasRelayed' } -} +}; export const Actions = { 'Availability': 'availability', 'Transaction': 'transaction' -} +}; const relayerSymmmetricKeyID = "0xd0d905c1c62b810b787141430417caf2b3f54cffadb395b7bb39fdeb8f17266b"; @@ -65,16 +65,23 @@ class StatusGasRelayer { payload: this.web3.utils.toHex(this.message) }; - return await web3.shh.post(sendOptions); + const msgId = await this.web3.shh.post(sendOptions); + return msgId; } validateBuild = (build) => { - return (String(build.constructor) === String(StatusGasRelayer.AvailableRelayers)); + return (String(build.constructor) === String(StatusGasRelayer.AvailableRelayers) || + String(build.constructor) === String(StatusGasRelayer.Identity) + ); } static get AvailableRelayers() { return AvailableRelayersAction; } + + static get Identity() { + return IdentityGasRelayedAction; + } } class Action { @@ -120,14 +127,35 @@ class IdentityGasRelayedAction extends Action { return this; } - getNonce = async (web3) => { - const contract = web3.eth.contract(identityGasRelayABI, this.contractAddress); + _nonce = async(contract) => { const nonce = await contract.methods.nonce().call(); return nonce; } - sign = (web3) => { + getNonce = async (web3) => { + const contract = new web3.eth.Contract(identityGasRelayABI, this.contractAddress); + const nonce = await this._nonce(contract); + return nonce; + } + sign = async (web3) => { + const contract = new web3.eth.Contract(identityGasRelayABI, this.contractAddress); + const nonce = await this._nonce(contract); + + // TODO: this depends of the operation to execute + const hashedMessage = await contract.methods.callGasRelayHash( + this.to, + this.value, + web3.utils.soliditySha3({t: 'bytes', v: this.data}), + nonce, + this.gasPrice, + this.gasLimit, + this.gasToken + ).call(); + + const signature = await web3.eth.sign(hashedMessage, this.accountAddress); + + return signature; } _getMessage = async web3 => { @@ -166,8 +194,6 @@ class AvailableRelayersAction extends Action { } } - - const identityGasRelayABI = [ { "constant": true, @@ -230,4 +256,4 @@ const identityGasRelayABI = [ } ]; -export default StatusGasRelayer; \ No newline at end of file +export default StatusGasRelayer;