Files
openzeppelin-contracts/test/metatx/ERC2771Context.test.js
Ernesto García ca7a4e39de Group typo fixes (#5466)
Co-authored-by: Bilog WEB3 <155262265+Bilogweb3@users.noreply.github.com>
Co-authored-by: Fallengirl <155266340+Fallengirl@users.noreply.github.com>
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
Co-authored-by: XxAlex74xX <30472093+XxAlex74xX@users.noreply.github.com>
Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
Co-authored-by: PixelPilot <161360836+PixelPil0t1@users.noreply.github.com>
Co-authored-by: kilavvy <140459108+kilavvy@users.noreply.github.com>
Co-authored-by: Devkuni <155117116+detrina@users.noreply.github.com>
Co-authored-by: Danbo <140512416+dannbbb1@users.noreply.github.com>
Co-authored-by: Ann Wagner <chant_77_swirly@icloud.com>
Co-authored-by: comfsrt <155266597+comfsrt@users.noreply.github.com>
Co-authored-by: Bob <158583129+bouchmann@users.noreply.github.com>
Co-authored-by: JohnBonny <158583902+JohnBonny@users.noreply.github.com>
Co-authored-by: moonman <155266991+moooonman@users.noreply.github.com>
Co-authored-by: kazak <alright-epsilon8h@icloud.com>
Co-authored-by: Wei <ybxerlvqtx@rambler.ru>
Co-authored-by: Maxim Evtush <154841002+maximevtush@users.noreply.github.com>
Co-authored-by: Vitalyr <158586577+Vitaliyr888@users.noreply.github.com>
Co-authored-by: pendrue <158588659+pendrue@users.noreply.github.com>
Co-authored-by: Tronica <wudmytrotest404@gmail.com>
Co-authored-by: emmmm <155267286+eeemmmmmm@users.noreply.github.com>
Co-authored-by: bigbear <155267841+aso20455@users.noreply.github.com>
Co-authored-by: Tomás Andróil <tomasandroil@gmail.com>
Co-authored-by: GooseMatrix <155266802+GooseMatrix@users.noreply.github.com>
Co-authored-by: jasmy <3776356370@qq.com>
Co-authored-by: SITADRITA1 <mrlime2018@gmail.com>
Co-authored-by: Ocenka <testoviydiman1@gmail.com>
2025-03-06 09:58:25 +01:00

134 lines
4.7 KiB
JavaScript

const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { impersonate } = require('../helpers/account');
const { getDomain, ForwardRequest } = require('../helpers/eip712');
const { MAX_UINT48 } = require('../helpers/constants');
const { shouldBehaveLikeRegularContext } = require('../utils/Context.behavior');
async function fixture() {
const [sender, other] = await ethers.getSigners();
const forwarder = await ethers.deployContract('ERC2771Forwarder', ['ERC2771Forwarder']);
const forwarderAsSigner = await impersonate(forwarder.target);
const context = await ethers.deployContract('ERC2771ContextMock', [forwarder]);
const domain = await getDomain(forwarder);
const types = { ForwardRequest };
return { sender, other, forwarder, forwarderAsSigner, context, domain, types };
}
describe('ERC2771Context', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('recognize trusted forwarder', async function () {
expect(await this.context.isTrustedForwarder(this.forwarder)).to.be.true;
});
it('returns the trusted forwarder', async function () {
expect(await this.context.trustedForwarder()).to.equal(this.forwarder);
});
describe('when called directly', function () {
shouldBehaveLikeRegularContext();
});
describe('when receiving a relayed call', function () {
describe('msgSender', function () {
it('returns the relayed transaction original sender', async function () {
const nonce = await this.forwarder.nonces(this.sender);
const data = this.context.interface.encodeFunctionData('msgSender');
const req = {
from: await this.sender.getAddress(),
to: await this.context.getAddress(),
value: 0n,
data,
gas: 100000n,
nonce,
deadline: MAX_UINT48,
};
req.signature = await this.sender.signTypedData(this.domain, this.types, req);
expect(await this.forwarder.verify(req)).to.be.true;
await expect(this.forwarder.execute(req)).to.emit(this.context, 'Sender').withArgs(this.sender);
});
it('returns the original sender when calldata length is less than 20 bytes (address length)', async function () {
// The forwarder doesn't produce calls with calldata length less than 20 bytes so `this.forwarderAsSigner` is used instead.
await expect(this.context.connect(this.forwarderAsSigner).msgSender())
.to.emit(this.context, 'Sender')
.withArgs(this.forwarder);
});
});
describe('msgData', function () {
it('returns the relayed transaction original data', async function () {
const args = [42n, 'OpenZeppelin'];
const nonce = await this.forwarder.nonces(this.sender);
const data = this.context.interface.encodeFunctionData('msgData', args);
const req = {
from: await this.sender.getAddress(),
to: await this.context.getAddress(),
value: 0n,
data,
gas: 100000n,
nonce,
deadline: MAX_UINT48,
};
req.signature = this.sender.signTypedData(this.domain, this.types, req);
expect(await this.forwarder.verify(req)).to.be.true;
await expect(this.forwarder.execute(req))
.to.emit(this.context, 'Data')
.withArgs(data, ...args);
});
});
it('returns the full original data when calldata length is less than 20 bytes (address length)', async function () {
const data = this.context.interface.encodeFunctionData('msgDataShort');
// The forwarder doesn't produce calls with calldata length less than 20 bytes so `this.forwarderAsSigner` is used instead.
await expect(await this.context.connect(this.forwarderAsSigner).msgDataShort())
.to.emit(this.context, 'DataShort')
.withArgs(data);
});
});
it('multicall poison attack', async function () {
const nonce = await this.forwarder.nonces(this.sender);
const data = this.context.interface.encodeFunctionData('multicall', [
[
// poisoned call to 'msgSender()'
ethers.concat([this.context.interface.encodeFunctionData('msgSender'), this.other.address]),
],
]);
const req = {
from: await this.sender.getAddress(),
to: await this.context.getAddress(),
value: 0n,
data,
gas: 100000n,
nonce,
deadline: MAX_UINT48,
};
req.signature = await this.sender.signTypedData(this.domain, this.types, req);
expect(await this.forwarder.verify(req)).to.be.true;
await expect(this.forwarder.execute(req)).to.emit(this.context, 'Sender').withArgs(this.sender);
});
});