Skip to content

improve(SpokeUtils): Use viem for RelayData hash computation #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 25 additions & 25 deletions src/utils/SpokeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { utils as ethersUtils } from "ethers";
import { encodeAbiParameters, keccak256 } from "viem";
import { MAX_SAFE_DEPOSIT_ID, ZERO_ADDRESS, ZERO_BYTES } from "../constants";
import { Deposit, RelayData } from "../interfaces";
import { toBytes32 } from "./AddressUtils";
import { keccak256 } from "./common";
import { BigNumber } from "./BigNumberUtils";
import { isMessageEmpty } from "./DepositUtils";

Expand Down Expand Up @@ -35,6 +34,27 @@ export function getDepositRelayData(deposit: Omit<Deposit, "messageHash">): Rela
* @returns The corresponding RelayData hash.
*/
export function getRelayDataHash(relayData: RelayData, destinationChainId: number): string {
const abi = [
{
type: "tuple",
components: [
{ type: "bytes32", name: "depositor" },
{ type: "bytes32", name: "recipient" },
{ type: "bytes32", name: "exclusiveRelayer" },
{ type: "bytes32", name: "inputToken" },
{ type: "bytes32", name: "outputToken" },
{ type: "uint256", name: "inputAmount" },
{ type: "uint256", name: "outputAmount" },
{ type: "uint256", name: "originChainId" },
{ type: "uint256", name: "depositId" },
{ type: "uint32", name: "fillDeadline" },
{ type: "uint32", name: "exclusivityDeadline" },
{ type: "bytes", name: "message" },
],
},
{ type: "uint256", name: "destinationChainId" },
];

const _relayData = {
...relayData,
depositor: toBytes32(relayData.depositor),
Expand All @@ -43,28 +63,8 @@ export function getRelayDataHash(relayData: RelayData, destinationChainId: numbe
outputToken: toBytes32(relayData.outputToken),
exclusiveRelayer: toBytes32(relayData.exclusiveRelayer),
};
return keccak256(
ethersUtils.defaultAbiCoder.encode(
[
"tuple(" +
"bytes32 depositor," +
"bytes32 recipient," +
"bytes32 exclusiveRelayer," +
"bytes32 inputToken," +
"bytes32 outputToken," +
"uint256 inputAmount," +
"uint256 outputAmount," +
"uint256 originChainId," +
"uint256 depositId," +
"uint32 fillDeadline," +
"uint32 exclusivityDeadline," +
"bytes message" +
")",
"uint256 destinationChainId",
],
[_relayData, destinationChainId]
)
);

return keccak256(encodeAbiParameters(abi, [_relayData, destinationChainId]));
}

export function getRelayHashFromEvent(e: RelayData & { destinationChainId: number }): string {
Expand All @@ -87,5 +87,5 @@ export function isZeroAddress(address: string): boolean {
}

export function getMessageHash(message: string): string {
return isMessageEmpty(message) ? ZERO_BYTES : keccak256(message);
return isMessageEmpty(message) ? ZERO_BYTES : keccak256(message as "0x{string}");
}