ERC20Permit.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* eslint-disable */
  2. const { BN, constants, time } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const { MAX_UINT256 } = constants;
  5. const { fromRpcSig } = require('ethereumjs-util');
  6. const ethSigUtil = require('eth-sig-util');
  7. const Wallet = require('ethereumjs-wallet').default;
  8. const ERC20Permit = artifacts.require('$ERC20Permit');
  9. const {
  10. types: { Permit },
  11. getDomain,
  12. domainType,
  13. domainSeparator,
  14. } = require('../../../helpers/eip712');
  15. const { getChainId } = require('../../../helpers/chainid');
  16. const { expectRevertCustomError } = require('../../../helpers/customError');
  17. contract('ERC20Permit', function (accounts) {
  18. const [initialHolder, spender] = accounts;
  19. const name = 'My Token';
  20. const symbol = 'MTKN';
  21. const initialSupply = new BN(100);
  22. beforeEach(async function () {
  23. this.chainId = await getChainId();
  24. this.token = await ERC20Permit.new(name, symbol, name);
  25. await this.token.$_mint(initialHolder, initialSupply);
  26. });
  27. it('initial nonce is 0', async function () {
  28. expect(await this.token.nonces(initialHolder)).to.be.bignumber.equal('0');
  29. });
  30. it('domain separator', async function () {
  31. expect(await this.token.DOMAIN_SEPARATOR()).to.equal(await getDomain(this.token).then(domainSeparator));
  32. });
  33. describe('permit', function () {
  34. const wallet = Wallet.generate();
  35. const owner = wallet.getAddressString();
  36. const value = new BN(42);
  37. const nonce = 0;
  38. const maxDeadline = MAX_UINT256;
  39. const buildData = (contract, deadline = maxDeadline) =>
  40. getDomain(contract).then(domain => ({
  41. primaryType: 'Permit',
  42. types: { EIP712Domain: domainType(domain), Permit },
  43. domain,
  44. message: { owner, spender, value, nonce, deadline },
  45. }));
  46. it('accepts owner signature', async function () {
  47. const { v, r, s } = await buildData(this.token)
  48. .then(data => ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data }))
  49. .then(fromRpcSig);
  50. await this.token.permit(owner, spender, value, maxDeadline, v, r, s);
  51. expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
  52. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(value);
  53. });
  54. it('rejects reused signature', async function () {
  55. const sig = await buildData(this.token).then(data =>
  56. ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data }),
  57. );
  58. const { r, s, v } = fromRpcSig(sig);
  59. await this.token.permit(owner, spender, value, maxDeadline, v, r, s);
  60. const domain = await getDomain(this.token);
  61. const typedMessage = {
  62. primaryType: 'Permit',
  63. types: { EIP712Domain: domainType(domain), Permit },
  64. domain,
  65. message: { owner, spender, value, nonce: nonce + 1, deadline: maxDeadline },
  66. };
  67. await expectRevertCustomError(
  68. this.token.permit(owner, spender, value, maxDeadline, v, r, s),
  69. 'ERC2612InvalidSigner',
  70. [ethSigUtil.recoverTypedSignature({ data: typedMessage, sig }), owner],
  71. );
  72. });
  73. it('rejects other signature', async function () {
  74. const otherWallet = Wallet.generate();
  75. const { v, r, s } = await buildData(this.token)
  76. .then(data => ethSigUtil.signTypedMessage(otherWallet.getPrivateKey(), { data }))
  77. .then(fromRpcSig);
  78. await expectRevertCustomError(
  79. this.token.permit(owner, spender, value, maxDeadline, v, r, s),
  80. 'ERC2612InvalidSigner',
  81. [await otherWallet.getAddressString(), owner],
  82. );
  83. });
  84. it('rejects expired permit', async function () {
  85. const deadline = (await time.latest()) - time.duration.weeks(1);
  86. const { v, r, s } = await buildData(this.token, deadline)
  87. .then(data => ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data }))
  88. .then(fromRpcSig);
  89. await expectRevertCustomError(
  90. this.token.permit(owner, spender, value, deadline, v, r, s),
  91. 'ERC2612ExpiredSignature',
  92. [deadline],
  93. );
  94. });
  95. });
  96. });