teleton 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +230 -294
- package/dist/{bootstrap-DDFVEMYI.js → bootstrap-NNEI3Z5H.js} +3 -5
- package/dist/{chunk-OIMAE24Q.js → chunk-5LOHRZYY.js} +15 -4
- package/dist/{chunk-2ERTYRHA.js → chunk-G7PCW63M.js} +10 -10
- package/dist/chunk-JROBTXWY.js +908 -0
- package/dist/{chunk-7MWKT67G.js → chunk-LZQOX6YY.js} +13 -670
- package/dist/{chunk-AERHOXGC.js → chunk-NH2CNRKJ.js} +223 -91
- package/dist/{chunk-33Z47EXI.js → chunk-UMUONAD6.js} +11 -13
- package/dist/cli/index.js +13 -16
- package/dist/index.js +4 -5
- package/dist/{server-N4T7E25M.js → server-AJCOURH7.js} +4 -5
- package/dist/{server-JF6FX772.js → server-WWGVDFPW.js} +5 -6
- package/dist/{setup-server-IX3BFPPH.js → setup-server-VDY64CWW.js} +3 -3
- package/package.json +1 -1
- package/dist/chunk-AEHTQI3H.js +0 -142
- package/dist/chunk-FUNF6H4W.js +0 -251
package/dist/chunk-FUNF6H4W.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
COINGECKO_API_URL,
|
|
3
|
-
tonapiFetch
|
|
4
|
-
} from "./chunk-VFA7QMCZ.js";
|
|
5
|
-
import {
|
|
6
|
-
fetchWithTimeout
|
|
7
|
-
} from "./chunk-XQUHC3JZ.js";
|
|
8
|
-
import {
|
|
9
|
-
TELETON_ROOT
|
|
10
|
-
} from "./chunk-EYWNOHMJ.js";
|
|
11
|
-
import {
|
|
12
|
-
createLogger
|
|
13
|
-
} from "./chunk-NQ6FZKCE.js";
|
|
14
|
-
|
|
15
|
-
// src/ton/endpoint.ts
|
|
16
|
-
var ENDPOINT_CACHE_TTL_MS = 6e4;
|
|
17
|
-
var ORBS_HOST = "ton.access.orbs.network";
|
|
18
|
-
var ORBS_TOPOLOGY_URL = `https://${ORBS_HOST}/mngr/nodes?npm_version=2.3.3`;
|
|
19
|
-
var TONCENTER_URL = `https://toncenter.com/api/v2/jsonRPC`;
|
|
20
|
-
var _cache = null;
|
|
21
|
-
var _toncenterApiKey;
|
|
22
|
-
function setToncenterApiKey(key) {
|
|
23
|
-
_toncenterApiKey = key;
|
|
24
|
-
}
|
|
25
|
-
function getToncenterApiKey() {
|
|
26
|
-
return _toncenterApiKey;
|
|
27
|
-
}
|
|
28
|
-
async function discoverOrbsEndpoint() {
|
|
29
|
-
const res = await fetch(ORBS_TOPOLOGY_URL, { signal: AbortSignal.timeout(5e3) });
|
|
30
|
-
const nodes = await res.json();
|
|
31
|
-
const healthy = nodes.filter(
|
|
32
|
-
(n) => n.Healthy === "1" && n.Weight > 0 && n.Mngr?.health?.["v2-mainnet"]
|
|
33
|
-
);
|
|
34
|
-
if (healthy.length === 0) throw new Error("no healthy orbs nodes");
|
|
35
|
-
const totalWeight = healthy.reduce((sum, n) => sum + n.Weight, 0);
|
|
36
|
-
let r = Math.floor(Math.random() * totalWeight);
|
|
37
|
-
let chosen = healthy[0];
|
|
38
|
-
for (const node of healthy) {
|
|
39
|
-
r -= node.Weight;
|
|
40
|
-
if (r < 0) {
|
|
41
|
-
chosen = node;
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return `https://${ORBS_HOST}/${chosen.NodeId}/1/mainnet/toncenter-api-v2/jsonRPC`;
|
|
46
|
-
}
|
|
47
|
-
async function getCachedHttpEndpoint() {
|
|
48
|
-
if (_cache && Date.now() - _cache.ts < ENDPOINT_CACHE_TTL_MS) {
|
|
49
|
-
return _cache.url;
|
|
50
|
-
}
|
|
51
|
-
let url;
|
|
52
|
-
if (_toncenterApiKey) {
|
|
53
|
-
try {
|
|
54
|
-
const testUrl = `https://toncenter.com/api/v2/getAddressInformation?address=EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c`;
|
|
55
|
-
const res = await fetch(testUrl, {
|
|
56
|
-
headers: { "X-API-Key": _toncenterApiKey },
|
|
57
|
-
signal: AbortSignal.timeout(5e3)
|
|
58
|
-
});
|
|
59
|
-
if (!res.ok) throw new Error(`TonCenter ${res.status}`);
|
|
60
|
-
url = TONCENTER_URL;
|
|
61
|
-
} catch {
|
|
62
|
-
try {
|
|
63
|
-
url = await discoverOrbsEndpoint();
|
|
64
|
-
} catch {
|
|
65
|
-
url = TONCENTER_URL;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
} else {
|
|
69
|
-
try {
|
|
70
|
-
url = await discoverOrbsEndpoint();
|
|
71
|
-
} catch {
|
|
72
|
-
url = TONCENTER_URL;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
_cache = { url, ts: Date.now() };
|
|
76
|
-
return url;
|
|
77
|
-
}
|
|
78
|
-
function invalidateEndpointCache() {
|
|
79
|
-
_cache = null;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// src/ton/wallet-service.ts
|
|
83
|
-
import { mnemonicNew, mnemonicToPrivateKey, mnemonicValidate } from "@ton/crypto";
|
|
84
|
-
import { WalletContractV5R1, TonClient, fromNano } from "@ton/ton";
|
|
85
|
-
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
|
|
86
|
-
import { join, dirname } from "path";
|
|
87
|
-
var log = createLogger("TON");
|
|
88
|
-
var WALLET_FILE = join(TELETON_ROOT, "wallet.json");
|
|
89
|
-
var _walletCache;
|
|
90
|
-
var _keyPairCache = null;
|
|
91
|
-
var _tonClientCache = null;
|
|
92
|
-
async function generateWallet() {
|
|
93
|
-
const mnemonic = await mnemonicNew(24);
|
|
94
|
-
const keyPair = await mnemonicToPrivateKey(mnemonic);
|
|
95
|
-
const wallet = WalletContractV5R1.create({
|
|
96
|
-
workchain: 0,
|
|
97
|
-
publicKey: keyPair.publicKey
|
|
98
|
-
});
|
|
99
|
-
const address = wallet.address.toString({ bounceable: true, testOnly: false });
|
|
100
|
-
return {
|
|
101
|
-
version: "w5r1",
|
|
102
|
-
address,
|
|
103
|
-
publicKey: keyPair.publicKey.toString("hex"),
|
|
104
|
-
mnemonic,
|
|
105
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function saveWallet(wallet) {
|
|
109
|
-
const dir = dirname(WALLET_FILE);
|
|
110
|
-
if (!existsSync(dir)) {
|
|
111
|
-
mkdirSync(dir, { recursive: true });
|
|
112
|
-
}
|
|
113
|
-
writeFileSync(WALLET_FILE, JSON.stringify(wallet, null, 2), { encoding: "utf-8", mode: 384 });
|
|
114
|
-
_walletCache = void 0;
|
|
115
|
-
_keyPairCache = null;
|
|
116
|
-
}
|
|
117
|
-
function loadWallet() {
|
|
118
|
-
if (_walletCache !== void 0) return _walletCache;
|
|
119
|
-
if (!existsSync(WALLET_FILE)) {
|
|
120
|
-
_walletCache = null;
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
try {
|
|
124
|
-
const content = readFileSync(WALLET_FILE, "utf-8");
|
|
125
|
-
const parsed = JSON.parse(content);
|
|
126
|
-
if (!parsed.mnemonic || !Array.isArray(parsed.mnemonic) || parsed.mnemonic.length !== 24) {
|
|
127
|
-
throw new Error("Invalid wallet.json: mnemonic must be a 24-word array");
|
|
128
|
-
}
|
|
129
|
-
_walletCache = parsed;
|
|
130
|
-
return _walletCache;
|
|
131
|
-
} catch (error) {
|
|
132
|
-
log.error({ err: error }, "Failed to load wallet");
|
|
133
|
-
_walletCache = null;
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
function walletExists() {
|
|
138
|
-
return existsSync(WALLET_FILE);
|
|
139
|
-
}
|
|
140
|
-
async function importWallet(mnemonic) {
|
|
141
|
-
const valid = await mnemonicValidate(mnemonic);
|
|
142
|
-
if (!valid) {
|
|
143
|
-
throw new Error("Invalid mnemonic: words do not form a valid TON seed phrase");
|
|
144
|
-
}
|
|
145
|
-
const keyPair = await mnemonicToPrivateKey(mnemonic);
|
|
146
|
-
const wallet = WalletContractV5R1.create({
|
|
147
|
-
workchain: 0,
|
|
148
|
-
publicKey: keyPair.publicKey
|
|
149
|
-
});
|
|
150
|
-
const address = wallet.address.toString({ bounceable: true, testOnly: false });
|
|
151
|
-
return {
|
|
152
|
-
version: "w5r1",
|
|
153
|
-
address,
|
|
154
|
-
publicKey: keyPair.publicKey.toString("hex"),
|
|
155
|
-
mnemonic,
|
|
156
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
function getWalletAddress() {
|
|
160
|
-
const wallet = loadWallet();
|
|
161
|
-
return wallet?.address || null;
|
|
162
|
-
}
|
|
163
|
-
async function getCachedTonClient() {
|
|
164
|
-
const endpoint = await getCachedHttpEndpoint();
|
|
165
|
-
if (_tonClientCache && _tonClientCache.endpoint === endpoint) {
|
|
166
|
-
return _tonClientCache.client;
|
|
167
|
-
}
|
|
168
|
-
const apiKey = getToncenterApiKey();
|
|
169
|
-
const client = new TonClient({ endpoint, ...apiKey && { apiKey } });
|
|
170
|
-
_tonClientCache = { client, endpoint };
|
|
171
|
-
return client;
|
|
172
|
-
}
|
|
173
|
-
function invalidateTonClientCache() {
|
|
174
|
-
_tonClientCache = null;
|
|
175
|
-
invalidateEndpointCache();
|
|
176
|
-
}
|
|
177
|
-
async function getKeyPair() {
|
|
178
|
-
if (_keyPairCache) return _keyPairCache;
|
|
179
|
-
const wallet = loadWallet();
|
|
180
|
-
if (!wallet) return null;
|
|
181
|
-
_keyPairCache = await mnemonicToPrivateKey(wallet.mnemonic);
|
|
182
|
-
return _keyPairCache;
|
|
183
|
-
}
|
|
184
|
-
async function getWalletBalance(address) {
|
|
185
|
-
try {
|
|
186
|
-
const client = await getCachedTonClient();
|
|
187
|
-
const { Address } = await import("@ton/core");
|
|
188
|
-
const addressObj = Address.parse(address);
|
|
189
|
-
const balance = await client.getBalance(addressObj);
|
|
190
|
-
const balanceFormatted = fromNano(balance);
|
|
191
|
-
return {
|
|
192
|
-
balance: balanceFormatted,
|
|
193
|
-
balanceNano: balance.toString()
|
|
194
|
-
};
|
|
195
|
-
} catch (error) {
|
|
196
|
-
log.error({ err: error }, "Failed to get balance");
|
|
197
|
-
return null;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
var TON_PRICE_CACHE_TTL_MS = 3e4;
|
|
201
|
-
var _tonPriceCache = null;
|
|
202
|
-
async function getTonPrice() {
|
|
203
|
-
if (_tonPriceCache && Date.now() - _tonPriceCache.timestamp < TON_PRICE_CACHE_TTL_MS) {
|
|
204
|
-
return { ..._tonPriceCache };
|
|
205
|
-
}
|
|
206
|
-
try {
|
|
207
|
-
const response = await tonapiFetch(`/rates?tokens=ton¤cies=usd`);
|
|
208
|
-
if (response.ok) {
|
|
209
|
-
const data = await response.json();
|
|
210
|
-
const price = data?.rates?.TON?.prices?.USD;
|
|
211
|
-
if (typeof price === "number" && price > 0) {
|
|
212
|
-
_tonPriceCache = { usd: price, source: "TonAPI", timestamp: Date.now() };
|
|
213
|
-
return _tonPriceCache;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
} catch {
|
|
217
|
-
}
|
|
218
|
-
try {
|
|
219
|
-
const response = await fetchWithTimeout(
|
|
220
|
-
`${COINGECKO_API_URL}/simple/price?ids=the-open-network&vs_currencies=usd`
|
|
221
|
-
);
|
|
222
|
-
if (!response.ok) {
|
|
223
|
-
throw new Error(`CoinGecko API error: ${response.status}`);
|
|
224
|
-
}
|
|
225
|
-
const data = await response.json();
|
|
226
|
-
const price = data["the-open-network"]?.usd;
|
|
227
|
-
if (typeof price === "number" && price > 0) {
|
|
228
|
-
_tonPriceCache = { usd: price, source: "CoinGecko", timestamp: Date.now() };
|
|
229
|
-
return _tonPriceCache;
|
|
230
|
-
}
|
|
231
|
-
} catch (error) {
|
|
232
|
-
log.error({ err: error }, "Failed to get TON price");
|
|
233
|
-
}
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export {
|
|
238
|
-
setToncenterApiKey,
|
|
239
|
-
invalidateEndpointCache,
|
|
240
|
-
generateWallet,
|
|
241
|
-
saveWallet,
|
|
242
|
-
loadWallet,
|
|
243
|
-
walletExists,
|
|
244
|
-
importWallet,
|
|
245
|
-
getWalletAddress,
|
|
246
|
-
getCachedTonClient,
|
|
247
|
-
invalidateTonClientCache,
|
|
248
|
-
getKeyPair,
|
|
249
|
-
getWalletBalance,
|
|
250
|
-
getTonPrice
|
|
251
|
-
};
|