This repository was archived by the owner on Dec 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocator.ts
More file actions
58 lines (53 loc) · 1.82 KB
/
allocator.ts
File metadata and controls
58 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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}`;
}