Include EIP-5267 discovery in EIP-712 (#3969)

Co-authored-by: Francisco <frangio.1@gmail.com>
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
Hadrien Croubois
2023-02-08 15:53:57 +01:00
committed by GitHub
parent 8177c4620e
commit d625cb45ea
16 changed files with 358 additions and 325 deletions

View File

@ -11,7 +11,7 @@ const Wallet = require('ethereumjs-wallet').default;
const ERC20Votes = artifacts.require('$ERC20Votes');
const { batchInBlock } = require('../../../helpers/txpool');
const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
const { getDomain, domainType, domainSeparator } = require('../../../helpers/eip712');
const { getChainId } = require('../../../helpers/chainid');
const Delegation = [
@ -38,9 +38,7 @@ contract('ERC20Votes', function (accounts) {
});
it('domain separator', async function () {
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(
await domainSeparator({ name, version, chainId: this.chainId, verifyingContract: this.token.address }),
);
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(await getDomain(this.token).then(domainSeparator));
});
it('minting restriction', async function () {
@ -107,30 +105,26 @@ contract('ERC20Votes', function (accounts) {
const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
const nonce = 0;
const buildData = (chainId, verifyingContract, message) => ({
data: {
const buildData = (contract, message) =>
getDomain(contract).then(domain => ({
primaryType: 'Delegation',
types: { EIP712Domain, Delegation },
domain: { name, version, chainId, verifyingContract },
types: { EIP712Domain: domainType(domain), Delegation },
domain,
message,
},
});
}));
beforeEach(async function () {
await this.token.$_mint(delegatorAddress, supply);
});
it('accept signed delegation', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
expect(await this.token.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
@ -155,16 +149,13 @@ contract('ERC20Votes', function (accounts) {
});
it('rejects reused signature', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
await this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
@ -175,16 +166,13 @@ contract('ERC20Votes', function (accounts) {
});
it('rejects bad delegatee', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
const receipt = await this.token.delegateBySig(holderDelegatee, nonce, MAX_UINT256, v, r, s);
const { args } = receipt.logs.find(({ event }) => event == 'DelegateChanged');
@ -194,16 +182,14 @@ contract('ERC20Votes', function (accounts) {
});
it('rejects bad nonce', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
await expectRevert(
this.token.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
'ERC20Votes: invalid nonce',
@ -212,16 +198,14 @@ contract('ERC20Votes', function (accounts) {
it('rejects expired permit', async function () {
const expiry = (await time.latest()) - time.duration.weeks(1);
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
await expectRevert(
this.token.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),

View File

@ -11,7 +11,7 @@ const Wallet = require('ethereumjs-wallet').default;
const ERC20VotesComp = artifacts.require('$ERC20VotesComp');
const { batchInBlock } = require('../../../helpers/txpool');
const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
const { getDomain, domainType, domainSeparator } = require('../../../helpers/eip712');
const { getChainId } = require('../../../helpers/chainid');
const Delegation = [
@ -38,9 +38,7 @@ contract('ERC20VotesComp', function (accounts) {
});
it('domain separator', async function () {
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(
await domainSeparator({ name, version, chainId: this.chainId, verifyingContract: this.token.address }),
);
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(await getDomain(this.token).then(domainSeparator));
});
it('minting restriction', async function () {
@ -94,30 +92,26 @@ contract('ERC20VotesComp', function (accounts) {
const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
const nonce = 0;
const buildData = (chainId, verifyingContract, message) => ({
data: {
const buildData = (contract, message) =>
getDomain(contract).then(domain => ({
primaryType: 'Delegation',
types: { EIP712Domain, Delegation },
domain: { name, version, chainId, verifyingContract },
types: { EIP712Domain: domainType(domain), Delegation },
domain,
message,
},
});
}));
beforeEach(async function () {
await this.token.$_mint(delegatorAddress, supply);
});
it('accept signed delegation', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
expect(await this.token.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
@ -142,16 +136,13 @@ contract('ERC20VotesComp', function (accounts) {
});
it('rejects reused signature', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
await this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
@ -162,16 +153,13 @@ contract('ERC20VotesComp', function (accounts) {
});
it('rejects bad delegatee', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
const receipt = await this.token.delegateBySig(holderDelegatee, nonce, MAX_UINT256, v, r, s);
const { args } = receipt.logs.find(({ event }) => event == 'DelegateChanged');
@ -181,16 +169,14 @@ contract('ERC20VotesComp', function (accounts) {
});
it('rejects bad nonce', async function () {
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
await expectRevert(
this.token.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
'ERC20Votes: invalid nonce',
@ -199,16 +185,14 @@ contract('ERC20VotesComp', function (accounts) {
it('rejects expired permit', async function () {
const expiry = (await time.latest()) - time.duration.weeks(1);
const { v, r, s } = fromRpcSig(
ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.token.address, {
delegatee: delegatorAddress,
nonce,
expiry,
}),
),
);
const { v, r, s } = await buildData(this.token, {
delegatee: delegatorAddress,
nonce,
expiry,
})
.then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }))
.then(fromRpcSig);
await expectRevert(
this.token.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),

View File

@ -10,7 +10,7 @@ const Wallet = require('ethereumjs-wallet').default;
const ERC20Permit = artifacts.require('$ERC20Permit');
const { EIP712Domain, Permit, domainSeparator } = require('../../../helpers/eip712');
const { Permit, getDomain, domainType, domainSeparator } = require('../../../helpers/eip712');
const { getChainId } = require('../../../helpers/chainid');
contract('ERC20Permit', function (accounts) {
@ -34,9 +34,7 @@ contract('ERC20Permit', function (accounts) {
});
it('domain separator', async function () {
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(
await domainSeparator({ name, version, chainId: this.chainId, verifyingContract: this.token.address }),
);
expect(await this.token.DOMAIN_SEPARATOR()).to.equal(await getDomain(this.token).then(domainSeparator));
});
describe('permit', function () {
@ -47,17 +45,18 @@ contract('ERC20Permit', function (accounts) {
const nonce = 0;
const maxDeadline = MAX_UINT256;
const buildData = (chainId, verifyingContract, deadline = maxDeadline) => ({
primaryType: 'Permit',
types: { EIP712Domain, Permit },
domain: { name, version, chainId, verifyingContract },
message: { owner, spender, value, nonce, deadline },
});
const buildData = (contract, deadline = maxDeadline) =>
getDomain(contract).then(domain => ({
primaryType: 'Permit',
types: { EIP712Domain: domainType(domain), Permit },
domain,
message: { owner, spender, value, nonce, deadline },
}));
it('accepts owner signature', async function () {
const data = buildData(this.chainId, this.token.address);
const signature = ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data });
const { v, r, s } = fromRpcSig(signature);
const { v, r, s } = await buildData(this.token)
.then(data => ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data }))
.then(fromRpcSig);
await this.token.permit(owner, spender, value, maxDeadline, v, r, s);
@ -66,9 +65,9 @@ contract('ERC20Permit', function (accounts) {
});
it('rejects reused signature', async function () {
const data = buildData(this.chainId, this.token.address);
const signature = ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data });
const { v, r, s } = fromRpcSig(signature);
const { v, r, s } = await buildData(this.token)
.then(data => ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data }))
.then(fromRpcSig);
await this.token.permit(owner, spender, value, maxDeadline, v, r, s);
@ -80,9 +79,10 @@ contract('ERC20Permit', function (accounts) {
it('rejects other signature', async function () {
const otherWallet = Wallet.generate();
const data = buildData(this.chainId, this.token.address);
const signature = ethSigUtil.signTypedMessage(otherWallet.getPrivateKey(), { data });
const { v, r, s } = fromRpcSig(signature);
const { v, r, s } = await buildData(this.token)
.then(data => ethSigUtil.signTypedMessage(otherWallet.getPrivateKey(), { data }))
.then(fromRpcSig);
await expectRevert(
this.token.permit(owner, spender, value, maxDeadline, v, r, s),
@ -93,9 +93,9 @@ contract('ERC20Permit', function (accounts) {
it('rejects expired permit', async function () {
const deadline = (await time.latest()) - time.duration.weeks(1);
const data = buildData(this.chainId, this.token.address, deadline);
const signature = ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data });
const { v, r, s } = fromRpcSig(signature);
const { v, r, s } = await buildData(this.token, deadline)
.then(data => ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data }))
.then(fromRpcSig);
await expectRevert(this.token.permit(owner, spender, value, deadline, v, r, s), 'ERC20Permit: expired deadline');
});

View File

@ -6,8 +6,7 @@ const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
const ERC20PermitNoRevertMock = artifacts.require('ERC20PermitNoRevertMock');
const { EIP712Domain, Permit } = require('../../../helpers/eip712');
const { getChainId } = require('../../../helpers/chainid');
const { getDomain, domainType, Permit } = require('../../../helpers/eip712');
const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
@ -58,16 +57,15 @@ contract('SafeERC20', function (accounts) {
const spender = hasNoCode;
beforeEach(async function () {
const chainId = await getChainId();
this.token = await ERC20PermitNoRevertMock.new();
this.data = {
this.data = await getDomain(this.token).then(domain => ({
primaryType: 'Permit',
types: { EIP712Domain, Permit },
domain: { name: 'ERC20PermitNoRevertMock', version: '1', chainId, verifyingContract: this.token.address },
types: { EIP712Domain: domainType(domain), Permit },
domain,
message: { owner, spender, value: '42', nonce: '0', deadline: constants.MAX_UINT256 },
};
}));
this.signature = fromRpcSig(ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data: this.data }));
});