MinimalForwarder.test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. const ethSigUtil = require('eth-sig-util');
  2. const Wallet = require('ethereumjs-wallet').default;
  3. const { EIP712Domain } = require('../helpers/eip712');
  4. const { expectRevert, constants } = require('@openzeppelin/test-helpers');
  5. const { expect } = require('chai');
  6. const MinimalForwarder = artifacts.require('MinimalForwarder');
  7. const name = 'MinimalForwarder';
  8. const version = '0.0.1';
  9. contract('MinimalForwarder', function (accounts) {
  10. beforeEach(async function () {
  11. this.forwarder = await MinimalForwarder.new();
  12. this.domain = {
  13. name,
  14. version,
  15. chainId: await web3.eth.getChainId(),
  16. verifyingContract: this.forwarder.address,
  17. };
  18. this.types = {
  19. EIP712Domain,
  20. ForwardRequest: [
  21. { name: 'from', type: 'address' },
  22. { name: 'to', type: 'address' },
  23. { name: 'value', type: 'uint256' },
  24. { name: 'gas', type: 'uint256' },
  25. { name: 'nonce', type: 'uint256' },
  26. { name: 'data', type: 'bytes' },
  27. ],
  28. };
  29. });
  30. context('with message', function () {
  31. beforeEach(async function () {
  32. this.wallet = Wallet.generate();
  33. this.sender = web3.utils.toChecksumAddress(this.wallet.getAddressString());
  34. this.req = {
  35. from: this.sender,
  36. to: constants.ZERO_ADDRESS,
  37. value: '0',
  38. gas: '100000',
  39. nonce: Number(await this.forwarder.getNonce(this.sender)),
  40. data: '0x',
  41. };
  42. this.sign = ethSigUtil.signTypedMessage(
  43. this.wallet.getPrivateKey(),
  44. {
  45. data: {
  46. types: this.types,
  47. domain: this.domain,
  48. primaryType: 'ForwardRequest',
  49. message: this.req,
  50. },
  51. },
  52. );
  53. });
  54. context('verify', function () {
  55. context('valid signature', function () {
  56. beforeEach(async function () {
  57. expect(await this.forwarder.getNonce(this.req.from))
  58. .to.be.bignumber.equal(web3.utils.toBN(this.req.nonce));
  59. });
  60. it('success', async function () {
  61. expect(await this.forwarder.verify(this.req, this.sign)).to.be.equal(true);
  62. });
  63. afterEach(async function () {
  64. expect(await this.forwarder.getNonce(this.req.from))
  65. .to.be.bignumber.equal(web3.utils.toBN(this.req.nonce));
  66. });
  67. });
  68. context('invalid signature', function () {
  69. it('tampered from', async function () {
  70. expect(await this.forwarder.verify({ ...this.req, from: accounts[0] }, this.sign))
  71. .to.be.equal(false);
  72. });
  73. it('tampered to', async function () {
  74. expect(await this.forwarder.verify({ ...this.req, to: accounts[0] }, this.sign))
  75. .to.be.equal(false);
  76. });
  77. it('tampered value', async function () {
  78. expect(await this.forwarder.verify({ ...this.req, value: web3.utils.toWei('1') }, this.sign))
  79. .to.be.equal(false);
  80. });
  81. it('tampered nonce', async function () {
  82. expect(await this.forwarder.verify({ ...this.req, nonce: this.req.nonce + 1 }, this.sign))
  83. .to.be.equal(false);
  84. });
  85. it('tampered data', async function () {
  86. expect(await this.forwarder.verify({ ...this.req, data: '0x1742' }, this.sign))
  87. .to.be.equal(false);
  88. });
  89. it('tampered signature', async function () {
  90. const tamperedsign = web3.utils.hexToBytes(this.sign);
  91. tamperedsign[42] ^= 0xff;
  92. expect(await this.forwarder.verify(this.req, web3.utils.bytesToHex(tamperedsign)))
  93. .to.be.equal(false);
  94. });
  95. });
  96. });
  97. context('execute', function () {
  98. context('valid signature', function () {
  99. beforeEach(async function () {
  100. expect(await this.forwarder.getNonce(this.req.from))
  101. .to.be.bignumber.equal(web3.utils.toBN(this.req.nonce));
  102. });
  103. it('success', async function () {
  104. await this.forwarder.execute(this.req, this.sign); // expect to not revert
  105. });
  106. afterEach(async function () {
  107. expect(await this.forwarder.getNonce(this.req.from))
  108. .to.be.bignumber.equal(web3.utils.toBN(this.req.nonce + 1));
  109. });
  110. });
  111. context('invalid signature', function () {
  112. it('tampered from', async function () {
  113. await expectRevert(
  114. this.forwarder.execute({ ...this.req, from: accounts[0] }, this.sign),
  115. 'MinimalForwarder: signature does not match request',
  116. );
  117. });
  118. it('tampered to', async function () {
  119. await expectRevert(
  120. this.forwarder.execute({ ...this.req, to: accounts[0] }, this.sign),
  121. 'MinimalForwarder: signature does not match request',
  122. );
  123. });
  124. it('tampered value', async function () {
  125. await expectRevert(
  126. this.forwarder.execute({ ...this.req, value: web3.utils.toWei('1') }, this.sign),
  127. 'MinimalForwarder: signature does not match request',
  128. );
  129. });
  130. it('tampered nonce', async function () {
  131. await expectRevert(
  132. this.forwarder.execute({ ...this.req, nonce: this.req.nonce + 1 }, this.sign),
  133. 'MinimalForwarder: signature does not match request',
  134. );
  135. });
  136. it('tampered data', async function () {
  137. await expectRevert(
  138. this.forwarder.execute({ ...this.req, data: '0x1742' }, this.sign),
  139. 'MinimalForwarder: signature does not match request',
  140. );
  141. });
  142. it('tampered signature', async function () {
  143. const tamperedsign = web3.utils.hexToBytes(this.sign);
  144. tamperedsign[42] ^= 0xff;
  145. await expectRevert(
  146. this.forwarder.execute(this.req, web3.utils.bytesToHex(tamperedsign)),
  147. 'MinimalForwarder: signature does not match request',
  148. );
  149. });
  150. });
  151. });
  152. });
  153. });