Skip to content

Commit

Permalink
feat: improve _getV3RelayHash method (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaree authored Dec 3, 2024
1 parent 0f2600f commit fa137a2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 17 deletions.
19 changes: 18 additions & 1 deletion contracts/SpokePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,24 @@ abstract contract SpokePool is
}

function _getV3RelayHash(V3RelayData memory relayData) private view returns (bytes32) {
return keccak256(abi.encode(relayData, chainId()));
return
keccak256(
abi.encode(
relayData.depositor,
relayData.recipient,
relayData.exclusiveRelayer,
relayData.inputToken,
relayData.outputToken,
relayData.inputAmount,
relayData.outputAmount,
relayData.originChainId,
relayData.depositId,
relayData.fillDeadline,
relayData.exclusivityDeadline,
_hashNonEmptyMessage(relayData.message),
chainId()
)
);
}

// Unwraps ETH and does a transfer to a recipient address. If the recipient is a smart contract then sends wrappedNativeToken.
Expand Down
2 changes: 1 addition & 1 deletion scripts/buildSampleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async function main() {
originChainId: SPOKE_POOL_CHAIN_ID,
fillDeadline: Math.floor(Date.now() / 1000) + 14400, // 4 hours from now
exclusivityDeadline: 0,
depositId: i,
depositId: toBN(i),
message: "0x",
},
updatedOutputAmount: toBNWeiWithDecimals(SLOW_RELAY_AMOUNT, DECIMALS),
Expand Down
2 changes: 1 addition & 1 deletion test/evm/hardhat/MerkleLib.Proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe("MerkleLib Proofs", async function () {
inputAmount: randomBigNumber(),
outputAmount: randomBigNumber(),
originChainId: randomBigNumber(2).toNumber(),
depositId: BigNumber.from(i).toNumber(),
depositId: BigNumber.from(i),
fillDeadline: randomBigNumber(2).toNumber(),
exclusivityDeadline: randomBigNumber(2).toNumber(),
message: ethers.utils.hexlify(ethers.utils.randomBytes(1024)),
Expand Down
8 changes: 4 additions & 4 deletions test/evm/hardhat/SpokePool.Deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe("SpokePool Depositor Logic", async function () {
inputAmount: amountToDeposit,
outputAmount: amountToDeposit.sub(19),
originChainId: originChainId,
depositId: 0,
depositId: toBN(0),
fillDeadline: quoteTimestamp + 1000,
exclusivityDeadline: 0,
message: "0x",
Expand Down Expand Up @@ -869,7 +869,7 @@ describe("SpokePool Depositor Logic", async function () {
const updatedOutputAmount = amountToDeposit.add(1);
const updatedRecipient = randomAddress();
const updatedMessage = "0x1234";
const depositId = 100;
const depositId = toBN(100);
it("_verifyUpdateV3DepositMessage", async function () {
const signature = await getUpdatedV3DepositSignature(
depositor,
Expand Down Expand Up @@ -905,7 +905,7 @@ describe("SpokePool Depositor Logic", async function () {
// @dev Creates an invalid signature using different params
const invalidSignature = await getUpdatedV3DepositSignature(
depositor,
depositId + 1,
depositId.add(toBN(1)),
originChainId,
updatedOutputAmount,
addressToBytes(updatedRecipient),
Expand Down Expand Up @@ -994,7 +994,7 @@ describe("SpokePool Depositor Logic", async function () {
const updatedOutputAmount = amountToDeposit.add(1);
const updatedRecipient = randomAddress();
const updatedMessage = "0x1234";
const depositId = 100;
const depositId = toBN(100);
const spokePoolChainId = await spokePool.chainId();

const signature = await getUpdatedV3DepositSignature(
Expand Down
7 changes: 4 additions & 3 deletions test/evm/hardhat/SpokePool.Relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
addressToBytes,
bytes32ToAddress,
hashNonEmptyMessage,
toBN,
} from "../../../utils/utils";
import {
spokePoolFixture,
Expand Down Expand Up @@ -306,7 +307,7 @@ describe("SpokePool Relayer Logic", async function () {
addressToBytes(relayer.address),
false // isSlowFill
);
const test = bytes32ToAddress(_relayData.outputToken);

expect(acrossMessageHandler.handleV3AcrossMessage).to.have.been.calledOnceWith(
bytes32ToAddress(_relayData.outputToken),
relayExecution.updatedOutputAmount,
Expand Down Expand Up @@ -522,7 +523,7 @@ describe("SpokePool Relayer Logic", async function () {
// Incorrect signature for new deposit ID
const otherSignature = await getUpdatedV3DepositSignature(
depositor,
relayData.depositId + 1,
relayData.depositId.add(toBN(1)),
relayData.originChainId,
updatedOutputAmount,
addressToBytes(updatedRecipient),
Expand Down Expand Up @@ -562,7 +563,7 @@ describe("SpokePool Relayer Logic", async function () {
spokePool
.connect(relayer)
.fillV3RelayWithUpdatedDeposit(
{ ...relayData, depositId: relayData.depositId + 1 },
{ ...relayData, depositId: relayData.depositId.add(toBN(1)) },
consts.repaymentChainId,
addressToBytes(relayer.address),
updatedOutputAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
FakeContract,
createFakeFromABI,
addressToBytes,
toBN,
} from "../../../../utils/utils";
import { hre } from "../../../../utils/utils.hre";
import { hubPoolFixture } from "../fixtures/HubPool.Fixture";
Expand Down Expand Up @@ -329,7 +330,7 @@ describe("Polygon Spoke Pool", function () {
inputAmount: toWei("1"),
outputAmount: toWei("1"),
originChainId: originChainId,
depositId: 0,
depositId: toBN(0),
fillDeadline: currentTime + 7200,
exclusivityDeadline: 0,
message: "0x1234",
Expand Down
2 changes: 1 addition & 1 deletion test/evm/hardhat/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const originChainId = 666;

export const repaymentChainId = 777;

export const firstDepositId = 0;
export const firstDepositId = toBN(0);

export const bondAmount = toWei("5");

Expand Down
36 changes: 31 additions & 5 deletions test/evm/hardhat/fixtures/SpokePool.Fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface V3RelayData {
inputAmount: BigNumber;
outputAmount: BigNumber;
originChainId: number;
depositId: number;
depositId: BigNumber;
fillDeadline: number;
exclusivityDeadline: number;
message: string;
Expand Down Expand Up @@ -177,13 +177,39 @@ export function getRelayHash(
}

export function getV3RelayHash(relayData: V3RelayData, destinationChainId: number): string {
const messageHash = relayData.message == "0x" ? ethers.constants.HashZero : ethers.utils.keccak256(relayData.message);
return ethers.utils.keccak256(
defaultAbiCoder.encode(
[
"tuple(bytes32 depositor, bytes32 recipient, bytes32 exclusiveRelayer, bytes32 inputToken, bytes32 outputToken, uint256 inputAmount, uint256 outputAmount, uint256 originChainId, uint32 depositId, uint32 fillDeadline, uint32 exclusivityDeadline, bytes message)",
"uint256 destinationChainId",
"bytes32", // depositor
"bytes32", // recipient
"bytes32", // exclusiveRelayer
"bytes32", // inputToken
"bytes32", // outputToken
"uint256", // inputAmount
"uint256", // outputAmount
"uint256", // originChainId
"uint256", // depositId
"uint32", // fillDeadline
"uint32", // exclusivityDeadline
"bytes32", // messageHash
"uint256", // destinationChainId
],
[relayData, destinationChainId]
[
relayData.depositor,
relayData.recipient,
relayData.exclusiveRelayer,
relayData.inputToken,
relayData.outputToken,
relayData.inputAmount,
relayData.outputAmount,
relayData.originChainId,
relayData.depositId,
relayData.fillDeadline,
relayData.exclusivityDeadline,
messageHash,
destinationChainId,
]
)
);
}
Expand Down Expand Up @@ -336,7 +362,7 @@ export async function modifyRelayHelper(

export async function getUpdatedV3DepositSignature(
depositor: SignerWithAddress,
depositId: number,
depositId: BigNumber,
originChainId: number,
updatedOutputAmount: BigNumber,
updatedRecipient: string,
Expand Down

0 comments on commit fa137a2

Please sign in to comment.