1
2
3import { createHash } from "node:crypto";
4
5export const DEFAULTS = {
6 ethRpc: "https://ethereum-rpc.publicnode.com",
7 trongrid: "https://api.trongrid.io",
8};
9
10export const CONTRACTS = {
11 usdtEthereum: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
12 usdcEthereum: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
13 usdtTron: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
14};
15
16
17const SEL_USDT_IS_BLACKLISTED = "0xe47d6060";
18const SEL_USDC_IS_BLACKLISTED = "0xfe575a87";
19
20const EVM_RE = /^0x[0-9a-fA-F]{40}$/;
21const TRON_RE = /^T[1-9A-HJ-NP-Za-km-z]{33}$/;
22
23export const isEvmAddress = (a) => EVM_RE.test(a);
24export const isTronAddress = (a) => TRON_RE.test(a);
25
26
27async function rpc(url, method, params) {
28 const res = await fetch(url, {
29 method: "POST",
30 headers: { "content-type": "application/json" },
31 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
32 });
33 if (!res.ok) throw new Error(`${method}: HTTP ${res.status}`);
34 const j = await res.json();
35 if (j.error) throw new Error(`${method}: ${j.error.message}`);
36 return j.result;
37}
38
39const word = (addr) => addr.toLowerCase().replace(/^0x/, "").padStart(64, "0");
40
41async function readFlag(url, token, selector, address) {
42 if (!isEvmAddress(address)) throw new Error("expected a 0x... address with 40 hex characters");
43 const out = await rpc(url, "eth_call", [{ to: token, data: selector + word(address) }, "latest"]);
44 return BigInt(out) === 1n;
45}
46
47
48export function usdtFrozenOnEthereum(address, opts = {}) {
49 return readFlag(opts.ethRpc || DEFAULTS.ethRpc, CONTRACTS.usdtEthereum, SEL_USDT_IS_BLACKLISTED, address);
50}
51
52
53export function usdcFrozenOnEthereum(address, opts = {}) {
54 return readFlag(opts.ethRpc || DEFAULTS.ethRpc, CONTRACTS.usdcEthereum, SEL_USDC_IS_BLACKLISTED, address);
55}
56
57
58const B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
59const sha256 = (b) => createHash("sha256").update(b).digest();
60
61
62export function tronToHex20(address) {
63 let n = 0n;
64 for (const c of address) {
65 const i = B58.indexOf(c);
66 if (i < 0) throw new Error("not a base58 address");
67 n = n * 58n + BigInt(i);
68 }
69 const bytes = Buffer.from(n.toString(16).padStart(50, "0"), "hex");
70 const payload = bytes.subarray(0, 21);
71 const ok = payload[0] === 0x41 && sha256(sha256(payload)).subarray(0, 4).equals(bytes.subarray(21));
72 if (!ok) throw new Error("invalid Tron address (checksum)");
73 return payload.subarray(1).toString("hex");
74}
75
76
77export async function usdtFrozenOnTron(address, opts = {}) {
78 if (!isTronAddress(address)) throw new Error("expected a T... Tron address");
79 const headers = { "content-type": "application/json" };
80 if (opts.tronApiKey) headers["TRON-PRO-API-KEY"] = opts.tronApiKey;
81 const res = await fetch(`${opts.trongrid || DEFAULTS.trongrid}/wallet/triggerconstantcontract`, {
82 method: "POST",
83 headers,
84 body: JSON.stringify({
85 owner_address: CONTRACTS.usdtTron,
86 contract_address: CONTRACTS.usdtTron,
87 function_selector: "getBlackListStatus(address)",
88 parameter: tronToHex20(address).padStart(64, "0"),
89 visible: true,
90 }),
91 });
92 if (!res.ok) throw new Error(`TronGrid: HTTP ${res.status}`);
93 const j = await res.json();
94 if (!j.result?.result) throw new Error("TronGrid: " + JSON.stringify(j).slice(0, 200));
95 return BigInt("0x" + j.constant_result[0]) === 1n;
96}
97
98
99
100
101
102
103export async function checkAddress(address, opts = {}) {
104 const a = String(address).trim();
105 if (isEvmAddress(a)) {
106 const [usdt, usdc] = await Promise.all([usdtFrozenOnEthereum(a, opts), usdcFrozenOnEthereum(a, opts)]);
107 return { address: a, chain: "ethereum", usdt, usdc };
108 }
109 if (isTronAddress(a)) {
110 return { address: a, chain: "tron", usdt: await usdtFrozenOnTron(a, opts) };
111 }
112 throw new Error("pass a 0x... (Ethereum) or T... (Tron) address");
113}