ERC2771Context.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 ERC2771Forwarder = artifacts.require('ERC2771Forwarder');
  8. const ContextMockCaller = artifacts.require('ContextMockCaller');
  9. const { shouldBehaveLikeRegularContext } = require('../utils/Context.behavior');
  10. contract('ERC2771Context', function (accounts) {
  11. const MAX_UINT48 = web3.utils.toBN(1).shln(48).subn(1).toString();
  12. beforeEach(async function () {
  13. this.forwarder = await ERC2771Forwarder.new('ERC2771Forwarder');
  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: 'deadline', type: 'uint48' },
  25. { name: 'data', type: 'bytes' },
  26. ],
  27. };
  28. });
  29. it('recognize trusted forwarder', async function () {
  30. expect(await this.recipient.isTrustedForwarder(this.forwarder.address));
  31. });
  32. context('when called directly', function () {
  33. beforeEach(async function () {
  34. this.context = this.recipient; // The Context behavior expects the contract in this.context
  35. this.caller = await ContextMockCaller.new();
  36. });
  37. shouldBehaveLikeRegularContext(...accounts);
  38. });
  39. context('when receiving a relayed call', function () {
  40. beforeEach(async function () {
  41. this.wallet = Wallet.generate();
  42. this.sender = web3.utils.toChecksumAddress(this.wallet.getAddressString());
  43. this.data = {
  44. types: this.types,
  45. domain: this.domain,
  46. primaryType: 'ForwardRequest',
  47. };
  48. });
  49. describe('msgSender', function () {
  50. it('returns the relayed transaction original sender', async function () {
  51. const data = this.recipient.contract.methods.msgSender().encodeABI();
  52. const req = {
  53. from: this.sender,
  54. to: this.recipient.address,
  55. value: '0',
  56. gas: '100000',
  57. nonce: (await this.forwarder.nonces(this.sender)).toString(),
  58. deadline: MAX_UINT48,
  59. data,
  60. };
  61. req.signature = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), {
  62. data: { ...this.data, message: req },
  63. });
  64. expect(await this.forwarder.verify(req)).to.equal(true);
  65. const { tx } = await this.forwarder.execute(req);
  66. await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Sender', { sender: this.sender });
  67. });
  68. });
  69. describe('msgData', function () {
  70. it('returns the relayed transaction original data', async function () {
  71. const integerValue = '42';
  72. const stringValue = 'OpenZeppelin';
  73. const data = this.recipient.contract.methods.msgData(integerValue, stringValue).encodeABI();
  74. const req = {
  75. from: this.sender,
  76. to: this.recipient.address,
  77. value: '0',
  78. gas: '100000',
  79. nonce: (await this.forwarder.nonces(this.sender)).toString(),
  80. deadline: MAX_UINT48,
  81. data,
  82. };
  83. req.signature = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), {
  84. data: { ...this.data, message: req },
  85. });
  86. expect(await this.forwarder.verify(req)).to.equal(true);
  87. const { tx } = await this.forwarder.execute(req);
  88. await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Data', { data, integerValue, stringValue });
  89. });
  90. });
  91. });
  92. });