Skip to content

Commit

Permalink
Add nonce to delegated batches (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislaw-glogowski authored Nov 9, 2020
1 parent 2019bb7 commit 55ba306
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 68 deletions.
73 changes: 63 additions & 10 deletions build/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,31 @@ module.exports = {
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": false,
"internalType": "bytes",
"name": "batch",
"type": "bytes"
},
{
"indexed": false,
"internalType": "bool",
"name": "succeeded",
"type": "bool"
}
],
"name": "BatchDelegated",
"type": "event"
},
{
"anonymous": false,
"inputs": [],
Expand Down Expand Up @@ -1402,6 +1427,11 @@ module.exports = {
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "nonce",
"type": "uint256"
},
{
"internalType": "address[]",
"name": "to",
Expand All @@ -1418,7 +1448,7 @@ module.exports = {
"type": "bytes"
}
],
"name": "delegateBatchFromAccount",
"name": "delegateBatch",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand All @@ -1430,6 +1460,11 @@ module.exports = {
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "nonce",
"type": "uint256"
},
{
"internalType": "address[]",
"name": "to",
Expand All @@ -1446,7 +1481,25 @@ module.exports = {
"type": "bytes"
}
],
"name": "delegateBatchWithoutGasPriceFromAccount",
"name": "delegateBatchWithGasPrice",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes[]",
"name": "batches",
"type": "bytes[]"
},
{
"internalType": "bool",
"name": "revertOnFailure",
"type": "bool"
}
],
"name": "delegateBatches",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
Expand All @@ -1469,11 +1522,6 @@ module.exports = {
"internalType": "bytes[]",
"name": "data",
"type": "bytes[]"
},
{
"internalType": "uint256",
"name": "gasPrice",
"type": "uint256"
}
],
"internalType": "struct Gateway.DelegatedBatch",
Expand Down Expand Up @@ -1511,14 +1559,19 @@ module.exports = {
"internalType": "bytes[]",
"name": "data",
"type": "bytes[]"
},
{
"internalType": "uint256",
"name": "gasPrice",
"type": "uint256"
}
],
"internalType": "struct Gateway.DelegatedBatchWithoutGasPrice",
"internalType": "struct Gateway.DelegatedBatchWithGasPrice",
"name": "delegatedBatch",
"type": "tuple"
}
],
"name": "hashDelegatedBatchWithoutGasPrice",
"name": "hashDelegatedBatchWithGasPrice",
"outputs": [
{
"internalType": "bytes32",
Expand Down Expand Up @@ -1551,7 +1604,7 @@ module.exports = {
"constant": true
}
],
"byteCodeHash": "0x47d2ba3882eb32577f973869e4cb02fb2331e1dfc7405a081d5453b5fd8c4305",
"byteCodeHash": "0x2a73271e1c51f75479b1071321e1d0fc93189989c1258f77bbe0a0cbe0f27d36",
"typedDataDomainName": "Pillar Gateway",
"typedDataDomainVersion": "1",
"addresses": {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etherspot/contracts",
"version": "1.0.0-alpha.30",
"version": "1.0.0-alpha.31",
"description": "ETHERspot contracts",
"license": "MIT",
"main": "./build/index.js",
Expand Down
52 changes: 31 additions & 21 deletions src/gateway/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ contract Gateway is Initializable, TypedDataContainer {
uint256 nonce;
address[] to;
bytes[] data;
uint256 gasPrice;
}

struct DelegatedBatchWithoutGasPrice {
struct DelegatedBatchWithGasPrice {
uint256 nonce;
address[] to;
bytes[] data;
uint256 gasPrice;
}

bytes32 private constant DELEGATED_BATCH_TYPE_HASH = keccak256(
"DelegatedBatch(uint256 nonce,address[] to,bytes[] data,uint256 gasPrice)"
"DelegatedBatch(uint256 nonce,address[] to,bytes[] data)"
);

bytes32 private constant DELEGATED_BATCH_TYPE_HASH_WITHOUT_GAS_PRICE = keccak256(
"DelegatedBatchWithoutGasPrice(uint256 nonce,address[] to,bytes[] data)"
bytes32 private constant DELEGATED_BATCH_TYPE_HASH_WITH_GAS_PRICE = keccak256(
"DelegatedBatchWithGasPrice(uint256 nonce,address[] to,bytes[] data,uint256 gasPrice)"
);

AccountOwnerRegistry public accountOwnerRegistry;
Expand Down Expand Up @@ -109,24 +109,28 @@ contract Gateway is Initializable, TypedDataContainer {
);
}

function delegateBatchFromAccount(
function delegateBatch(
address account,
uint256 nonce,
address[] memory to,
bytes[] memory data,
bytes memory senderSignature
)
public
{
require(
nonce > accountNonce[account]
);

address sender = _hashPrimaryTypedData(
_hashTypedData(
accountNonce[account],
nonce,
to,
data,
tx.gasprice
data
)
).recoverAddress(senderSignature);

accountNonce[account] = accountNonce[account].add(1);
accountNonce[account] = nonce;

_sendBatch(
account,
Expand All @@ -136,23 +140,29 @@ contract Gateway is Initializable, TypedDataContainer {
);
}

function delegateBatchWithoutGasPriceFromAccount(
function delegateBatchWithGasPrice(
address account,
uint256 nonce,
address[] memory to,
bytes[] memory data,
bytes memory senderSignature
)
public
{
require(
nonce > accountNonce[account]
);

address sender = _hashPrimaryTypedData(
_hashTypedData(
accountNonce[account],
nonce,
to,
data
data,
tx.gasprice
)
).recoverAddress(senderSignature);

accountNonce[account] = accountNonce[account].add(1);
accountNonce[account] = nonce;

_sendBatch(
account,
Expand Down Expand Up @@ -203,14 +213,13 @@ contract Gateway is Initializable, TypedDataContainer {
_hashTypedData(
delegatedBatch.nonce,
delegatedBatch.to,
delegatedBatch.data,
delegatedBatch.gasPrice
delegatedBatch.data
)
);
}

function hashDelegatedBatchWithoutGasPrice(
DelegatedBatchWithoutGasPrice memory delegatedBatch
function hashDelegatedBatchWithGasPrice(
DelegatedBatchWithGasPrice memory delegatedBatch
)
public
view
Expand All @@ -220,7 +229,8 @@ contract Gateway is Initializable, TypedDataContainer {
_hashTypedData(
delegatedBatch.nonce,
delegatedBatch.to,
delegatedBatch.data
delegatedBatch.data,
delegatedBatch.gasPrice
)
);
}
Expand Down Expand Up @@ -299,7 +309,7 @@ contract Gateway is Initializable, TypedDataContainer {
}

return keccak256(abi.encode(
DELEGATED_BATCH_TYPE_HASH_WITHOUT_GAS_PRICE,
DELEGATED_BATCH_TYPE_HASH,
nonce,
keccak256(abi.encodePacked(to)),
keccak256(abi.encodePacked(dataHashes))
Expand All @@ -323,7 +333,7 @@ contract Gateway is Initializable, TypedDataContainer {
}

return keccak256(abi.encode(
DELEGATED_BATCH_TYPE_HASH,
DELEGATED_BATCH_TYPE_HASH_WITH_GAS_PRICE,
nonce,
keccak256(abi.encodePacked(to)),
keccak256(abi.encodePacked(dataHashes)),
Expand Down
Loading

0 comments on commit 55ba306

Please sign in to comment.