ERC2771Context.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const ethSigUtil = require('eth-sig-util');
  2. const Wallet = require('ethereumjs-wallet').default;
  3. const { EIP712Domain } = 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. const name = 'MinimalForwarder';
  11. const version = '0.0.1';
  12. contract('ERC2771Context', function (accounts) {
  13. beforeEach(async function () {
  14. this.forwarder = await MinimalForwarder.new();
  15. this.recipient = await ERC2771ContextMock.new(this.forwarder.address);
  16. this.domain = {
  17. name,
  18. version,
  19. chainId: await web3.eth.getChainId(),
  20. verifyingContract: this.forwarder.address,
  21. };
  22. this.types = {
  23. EIP712Domain,
  24. ForwardRequest: [
  25. { name: 'from', type: 'address' },
  26. { name: 'to', type: 'address' },
  27. { name: 'value', type: 'uint256' },
  28. { name: 'gas', type: 'uint256' },
  29. { name: 'nonce', type: 'uint256' },
  30. { name: 'data', type: 'bytes' },
  31. ],
  32. };
  33. });
  34. it('recognize trusted forwarder', async function () {
  35. expect(await this.recipient.isTrustedForwarder(this.forwarder.address));
  36. });
  37. context('when called directly', function () {
  38. beforeEach(async function () {
  39. this.context = this.recipient; // The Context behavior expects the contract in this.context
  40. this.caller = await ContextMockCaller.new();
  41. });
  42. shouldBehaveLikeRegularContext(...accounts);
  43. });
  44. context('when receiving a relayed call', function () {
  45. beforeEach(async function () {
  46. this.wallet = Wallet.generate();
  47. this.sender = web3.utils.toChecksumAddress(this.wallet.getAddressString());
  48. this.data = {
  49. types: this.types,
  50. domain: this.domain,
  51. primaryType: 'ForwardRequest',
  52. };
  53. });
  54. describe('msgSender', function () {
  55. it('returns the relayed transaction original sender', async function () {
  56. const data = this.recipient.contract.methods.msgSender().encodeABI();
  57. const req = {
  58. from: this.sender,
  59. to: this.recipient.address,
  60. value: '0',
  61. gas: '100000',
  62. nonce: (await this.forwarder.getNonce(this.sender)).toString(),
  63. data,
  64. };
  65. const sign = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), { data: { ...this.data, message: req } });
  66. expect(await this.forwarder.verify(req, sign)).to.equal(true);
  67. const { tx } = await this.forwarder.execute(req, sign);
  68. await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Sender', { sender: this.sender });
  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. });
  91. });