@tonconnect/sdk 3.0.3-beta.0 → 3.0.3-beta.1

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/lib/esm/index.mjs CHANGED
@@ -498,9 +498,7 @@ function createResource(createFn, disposeFn) {
498
498
  promise ? disposeFn(yield promise) : Promise.resolve()
499
499
  ]);
500
500
  }
501
- catch (e) {
502
- logError('Failed to dispose the resource', e);
503
- }
501
+ catch (e) { }
504
502
  });
505
503
  // recreate the current resource
506
504
  const recreate = (delayMs) => __awaiter(this, void 0, void 0, function* () {
@@ -2006,7 +2004,35 @@ function checkSendTransactionSupport(features, options) {
2006
2004
  logWarning("Connected wallet didn't provide information about max allowed messages in the SendTransaction request. Request may be rejected by the wallet.");
2007
2005
  }
2008
2006
 
2009
- function createConnectionInfo(wallet) {
2007
+ /**
2008
+ * Create a request version event.
2009
+ */
2010
+ function createRequestVersionEvent() {
2011
+ return {
2012
+ type: 'request-version'
2013
+ };
2014
+ }
2015
+ /**
2016
+ * Create a response version event.
2017
+ * @param version
2018
+ */
2019
+ function createResponseVersionEvent(version) {
2020
+ return {
2021
+ type: 'response-version',
2022
+ version: version
2023
+ };
2024
+ }
2025
+ /**
2026
+ * Create a version info.
2027
+ * @param version
2028
+ */
2029
+ function createVersionInfo(version) {
2030
+ return {
2031
+ ton_connect_sdk_lib: version.ton_connect_sdk_lib,
2032
+ ton_connect_ui_lib: version.ton_connect_ui_lib
2033
+ };
2034
+ }
2035
+ function createConnectionInfo(version, wallet) {
2010
2036
  var _a, _b, _c, _d, _e, _f, _g, _h;
2011
2037
  const isTonProof = ((_a = wallet === null || wallet === void 0 ? void 0 : wallet.connectItems) === null || _a === void 0 ? void 0 : _a.tonProof) && 'proof' in wallet.connectItems.tonProof;
2012
2038
  const authType = isTonProof ? 'ton_proof' : 'ton_addr';
@@ -2015,62 +2041,69 @@ function createConnectionInfo(wallet) {
2015
2041
  wallet_type: (_d = wallet === null || wallet === void 0 ? void 0 : wallet.device.appName) !== null && _d !== void 0 ? _d : null,
2016
2042
  wallet_version: (_e = wallet === null || wallet === void 0 ? void 0 : wallet.device.appVersion) !== null && _e !== void 0 ? _e : null,
2017
2043
  auth_type: authType,
2018
- custom_data: {
2019
- chain_id: (_g = (_f = wallet === null || wallet === void 0 ? void 0 : wallet.account) === null || _f === void 0 ? void 0 : _f.chain) !== null && _g !== void 0 ? _g : null,
2020
- provider: (_h = wallet === null || wallet === void 0 ? void 0 : wallet.provider) !== null && _h !== void 0 ? _h : null
2021
- }
2044
+ custom_data: Object.assign({ chain_id: (_g = (_f = wallet === null || wallet === void 0 ? void 0 : wallet.account) === null || _f === void 0 ? void 0 : _f.chain) !== null && _g !== void 0 ? _g : null, provider: (_h = wallet === null || wallet === void 0 ? void 0 : wallet.provider) !== null && _h !== void 0 ? _h : null }, createVersionInfo(version))
2022
2045
  };
2023
2046
  }
2024
2047
  /**
2025
2048
  * Create a connection init event.
2026
2049
  */
2027
- function createConnectionStartedEvent() {
2050
+ function createConnectionStartedEvent(version) {
2028
2051
  return {
2029
- type: 'connection-started'
2052
+ type: 'connection-started',
2053
+ custom_data: createVersionInfo(version)
2030
2054
  };
2031
2055
  }
2032
2056
  /**
2033
2057
  * Create a connection completed event.
2058
+ * @param version
2034
2059
  * @param wallet
2035
2060
  */
2036
- function createConnectionCompletedEvent(wallet) {
2037
- return Object.assign({ type: 'connection-completed' }, createConnectionInfo(wallet));
2061
+ function createConnectionCompletedEvent(version, wallet) {
2062
+ return Object.assign({ type: 'connection-completed', is_success: true }, createConnectionInfo(version, wallet));
2038
2063
  }
2039
2064
  /**
2040
2065
  * Create a connection error event.
2066
+ * @param version
2041
2067
  * @param error_message
2042
2068
  * @param errorCode
2043
2069
  */
2044
- function createConnectionErrorEvent(error_message, errorCode) {
2070
+ function createConnectionErrorEvent(version, error_message, errorCode) {
2045
2071
  return {
2046
2072
  type: 'connection-error',
2073
+ is_success: false,
2047
2074
  error_message: error_message,
2048
- error_code: errorCode !== null && errorCode !== void 0 ? errorCode : null
2075
+ error_code: errorCode !== null && errorCode !== void 0 ? errorCode : null,
2076
+ custom_data: createVersionInfo(version)
2049
2077
  };
2050
2078
  }
2051
2079
  /**
2052
2080
  * Create a connection restoring started event.
2053
2081
  */
2054
- function createConnectionRestoringStartedEvent() {
2082
+ function createConnectionRestoringStartedEvent(version) {
2055
2083
  return {
2056
- type: 'connection-restoring-started'
2084
+ type: 'connection-restoring-started',
2085
+ custom_data: createVersionInfo(version)
2057
2086
  };
2058
2087
  }
2059
2088
  /**
2060
2089
  * Create a connection restoring completed event.
2090
+ * @param version
2061
2091
  * @param wallet
2062
2092
  */
2063
- function createConnectionRestoringCompletedEvent(wallet) {
2064
- return Object.assign({ type: 'connection-restoring-completed' }, createConnectionInfo(wallet));
2093
+ function createConnectionRestoringCompletedEvent(version, wallet) {
2094
+ return Object.assign({ type: 'connection-restoring-completed', is_success: true }, createConnectionInfo(version, wallet));
2065
2095
  }
2066
2096
  /**
2067
2097
  * Create a connection restoring error event.
2098
+ * @param version
2068
2099
  * @param errorMessage
2069
2100
  */
2070
- function createConnectionRestoringErrorEvent(errorMessage) {
2101
+ function createConnectionRestoringErrorEvent(version, errorMessage) {
2071
2102
  return {
2072
2103
  type: 'connection-restoring-error',
2073
- error_message: errorMessage
2104
+ is_success: false,
2105
+ error_message: errorMessage,
2106
+ custom_data: createVersionInfo(version)
2074
2107
  };
2075
2108
  }
2076
2109
  function createTransactionInfo(wallet, transaction) {
@@ -2089,39 +2122,43 @@ function createTransactionInfo(wallet, transaction) {
2089
2122
  }
2090
2123
  /**
2091
2124
  * Create a transaction init event.
2125
+ * @param version
2092
2126
  * @param wallet
2093
2127
  * @param transaction
2094
2128
  */
2095
- function createTransactionSentForSignatureEvent(wallet, transaction) {
2096
- return Object.assign(Object.assign({ type: 'transaction-sent-for-signature' }, createConnectionInfo(wallet)), createTransactionInfo(wallet, transaction));
2129
+ function createTransactionSentForSignatureEvent(version, wallet, transaction) {
2130
+ return Object.assign(Object.assign({ type: 'transaction-sent-for-signature' }, createConnectionInfo(version, wallet)), createTransactionInfo(wallet, transaction));
2097
2131
  }
2098
2132
  /**
2099
2133
  * Create a transaction signed event.
2134
+ * @param version
2100
2135
  * @param wallet
2101
2136
  * @param transaction
2102
2137
  * @param signedTransaction
2103
2138
  */
2104
- function createTransactionSignedEvent(wallet, transaction, signedTransaction) {
2105
- return Object.assign(Object.assign({ type: 'transaction-signed', signed_transaction: signedTransaction.boc }, createConnectionInfo(wallet)), createTransactionInfo(wallet, transaction));
2139
+ function createTransactionSignedEvent(version, wallet, transaction, signedTransaction) {
2140
+ return Object.assign(Object.assign({ type: 'transaction-signed', is_success: true, signed_transaction: signedTransaction.boc }, createConnectionInfo(version, wallet)), createTransactionInfo(wallet, transaction));
2106
2141
  }
2107
2142
  /**
2108
2143
  * Create a transaction error event.
2144
+ * @param version
2109
2145
  * @param wallet
2110
2146
  * @param transaction
2111
2147
  * @param errorMessage
2112
2148
  * @param errorCode
2113
2149
  */
2114
- function createTransactionSigningFailedEvent(wallet, transaction, errorMessage, errorCode) {
2115
- return Object.assign(Object.assign({ type: 'transaction-signing-failed', error_message: errorMessage, error_code: errorCode !== null && errorCode !== void 0 ? errorCode : null }, createConnectionInfo(wallet)), createTransactionInfo(wallet, transaction));
2150
+ function createTransactionSigningFailedEvent(version, wallet, transaction, errorMessage, errorCode) {
2151
+ return Object.assign(Object.assign({ type: 'transaction-signing-failed', is_success: false, error_message: errorMessage, error_code: errorCode !== null && errorCode !== void 0 ? errorCode : null }, createConnectionInfo(version, wallet)), createTransactionInfo(wallet, transaction));
2116
2152
  }
2117
2153
  /**
2118
2154
  * Create a disconnect event.
2155
+ * @param version
2119
2156
  * @param wallet
2120
2157
  * @param scope
2121
2158
  * @returns
2122
2159
  */
2123
- function createDisconnectionEvent(wallet, scope) {
2124
- return Object.assign({ type: 'disconnection', scope: scope }, createConnectionInfo(wallet));
2160
+ function createDisconnectionEvent(version, wallet, scope) {
2161
+ return Object.assign({ type: 'disconnection', scope: scope }, createConnectionInfo(version, wallet));
2125
2162
  }
2126
2163
 
2127
2164
  /**
@@ -2148,6 +2185,23 @@ class BrowserEventDispatcher {
2148
2185
  (_a = this.window) === null || _a === void 0 ? void 0 : _a.dispatchEvent(event);
2149
2186
  });
2150
2187
  }
2188
+ /**
2189
+ * Adds an event listener to the browser window.
2190
+ * @param eventName - The name of the event to listen for.
2191
+ * @param listener - The listener to add.
2192
+ * @param options - The options for the listener.
2193
+ * @returns A function that removes the listener.
2194
+ */
2195
+ addEventListener(eventName, listener, options) {
2196
+ var _a;
2197
+ return __awaiter(this, void 0, void 0, function* () {
2198
+ (_a = this.window) === null || _a === void 0 ? void 0 : _a.addEventListener(eventName, listener, options);
2199
+ return () => {
2200
+ var _a;
2201
+ return (_a = this.window) === null || _a === void 0 ? void 0 : _a.removeEventListener(eventName, listener);
2202
+ };
2203
+ });
2204
+ }
2151
2205
  }
2152
2206
 
2153
2207
  /**
@@ -2175,13 +2229,71 @@ class BrowserEventDispatcher {
2175
2229
  * @internal
2176
2230
  */
2177
2231
  class TonConnectTracker {
2178
- constructor(eventDispatcher) {
2232
+ constructor(options) {
2233
+ var _a;
2179
2234
  /**
2180
2235
  * Event prefix for user actions.
2181
2236
  * @private
2182
2237
  */
2183
2238
  this.eventPrefix = 'ton-connect-';
2184
- this.eventDispatcher = eventDispatcher !== null && eventDispatcher !== void 0 ? eventDispatcher : new BrowserEventDispatcher();
2239
+ /**
2240
+ * TonConnect UI version.
2241
+ */
2242
+ this.tonConnectUiVersion = null;
2243
+ this.eventDispatcher = (_a = options === null || options === void 0 ? void 0 : options.eventDispatcher) !== null && _a !== void 0 ? _a : new BrowserEventDispatcher();
2244
+ this.tonConnectSdkVersion = options.tonConnectSdkVersion;
2245
+ this.init().catch();
2246
+ }
2247
+ /**
2248
+ * Version of the library.
2249
+ */
2250
+ get version() {
2251
+ return createVersionInfo({
2252
+ ton_connect_sdk_lib: this.tonConnectSdkVersion,
2253
+ ton_connect_ui_lib: this.tonConnectUiVersion
2254
+ });
2255
+ }
2256
+ /**
2257
+ * Called once when the tracker is created and request version other libraries.
2258
+ */
2259
+ init() {
2260
+ return __awaiter(this, void 0, void 0, function* () {
2261
+ try {
2262
+ yield this.setRequestVersionHandler();
2263
+ this.tonConnectUiVersion = yield this.requestTonConnectUiVersion();
2264
+ }
2265
+ catch (e) { }
2266
+ });
2267
+ }
2268
+ /**
2269
+ * Set request version handler.
2270
+ * @private
2271
+ */
2272
+ setRequestVersionHandler() {
2273
+ return __awaiter(this, void 0, void 0, function* () {
2274
+ yield this.eventDispatcher.addEventListener('ton-connect-request-version', () => __awaiter(this, void 0, void 0, function* () {
2275
+ yield this.eventDispatcher.dispatchEvent('ton-connect-response-version', createResponseVersionEvent(this.tonConnectSdkVersion));
2276
+ }));
2277
+ });
2278
+ }
2279
+ /**
2280
+ * Request TonConnect UI version.
2281
+ * @private
2282
+ */
2283
+ requestTonConnectUiVersion() {
2284
+ return __awaiter(this, void 0, void 0, function* () {
2285
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
2286
+ try {
2287
+ yield this.eventDispatcher.addEventListener('ton-connect-ui-response-version', (event) => {
2288
+ resolve(event.detail.version);
2289
+ }, { once: true });
2290
+ yield this.eventDispatcher.dispatchEvent('ton-connect-ui-request-version', createRequestVersionEvent());
2291
+ }
2292
+ catch (e) {
2293
+ reject(e);
2294
+ }
2295
+ }));
2296
+ });
2185
2297
  }
2186
2298
  /**
2187
2299
  * Emit user action event to the window.
@@ -2190,8 +2302,9 @@ class TonConnectTracker {
2190
2302
  */
2191
2303
  dispatchUserActionEvent(eventDetails) {
2192
2304
  try {
2193
- const eventName = `${this.eventPrefix}${eventDetails.type}`;
2194
- this.eventDispatcher.dispatchEvent(eventName, eventDetails).catch();
2305
+ this.eventDispatcher
2306
+ .dispatchEvent(`${this.eventPrefix}${eventDetails.type}`, eventDetails)
2307
+ .catch();
2195
2308
  }
2196
2309
  catch (e) { }
2197
2310
  }
@@ -2201,7 +2314,7 @@ class TonConnectTracker {
2201
2314
  */
2202
2315
  trackConnectionStarted(...args) {
2203
2316
  try {
2204
- const event = createConnectionStartedEvent(...args);
2317
+ const event = createConnectionStartedEvent(this.version, ...args);
2205
2318
  this.dispatchUserActionEvent(event);
2206
2319
  }
2207
2320
  catch (e) { }
@@ -2212,7 +2325,7 @@ class TonConnectTracker {
2212
2325
  */
2213
2326
  trackConnectionCompleted(...args) {
2214
2327
  try {
2215
- const event = createConnectionCompletedEvent(...args);
2328
+ const event = createConnectionCompletedEvent(this.version, ...args);
2216
2329
  this.dispatchUserActionEvent(event);
2217
2330
  }
2218
2331
  catch (e) { }
@@ -2223,7 +2336,7 @@ class TonConnectTracker {
2223
2336
  */
2224
2337
  trackConnectionError(...args) {
2225
2338
  try {
2226
- const event = createConnectionErrorEvent(...args);
2339
+ const event = createConnectionErrorEvent(this.version, ...args);
2227
2340
  this.dispatchUserActionEvent(event);
2228
2341
  }
2229
2342
  catch (e) { }
@@ -2234,7 +2347,7 @@ class TonConnectTracker {
2234
2347
  */
2235
2348
  trackConnectionRestoringStarted(...args) {
2236
2349
  try {
2237
- const event = createConnectionRestoringStartedEvent(...args);
2350
+ const event = createConnectionRestoringStartedEvent(this.version, ...args);
2238
2351
  this.dispatchUserActionEvent(event);
2239
2352
  }
2240
2353
  catch (e) { }
@@ -2245,7 +2358,7 @@ class TonConnectTracker {
2245
2358
  */
2246
2359
  trackConnectionRestoringCompleted(...args) {
2247
2360
  try {
2248
- const event = createConnectionRestoringCompletedEvent(...args);
2361
+ const event = createConnectionRestoringCompletedEvent(this.version, ...args);
2249
2362
  this.dispatchUserActionEvent(event);
2250
2363
  }
2251
2364
  catch (e) { }
@@ -2256,7 +2369,7 @@ class TonConnectTracker {
2256
2369
  */
2257
2370
  trackConnectionRestoringError(...args) {
2258
2371
  try {
2259
- const event = createConnectionRestoringErrorEvent(...args);
2372
+ const event = createConnectionRestoringErrorEvent(this.version, ...args);
2260
2373
  this.dispatchUserActionEvent(event);
2261
2374
  }
2262
2375
  catch (e) { }
@@ -2267,7 +2380,7 @@ class TonConnectTracker {
2267
2380
  */
2268
2381
  trackDisconnection(...args) {
2269
2382
  try {
2270
- const event = createDisconnectionEvent(...args);
2383
+ const event = createDisconnectionEvent(this.version, ...args);
2271
2384
  this.dispatchUserActionEvent(event);
2272
2385
  }
2273
2386
  catch (e) { }
@@ -2278,7 +2391,7 @@ class TonConnectTracker {
2278
2391
  */
2279
2392
  trackTransactionSentForSignature(...args) {
2280
2393
  try {
2281
- const event = createTransactionSentForSignatureEvent(...args);
2394
+ const event = createTransactionSentForSignatureEvent(this.version, ...args);
2282
2395
  this.dispatchUserActionEvent(event);
2283
2396
  }
2284
2397
  catch (e) { }
@@ -2289,7 +2402,7 @@ class TonConnectTracker {
2289
2402
  */
2290
2403
  trackTransactionSigned(...args) {
2291
2404
  try {
2292
- const event = createTransactionSignedEvent(...args);
2405
+ const event = createTransactionSignedEvent(this.version, ...args);
2293
2406
  this.dispatchUserActionEvent(event);
2294
2407
  }
2295
2408
  catch (e) { }
@@ -2300,13 +2413,15 @@ class TonConnectTracker {
2300
2413
  */
2301
2414
  trackTransactionSigningFailed(...args) {
2302
2415
  try {
2303
- const event = createTransactionSigningFailedEvent(...args);
2416
+ const event = createTransactionSigningFailedEvent(this.version, ...args);
2304
2417
  this.dispatchUserActionEvent(event);
2305
2418
  }
2306
2419
  catch (e) { }
2307
2420
  }
2308
2421
  }
2309
2422
 
2423
+ const tonConnectSdkVersion = "3.0.3-beta.1";
2424
+
2310
2425
  class TonConnect {
2311
2426
  constructor(options) {
2312
2427
  this.walletsList = new WalletsListManager();
@@ -2322,7 +2437,10 @@ class TonConnect {
2322
2437
  walletsListSource: options === null || options === void 0 ? void 0 : options.walletsListSource,
2323
2438
  cacheTTLMs: options === null || options === void 0 ? void 0 : options.walletsListCacheTTLMs
2324
2439
  });
2325
- this.tracker = new TonConnectTracker(options === null || options === void 0 ? void 0 : options.eventDispatcher);
2440
+ this.tracker = new TonConnectTracker({
2441
+ eventDispatcher: options === null || options === void 0 ? void 0 : options.eventDispatcher,
2442
+ tonConnectSdkVersion: tonConnectSdkVersion
2443
+ });
2326
2444
  if (!this.dappSettings.manifestUrl) {
2327
2445
  throw new DappMetadataError('Dapp tonconnect-manifest.json must be specified if window.location.origin is undefined. See more https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest');
2328
2446
  }
@@ -2595,7 +2713,7 @@ class TonConnect {
2595
2713
  this.pauseConnection();
2596
2714
  }
2597
2715
  else {
2598
- this.unPauseConnection().catch(e => logError('Cannot unpause connection', e));
2716
+ this.unPauseConnection().catch();
2599
2717
  }
2600
2718
  });
2601
2719
  }
@@ -2653,10 +2771,10 @@ class TonConnect {
2653
2771
  onWalletConnectError(connectEventError) {
2654
2772
  const error = connectErrorsParser.parseError(connectEventError);
2655
2773
  this.statusChangeErrorSubscriptions.forEach(errorsHandler => errorsHandler(error));
2656
- console.debug(error);
2774
+ logDebug(error);
2657
2775
  this.tracker.trackConnectionError(connectEventError.message, connectEventError.code);
2658
2776
  if (error instanceof ManifestNotFoundError || error instanceof ManifestContentErrorError) {
2659
- console.error(error);
2777
+ logError(error);
2660
2778
  throw error;
2661
2779
  }
2662
2780
  }
@@ -2791,5 +2909,5 @@ function hexToBytes(hex) {
2791
2909
  return result;
2792
2910
  }
2793
2911
 
2794
- export { BadRequestError, BrowserEventDispatcher, FetchWalletsError, LocalstorageNotFoundError, ParseHexError, TonConnect, TonConnectError, UnknownAppError, UnknownError, UserRejectsError, WalletAlreadyConnectedError, WalletNotConnectedError, WalletNotInjectedError, WalletsListManager, WrongAddressError, createConnectionCompletedEvent, createConnectionErrorEvent, createConnectionRestoringCompletedEvent, createConnectionRestoringErrorEvent, createConnectionRestoringStartedEvent, createConnectionStartedEvent, createDisconnectionEvent, createTransactionSentForSignatureEvent, createTransactionSignedEvent, createTransactionSigningFailedEvent, TonConnect as default, encodeTelegramUrlParameters, isTelegramUrl, isWalletInfoCurrentlyEmbedded, isWalletInfoCurrentlyInjected, isWalletInfoInjectable, isWalletInfoInjected, isWalletInfoRemote, toUserFriendlyAddress };
2912
+ export { BadRequestError, BrowserEventDispatcher, FetchWalletsError, LocalstorageNotFoundError, ParseHexError, TonConnect, TonConnectError, UnknownAppError, UnknownError, UserRejectsError, WalletAlreadyConnectedError, WalletNotConnectedError, WalletNotInjectedError, WalletsListManager, WrongAddressError, createConnectionCompletedEvent, createConnectionErrorEvent, createConnectionRestoringCompletedEvent, createConnectionRestoringErrorEvent, createConnectionRestoringStartedEvent, createConnectionStartedEvent, createDisconnectionEvent, createRequestVersionEvent, createResponseVersionEvent, createTransactionSentForSignatureEvent, createTransactionSignedEvent, createTransactionSigningFailedEvent, createVersionInfo, TonConnect as default, encodeTelegramUrlParameters, isTelegramUrl, isWalletInfoCurrentlyEmbedded, isWalletInfoCurrentlyInjected, isWalletInfoInjectable, isWalletInfoInjected, isWalletInfoRemote, toUserFriendlyAddress };
2795
2913
  //# sourceMappingURL=index.mjs.map