@tonconnect/sdk 3.0.7 → 3.1.0

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
@@ -120,6 +120,45 @@ class WalletNotConnectedError extends TonConnectError {
120
120
  }
121
121
  }
122
122
 
123
+ /**
124
+ * Thrown when there is an attempt to connect to the injected wallet while it is not exists in the webpage.
125
+ */
126
+ class WalletNotInjectedError extends TonConnectError {
127
+ get info() {
128
+ return 'There is an attempt to connect to the injected wallet while it is not exists in the webpage.';
129
+ }
130
+ constructor(...args) {
131
+ super(...args);
132
+ Object.setPrototypeOf(this, WalletNotInjectedError.prototype);
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Thrown when wallet doesn't support requested feature method.
138
+ */
139
+ class WalletNotSupportFeatureError extends TonConnectError {
140
+ get info() {
141
+ return "Wallet doesn't support requested feature method.";
142
+ }
143
+ constructor(...args) {
144
+ super(...args);
145
+ Object.setPrototypeOf(this, WalletNotSupportFeatureError.prototype);
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Thrown when wallet can't get manifest by passed manifestUrl.
151
+ */
152
+ class WalletMissingRequiredFeaturesError extends TonConnectError {
153
+ get info() {
154
+ return 'Missing required features. You need to update your wallet.';
155
+ }
156
+ constructor(message, options) {
157
+ super(message, options);
158
+ Object.setPrototypeOf(this, WalletMissingRequiredFeaturesError.prototype);
159
+ }
160
+ }
161
+
123
162
  function isWalletConnectionSourceJS(value) {
124
163
  return 'jsBridgeKey' in value;
125
164
  }
@@ -163,19 +202,6 @@ class UnknownAppError extends TonConnectError {
163
202
  }
164
203
  }
165
204
 
166
- /**
167
- * Thrown when there is an attempt to connect to the injected wallet while it is not exists in the webpage.
168
- */
169
- class WalletNotInjectedError extends TonConnectError {
170
- get info() {
171
- return 'There is an attempt to connect to the injected wallet while it is not exists in the webpage.';
172
- }
173
- constructor(...args) {
174
- super(...args);
175
- Object.setPrototypeOf(this, WalletNotInjectedError.prototype);
176
- }
177
- }
178
-
179
205
  /**
180
206
  * Thrown when `Storage` was not specified in the `DappMetadata` and default `localStorage` was not detected in the Node.js environment.
181
207
  */
@@ -1505,7 +1531,8 @@ class InjectedProvider {
1505
1531
  jsBridgeKey,
1506
1532
  injected: true,
1507
1533
  embedded: wallet.tonconnect.isWalletBrowser,
1508
- platforms: wallet.tonconnect.walletInfo.platforms
1534
+ platforms: wallet.tonconnect.walletInfo.platforms,
1535
+ features: wallet.tonconnect.walletInfo.features
1509
1536
  }));
1510
1537
  }
1511
1538
  static isWindowContainsWallet(window, injectedWalletKey) {
@@ -2017,15 +2044,12 @@ const FALLBACK_WALLETS_LIST = [
2017
2044
 
2018
2045
  class WalletsListManager {
2019
2046
  constructor(options) {
2047
+ var _a;
2020
2048
  this.walletsListCache = null;
2021
2049
  this.walletsListCacheCreationTimestamp = null;
2022
- this.walletsListSource = 'https://raw.githubusercontent.com/ton-blockchain/wallets-list/main/wallets-v2.json';
2023
- if (options === null || options === void 0 ? void 0 : options.walletsListSource) {
2024
- this.walletsListSource = options.walletsListSource;
2025
- }
2026
- if (options === null || options === void 0 ? void 0 : options.cacheTTLMs) {
2027
- this.cacheTTLMs = options.cacheTTLMs;
2028
- }
2050
+ this.walletsListSource =
2051
+ (_a = options === null || options === void 0 ? void 0 : options.walletsListSource) !== null && _a !== void 0 ? _a : 'https://raw.githubusercontent.com/ton-blockchain/wallets-list/main/wallets-v2.json';
2052
+ this.cacheTTLMs = options === null || options === void 0 ? void 0 : options.cacheTTLMs;
2029
2053
  }
2030
2054
  getWallets() {
2031
2055
  return __awaiter(this, void 0, void 0, function* () {
@@ -2052,10 +2076,7 @@ class WalletsListManager {
2052
2076
  return __awaiter(this, void 0, void 0, function* () {
2053
2077
  const walletsList = yield this.getWallets();
2054
2078
  const embeddedWallets = walletsList.filter(isWalletInfoCurrentlyEmbedded);
2055
- if (embeddedWallets.length !== 1) {
2056
- return null;
2057
- }
2058
- return embeddedWallets[0];
2079
+ return embeddedWallets.length === 1 ? embeddedWallets[0] : null;
2059
2080
  });
2060
2081
  }
2061
2082
  fetchWalletsList() {
@@ -2070,7 +2091,7 @@ class WalletsListManager {
2070
2091
  const wrongFormatWallets = walletsList.filter(wallet => !this.isCorrectWalletConfigDTO(wallet));
2071
2092
  if (wrongFormatWallets.length) {
2072
2093
  logError(`Wallet(s) ${wrongFormatWallets
2073
- .map(wallet => wallet.name)
2094
+ .map(wallet => (wallet === null || wallet === void 0 ? void 0 : wallet.name) || 'unknown')
2074
2095
  .join(', ')} config format is wrong. They were removed from the wallets list.`);
2075
2096
  walletsList = walletsList.filter(wallet => this.isCorrectWalletConfigDTO(wallet));
2076
2097
  }
@@ -2091,15 +2112,15 @@ class WalletsListManager {
2091
2112
  }
2092
2113
  walletConfigDTOListToWalletConfigList(walletConfigDTO) {
2093
2114
  return walletConfigDTO.map(walletConfigDTO => {
2094
- const walletConfigBase = {
2115
+ const walletConfig = {
2095
2116
  name: walletConfigDTO.name,
2096
2117
  appName: walletConfigDTO.app_name,
2097
2118
  imageUrl: walletConfigDTO.image,
2098
2119
  aboutUrl: walletConfigDTO.about_url,
2099
2120
  tondns: walletConfigDTO.tondns,
2100
- platforms: walletConfigDTO.platforms
2121
+ platforms: walletConfigDTO.platforms,
2122
+ features: walletConfigDTO.features
2101
2123
  };
2102
- const walletConfig = walletConfigBase;
2103
2124
  walletConfigDTO.bridge.forEach(bridge => {
2104
2125
  if (bridge.type === 'sse') {
2105
2126
  walletConfig.bridgeUrl = bridge.url;
@@ -2160,7 +2181,7 @@ class WalletsListManager {
2160
2181
  }
2161
2182
  const sseBridge = bridge.find(item => item.type === 'sse');
2162
2183
  if (sseBridge) {
2163
- if (!('url' in sseBridge) ||
2184
+ if (!(typeof sseBridge === 'object' && 'url' in sseBridge) ||
2164
2185
  !sseBridge.url ||
2165
2186
  !value.universal_url) {
2166
2187
  return false;
@@ -2168,7 +2189,9 @@ class WalletsListManager {
2168
2189
  }
2169
2190
  const jsBridge = bridge.find(item => item.type === 'js');
2170
2191
  if (jsBridge) {
2171
- if (!('key' in jsBridge) || !jsBridge.key) {
2192
+ if (typeof jsBridge !== 'object' ||
2193
+ !('key' in jsBridge) ||
2194
+ !jsBridge.key) {
2172
2195
  return false;
2173
2196
  }
2174
2197
  }
@@ -2176,22 +2199,9 @@ class WalletsListManager {
2176
2199
  }
2177
2200
  }
2178
2201
 
2179
- /**
2180
- * Thrown when wallet doesn't support requested feature method.
2181
- */
2182
- class WalletNotSupportFeatureError extends TonConnectError {
2183
- get info() {
2184
- return "Wallet doesn't support requested feature method.";
2185
- }
2186
- constructor(...args) {
2187
- super(...args);
2188
- Object.setPrototypeOf(this, WalletNotSupportFeatureError.prototype);
2189
- }
2190
- }
2191
-
2192
2202
  function checkSendTransactionSupport(features, options) {
2193
2203
  const supportsDeprecatedSendTransactionFeature = features.includes('SendTransaction');
2194
- const sendTransactionFeature = features.find(feature => feature && typeof feature === 'object' && feature.name === 'SendTransaction');
2204
+ const sendTransactionFeature = findFeature(features, 'SendTransaction');
2195
2205
  if (!supportsDeprecatedSendTransactionFeature && !sendTransactionFeature) {
2196
2206
  throw new WalletNotSupportFeatureError("Wallet doesn't support SendTransaction feature.");
2197
2207
  }
@@ -2208,6 +2218,31 @@ function checkSendTransactionSupport(features, options) {
2208
2218
  }
2209
2219
  logWarning("Connected wallet didn't provide information about max allowed messages in the SendTransaction request. Request may be rejected by the wallet.");
2210
2220
  }
2221
+ function checkRequiredWalletFeatures(features, walletsRequiredFeatures) {
2222
+ if (typeof walletsRequiredFeatures !== 'object') {
2223
+ return true;
2224
+ }
2225
+ const { sendTransaction } = walletsRequiredFeatures;
2226
+ if (sendTransaction) {
2227
+ const feature = findFeature(features, 'SendTransaction');
2228
+ if (!feature) {
2229
+ return false;
2230
+ }
2231
+ if (!checkSendTransaction(feature, sendTransaction)) {
2232
+ return false;
2233
+ }
2234
+ }
2235
+ return true;
2236
+ }
2237
+ function findFeature(features, requiredFeatureName) {
2238
+ return features.find(f => f && typeof f === 'object' && f.name === requiredFeatureName);
2239
+ }
2240
+ function checkSendTransaction(feature, requiredFeature) {
2241
+ const correctMessagesNumber = requiredFeature.minMessages === undefined ||
2242
+ requiredFeature.minMessages <= feature.maxMessages;
2243
+ const correctExtraCurrency = !requiredFeature.extraCurrencyRequired || feature.extraCurrencySupported;
2244
+ return !!(correctMessagesNumber && correctExtraCurrency);
2245
+ }
2211
2246
 
2212
2247
  /**
2213
2248
  * Create a request version event.
@@ -2625,7 +2660,7 @@ class TonConnectTracker {
2625
2660
  }
2626
2661
  }
2627
2662
 
2628
- const tonConnectSdkVersion = "3.0.7";
2663
+ const tonConnectSdkVersion = "3.1.0";
2629
2664
 
2630
2665
  class TonConnect {
2631
2666
  constructor(options) {
@@ -2638,6 +2673,7 @@ class TonConnect {
2638
2673
  manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
2639
2674
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
2640
2675
  };
2676
+ this.walletsRequiredFeatures = options === null || options === void 0 ? void 0 : options.walletsRequiredFeatures;
2641
2677
  this.walletsList = new WalletsListManager({
2642
2678
  walletsListSource: options === null || options === void 0 ? void 0 : options.walletsListSource,
2643
2679
  cacheTTLMs: options === null || options === void 0 ? void 0 : options.walletsListCacheTTLMs
@@ -2952,18 +2988,27 @@ class TonConnect {
2952
2988
  this.onWalletConnected(e.payload);
2953
2989
  break;
2954
2990
  case 'connect_error':
2955
- this.onWalletConnectError(e.payload);
2991
+ this.tracker.trackConnectionError(e.payload.message, e.payload.code);
2992
+ const walletError = connectErrorsParser.parseError(e.payload);
2993
+ this.onWalletConnectError(walletError);
2956
2994
  break;
2957
2995
  case 'disconnect':
2958
2996
  this.onWalletDisconnected('wallet');
2959
2997
  }
2960
2998
  }
2961
2999
  onWalletConnected(connectEvent) {
3000
+ var _a;
2962
3001
  const tonAccountItem = connectEvent.items.find(item => item.name === 'ton_addr');
2963
3002
  const tonProofItem = connectEvent.items.find(item => item.name === 'ton_proof');
2964
3003
  if (!tonAccountItem) {
2965
3004
  throw new TonConnectError('ton_addr connection item was not found');
2966
3005
  }
3006
+ const hasRequiredFeatures = checkRequiredWalletFeatures(connectEvent.device.features, this.walletsRequiredFeatures);
3007
+ if (!hasRequiredFeatures) {
3008
+ (_a = this.provider) === null || _a === void 0 ? void 0 : _a.disconnect();
3009
+ this.onWalletConnectError(new WalletMissingRequiredFeaturesError('Wallet does not support required features', { cause: { connectEvent } }));
3010
+ return;
3011
+ }
2967
3012
  const wallet = {
2968
3013
  device: connectEvent.device,
2969
3014
  provider: this.provider.type,
@@ -2982,11 +3027,9 @@ class TonConnect {
2982
3027
  this.wallet = wallet;
2983
3028
  this.tracker.trackConnectionCompleted(wallet);
2984
3029
  }
2985
- onWalletConnectError(connectEventError) {
2986
- const error = connectErrorsParser.parseError(connectEventError);
3030
+ onWalletConnectError(error) {
2987
3031
  this.statusChangeErrorSubscriptions.forEach(errorsHandler => errorsHandler(error));
2988
3032
  logDebug(error);
2989
- this.tracker.trackConnectionError(connectEventError.message, connectEventError.code);
2990
3033
  if (error instanceof ManifestNotFoundError || error instanceof ManifestContentErrorError) {
2991
3034
  logError(error);
2992
3035
  throw error;
@@ -3123,5 +3166,5 @@ function hexToBytes(hex) {
3123
3166
  return result;
3124
3167
  }
3125
3168
 
3126
- 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 };
3169
+ export { BadRequestError, BrowserEventDispatcher, FetchWalletsError, LocalstorageNotFoundError, ParseHexError, TonConnect, TonConnectError, UnknownAppError, UnknownError, UserRejectsError, WalletAlreadyConnectedError, WalletMissingRequiredFeaturesError, WalletNotConnectedError, WalletNotInjectedError, WalletNotSupportFeatureError, WalletsListManager, WrongAddressError, checkRequiredWalletFeatures, createConnectionCompletedEvent, createConnectionErrorEvent, createConnectionRestoringCompletedEvent, createConnectionRestoringErrorEvent, createConnectionRestoringStartedEvent, createConnectionStartedEvent, createDisconnectionEvent, createRequestVersionEvent, createResponseVersionEvent, createTransactionSentForSignatureEvent, createTransactionSignedEvent, createTransactionSigningFailedEvent, createVersionInfo, TonConnect as default, encodeTelegramUrlParameters, isTelegramUrl, isWalletInfoCurrentlyEmbedded, isWalletInfoCurrentlyInjected, isWalletInfoInjectable, isWalletInfoInjected, isWalletInfoRemote, toUserFriendlyAddress };
3127
3170
  //# sourceMappingURL=index.mjs.map