forked from OpenZeppelin/openzeppelin-contracts
/
draft-ERC20Permit.test.js
109 lines (84 loc) · 3.87 KB
/
draft-ERC20Permit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable */
const { BN, constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const { MAX_UINT256, ZERO_ADDRESS, ZERO_BYTES32 } = constants;
const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
const ERC20PermitMock = artifacts.require('ERC20PermitMock');
const { EIP712Domain, Permit, domainSeparator } = require('../../../helpers/eip712');
contract('ERC20Permit', function (accounts) {
const [ initialHolder, spender, recipient, other ] = accounts;
const name = 'My Token';
const symbol = 'MTKN';
const version = '1';
const initialSupply = new BN(100);
beforeEach(async function () {
this.token = await ERC20PermitMock.new(name, symbol, initialHolder, initialSupply);
// We get the chain id from the contract because Ganache (used for coverage) does not return the same chain id
// from within the EVM as from the JSON RPC interface.
// See https://github.com/trufflesuite/ganache-core/issues/515
this.chainId = await this.token.getChainId();
});
it('initial nonce is 0', async function () {
expect(await this.token.nonces(initialHolder)).to.be.bignumber.equal('0');
});
it('domain separator', async function () {
expect(
await this.token.DOMAIN_SEPARATOR(),
).to.equal(
await domainSeparator(name, version, this.chainId, this.token.address),
);
});
describe('permit', function () {
const wallet = Wallet.generate();
const owner = wallet.getAddressString();
const value = new BN(42);
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 },
});
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 receipt = await this.token.permit(owner, spender, value, maxDeadline, v, r, s);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(value);
});
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);
await this.token.permit(owner, spender, value, maxDeadline, v, r, s);
await expectRevert(
this.token.permit(owner, spender, value, maxDeadline, v, r, s),
'ERC20Permit: invalid signature',
);
});
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);
await expectRevert(
this.token.permit(owner, spender, value, maxDeadline, v, r, s),
'ERC20Permit: invalid signature',
);
});
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);
await expectRevert(
this.token.permit(owner, spender, value, deadline, v, r, s),
'ERC20Permit: expired deadline',
);
});
});
});