@blazium/ton-connect-mobile 1.2.5 → 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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * React integration layer for @tonconnect/ui-react compatibility
2
+ * React integration layer for TON Connect Mobile SDK
3
3
  * Provides TonConnectUIProvider, hooks, and components compatible with @tonconnect/ui-react API
4
4
  */
5
5
  import { ReactNode } from 'react';
@@ -22,101 +22,53 @@ export interface WalletState {
22
22
  connected: boolean;
23
23
  }
24
24
  /**
25
- * Transaction response (compatible with @tonconnect/ui-react)
25
+ * Transaction response
26
26
  */
27
27
  export interface TransactionResponse {
28
28
  boc: string;
29
- signature: string;
30
29
  }
31
30
  /**
32
- * Sign data request
33
- */
34
- export interface SignDataRequest {
35
- /** Data to sign (will be base64 encoded) */
36
- data: string | Uint8Array;
37
- /** Optional version */
38
- version?: string;
39
- }
40
- /**
41
- * Sign data response
42
- */
43
- export interface SignDataResponse {
44
- signature: string;
45
- timestamp: number;
46
- }
47
- /**
48
- * TonConnect UI instance interface (compatible with @tonconnect/ui-react)
49
- * Includes all features from @tonconnect/ui-react for full compatibility
31
+ * TonConnect UI instance interface
50
32
  */
51
33
  export interface TonConnectUI {
52
- /** Open connection modal */
53
34
  openModal: () => Promise<void>;
54
- /** Close connection modal */
55
35
  closeModal: () => void;
56
- /** Connect to wallet */
57
36
  connectWallet: () => Promise<void>;
58
- /** Disconnect from wallet */
59
37
  disconnect: () => Promise<void>;
60
- /** Send transaction */
61
38
  sendTransaction: (transaction: SendTransactionRequest) => Promise<TransactionResponse>;
62
- /** Sign data */
63
- signData: (request: SignDataRequest) => Promise<SignDataResponse>;
64
- /** Restore connection from stored session */
65
39
  restoreConnection: () => Promise<void>;
66
- /** Set wallet list (customize available wallets) */
67
40
  setWalletList: (wallets: WalletDefinition[]) => void;
68
- /** Get current network */
69
41
  getNetwork: () => Network;
70
- /** Set network (mainnet/testnet) */
71
42
  setNetwork: (network: Network) => void;
72
- /** Get wallet balance */
73
43
  getBalance: (address?: string) => Promise<BalanceResponse>;
74
- /** Get transaction status */
75
- getTransactionStatus: (boc: string, maxAttempts?: number, intervalMs?: number) => Promise<TransactionStatusResponse>;
76
- /** Get transaction status by hash */
77
44
  getTransactionStatusByHash: (txHash: string, address: string) => Promise<TransactionStatusResponse>;
78
- /** Add event listener */
79
45
  on: <T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>) => () => void;
80
- /** Remove event listener */
81
46
  off: <T = any>(event: TonConnectEventType, listener: TonConnectEventListener<T>) => void;
82
- /** Current wallet state */
83
47
  wallet: WalletState | null;
84
- /** Modal open state */
85
48
  modalState: {
86
49
  open: boolean;
87
50
  };
88
- /** UI kit version */
89
51
  uiVersion: string;
90
52
  }
91
- /**
92
- * TonConnectUIProvider props
93
- */
94
53
  export interface TonConnectUIProviderProps {
95
- /** SDK configuration */
96
54
  config: TonConnectMobileConfig;
97
- /** Children */
98
55
  children: ReactNode;
99
- /** Optional SDK instance (for testing or custom instances) */
100
56
  sdkInstance?: TonConnectMobile;
101
57
  }
102
58
  /**
103
59
  * TonConnectUIProvider - React context provider for TON Connect
104
- * Compatible with @tonconnect/ui-react API
105
60
  */
106
61
  export declare function TonConnectUIProvider({ config, children, sdkInstance, }: TonConnectUIProviderProps): JSX.Element;
107
62
  /**
108
63
  * Hook to access TonConnectUI instance
109
- * Compatible with @tonconnect/ui-react useTonConnectUI hook
110
64
  */
111
65
  export declare function useTonConnectUI(): TonConnectUI;
112
66
  /**
113
67
  * Hook to access wallet state
114
- * Compatible with @tonconnect/ui-react useTonWallet hook
115
68
  */
116
69
  export declare function useTonWallet(): WalletState | null;
117
70
  /**
118
71
  * Hook to access modal state
119
- * Compatible with @tonconnect/ui-react useTonConnectModal hook
120
72
  */
121
73
  export declare function useTonConnectModal(): {
122
74
  open: boolean;
@@ -124,6 +76,6 @@ export declare function useTonConnectModal(): {
124
76
  openModal: () => Promise<void>;
125
77
  };
126
78
  /**
127
- * Hook to access SDK instance (for advanced usage)
79
+ * Hook to access SDK instance
128
80
  */
129
81
  export declare function useTonConnectSDK(): TonConnectMobile;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /**
3
- * React integration layer for @tonconnect/ui-react compatibility
3
+ * React integration layer for TON Connect Mobile SDK
4
4
  * Provides TonConnectUIProvider, hooks, and components compatible with @tonconnect/ui-react API
5
5
  */
6
6
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
@@ -48,33 +48,20 @@ const WalletSelectionModal_1 = require("./WalletSelectionModal");
48
48
  const TonConnectUIContext = (0, react_1.createContext)(null);
49
49
  /**
50
50
  * TonConnectUIProvider - React context provider for TON Connect
51
- * Compatible with @tonconnect/ui-react API
52
51
  */
53
52
  function TonConnectUIProvider({ config, children, sdkInstance, }) {
54
- // CRITICAL: Initialize SDK only once
55
53
  const [sdk] = (0, react_1.useState)(() => {
56
- if (sdkInstance) {
54
+ if (sdkInstance)
57
55
  return sdkInstance;
58
- }
59
- try {
60
- return new index_1.TonConnectMobile(config);
61
- }
62
- catch (error) {
63
- console.error('[TonConnectUIProvider] Failed to initialize SDK:', error);
64
- throw error;
65
- }
56
+ return new index_1.TonConnectMobile(config);
66
57
  });
67
58
  const [walletState, setWalletState] = (0, react_1.useState)(null);
68
59
  const [modalOpen, setModalOpen] = (0, react_1.useState)(false);
69
60
  const [isConnecting, setIsConnecting] = (0, react_1.useState)(false);
70
61
  const [customWalletList, setCustomWalletList] = (0, react_1.useState)(null);
71
- // Get chain ID based on network
72
62
  const getChainId = (0, react_1.useCallback)((network) => {
73
- // TON mainnet chain ID: -239
74
- // TON testnet chain ID: -3
75
63
  return network === 'testnet' ? -3 : -239;
76
64
  }, []);
77
- // Update wallet state from SDK status
78
65
  const updateWalletState = (0, react_1.useCallback)((status) => {
79
66
  if (status.connected && status.wallet) {
80
67
  const network = sdk.getNetwork();
@@ -96,188 +83,99 @@ function TonConnectUIProvider({ config, children, sdkInstance, }) {
96
83
  });
97
84
  }
98
85
  }, [sdk, getChainId]);
99
- // Subscribe to SDK status changes
100
86
  (0, react_1.useEffect)(() => {
101
- // Set initial state
102
87
  const initialStatus = sdk.getStatus();
103
88
  updateWalletState(initialStatus);
104
- // Subscribe to changes
105
89
  const unsubscribe = sdk.onStatusChange((status) => {
106
90
  updateWalletState(status);
107
- // Close modal when connected
108
91
  if (status.connected) {
109
92
  setModalOpen(false);
110
93
  setIsConnecting(false);
111
94
  }
112
95
  });
113
- return () => {
114
- unsubscribe();
115
- // CRITICAL FIX: Cleanup SDK on unmount to prevent memory leaks
116
- // Note: SDK has its own cleanup via destroy(), but we don't call it here
117
- // to allow SDK to persist across component remounts (e.g., navigation)
118
- };
96
+ return () => { unsubscribe(); };
119
97
  }, [sdk, updateWalletState]);
120
- // Open modal
121
98
  const openModal = (0, react_1.useCallback)(async () => {
122
99
  if (!walletState?.connected) {
123
100
  setModalOpen(true);
124
101
  }
125
102
  }, [walletState?.connected]);
126
- // Close modal
127
103
  const closeModal = (0, react_1.useCallback)(() => {
128
104
  setModalOpen(false);
129
105
  setIsConnecting(false);
130
106
  }, []);
131
- // Connect wallet
132
107
  const connectWallet = (0, react_1.useCallback)(async () => {
133
- // CRITICAL FIX: Use functional update to avoid race condition
134
- setIsConnecting((prev) => {
135
- if (prev) {
136
- // Already connecting, return early
137
- return prev;
138
- }
139
- return true;
140
- });
141
- // Wait for connection
108
+ if (isConnecting)
109
+ return;
110
+ setIsConnecting(true);
142
111
  try {
143
112
  await sdk.connect();
144
- // Status update will be handled by the subscription
145
113
  }
146
114
  catch (error) {
147
115
  setIsConnecting(false);
148
116
  throw error;
149
117
  }
150
- }, [sdk]);
151
- // Disconnect
118
+ }, [sdk, isConnecting]);
152
119
  const disconnect = (0, react_1.useCallback)(async () => {
153
120
  await sdk.disconnect();
154
121
  setModalOpen(false);
155
122
  }, [sdk]);
156
- // Send transaction
157
123
  const sendTransaction = (0, react_1.useCallback)(async (transaction) => {
158
- try {
159
- // Validate transaction before sending
160
- if (!transaction || !transaction.messages || transaction.messages.length === 0) {
161
- throw new Error('Invalid transaction: messages array is required and cannot be empty');
162
- }
163
- if (!transaction.validUntil || transaction.validUntil <= Date.now()) {
164
- throw new Error('Invalid transaction: validUntil must be in the future');
165
- }
166
- const response = await sdk.sendTransaction(transaction);
167
- return {
168
- boc: response.boc,
169
- signature: response.signature,
170
- };
171
- }
172
- catch (error) {
173
- console.error('[TonConnectUIProvider] Transaction error:', error);
174
- throw error;
175
- }
176
- }, [sdk]);
177
- // Sign data
178
- const signData = (0, react_1.useCallback)(async (request) => {
179
- try {
180
- // Validate request
181
- if (!request || (!request.data && request.data !== '')) {
182
- throw new Error('Invalid sign data request: data is required');
183
- }
184
- const response = await sdk.signData(request.data, request.version);
185
- return {
186
- signature: response.signature,
187
- timestamp: response.timestamp,
188
- };
189
- }
190
- catch (error) {
191
- console.error('[TonConnectUIProvider] Sign data error:', error);
192
- throw error;
193
- }
124
+ const response = await sdk.sendTransaction(transaction);
125
+ return { boc: response.boc };
194
126
  }, [sdk]);
195
- // Restore connection from stored session
196
127
  const restoreConnection = (0, react_1.useCallback)(async () => {
197
- try {
198
- // SDK automatically loads session on initialization
199
- // This method triggers a re-check of the stored session
200
- const status = sdk.getStatus();
201
- if (status.connected && status.wallet) {
202
- updateWalletState(status);
203
- }
204
- }
205
- catch (error) {
206
- console.error('[TonConnectUIProvider] Restore connection error:', error);
207
- throw error;
128
+ const status = sdk.getStatus();
129
+ if (status.connected && status.wallet) {
130
+ updateWalletState(status);
208
131
  }
209
132
  }, [sdk, updateWalletState]);
210
- // Set wallet list (customize available wallets)
211
133
  const setWalletList = (0, react_1.useCallback)((wallets) => {
212
- if (!wallets || !Array.isArray(wallets)) {
213
- throw new Error('Wallet list must be an array');
214
- }
215
134
  setCustomWalletList(wallets);
216
135
  }, []);
217
- // Get network
218
- const getNetwork = (0, react_1.useCallback)(() => {
219
- return sdk.getNetwork();
220
- }, [sdk]);
221
- // Set network
136
+ const getNetwork = (0, react_1.useCallback)(() => sdk.getNetwork(), [sdk]);
222
137
  const setNetwork = (0, react_1.useCallback)((network) => {
223
138
  sdk.setNetwork(network);
224
- // Update wallet state to reflect new chain ID
225
139
  const status = sdk.getStatus();
226
140
  updateWalletState(status);
227
141
  }, [sdk, updateWalletState]);
228
- // Get balance
229
142
  const getBalance = (0, react_1.useCallback)(async (address) => {
230
143
  return await sdk.getBalance(address);
231
144
  }, [sdk]);
232
- // Get transaction status
233
- const getTransactionStatus = (0, react_1.useCallback)(async (boc, maxAttempts = 10, intervalMs = 2000) => {
234
- return await sdk.getTransactionStatus(boc, maxAttempts, intervalMs);
235
- }, [sdk]);
236
- // Get transaction status by hash
237
145
  const getTransactionStatusByHash = (0, react_1.useCallback)(async (txHash, address) => {
238
146
  return await sdk.getTransactionStatusByHash(txHash, address);
239
147
  }, [sdk]);
240
- // Event listeners
241
148
  const on = (0, react_1.useCallback)((event, listener) => {
242
149
  return sdk.on(event, listener);
243
150
  }, [sdk]);
244
151
  const off = (0, react_1.useCallback)((event, listener) => {
245
152
  sdk.off(event, listener);
246
153
  }, [sdk]);
247
- // Create TonConnectUI instance
248
154
  const tonConnectUI = {
249
155
  openModal,
250
156
  closeModal,
251
157
  connectWallet,
252
158
  disconnect,
253
159
  sendTransaction,
254
- signData,
255
160
  restoreConnection,
256
161
  setWalletList,
257
162
  getNetwork,
258
163
  setNetwork,
259
164
  getBalance,
260
- getTransactionStatus,
261
165
  getTransactionStatusByHash,
262
166
  on,
263
167
  off,
264
168
  wallet: walletState,
265
- modalState: {
266
- open: modalOpen,
267
- },
268
- uiVersion: '1.0.0',
269
- };
270
- const contextValue = {
271
- tonConnectUI,
272
- sdk,
169
+ modalState: { open: modalOpen },
170
+ uiVersion: '2.0.0',
273
171
  };
172
+ const contextValue = { tonConnectUI, sdk };
274
173
  return (react_1.default.createElement(TonConnectUIContext.Provider, { value: contextValue },
275
174
  children,
276
175
  react_1.default.createElement(WalletSelectionModal_1.WalletSelectionModal, { visible: modalOpen && !walletState?.connected, onClose: closeModal, wallets: customWalletList || undefined })));
277
176
  }
278
177
  /**
279
178
  * Hook to access TonConnectUI instance
280
- * Compatible with @tonconnect/ui-react useTonConnectUI hook
281
179
  */
282
180
  function useTonConnectUI() {
283
181
  const context = (0, react_1.useContext)(TonConnectUIContext);
@@ -288,7 +186,6 @@ function useTonConnectUI() {
288
186
  }
289
187
  /**
290
188
  * Hook to access wallet state
291
- * Compatible with @tonconnect/ui-react useTonWallet hook
292
189
  */
293
190
  function useTonWallet() {
294
191
  const tonConnectUI = useTonConnectUI();
@@ -296,7 +193,6 @@ function useTonWallet() {
296
193
  }
297
194
  /**
298
195
  * Hook to access modal state
299
- * Compatible with @tonconnect/ui-react useTonConnectModal hook
300
196
  */
301
197
  function useTonConnectModal() {
302
198
  const tonConnectUI = useTonConnectUI();
@@ -307,7 +203,7 @@ function useTonConnectModal() {
307
203
  };
308
204
  }
309
205
  /**
310
- * Hook to access SDK instance (for advanced usage)
206
+ * Hook to access SDK instance
311
207
  */
312
208
  function useTonConnectSDK() {
313
209
  const context = (0, react_1.useContext)(TonConnectUIContext);
@@ -1,9 +1,8 @@
1
1
  /**
2
2
  * React integration exports
3
- * Re-export all React components and hooks
4
3
  */
5
4
  export { TonConnectUIProvider, useTonConnectUI, useTonWallet, useTonConnectModal, useTonConnectSDK, } from './TonConnectUIProvider';
6
- export type { TonConnectUIProviderProps, TonConnectUI, WalletState, Account, TransactionResponse, SignDataRequest, SignDataResponse, } from './TonConnectUIProvider';
5
+ export type { TonConnectUIProviderProps, TonConnectUI, WalletState, Account, TransactionResponse, } from './TonConnectUIProvider';
7
6
  export type { Network, BalanceResponse, TransactionStatusResponse, TransactionStatus } from '../types';
8
7
  export { TonConnectButton } from './TonConnectButton';
9
8
  export type { TonConnectButtonProps } from './TonConnectButton';
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  /**
3
3
  * React integration exports
4
- * Re-export all React components and hooks
5
4
  */
6
5
  Object.defineProperty(exports, "__esModule", { value: true });
7
6
  exports.WalletSelectionModal = exports.TonConnectButton = exports.useTonConnectSDK = exports.useTonConnectModal = exports.useTonWallet = exports.useTonConnectUI = exports.TonConnectUIProvider = void 0;