@blazium/ton-connect-mobile 1.2.4 → 1.2.6
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 +7 -20
- package/dist/adapters/react-native.js +8 -1
- package/dist/core/bridge.d.ts +61 -0
- package/dist/core/bridge.js +237 -0
- package/dist/core/crypto.d.ts +8 -19
- package/dist/core/crypto.js +15 -141
- package/dist/core/index.d.ts +5 -3
- package/dist/core/index.js +20 -17
- package/dist/core/protocol.d.ts +35 -32
- package/dist/core/protocol.js +109 -285
- package/dist/core/session.d.ts +65 -0
- package/dist/core/session.js +235 -0
- package/dist/core/wallets.d.ts +6 -6
- package/dist/core/wallets.js +17 -18
- package/dist/index.d.ts +33 -72
- package/dist/index.js +322 -769
- package/dist/react/TonConnectUIProvider.d.ts +4 -52
- package/dist/react/TonConnectUIProvider.js +18 -122
- package/dist/react/index.d.ts +1 -2
- package/dist/react/index.js +0 -1
- package/dist/types/index.d.ts +84 -139
- package/dist/types/index.js +1 -1
- package/package.json +2 -3
- package/src/adapters/react-native.ts +7 -1
- package/src/core/bridge.ts +307 -0
- package/src/core/crypto.ts +62 -238
- package/src/core/index.ts +17 -7
- package/src/core/protocol.ts +217 -441
- package/src/core/session.ts +247 -0
- package/src/core/wallets.ts +90 -93
- package/src/index.ts +811 -1338
- package/src/react/TonConnectUIProvider.tsx +272 -441
- package/src/react/index.ts +23 -27
- package/src/types/index.ts +217 -272
|
@@ -1,441 +1,272 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React integration layer for
|
|
3
|
-
* Provides TonConnectUIProvider, hooks, and components compatible with @tonconnect/ui-react API
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import React, { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
|
7
|
-
import { TonConnectMobile, ConnectionStatus, WalletInfo, SendTransactionRequest, WalletDefinition, Network, BalanceResponse, TransactionStatusResponse } from '../index';
|
|
8
|
-
import type { TonConnectMobileConfig, TonConnectEventType, TonConnectEventListener } from '../types';
|
|
9
|
-
import { WalletSelectionModal } from './WalletSelectionModal';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Account information (compatible with @tonconnect/ui-react)
|
|
13
|
-
*/
|
|
14
|
-
export interface Account {
|
|
15
|
-
address: string;
|
|
16
|
-
chain: number;
|
|
17
|
-
publicKey?: string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Wallet state (compatible with @tonconnect/ui-react)
|
|
22
|
-
*/
|
|
23
|
-
export interface WalletState {
|
|
24
|
-
account: Account | null;
|
|
25
|
-
wallet: WalletInfo | null;
|
|
26
|
-
connected: boolean;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Transaction response
|
|
31
|
-
*/
|
|
32
|
-
export interface TransactionResponse {
|
|
33
|
-
boc: string;
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}, []);
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const response = await sdk.signData(request.data, request.version);
|
|
276
|
-
return {
|
|
277
|
-
signature: response.signature,
|
|
278
|
-
timestamp: response.timestamp,
|
|
279
|
-
};
|
|
280
|
-
} catch (error) {
|
|
281
|
-
console.error('[TonConnectUIProvider] Sign data error:', error);
|
|
282
|
-
throw error;
|
|
283
|
-
}
|
|
284
|
-
},
|
|
285
|
-
[sdk]
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
// Restore connection from stored session
|
|
289
|
-
const restoreConnection = useCallback(async (): Promise<void> => {
|
|
290
|
-
try {
|
|
291
|
-
// SDK automatically loads session on initialization
|
|
292
|
-
// This method triggers a re-check of the stored session
|
|
293
|
-
const status = sdk.getStatus();
|
|
294
|
-
if (status.connected && status.wallet) {
|
|
295
|
-
updateWalletState(status);
|
|
296
|
-
}
|
|
297
|
-
} catch (error) {
|
|
298
|
-
console.error('[TonConnectUIProvider] Restore connection error:', error);
|
|
299
|
-
throw error;
|
|
300
|
-
}
|
|
301
|
-
}, [sdk, updateWalletState]);
|
|
302
|
-
|
|
303
|
-
// Set wallet list (customize available wallets)
|
|
304
|
-
const setWalletList = useCallback((wallets: WalletDefinition[]): void => {
|
|
305
|
-
if (!wallets || !Array.isArray(wallets)) {
|
|
306
|
-
throw new Error('Wallet list must be an array');
|
|
307
|
-
}
|
|
308
|
-
setCustomWalletList(wallets);
|
|
309
|
-
}, []);
|
|
310
|
-
|
|
311
|
-
// Get network
|
|
312
|
-
const getNetwork = useCallback((): Network => {
|
|
313
|
-
return sdk.getNetwork();
|
|
314
|
-
}, [sdk]);
|
|
315
|
-
|
|
316
|
-
// Set network
|
|
317
|
-
const setNetwork = useCallback((network: Network): void => {
|
|
318
|
-
sdk.setNetwork(network);
|
|
319
|
-
// Update wallet state to reflect new chain ID
|
|
320
|
-
const status = sdk.getStatus();
|
|
321
|
-
updateWalletState(status);
|
|
322
|
-
}, [sdk, updateWalletState]);
|
|
323
|
-
|
|
324
|
-
// Get balance
|
|
325
|
-
const getBalance = useCallback(async (address?: string): Promise<BalanceResponse> => {
|
|
326
|
-
return await sdk.getBalance(address);
|
|
327
|
-
}, [sdk]);
|
|
328
|
-
|
|
329
|
-
// Get transaction status
|
|
330
|
-
const getTransactionStatus = useCallback(async (
|
|
331
|
-
boc: string,
|
|
332
|
-
maxAttempts: number = 10,
|
|
333
|
-
intervalMs: number = 2000
|
|
334
|
-
): Promise<TransactionStatusResponse> => {
|
|
335
|
-
return await sdk.getTransactionStatus(boc, maxAttempts, intervalMs);
|
|
336
|
-
}, [sdk]);
|
|
337
|
-
|
|
338
|
-
// Get transaction status by hash
|
|
339
|
-
const getTransactionStatusByHash = useCallback(async (
|
|
340
|
-
txHash: string,
|
|
341
|
-
address: string
|
|
342
|
-
): Promise<TransactionStatusResponse> => {
|
|
343
|
-
return await sdk.getTransactionStatusByHash(txHash, address);
|
|
344
|
-
}, [sdk]);
|
|
345
|
-
|
|
346
|
-
// Event listeners
|
|
347
|
-
const on = useCallback(<T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>): (() => void) => {
|
|
348
|
-
return sdk.on(event, listener);
|
|
349
|
-
}, [sdk]);
|
|
350
|
-
|
|
351
|
-
const off = useCallback(<T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>): void => {
|
|
352
|
-
sdk.off(event, listener);
|
|
353
|
-
}, [sdk]);
|
|
354
|
-
|
|
355
|
-
// Create TonConnectUI instance
|
|
356
|
-
const tonConnectUI: TonConnectUI = {
|
|
357
|
-
openModal,
|
|
358
|
-
closeModal,
|
|
359
|
-
connectWallet,
|
|
360
|
-
disconnect,
|
|
361
|
-
sendTransaction,
|
|
362
|
-
signData,
|
|
363
|
-
restoreConnection,
|
|
364
|
-
setWalletList,
|
|
365
|
-
getNetwork,
|
|
366
|
-
setNetwork,
|
|
367
|
-
getBalance,
|
|
368
|
-
getTransactionStatus,
|
|
369
|
-
getTransactionStatusByHash,
|
|
370
|
-
on,
|
|
371
|
-
off,
|
|
372
|
-
wallet: walletState,
|
|
373
|
-
modalState: {
|
|
374
|
-
open: modalOpen,
|
|
375
|
-
},
|
|
376
|
-
uiVersion: '1.0.0',
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
const contextValue: TonConnectUIContextValue = {
|
|
380
|
-
tonConnectUI,
|
|
381
|
-
sdk,
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
return (
|
|
385
|
-
<TonConnectUIContext.Provider value={contextValue}>
|
|
386
|
-
{children}
|
|
387
|
-
{/* Auto-show wallet selection modal when modalOpen is true */}
|
|
388
|
-
<WalletSelectionModal
|
|
389
|
-
visible={modalOpen && !walletState?.connected}
|
|
390
|
-
onClose={closeModal}
|
|
391
|
-
wallets={customWalletList || undefined}
|
|
392
|
-
/>
|
|
393
|
-
</TonConnectUIContext.Provider>
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Hook to access TonConnectUI instance
|
|
399
|
-
* Compatible with @tonconnect/ui-react useTonConnectUI hook
|
|
400
|
-
*/
|
|
401
|
-
export function useTonConnectUI(): TonConnectUI {
|
|
402
|
-
const context = useContext(TonConnectUIContext);
|
|
403
|
-
if (!context) {
|
|
404
|
-
throw new Error('useTonConnectUI must be used within TonConnectUIProvider');
|
|
405
|
-
}
|
|
406
|
-
return context.tonConnectUI;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* Hook to access wallet state
|
|
411
|
-
* Compatible with @tonconnect/ui-react useTonWallet hook
|
|
412
|
-
*/
|
|
413
|
-
export function useTonWallet(): WalletState | null {
|
|
414
|
-
const tonConnectUI = useTonConnectUI();
|
|
415
|
-
return tonConnectUI.wallet;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* Hook to access modal state
|
|
420
|
-
* Compatible with @tonconnect/ui-react useTonConnectModal hook
|
|
421
|
-
*/
|
|
422
|
-
export function useTonConnectModal(): { open: boolean; close: () => void; openModal: () => Promise<void> } {
|
|
423
|
-
const tonConnectUI = useTonConnectUI();
|
|
424
|
-
return {
|
|
425
|
-
open: tonConnectUI.modalState.open,
|
|
426
|
-
close: tonConnectUI.closeModal,
|
|
427
|
-
openModal: tonConnectUI.openModal,
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Hook to access SDK instance (for advanced usage)
|
|
433
|
-
*/
|
|
434
|
-
export function useTonConnectSDK(): TonConnectMobile {
|
|
435
|
-
const context = useContext(TonConnectUIContext);
|
|
436
|
-
if (!context) {
|
|
437
|
-
throw new Error('useTonConnectSDK must be used within TonConnectUIProvider');
|
|
438
|
-
}
|
|
439
|
-
return context.sdk;
|
|
440
|
-
}
|
|
441
|
-
|
|
1
|
+
/**
|
|
2
|
+
* React integration layer for TON Connect Mobile SDK
|
|
3
|
+
* Provides TonConnectUIProvider, hooks, and components compatible with @tonconnect/ui-react API
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
|
7
|
+
import { TonConnectMobile, ConnectionStatus, WalletInfo, SendTransactionRequest, WalletDefinition, Network, BalanceResponse, TransactionStatusResponse } from '../index';
|
|
8
|
+
import type { TonConnectMobileConfig, TonConnectEventType, TonConnectEventListener } from '../types';
|
|
9
|
+
import { WalletSelectionModal } from './WalletSelectionModal';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Account information (compatible with @tonconnect/ui-react)
|
|
13
|
+
*/
|
|
14
|
+
export interface Account {
|
|
15
|
+
address: string;
|
|
16
|
+
chain: number;
|
|
17
|
+
publicKey?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Wallet state (compatible with @tonconnect/ui-react)
|
|
22
|
+
*/
|
|
23
|
+
export interface WalletState {
|
|
24
|
+
account: Account | null;
|
|
25
|
+
wallet: WalletInfo | null;
|
|
26
|
+
connected: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Transaction response
|
|
31
|
+
*/
|
|
32
|
+
export interface TransactionResponse {
|
|
33
|
+
boc: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* TonConnect UI instance interface
|
|
38
|
+
*/
|
|
39
|
+
export interface TonConnectUI {
|
|
40
|
+
openModal: () => Promise<void>;
|
|
41
|
+
closeModal: () => void;
|
|
42
|
+
connectWallet: () => Promise<void>;
|
|
43
|
+
disconnect: () => Promise<void>;
|
|
44
|
+
sendTransaction: (transaction: SendTransactionRequest) => Promise<TransactionResponse>;
|
|
45
|
+
restoreConnection: () => Promise<void>;
|
|
46
|
+
setWalletList: (wallets: WalletDefinition[]) => void;
|
|
47
|
+
getNetwork: () => Network;
|
|
48
|
+
setNetwork: (network: Network) => void;
|
|
49
|
+
getBalance: (address?: string) => Promise<BalanceResponse>;
|
|
50
|
+
getTransactionStatusByHash: (txHash: string, address: string) => Promise<TransactionStatusResponse>;
|
|
51
|
+
on: <T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>) => () => void;
|
|
52
|
+
off: <T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>) => void;
|
|
53
|
+
wallet: WalletState | null;
|
|
54
|
+
modalState: { open: boolean };
|
|
55
|
+
uiVersion: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface TonConnectUIContextValue {
|
|
59
|
+
tonConnectUI: TonConnectUI;
|
|
60
|
+
sdk: TonConnectMobile;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const TonConnectUIContext = createContext<TonConnectUIContextValue | null>(null);
|
|
64
|
+
|
|
65
|
+
export interface TonConnectUIProviderProps {
|
|
66
|
+
config: TonConnectMobileConfig;
|
|
67
|
+
children: ReactNode;
|
|
68
|
+
sdkInstance?: TonConnectMobile;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* TonConnectUIProvider - React context provider for TON Connect
|
|
73
|
+
*/
|
|
74
|
+
export function TonConnectUIProvider({
|
|
75
|
+
config,
|
|
76
|
+
children,
|
|
77
|
+
sdkInstance,
|
|
78
|
+
}: TonConnectUIProviderProps): JSX.Element {
|
|
79
|
+
const [sdk] = useState<TonConnectMobile>(() => {
|
|
80
|
+
if (sdkInstance) return sdkInstance;
|
|
81
|
+
return new TonConnectMobile(config);
|
|
82
|
+
});
|
|
83
|
+
const [walletState, setWalletState] = useState<WalletState | null>(null);
|
|
84
|
+
const [modalOpen, setModalOpen] = useState(false);
|
|
85
|
+
const [isConnecting, setIsConnecting] = useState(false);
|
|
86
|
+
const [customWalletList, setCustomWalletList] = useState<WalletDefinition[] | null>(null);
|
|
87
|
+
|
|
88
|
+
const getChainId = useCallback((network: Network): number => {
|
|
89
|
+
return network === 'testnet' ? -3 : -239;
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
const updateWalletState = useCallback((status: ConnectionStatus) => {
|
|
93
|
+
if (status.connected && status.wallet) {
|
|
94
|
+
const network = sdk.getNetwork();
|
|
95
|
+
setWalletState({
|
|
96
|
+
account: {
|
|
97
|
+
address: status.wallet.address,
|
|
98
|
+
chain: getChainId(network),
|
|
99
|
+
publicKey: status.wallet.publicKey,
|
|
100
|
+
},
|
|
101
|
+
wallet: status.wallet,
|
|
102
|
+
connected: true,
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
setWalletState({
|
|
106
|
+
account: null,
|
|
107
|
+
wallet: null,
|
|
108
|
+
connected: false,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}, [sdk, getChainId]);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
const initialStatus = sdk.getStatus();
|
|
115
|
+
updateWalletState(initialStatus);
|
|
116
|
+
|
|
117
|
+
const unsubscribe = sdk.onStatusChange((status) => {
|
|
118
|
+
updateWalletState(status);
|
|
119
|
+
if (status.connected) {
|
|
120
|
+
setModalOpen(false);
|
|
121
|
+
setIsConnecting(false);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return () => { unsubscribe(); };
|
|
126
|
+
}, [sdk, updateWalletState]);
|
|
127
|
+
|
|
128
|
+
const openModal = useCallback(async () => {
|
|
129
|
+
if (!walletState?.connected) {
|
|
130
|
+
setModalOpen(true);
|
|
131
|
+
}
|
|
132
|
+
}, [walletState?.connected]);
|
|
133
|
+
|
|
134
|
+
const closeModal = useCallback(() => {
|
|
135
|
+
setModalOpen(false);
|
|
136
|
+
setIsConnecting(false);
|
|
137
|
+
}, []);
|
|
138
|
+
|
|
139
|
+
const connectWallet = useCallback(async () => {
|
|
140
|
+
if (isConnecting) return;
|
|
141
|
+
setIsConnecting(true);
|
|
142
|
+
try {
|
|
143
|
+
await sdk.connect();
|
|
144
|
+
} catch (error) {
|
|
145
|
+
setIsConnecting(false);
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}, [sdk, isConnecting]);
|
|
149
|
+
|
|
150
|
+
const disconnect = useCallback(async () => {
|
|
151
|
+
await sdk.disconnect();
|
|
152
|
+
setModalOpen(false);
|
|
153
|
+
}, [sdk]);
|
|
154
|
+
|
|
155
|
+
const sendTransaction = useCallback(
|
|
156
|
+
async (transaction: SendTransactionRequest): Promise<TransactionResponse> => {
|
|
157
|
+
const response = await sdk.sendTransaction(transaction);
|
|
158
|
+
return { boc: response.boc };
|
|
159
|
+
},
|
|
160
|
+
[sdk]
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const restoreConnection = useCallback(async (): Promise<void> => {
|
|
164
|
+
const status = sdk.getStatus();
|
|
165
|
+
if (status.connected && status.wallet) {
|
|
166
|
+
updateWalletState(status);
|
|
167
|
+
}
|
|
168
|
+
}, [sdk, updateWalletState]);
|
|
169
|
+
|
|
170
|
+
const setWalletList = useCallback((wallets: WalletDefinition[]): void => {
|
|
171
|
+
setCustomWalletList(wallets);
|
|
172
|
+
}, []);
|
|
173
|
+
|
|
174
|
+
const getNetwork = useCallback((): Network => sdk.getNetwork(), [sdk]);
|
|
175
|
+
const setNetwork = useCallback((network: Network): void => {
|
|
176
|
+
sdk.setNetwork(network);
|
|
177
|
+
const status = sdk.getStatus();
|
|
178
|
+
updateWalletState(status);
|
|
179
|
+
}, [sdk, updateWalletState]);
|
|
180
|
+
|
|
181
|
+
const getBalance = useCallback(async (address?: string): Promise<BalanceResponse> => {
|
|
182
|
+
return await sdk.getBalance(address);
|
|
183
|
+
}, [sdk]);
|
|
184
|
+
|
|
185
|
+
const getTransactionStatusByHash = useCallback(async (
|
|
186
|
+
txHash: string, address: string
|
|
187
|
+
): Promise<TransactionStatusResponse> => {
|
|
188
|
+
return await sdk.getTransactionStatusByHash(txHash, address);
|
|
189
|
+
}, [sdk]);
|
|
190
|
+
|
|
191
|
+
const on = useCallback(<T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>): (() => void) => {
|
|
192
|
+
return sdk.on(event, listener);
|
|
193
|
+
}, [sdk]);
|
|
194
|
+
|
|
195
|
+
const off = useCallback(<T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>): void => {
|
|
196
|
+
sdk.off(event, listener);
|
|
197
|
+
}, [sdk]);
|
|
198
|
+
|
|
199
|
+
const tonConnectUI: TonConnectUI = {
|
|
200
|
+
openModal,
|
|
201
|
+
closeModal,
|
|
202
|
+
connectWallet,
|
|
203
|
+
disconnect,
|
|
204
|
+
sendTransaction,
|
|
205
|
+
restoreConnection,
|
|
206
|
+
setWalletList,
|
|
207
|
+
getNetwork,
|
|
208
|
+
setNetwork,
|
|
209
|
+
getBalance,
|
|
210
|
+
getTransactionStatusByHash,
|
|
211
|
+
on,
|
|
212
|
+
off,
|
|
213
|
+
wallet: walletState,
|
|
214
|
+
modalState: { open: modalOpen },
|
|
215
|
+
uiVersion: '2.0.0',
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const contextValue: TonConnectUIContextValue = { tonConnectUI, sdk };
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<TonConnectUIContext.Provider value={contextValue}>
|
|
222
|
+
{children}
|
|
223
|
+
<WalletSelectionModal
|
|
224
|
+
visible={modalOpen && !walletState?.connected}
|
|
225
|
+
onClose={closeModal}
|
|
226
|
+
wallets={customWalletList || undefined}
|
|
227
|
+
/>
|
|
228
|
+
</TonConnectUIContext.Provider>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Hook to access TonConnectUI instance
|
|
234
|
+
*/
|
|
235
|
+
export function useTonConnectUI(): TonConnectUI {
|
|
236
|
+
const context = useContext(TonConnectUIContext);
|
|
237
|
+
if (!context) {
|
|
238
|
+
throw new Error('useTonConnectUI must be used within TonConnectUIProvider');
|
|
239
|
+
}
|
|
240
|
+
return context.tonConnectUI;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Hook to access wallet state
|
|
245
|
+
*/
|
|
246
|
+
export function useTonWallet(): WalletState | null {
|
|
247
|
+
const tonConnectUI = useTonConnectUI();
|
|
248
|
+
return tonConnectUI.wallet;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Hook to access modal state
|
|
253
|
+
*/
|
|
254
|
+
export function useTonConnectModal(): { open: boolean; close: () => void; openModal: () => Promise<void> } {
|
|
255
|
+
const tonConnectUI = useTonConnectUI();
|
|
256
|
+
return {
|
|
257
|
+
open: tonConnectUI.modalState.open,
|
|
258
|
+
close: tonConnectUI.closeModal,
|
|
259
|
+
openModal: tonConnectUI.openModal,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Hook to access SDK instance
|
|
265
|
+
*/
|
|
266
|
+
export function useTonConnectSDK(): TonConnectMobile {
|
|
267
|
+
const context = useContext(TonConnectUIContext);
|
|
268
|
+
if (!context) {
|
|
269
|
+
throw new Error('useTonConnectSDK must be used within TonConnectUIProvider');
|
|
270
|
+
}
|
|
271
|
+
return context.sdk;
|
|
272
|
+
}
|