From c6ff0801fb63f78739e6256d03e13f3520ce3dcd Mon Sep 17 00:00:00 2001 From: apanizo Date: Thu, 27 Sep 2018 10:57:44 +0200 Subject: [PATCH] Fix Metamasks' signatures following EIP 712 --- src/logic/safe/safeBlockchainOperations.js | 12 ++++++----- src/logic/safe/safeTxSigner.js | 25 +++++++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/logic/safe/safeBlockchainOperations.js b/src/logic/safe/safeBlockchainOperations.js index d71be6c7..ac083a8e 100644 --- a/src/logic/safe/safeBlockchainOperations.js +++ b/src/logic/safe/safeBlockchainOperations.js @@ -60,19 +60,21 @@ export const executeTransaction = async ( const sigs = getSignaturesFrom(safeAddress, nonce) const threshold = await safe.getThreshold() - const gas = await estimateDataGas(safe, to, valueInWei, data, operation, txGasEstimate, 0, nonce, Number(threshold)) + const gas = + await estimateDataGas(safe, to, valueInWei, data, operation, txGasEstimate, 0, nonce, Number(threshold), 0) const numOwners = await safe.getOwners() const gasIncludingRemovingStoreUpfront = gas + txGasEstimate + (numOwners.length * 15000) - const txReceipt = await safe.execTransactionAndPaySubmitter( + const txReceipt = await safe.execTransaction( to, valueInWei, data, operation, txGasEstimate, - 0, - 0, - 0, + 0, // dataGasEstimate + 0, // gasPrice + 0, // txGasToken + 0, // refundReceiver sigs, { from: sender, gas: gasIncludingRemovingStoreUpfront, gasPrice }, ) diff --git a/src/logic/safe/safeTxSigner.js b/src/logic/safe/safeTxSigner.js index d6b0b92d..7d99efde 100644 --- a/src/logic/safe/safeTxSigner.js +++ b/src/logic/safe/safeTxSigner.js @@ -32,6 +32,7 @@ export const estimateDataGas = ( gasToken: number, nonce: number, signatureCount: number, + refundReceiver: number, ) => { // numbers < 256 are 192 -> 31 * 4 + 68 // numbers < 65k are 256 -> 30 * 4 + 2 * 68 @@ -41,8 +42,8 @@ export const estimateDataGas = ( const signatureCost = signatureCount * (68 + 2176 + 2176) // array count (3 -> r, s, v) * signature count const sigs = getSignaturesFrom(safe.address, nonce) - const payload = safe.contract.execTransactionAndPaySubmitter - .getData(to, valueInWei, data, operation, txGasEstimate, 0, gasPrice, gasToken, sigs) + const payload = safe.contract.execTransaction + .getData(to, valueInWei, data, operation, txGasEstimate, 0, gasPrice, gasToken, refundReceiver, sigs) let dataGasEstimate = estimateDataGasCosts(payload) + signatureCost if (dataGasEstimate > 65536) { @@ -95,6 +96,7 @@ const generateTypedDataFrom = async ( // estimateDataGas(safe, to, valueInWei, data, operation, txGasEstimate, txGasToken, nonce, threshold) const dataGasEstimate = 0 const gasPrice = 0 + const refundReceiver = 0 const typedData = { types: { EIP712Domain: [ @@ -103,7 +105,7 @@ const generateTypedDataFrom = async ( name: 'verifyingContract', }, ], - PersonalSafeTx: [ + SafeTx: [ { type: 'address', name: 'to' }, { type: 'uint256', name: 'value' }, { type: 'bytes', name: 'data' }, @@ -112,23 +114,25 @@ const generateTypedDataFrom = async ( { type: 'uint256', name: 'dataGas' }, { type: 'uint256', name: 'gasPrice' }, { type: 'address', name: 'gasToken' }, + { type: 'address', name: 'refundReceiver' }, { type: 'uint256', name: 'nonce' }, ], }, domain: { verifyingContract: safeAddress, }, - primaryType: 'PersonalSafeTx', + primaryType: 'SafeTx', message: { to, - value: valueInWei, + value: Number(valueInWei), data, operation, safeTxGas: txGasEstimate, dataGas: dataGasEstimate, gasPrice, gasToken: txGasToken, - nonce, + refundReceiver, + nonce: Number(nonce), }, } @@ -149,11 +153,12 @@ export const generateMetamaskSignature = async ( const web3 = getWeb3() const typedData = await generateTypedDataFrom(safe, safeAddress, to, valueInWei, nonce, data, operation, txGasEstimate) + + const jsonTypedData = JSON.stringify(typedData) const signedTypedData = { - jsonrpc: '2.0', - method: 'eth_signTypedData', - params: [sender, typedData], - id: Date.now(), + method: 'eth_signTypedData_v3', + params: [jsonTypedData, sender], + from: sender, } const txSignedResponse = await promisify(cb => web3.currentProvider.sendAsync(signedTypedData, cb))