diff --git a/package.json b/package.json index e91c61c..557674a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@reservoir0x/relay-protocol-sdk", - "version": "0.0.56", + "version": "0.0.57", "description": "Relay protocol SDK", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index a3ceab7..cb17687 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,6 +60,12 @@ import { getVmTypeNativeCurrency, } from "./utils"; +import { + SubmitWithdrawRequest, + getSubmitWithdrawRequestHash, + getWithdrawalAddress, +} from "./messages/v2.2/allocator"; + export { // Order Order, @@ -119,4 +125,9 @@ export { getExecutionMessageId, encodeAction, decodeAction, + + // Allocator + SubmitWithdrawRequest, + getSubmitWithdrawRequestHash, + getWithdrawalAddress, }; diff --git a/src/messages/v2.2/allocator.ts b/src/messages/v2.2/allocator.ts new file mode 100644 index 0000000..5d0bc40 --- /dev/null +++ b/src/messages/v2.2/allocator.ts @@ -0,0 +1,58 @@ +import { Hex, Address, encodePacked, keccak256 } from "viem"; + +export interface SubmitWithdrawRequest { + chainId: string; // chainId of the destination chain on which the user will withdraw + depository: string; // address of the depository account + currency: string; + amount: string; // Amount to withdraw + spender: string; // address of the account that owns the balance in the Hub contract (can be an alias) + receiver: string; // Address of the account on the destination chain + data: string; // add tional data + nonce: string; // Nonce for replay protection +} + +export const getSubmitWithdrawRequestHash = ( + request: SubmitWithdrawRequest +) => { + // EIP712 type from RelayAllocator + const PAYLOAD_TYPEHASH = keccak256( + "SubmitWithdrawRequest(uint256 chainId,string depository,string currency,uint256 amount,address spender,string receiver,bytes data,bytes32 nonce)" as Hex + ); + + // Create EIP712 digest + const digest = keccak256( + encodePacked( + [ + "bytes32", + "uint256", + "bytes32", + "bytes32", + "uint256", + "address", + "bytes32", + "bytes32", + "bytes32", + ], + [ + PAYLOAD_TYPEHASH, + BigInt(request.chainId), + keccak256(request.depository as Hex), + keccak256(request.currency as Hex), + BigInt(request.amount), + request.spender as Address, + keccak256(request.receiver as Hex), + keccak256(request.data as Hex), + request.nonce as Hex, + ] + ) + ); + + // The withdrawal address is the digest itself (as a hex string) + return digest; +}; + +export function getWithdrawalAddress(request: SubmitWithdrawRequest): string { + const withdrawalHash = getSubmitWithdrawRequestHash(request); + const withdrawalAddress = withdrawalHash.slice(2).slice(-40); + return `0x${withdrawalAddress}` as `0x${string}`; +}