|
| 1 | +import { Hex, Address, encodePacked, keccak256 } from "viem"; |
| 2 | + |
| 3 | +export interface SubmitWithdrawRequest { |
| 4 | + chainId: string; // chainId of the destination chain on which the user will withdraw |
| 5 | + depository: string; // address of the depository account |
| 6 | + currency: string; |
| 7 | + amount: string; // Amount to withdraw |
| 8 | + spender: string; // address of the account that owns the balance in the Hub contract (can be an alias) |
| 9 | + receiver: string; // Address of the account on the destination chain |
| 10 | + data: string; // add tional data |
| 11 | + nonce: string; // Nonce for replay protection |
| 12 | +} |
| 13 | + |
| 14 | +export const getSubmitWithdrawRequestHash = ( |
| 15 | + request: SubmitWithdrawRequest |
| 16 | +) => { |
| 17 | + // EIP712 type from RelayAllocator |
| 18 | + const PAYLOAD_TYPEHASH = keccak256( |
| 19 | + "SubmitWithdrawRequest(uint256 chainId,string depository,string currency,uint256 amount,address spender,string receiver,bytes data,bytes32 nonce)" as Hex |
| 20 | + ); |
| 21 | + |
| 22 | + // Create EIP712 digest |
| 23 | + const digest = keccak256( |
| 24 | + encodePacked( |
| 25 | + [ |
| 26 | + "bytes32", |
| 27 | + "uint256", |
| 28 | + "bytes32", |
| 29 | + "bytes32", |
| 30 | + "uint256", |
| 31 | + "address", |
| 32 | + "bytes32", |
| 33 | + "bytes32", |
| 34 | + "bytes32", |
| 35 | + ], |
| 36 | + [ |
| 37 | + PAYLOAD_TYPEHASH, |
| 38 | + BigInt(request.chainId), |
| 39 | + keccak256(request.depository as Hex), |
| 40 | + keccak256(request.currency as Hex), |
| 41 | + BigInt(request.amount), |
| 42 | + request.spender as Address, |
| 43 | + keccak256(request.receiver as Hex), |
| 44 | + keccak256(request.data as Hex), |
| 45 | + request.nonce as Hex, |
| 46 | + ] |
| 47 | + ) |
| 48 | + ); |
| 49 | + |
| 50 | + // The withdrawal address is the digest itself (as a hex string) |
| 51 | + return digest; |
| 52 | +}; |
| 53 | + |
| 54 | +export function getWithdrawalAddress(request: SubmitWithdrawRequest): string { |
| 55 | + const withdrawalHash = getSubmitWithdrawRequestHash(request); |
| 56 | + const withdrawalAddress = withdrawalHash.slice(2).slice(-40); |
| 57 | + return `0x${withdrawalAddress}` as `0x${string}`; |
| 58 | +} |
0 commit comments