|
| 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 type WithdrawalAddressParams = { |
| 55 | + depositoryAddress: string; |
| 56 | + depositoryChainId: bigint; |
| 57 | + currency: string; |
| 58 | + recipientAddress: string; |
| 59 | + owner: string; |
| 60 | + ownerChainId: string; |
| 61 | + amount: bigint; |
| 62 | + withdrawalNonce: string; |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Compute deterministic withdrawal address |
| 67 | + * |
| 68 | + * @param depositoryAddress the depository contract holding the funds on origin chain |
| 69 | + * @param depositoryChainId the chain id of the depository contract currently holding the funds |
| 70 | + * @param currency the id of the currency as expressed on origin chain (string) |
| 71 | + * @param recipientAddress the address that will receive the withdrawn funds on destination chain |
| 72 | + * @param owner the address that owns the balance before the withdrawal is initiated |
| 73 | + * @param amount the balance to withdraw |
| 74 | + * @param withdrawalNonce nonce to prevent collisions for similar withdrawals |
| 75 | + * @returns withdrawal address (in lower case) |
| 76 | + */ |
| 77 | +export function getWithdrawalAddress( |
| 78 | + withdrawalParams: WithdrawalAddressParams |
| 79 | +): string { |
| 80 | + // pack and hash data |
| 81 | + const nonce = keccak256( |
| 82 | + encodePacked(["string"], [withdrawalParams.withdrawalNonce]) |
| 83 | + ); |
| 84 | + const hash = keccak256( |
| 85 | + encodePacked( |
| 86 | + [ |
| 87 | + "address", |
| 88 | + "uint256", |
| 89 | + "string", |
| 90 | + "address", |
| 91 | + "address", |
| 92 | + "uint256", |
| 93 | + "bytes32", |
| 94 | + ], |
| 95 | + [ |
| 96 | + withdrawalParams.depositoryAddress as `0x${string}`, |
| 97 | + withdrawalParams.depositoryChainId, |
| 98 | + withdrawalParams.currency, |
| 99 | + withdrawalParams.recipientAddress as `0x${string}`, |
| 100 | + withdrawalParams.owner as `0x${string}`, |
| 101 | + withdrawalParams.amount, |
| 102 | + nonce, |
| 103 | + ] |
| 104 | + ) |
| 105 | + ); |
| 106 | + |
| 107 | + // get 40 bytes for an address |
| 108 | + const withdrawalAddress = hash.slice(2).slice(-40).toLowerCase(); |
| 109 | + return `0x${withdrawalAddress}` as `0x${string}`; |
| 110 | +} |
| 111 | + |
| 112 | +// for oracle requests, we replace the hub chain id by a slug (e.g. 'base') |
| 113 | +// and we pass the amount as a string |
| 114 | +export type WithdrawalAddressRequest = Omit< |
| 115 | + WithdrawalAddressParams, |
| 116 | + "depositoryChainId" | "amount" |
| 117 | +> & { |
| 118 | + depositoryChainSlug: string; |
| 119 | + amount: string; |
| 120 | +}; |
| 121 | + |
| 122 | +// types for oracle routes |
| 123 | +export type WithdrawalInitiationMessage = { |
| 124 | + data: WithdrawalAddressRequest & { settlementChainId: string }; |
| 125 | + result: { |
| 126 | + withdrawalAddress: string; |
| 127 | + }; |
| 128 | +}; |
| 129 | + |
| 130 | +export type WithdrawalInitiatedMessage = { |
| 131 | + data: WithdrawalAddressRequest & { |
| 132 | + settlementChainId: string; |
| 133 | + }; |
| 134 | + result: { |
| 135 | + proofOfWithdrawalAddressBalance: string; |
| 136 | + withdrawalAddress: string; |
| 137 | + }; |
| 138 | +}; |
0 commit comments