ERC2771Context.test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const ethSigUtil = require('eth-sig-util');
  2. const Wallet = require('ethereumjs-wallet').default;
  3. const { getDomain, domainType } = require('../helpers/eip712');
  4. const { expectEvent } = require('@openzeppelin/test-helpers');
  5. const { expect } = require('chai');
  6. const ERC2771ContextMock = artifacts.require('ERC2771ContextMock');
  7. const MinimalForwarder = artifacts.require('MinimalForwarder');
  8. const ContextMockCaller = artifacts.require('ContextMockCaller');
  9. const { shouldBehaveLikeRegularContext } = require('../utils/Context.behavior');
  10. contract('ERC2771Context', function (accounts) {
  11. const [, trustedForwarder, other] = accounts;
  12. beforeEach(async function () {
  13. this.forwarder = await MinimalForwarder.new();
  14. this.recipient = await ERC2771ContextMock.new(this.forwarder.address);
  15. this.domain = await getDomain(this.forwarder);
  16. this.types = {
  17. EIP712Domain: domainType(this.domain),
  18. ForwardRequest: [
  19. { name: 'from', type: 'address' },
  20. { name: 'to', type: 'address' },
  21. { name: 'value', type: 'uint256' },
  22. { name: 'gas', type: 'uint256' },
  23. { name: 'nonce', type: 'uint256' },
  24. { name: 'data', type: 'bytes' },
  25. ],
  26. };
  27. });
  28. it('recognize trusted forwarder', async function () {
  29. expect(await this.recipient.isTrustedForwarder(this.forwarder.address));
  30. });
  31. context('when called directly', function () {
  32. beforeEach(async function () {
  33. this.context = this.recipient; // The Context behavior expects the contract in this.context
  34. this.caller = await ContextMockCaller.new();
  35. });
  36. shouldBehaveLikeRegularContext(...accounts);
  37. });
  38. context('when receiving a relayed call', function () {
  39. beforeEach(async function () {
  40. this.wallet = Wallet.generate();
  41. this.sender = web3.utils.toChecksumAddress(this.wallet.getAddressString());
  42. this.data = {
  43. types: this.types,
  44. domain: this.domain,
  45. primaryType: 'ForwardRequest',
  46. };
  47. });
  48. describe('msgSender', function () {
  49. it('returns the relayed transaction original sender', async function () {
  50. const data = this.recipient.contract.methods.msgSender().encodeABI();
  51. const req = {
  52. from: this.sender,
  53. to: this.recipient.address,
  54. value: '0',
  55. gas: '100000',
  56. nonce: (await this.forwarder.getNonce(this.sender)).toString(),
  57. data,
  58. };
  59. const sign = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), { data: { ...this.data, message: req } });
  60. expect(await this.forwarder.verify(req, sign)).to.equal(true);
  61. const { tx } = await this.forwarder.execute(req, sign);
  62. await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Sender', { sender: this.sender });
  63. });
  64. it('returns the original sender when calldata length is less than 20 bytes (address length)', async function () {
  65. // The forwarder doesn't produce calls with calldata length less than 20 bytes
  66. const recipient = await ERC2771ContextMock.new(trustedForwarder);
  67. const { receipt } = await recipient.msgSender({ from: trustedForwarder });
  68. await expectEvent(receipt, 'Sender', { sender: trustedForwarder });
  69. });
  70. });
  71. describe('msgData', function () {
  72. it('returns the relayed transaction original data', async function () {
  73. const integerValue = '42';
  74. const stringValue = 'OpenZeppelin';
  75. const data = this.recipient.contract.methods.msgData(integerValue, stringValue).encodeABI();
  76. const req = {
  77. from: this.sender,
  78. to: this.recipient.address,
  79. value: '0',
  80. gas: '100000',
  81. nonce: (await this.forwarder.getNonce(this.sender)).toString(),
  82. data,
  83. };
  84. const sign = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), { data: { ...this.data, message: req } });
  85. expect(await this.forwarder.verify(req, sign)).to.equal(true);
  86. const { tx } = await this.forwarder.execute(req, sign);
  87. await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Data', { data, integerValue, stringValue });
  88. });
  89. });
  90. it('returns the full original data when calldata length is less than 20 bytes (address length)', async function () {
  91. // The forwarder doesn't produce calls with calldata length less than 20 bytes
  92. const recipient = await ERC2771ContextMock.new(trustedForwarder);
  93. const { receipt } = await recipient.msgDataShort({ from: trustedForwarder });
  94. const data = recipient.contract.methods.msgDataShort().encodeABI();
  95. await expectEvent(receipt, 'DataShort', { data });
  96. });
  97. it('multicall poison attack', async function () {
  98. const attacker = Wallet.generate();
  99. const attackerAddress = attacker.getChecksumAddressString();
  100. const nonce = await this.forwarder.getNonce(attackerAddress);
  101. const msgSenderCall = web3.eth.abi.encodeFunctionCall(
  102. {
  103. name: 'msgSender',
  104. type: 'function',
  105. inputs: [],
  106. },
  107. [],
  108. );
  109. const data = web3.eth.abi.encodeFunctionCall(
  110. {
  111. name: 'multicall',
  112. type: 'function',
  113. inputs: [
  114. {
  115. internalType: 'bytes[]',
  116. name: 'data',
  117. type: 'bytes[]',
  118. },
  119. ],
  120. },
  121. [[web3.utils.encodePacked({ value: msgSenderCall, type: 'bytes' }, { value: other, type: 'address' })]],
  122. );
  123. const req = {
  124. from: attackerAddress,
  125. to: this.recipient.address,
  126. value: '0',
  127. gas: '100000',
  128. data,
  129. nonce: Number(nonce),
  130. };
  131. const signature = await ethSigUtil.signTypedMessage(attacker.getPrivateKey(), {
  132. data: {
  133. types: this.types,
  134. domain: this.domain,
  135. primaryType: 'ForwardRequest',
  136. message: req,
  137. },
  138. });
  139. expect(await this.forwarder.verify(req, signature)).to.equal(true);
  140. const receipt = await this.forwarder.execute(req, signature);
  141. await expectEvent.inTransaction(receipt.tx, ERC2771ContextMock, 'Sender', { sender: attackerAddress });
  142. });
  143. });
  144. });