Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/crosschain-linked-calldata-parse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-tron-solidity': patch
---

`CrosschainLinked`: Parse the counterpart chain from calldata in `_isAuthorizedGateway`, avoiding an unnecessary memory copy of `sender`.
7 changes: 6 additions & 1 deletion contracts/crosschain/CrosschainLinked.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ abstract contract CrosschainLinked is TRC7786Recipient {
address instance,
bytes calldata sender
) internal view virtual override returns (bool) {
(address gateway, bytes memory router) = getLink(_extractChain(sender));
(address gateway, bytes memory router) = getLink(_extractChainCalldata(sender));
return instance == gateway && sender.equal(router);
}

function _extractChain(bytes memory self) private pure returns (bytes memory) {
(bytes2 chainType, bytes memory chainReference, ) = self.parseV1();
return InteroperableAddress.formatV1(chainType, chainReference, hex"");
}

function _extractChainCalldata(bytes calldata self) private pure returns (bytes memory) {
(bytes2 chainType, bytes calldata chainReference, ) = self.parseV1Calldata();
return InteroperableAddress.formatV1(chainType, chainReference, hex"");
}
}
48 changes: 48 additions & 0 deletions test/crosschain/CrosschainLinked.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { getLocalChain } = require('../helpers/chains');

async function fixture() {
const chain = await getLocalChain();
const [counterpart, other] = await ethers.getSigners();

const gateway = await ethers.deployContract('$TRC7786GatewayMock');
const token = await ethers.deployContract('$TRC20', ['Token', 'T']);
// `$BridgeTRC20` is a concrete `CrosschainLinked`. Register a link to `counterpart` on the local chain.
const linked = await ethers.deployContract('$BridgeTRC20', [[], token]);
await linked.$_setLink(gateway, chain.toErc7930(counterpart), false);

return { chain, gateway, counterpart, other, linked };
}

describe('CrosschainLinked', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});

describe('_isAuthorizedGateway', function () {
it('authorizes the registered gateway and counterpart', async function () {
const sender = this.chain.toErc7930(this.counterpart);
await expect(this.linked.$_isAuthorizedGateway(this.gateway, sender)).to.eventually.be.true;
});

it('rejects an instance that is not the registered gateway', async function () {
const sender = this.chain.toErc7930(this.counterpart);
await expect(this.linked.$_isAuthorizedGateway(this.other, sender)).to.eventually.be.false;
});

it('rejects a sender that is not the registered counterpart', async function () {
const sender = this.chain.toErc7930(this.other);
await expect(this.linked.$_isAuthorizedGateway(this.gateway, sender)).to.eventually.be.false;
});

it('reverts on a malformed sender', async function () {
await expect(this.linked.$_isAuthorizedGateway(this.gateway, '0x00010042')).to.be.revertedWithCustomError(
this.linked,
'InteroperableAddressParsingError',
);
});
});
});
Loading