ERC2771Context.test.js 3.8 KB

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