|
| 1 | +const ethSigUtil = require('eth-sig-util'); |
| 2 | +const Wallet = require('ethereumjs-wallet').default; |
| 3 | +const { EIP712Domain } = require('../helpers/eip712'); |
| 4 | + |
| 5 | +const { expectEvent } = require('@openzeppelin/test-helpers'); |
| 6 | +const { expect } = require('chai'); |
| 7 | + |
| 8 | +const ERC2771ContextMock = artifacts.require('ERC2771ContextMock'); |
| 9 | +const MinimalForwarder = artifacts.require('MinimalForwarder'); |
| 10 | +const ContextMockCaller = artifacts.require('ContextMockCaller'); |
| 11 | + |
| 12 | +const { shouldBehaveLikeRegularContext } = require('../utils/Context.behavior'); |
| 13 | + |
| 14 | +const name = 'MinimalForwarder'; |
| 15 | +const version = '0.0.1'; |
| 16 | + |
| 17 | +contract('ERC2771Context', function (accounts) { |
| 18 | + beforeEach(async function () { |
| 19 | + this.forwarder = await MinimalForwarder.new(); |
| 20 | + this.recipient = await ERC2771ContextMock.new(this.forwarder.address); |
| 21 | + |
| 22 | + this.domain = { |
| 23 | + name, |
| 24 | + version, |
| 25 | + chainId: await web3.eth.getChainId(), |
| 26 | + verifyingContract: this.forwarder.address, |
| 27 | + }; |
| 28 | + this.types = { |
| 29 | + EIP712Domain, |
| 30 | + ForwardRequest: [ |
| 31 | + { name: 'from', type: 'address' }, |
| 32 | + { name: 'to', type: 'address' }, |
| 33 | + { name: 'value', type: 'uint256' }, |
| 34 | + { name: 'gas', type: 'uint256' }, |
| 35 | + { name: 'nonce', type: 'uint256' }, |
| 36 | + { name: 'data', type: 'bytes' }, |
| 37 | + ], |
| 38 | + }; |
| 39 | + }); |
| 40 | + |
| 41 | + it('recognize trusted forwarder', async function () { |
| 42 | + expect(await this.recipient.isTrustedForwarder(this.forwarder.address)); |
| 43 | + }); |
| 44 | + |
| 45 | + context('when called directly', function () { |
| 46 | + beforeEach(async function () { |
| 47 | + this.context = this.recipient; // The Context behavior expects the contract in this.context |
| 48 | + this.caller = await ContextMockCaller.new(); |
| 49 | + }); |
| 50 | + |
| 51 | + shouldBehaveLikeRegularContext(...accounts); |
| 52 | + }); |
| 53 | + |
| 54 | + context('when receiving a relayed call', function () { |
| 55 | + beforeEach(async function () { |
| 56 | + this.wallet = Wallet.generate(); |
| 57 | + this.sender = web3.utils.toChecksumAddress(this.wallet.getAddressString()); |
| 58 | + this.data = { |
| 59 | + types: this.types, |
| 60 | + domain: this.domain, |
| 61 | + primaryType: 'ForwardRequest', |
| 62 | + }; |
| 63 | + }); |
| 64 | + |
| 65 | + describe('msgSender', function () { |
| 66 | + it('returns the relayed transaction original sender', async function () { |
| 67 | + const data = this.recipient.contract.methods.msgSender().encodeABI(); |
| 68 | + |
| 69 | + const req = { |
| 70 | + from: this.sender, |
| 71 | + to: this.recipient.address, |
| 72 | + value: '0', |
| 73 | + gas: '100000', |
| 74 | + nonce: (await this.forwarder.getNonce(this.sender)).toString(), |
| 75 | + data, |
| 76 | + }; |
| 77 | + |
| 78 | + const sign = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), { data: { ...this.data, message: req } }); |
| 79 | + |
| 80 | + // rejected by lint :/ |
| 81 | + // expect(await this.forwarder.verify(req, sign)).to.be.true; |
| 82 | + |
| 83 | + const { tx } = await this.forwarder.execute(req, sign); |
| 84 | + await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Sender', { sender: this.sender }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('msgData', function () { |
| 89 | + it('returns the relayed transaction original data', async function () { |
| 90 | + const integerValue = '42'; |
| 91 | + const stringValue = 'OpenZeppelin'; |
| 92 | + const data = this.recipient.contract.methods.msgData(integerValue, stringValue).encodeABI(); |
| 93 | + |
| 94 | + const req = { |
| 95 | + from: this.sender, |
| 96 | + to: this.recipient.address, |
| 97 | + value: '0', |
| 98 | + gas: '100000', |
| 99 | + nonce: (await this.forwarder.getNonce(this.sender)).toString(), |
| 100 | + data, |
| 101 | + }; |
| 102 | + |
| 103 | + const sign = ethSigUtil.signTypedMessage(this.wallet.getPrivateKey(), { data: { ...this.data, message: req } }); |
| 104 | + |
| 105 | + // rejected by lint :/ |
| 106 | + // expect(await this.forwarder.verify(req, sign)).to.be.true; |
| 107 | + |
| 108 | + const { tx } = await this.forwarder.execute(req, sign); |
| 109 | + await expectEvent.inTransaction(tx, ERC2771ContextMock, 'Data', { data, integerValue, stringValue }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments