@blazium/ton-connect-mobile 1.1.1 → 1.1.3

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.
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ /**
3
+ * React integration layer for @tonconnect/ui-react compatibility
4
+ * Provides TonConnectUIProvider, hooks, and components compatible with @tonconnect/ui-react API
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.TonConnectUIProvider = TonConnectUIProvider;
41
+ exports.useTonConnectUI = useTonConnectUI;
42
+ exports.useTonWallet = useTonWallet;
43
+ exports.useTonConnectModal = useTonConnectModal;
44
+ exports.useTonConnectSDK = useTonConnectSDK;
45
+ const react_1 = __importStar(require("react"));
46
+ const index_1 = require("../index");
47
+ const TonConnectUIContext = (0, react_1.createContext)(null);
48
+ /**
49
+ * TonConnectUIProvider - React context provider for TON Connect
50
+ * Compatible with @tonconnect/ui-react API
51
+ */
52
+ function TonConnectUIProvider({ config, children, sdkInstance, }) {
53
+ const [sdk] = (0, react_1.useState)(() => sdkInstance || new index_1.TonConnectMobile(config));
54
+ const [walletState, setWalletState] = (0, react_1.useState)(null);
55
+ const [modalOpen, setModalOpen] = (0, react_1.useState)(false);
56
+ const [isConnecting, setIsConnecting] = (0, react_1.useState)(false);
57
+ // Update wallet state from SDK status
58
+ const updateWalletState = (0, react_1.useCallback)((status) => {
59
+ if (status.connected && status.wallet) {
60
+ setWalletState({
61
+ account: {
62
+ address: status.wallet.address,
63
+ chain: -239, // TON mainnet chain ID
64
+ publicKey: status.wallet.publicKey,
65
+ },
66
+ wallet: status.wallet,
67
+ connected: true,
68
+ });
69
+ }
70
+ else {
71
+ setWalletState({
72
+ account: null,
73
+ wallet: null,
74
+ connected: false,
75
+ });
76
+ }
77
+ }, []);
78
+ // Subscribe to SDK status changes
79
+ (0, react_1.useEffect)(() => {
80
+ // Set initial state
81
+ const initialStatus = sdk.getStatus();
82
+ updateWalletState(initialStatus);
83
+ // Subscribe to changes
84
+ const unsubscribe = sdk.onStatusChange((status) => {
85
+ updateWalletState(status);
86
+ // Close modal when connected
87
+ if (status.connected) {
88
+ setModalOpen(false);
89
+ setIsConnecting(false);
90
+ }
91
+ });
92
+ return () => {
93
+ unsubscribe();
94
+ // CRITICAL FIX: Cleanup SDK on unmount to prevent memory leaks
95
+ // Note: SDK has its own cleanup via destroy(), but we don't call it here
96
+ // to allow SDK to persist across component remounts (e.g., navigation)
97
+ };
98
+ }, [sdk, updateWalletState]);
99
+ // Open modal
100
+ const openModal = (0, react_1.useCallback)(async () => {
101
+ setModalOpen(true);
102
+ }, []);
103
+ // Close modal
104
+ const closeModal = (0, react_1.useCallback)(() => {
105
+ setModalOpen(false);
106
+ setIsConnecting(false);
107
+ }, []);
108
+ // Connect wallet
109
+ const connectWallet = (0, react_1.useCallback)(async () => {
110
+ // CRITICAL FIX: Use functional update to avoid race condition
111
+ setIsConnecting((prev) => {
112
+ if (prev) {
113
+ // Already connecting, return early
114
+ return prev;
115
+ }
116
+ return true;
117
+ });
118
+ // Wait for connection
119
+ try {
120
+ await sdk.connect();
121
+ // Status update will be handled by the subscription
122
+ }
123
+ catch (error) {
124
+ setIsConnecting(false);
125
+ throw error;
126
+ }
127
+ }, [sdk]);
128
+ // Disconnect
129
+ const disconnect = (0, react_1.useCallback)(async () => {
130
+ await sdk.disconnect();
131
+ setModalOpen(false);
132
+ }, [sdk]);
133
+ // Send transaction
134
+ const sendTransaction = (0, react_1.useCallback)(async (transaction) => {
135
+ const response = await sdk.sendTransaction(transaction);
136
+ return {
137
+ boc: response.boc,
138
+ signature: response.signature,
139
+ };
140
+ }, [sdk]);
141
+ // Sign data
142
+ const signData = (0, react_1.useCallback)(async (request) => {
143
+ const response = await sdk.signData(request.data, request.version);
144
+ return {
145
+ signature: response.signature,
146
+ timestamp: response.timestamp,
147
+ };
148
+ }, [sdk]);
149
+ // Create TonConnectUI instance
150
+ const tonConnectUI = {
151
+ openModal,
152
+ closeModal,
153
+ connectWallet,
154
+ disconnect,
155
+ sendTransaction,
156
+ signData,
157
+ wallet: walletState,
158
+ modalState: {
159
+ open: modalOpen,
160
+ },
161
+ uiVersion: '1.0.0',
162
+ };
163
+ const contextValue = {
164
+ tonConnectUI,
165
+ sdk,
166
+ };
167
+ return react_1.default.createElement(TonConnectUIContext.Provider, { value: contextValue }, children);
168
+ }
169
+ /**
170
+ * Hook to access TonConnectUI instance
171
+ * Compatible with @tonconnect/ui-react useTonConnectUI hook
172
+ */
173
+ function useTonConnectUI() {
174
+ const context = (0, react_1.useContext)(TonConnectUIContext);
175
+ if (!context) {
176
+ throw new Error('useTonConnectUI must be used within TonConnectUIProvider');
177
+ }
178
+ return context.tonConnectUI;
179
+ }
180
+ /**
181
+ * Hook to access wallet state
182
+ * Compatible with @tonconnect/ui-react useTonWallet hook
183
+ */
184
+ function useTonWallet() {
185
+ const tonConnectUI = useTonConnectUI();
186
+ return tonConnectUI.wallet;
187
+ }
188
+ /**
189
+ * Hook to access modal state
190
+ * Compatible with @tonconnect/ui-react useTonConnectModal hook
191
+ */
192
+ function useTonConnectModal() {
193
+ const tonConnectUI = useTonConnectUI();
194
+ return {
195
+ open: tonConnectUI.modalState.open,
196
+ close: tonConnectUI.closeModal,
197
+ openModal: tonConnectUI.openModal,
198
+ };
199
+ }
200
+ /**
201
+ * Hook to access SDK instance (for advanced usage)
202
+ */
203
+ function useTonConnectSDK() {
204
+ const context = (0, react_1.useContext)(TonConnectUIContext);
205
+ if (!context) {
206
+ throw new Error('useTonConnectSDK must be used within TonConnectUIProvider');
207
+ }
208
+ return context.sdk;
209
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * React integration exports
3
+ * Re-export all React components and hooks
4
+ */
5
+ export { TonConnectUIProvider, useTonConnectUI, useTonWallet, useTonConnectModal, useTonConnectSDK, } from './TonConnectUIProvider';
6
+ export type { TonConnectUIProviderProps, TonConnectUI, WalletState, Account, TransactionResponse, SignDataRequest, SignDataResponse, } from './TonConnectUIProvider';
7
+ export { TonConnectButton } from './TonConnectButton';
8
+ export type { TonConnectButtonProps } from './TonConnectButton';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ /**
3
+ * React integration exports
4
+ * Re-export all React components and hooks
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.TonConnectButton = exports.useTonConnectSDK = exports.useTonConnectModal = exports.useTonWallet = exports.useTonConnectUI = exports.TonConnectUIProvider = void 0;
8
+ var TonConnectUIProvider_1 = require("./TonConnectUIProvider");
9
+ Object.defineProperty(exports, "TonConnectUIProvider", { enumerable: true, get: function () { return TonConnectUIProvider_1.TonConnectUIProvider; } });
10
+ Object.defineProperty(exports, "useTonConnectUI", { enumerable: true, get: function () { return TonConnectUIProvider_1.useTonConnectUI; } });
11
+ Object.defineProperty(exports, "useTonWallet", { enumerable: true, get: function () { return TonConnectUIProvider_1.useTonWallet; } });
12
+ Object.defineProperty(exports, "useTonConnectModal", { enumerable: true, get: function () { return TonConnectUIProvider_1.useTonConnectModal; } });
13
+ Object.defineProperty(exports, "useTonConnectSDK", { enumerable: true, get: function () { return TonConnectUIProvider_1.useTonConnectSDK; } });
14
+ var TonConnectButton_1 = require("./TonConnectButton");
15
+ Object.defineProperty(exports, "TonConnectButton", { enumerable: true, get: function () { return TonConnectButton_1.TonConnectButton; } });
@@ -0,0 +1,4 @@
1
+ {
2
+ "main": "./index.js",
3
+ "types": "./index.d.ts"
4
+ }
@@ -76,7 +76,9 @@ export interface ConnectionRequestPayload {
76
76
  name: 'ton_addr';
77
77
  }>;
78
78
  /** Return strategy - how wallet should return to the app */
79
- returnStrategy?: 'back' | 'none';
79
+ returnStrategy?: 'back' | 'post_redirect' | 'none';
80
+ /** Return scheme for mobile apps (required by many wallets for proper callback handling) */
81
+ returnScheme?: string;
80
82
  }
81
83
  /**
82
84
  * Connection response payload (received from wallet)
@@ -133,7 +135,7 @@ export interface TransactionRequestPayload {
133
135
  /** Return URL scheme (for mobile apps) */
134
136
  returnScheme?: string;
135
137
  /** Return strategy */
136
- returnStrategy?: 'back' | 'none';
138
+ returnStrategy?: 'back' | 'post_redirect' | 'none';
137
139
  }
138
140
  /**
139
141
  * Transaction response payload (received from wallet)
package/package.json CHANGED
@@ -1,15 +1,27 @@
1
1
  {
2
2
  "name": "@blazium/ton-connect-mobile",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Production-ready TON Connect Mobile SDK for React Native and Expo. Implements the real TonConnect protocol for mobile applications using deep links and callbacks.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./react": {
13
+ "types": "./dist/react/index.d.ts",
14
+ "default": "./dist/react/index.js",
15
+ "react-native": "./dist/react/index.js"
16
+ }
17
+ },
7
18
  "repository": {
8
19
  "type": "git",
9
20
  "url": "git+https://github.com/blaziumdev/ton-connect-mobile.git"
10
21
  },
11
22
  "scripts": {
12
23
  "build": "tsc",
24
+ "postbuild": "node scripts/postbuild.js",
13
25
  "prepublishOnly": "npm run build",
14
26
  "example": "cd example && npm start"
15
27
  },
@@ -31,6 +43,7 @@
31
43
  "react-native-get-random-values": "^1.9.0"
32
44
  },
33
45
  "peerDependencies": {
46
+ "react": "*",
34
47
  "react-native": "*"
35
48
  },
36
49
  "peerDependenciesMeta": {
@@ -130,19 +130,25 @@ export function decodeBase64URL<T>(encoded: string): T {
130
130
  export function buildConnectionRequest(
131
131
  manifestUrl: string,
132
132
  returnScheme: string,
133
- walletUniversalLink?: string
133
+ walletUniversalLink?: string,
134
+ returnStrategy?: 'back' | 'post_redirect' | 'none',
135
+ requiresReturnScheme?: boolean
134
136
  ): string {
135
- // TON Connect protocol: payload should only contain manifestUrl, items, and returnStrategy
136
- // returnScheme is NOT part of the official protocol payload
137
- // For mobile apps, wallets should infer the callback scheme from the manifest or use a default
137
+ // Build payload with required fields
138
138
  const payload: ConnectionRequestPayload = {
139
139
  manifestUrl,
140
140
  items: [{ name: 'ton_addr' }],
141
- returnStrategy: 'back',
142
- // NOTE: returnScheme is NOT in the official TON Connect protocol
143
- // Wallets should handle mobile app callbacks differently
141
+ returnStrategy: returnStrategy || 'back',
144
142
  };
145
143
 
144
+ // CRITICAL FIX: Many wallets (Tonhub, MyTonWallet, Telegram Wallet) require returnScheme
145
+ // in the payload to properly handle mobile app callbacks. While not in the official
146
+ // protocol spec, it's a de-facto requirement for mobile apps.
147
+ if (requiresReturnScheme !== false) {
148
+ // Default to true if not specified - safer to include it
149
+ payload.returnScheme = returnScheme;
150
+ }
151
+
146
152
  const encoded = encodeBase64URL(payload);
147
153
 
148
154
  // Use custom wallet universal link if provided
@@ -164,7 +170,9 @@ export function buildTransactionRequest(
164
170
  manifestUrl: string,
165
171
  request: SendTransactionRequest,
166
172
  returnScheme: string,
167
- walletUniversalLink?: string
173
+ walletUniversalLink?: string,
174
+ returnStrategy?: 'back' | 'post_redirect' | 'none',
175
+ requiresReturnScheme?: boolean
168
176
  ): string {
169
177
  const payload: TransactionRequestPayload = {
170
178
  manifestUrl,
@@ -179,11 +187,14 @@ export function buildTransactionRequest(
179
187
  network: request.network,
180
188
  from: request.from,
181
189
  },
182
- returnStrategy: 'back',
183
- // NOTE: returnScheme is NOT in the official TON Connect protocol
184
- // Wallets should handle mobile app callbacks differently
190
+ returnStrategy: returnStrategy || 'back',
185
191
  };
186
192
 
193
+ // CRITICAL FIX: Include returnScheme for mobile wallets that require it
194
+ if (requiresReturnScheme !== false) {
195
+ payload.returnScheme = returnScheme;
196
+ }
197
+
187
198
  const encoded = encodeBase64URL(payload);
188
199
 
189
200
  // Use custom wallet universal link if provided
@@ -16,6 +16,10 @@ export interface WalletDefinition {
16
16
  iconUrl?: string;
17
17
  /** Platform support */
18
18
  platforms: ('ios' | 'android' | 'web')[];
19
+ /** Preferred return strategy for this wallet */
20
+ preferredReturnStrategy?: 'back' | 'post_redirect' | 'none';
21
+ /** Whether this wallet requires returnScheme in payload */
22
+ requiresReturnScheme?: boolean;
19
23
  }
20
24
 
21
25
  /**
@@ -28,6 +32,8 @@ export const SUPPORTED_WALLETS: WalletDefinition[] = [
28
32
  universalLink: 'https://app.tonkeeper.com/ton-connect',
29
33
  deepLink: 'tonkeeper://',
30
34
  platforms: ['ios', 'android'],
35
+ preferredReturnStrategy: 'back',
36
+ requiresReturnScheme: true, // CRITICAL FIX: Mobile apps need returnScheme for proper callback handling
31
37
  },
32
38
  {
33
39
  name: 'MyTonWallet',
@@ -35,6 +41,8 @@ export const SUPPORTED_WALLETS: WalletDefinition[] = [
35
41
  universalLink: 'https://connect.mytonwallet.org',
36
42
  deepLink: 'mytonwallet://',
37
43
  platforms: ['ios', 'android', 'web'],
44
+ preferredReturnStrategy: 'post_redirect',
45
+ requiresReturnScheme: true, // MyTonWallet requires explicit returnScheme
38
46
  },
39
47
  {
40
48
  name: 'Wallet in Telegram',
@@ -42,6 +50,8 @@ export const SUPPORTED_WALLETS: WalletDefinition[] = [
42
50
  universalLink: 'https://wallet.tonapi.io/ton-connect',
43
51
  deepLink: 'tg://',
44
52
  platforms: ['ios', 'android'],
53
+ preferredReturnStrategy: 'post_redirect',
54
+ requiresReturnScheme: true, // Telegram Wallet requires explicit returnScheme
45
55
  },
46
56
  {
47
57
  name: 'Tonhub',
@@ -49,6 +59,8 @@ export const SUPPORTED_WALLETS: WalletDefinition[] = [
49
59
  universalLink: 'https://tonhub.com/ton-connect',
50
60
  deepLink: 'tonhub://',
51
61
  platforms: ['ios', 'android'],
62
+ preferredReturnStrategy: 'post_redirect',
63
+ requiresReturnScheme: true, // Tonhub requires explicit returnScheme for proper callback
52
64
  },
53
65
  ];
54
66