@tonconnect/sdk 3.0.7-alpha.1 → 3.0.8-beta.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/cjs/index.cjs CHANGED
@@ -123,6 +123,45 @@ class WalletNotConnectedError extends TonConnectError {
123
123
  }
124
124
  }
125
125
 
126
+ /**
127
+ * Thrown when there is an attempt to connect to the injected wallet while it is not exists in the webpage.
128
+ */
129
+ class WalletNotInjectedError extends TonConnectError {
130
+ get info() {
131
+ return 'There is an attempt to connect to the injected wallet while it is not exists in the webpage.';
132
+ }
133
+ constructor(...args) {
134
+ super(...args);
135
+ Object.setPrototypeOf(this, WalletNotInjectedError.prototype);
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Thrown when wallet doesn't support requested feature method.
141
+ */
142
+ class WalletNotSupportFeatureError extends TonConnectError {
143
+ get info() {
144
+ return "Wallet doesn't support requested feature method.";
145
+ }
146
+ constructor(...args) {
147
+ super(...args);
148
+ Object.setPrototypeOf(this, WalletNotSupportFeatureError.prototype);
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Thrown when wallet can't get manifest by passed manifestUrl.
154
+ */
155
+ class WalletMissingRequiredFeaturesError extends TonConnectError {
156
+ get info() {
157
+ return 'Missing required features. You need to update your wallet.';
158
+ }
159
+ constructor(message, options) {
160
+ super(message, options);
161
+ Object.setPrototypeOf(this, WalletMissingRequiredFeaturesError.prototype);
162
+ }
163
+ }
164
+
126
165
  function isWalletConnectionSourceJS(value) {
127
166
  return 'jsBridgeKey' in value;
128
167
  }
@@ -166,19 +205,6 @@ class UnknownAppError extends TonConnectError {
166
205
  }
167
206
  }
168
207
 
169
- /**
170
- * Thrown when there is an attempt to connect to the injected wallet while it is not exists in the webpage.
171
- */
172
- class WalletNotInjectedError extends TonConnectError {
173
- get info() {
174
- return 'There is an attempt to connect to the injected wallet while it is not exists in the webpage.';
175
- }
176
- constructor(...args) {
177
- super(...args);
178
- Object.setPrototypeOf(this, WalletNotInjectedError.prototype);
179
- }
180
- }
181
-
182
208
  /**
183
209
  * Thrown when `Storage` was not specified in the `DappMetadata` and default `localStorage` was not detected in the Node.js environment.
184
210
  */
@@ -1508,7 +1534,8 @@ class InjectedProvider {
1508
1534
  jsBridgeKey,
1509
1535
  injected: true,
1510
1536
  embedded: wallet.tonconnect.isWalletBrowser,
1511
- platforms: wallet.tonconnect.walletInfo.platforms
1537
+ platforms: wallet.tonconnect.walletInfo.platforms,
1538
+ features: wallet.tonconnect.walletInfo.features
1512
1539
  }));
1513
1540
  }
1514
1541
  static isWindowContainsWallet(window, injectedWalletKey) {
@@ -2018,17 +2045,61 @@ const FALLBACK_WALLETS_LIST = [
2018
2045
  }
2019
2046
  ];
2020
2047
 
2048
+ function checkSendTransactionSupport(features, options) {
2049
+ const supportsDeprecatedSendTransactionFeature = features.includes('SendTransaction');
2050
+ const sendTransactionFeature = features.find(feature => feature && typeof feature === 'object' && feature.name === 'SendTransaction');
2051
+ if (!supportsDeprecatedSendTransactionFeature && !sendTransactionFeature) {
2052
+ throw new WalletNotSupportFeatureError("Wallet doesn't support SendTransaction feature.");
2053
+ }
2054
+ if (options.requireExtraCurrencies) {
2055
+ if (!sendTransactionFeature || !sendTransactionFeature.extraCurrencySupported) {
2056
+ throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Extra currencies support is required.`);
2057
+ }
2058
+ }
2059
+ if (sendTransactionFeature && sendTransactionFeature.maxMessages !== undefined) {
2060
+ if (sendTransactionFeature.maxMessages < options.requiredMessagesNumber) {
2061
+ throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Max support messages number is ${sendTransactionFeature.maxMessages}, but ${options.requiredMessagesNumber} is required.`);
2062
+ }
2063
+ return;
2064
+ }
2065
+ logWarning("Connected wallet didn't provide information about max allowed messages in the SendTransaction request. Request may be rejected by the wallet.");
2066
+ }
2067
+ function checkRequiredWalletFeatures(features, walletsRequiredFeatures) {
2068
+ if (typeof walletsRequiredFeatures === 'function') {
2069
+ return walletsRequiredFeatures(features);
2070
+ }
2071
+ const res = walletsRequiredFeatures.every(requiredFeature => {
2072
+ const feature = features.find(f => typeof f === 'object' && f.name === requiredFeature.name);
2073
+ if (!feature) {
2074
+ return false;
2075
+ }
2076
+ switch (requiredFeature.name) {
2077
+ case 'SendTransaction': {
2078
+ const sendTransactionFeature = feature;
2079
+ const correctMessagesNumber = requiredFeature.minMessages === undefined ||
2080
+ requiredFeature.minMessages <= sendTransactionFeature.maxMessages;
2081
+ const correctExtraCurrency = !requiredFeature.extraCurrencyRequired ||
2082
+ sendTransactionFeature.extraCurrencySupported;
2083
+ return correctMessagesNumber && correctExtraCurrency;
2084
+ }
2085
+ default:
2086
+ return false;
2087
+ }
2088
+ });
2089
+ return res;
2090
+ }
2091
+
2021
2092
  class WalletsListManager {
2022
2093
  constructor(options) {
2094
+ var _a;
2023
2095
  this.walletsListCache = null;
2024
2096
  this.walletsListCacheCreationTimestamp = null;
2025
- this.walletsListSource = 'https://raw.githubusercontent.com/ton-blockchain/wallets-list/main/wallets-v2.json';
2026
- if (options === null || options === void 0 ? void 0 : options.walletsListSource) {
2027
- this.walletsListSource = options.walletsListSource;
2028
- }
2029
- if (options === null || options === void 0 ? void 0 : options.cacheTTLMs) {
2030
- this.cacheTTLMs = options.cacheTTLMs;
2031
- }
2097
+ this.walletsListSource =
2098
+ (_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';
2099
+ this.cacheTTLMs = options === null || options === void 0 ? void 0 : options.cacheTTLMs;
2100
+ this.checkRequiredFeatures = (options === null || options === void 0 ? void 0 : options.walletsRequiredFeatures)
2101
+ ? features => checkRequiredWalletFeatures(features !== null && features !== void 0 ? features : [], options.walletsRequiredFeatures)
2102
+ : () => true;
2032
2103
  }
2033
2104
  getWallets() {
2034
2105
  return __awaiter(this, void 0, void 0, function* () {
@@ -2055,10 +2126,7 @@ class WalletsListManager {
2055
2126
  return __awaiter(this, void 0, void 0, function* () {
2056
2127
  const walletsList = yield this.getWallets();
2057
2128
  const embeddedWallets = walletsList.filter(isWalletInfoCurrentlyEmbedded);
2058
- if (embeddedWallets.length !== 1) {
2059
- return null;
2060
- }
2061
- return embeddedWallets[0];
2129
+ return embeddedWallets.length === 1 ? embeddedWallets[0] : null;
2062
2130
  });
2063
2131
  }
2064
2132
  fetchWalletsList() {
@@ -2073,7 +2141,7 @@ class WalletsListManager {
2073
2141
  const wrongFormatWallets = walletsList.filter(wallet => !this.isCorrectWalletConfigDTO(wallet));
2074
2142
  if (wrongFormatWallets.length) {
2075
2143
  logError(`Wallet(s) ${wrongFormatWallets
2076
- .map(wallet => wallet.name)
2144
+ .map(wallet => (wallet === null || wallet === void 0 ? void 0 : wallet.name) || 'unknown')
2077
2145
  .join(', ')} config format is wrong. They were removed from the wallets list.`);
2078
2146
  walletsList = walletsList.filter(wallet => this.isCorrectWalletConfigDTO(wallet));
2079
2147
  }
@@ -2094,15 +2162,15 @@ class WalletsListManager {
2094
2162
  }
2095
2163
  walletConfigDTOListToWalletConfigList(walletConfigDTO) {
2096
2164
  return walletConfigDTO.map(walletConfigDTO => {
2097
- const walletConfigBase = {
2165
+ const walletConfig = {
2098
2166
  name: walletConfigDTO.name,
2099
2167
  appName: walletConfigDTO.app_name,
2100
2168
  imageUrl: walletConfigDTO.image,
2101
2169
  aboutUrl: walletConfigDTO.about_url,
2102
2170
  tondns: walletConfigDTO.tondns,
2103
- platforms: walletConfigDTO.platforms
2171
+ platforms: walletConfigDTO.platforms,
2172
+ features: walletConfigDTO.features
2104
2173
  };
2105
- const walletConfig = walletConfigBase;
2106
2174
  walletConfigDTO.bridge.forEach(bridge => {
2107
2175
  if (bridge.type === 'sse') {
2108
2176
  walletConfig.bridgeUrl = bridge.url;
@@ -2163,7 +2231,7 @@ class WalletsListManager {
2163
2231
  }
2164
2232
  const sseBridge = bridge.find(item => item.type === 'sse');
2165
2233
  if (sseBridge) {
2166
- if (!('url' in sseBridge) ||
2234
+ if (!(typeof sseBridge === 'object' && 'url' in sseBridge) ||
2167
2235
  !sseBridge.url ||
2168
2236
  !value.universal_url) {
2169
2237
  return false;
@@ -2171,7 +2239,9 @@ class WalletsListManager {
2171
2239
  }
2172
2240
  const jsBridge = bridge.find(item => item.type === 'js');
2173
2241
  if (jsBridge) {
2174
- if (!('key' in jsBridge) || !jsBridge.key) {
2242
+ if (typeof jsBridge !== 'object' ||
2243
+ !('key' in jsBridge) ||
2244
+ !jsBridge.key) {
2175
2245
  return false;
2176
2246
  }
2177
2247
  }
@@ -2179,39 +2249,6 @@ class WalletsListManager {
2179
2249
  }
2180
2250
  }
2181
2251
 
2182
- /**
2183
- * Thrown when wallet doesn't support requested feature method.
2184
- */
2185
- class WalletNotSupportFeatureError extends TonConnectError {
2186
- get info() {
2187
- return "Wallet doesn't support requested feature method.";
2188
- }
2189
- constructor(...args) {
2190
- super(...args);
2191
- Object.setPrototypeOf(this, WalletNotSupportFeatureError.prototype);
2192
- }
2193
- }
2194
-
2195
- function checkSendTransactionSupport(features, options) {
2196
- const supportsDeprecatedSendTransactionFeature = features.includes('SendTransaction');
2197
- const sendTransactionFeature = features.find(feature => feature && typeof feature === 'object' && feature.name === 'SendTransaction');
2198
- if (!supportsDeprecatedSendTransactionFeature && !sendTransactionFeature) {
2199
- throw new WalletNotSupportFeatureError("Wallet doesn't support SendTransaction feature.");
2200
- }
2201
- if (options.requireExtraCurrencies) {
2202
- if (!sendTransactionFeature || !sendTransactionFeature.extraCurrencySupported) {
2203
- throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Extra currencies support is required.`);
2204
- }
2205
- }
2206
- if (sendTransactionFeature && sendTransactionFeature.maxMessages !== undefined) {
2207
- if (sendTransactionFeature.maxMessages < options.requiredMessagesNumber) {
2208
- throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Max support messages number is ${sendTransactionFeature.maxMessages}, but ${options.requiredMessagesNumber} is required.`);
2209
- }
2210
- return;
2211
- }
2212
- logWarning("Connected wallet didn't provide information about max allowed messages in the SendTransaction request. Request may be rejected by the wallet.");
2213
- }
2214
-
2215
2252
  /**
2216
2253
  * Create a request version event.
2217
2254
  */
@@ -2628,7 +2665,7 @@ class TonConnectTracker {
2628
2665
  }
2629
2666
  }
2630
2667
 
2631
- const tonConnectSdkVersion = "3.0.7-alpha.1";
2668
+ const tonConnectSdkVersion = "3.0.8-beta.0";
2632
2669
 
2633
2670
  class TonConnect {
2634
2671
  constructor(options) {
@@ -2641,9 +2678,11 @@ class TonConnect {
2641
2678
  manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
2642
2679
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
2643
2680
  };
2681
+ this.walletsRequiredFeatures = options === null || options === void 0 ? void 0 : options.walletsRequiredFeatures;
2644
2682
  this.walletsList = new WalletsListManager({
2645
2683
  walletsListSource: options === null || options === void 0 ? void 0 : options.walletsListSource,
2646
- cacheTTLMs: options === null || options === void 0 ? void 0 : options.walletsListCacheTTLMs
2684
+ cacheTTLMs: options === null || options === void 0 ? void 0 : options.walletsListCacheTTLMs,
2685
+ walletsRequiredFeatures: options === null || options === void 0 ? void 0 : options.walletsRequiredFeatures
2647
2686
  });
2648
2687
  this.tracker = new TonConnectTracker({
2649
2688
  eventDispatcher: options === null || options === void 0 ? void 0 : options.eventDispatcher,
@@ -2955,18 +2994,27 @@ class TonConnect {
2955
2994
  this.onWalletConnected(e.payload);
2956
2995
  break;
2957
2996
  case 'connect_error':
2958
- this.onWalletConnectError(e.payload);
2997
+ this.tracker.trackConnectionError(e.payload.message, e.payload.code);
2998
+ const walletError = connectErrorsParser.parseError(e.payload);
2999
+ this.onWalletConnectError(walletError);
2959
3000
  break;
2960
3001
  case 'disconnect':
2961
3002
  this.onWalletDisconnected('wallet');
2962
3003
  }
2963
3004
  }
2964
3005
  onWalletConnected(connectEvent) {
3006
+ var _a, _b;
2965
3007
  const tonAccountItem = connectEvent.items.find(item => item.name === 'ton_addr');
2966
3008
  const tonProofItem = connectEvent.items.find(item => item.name === 'ton_proof');
2967
3009
  if (!tonAccountItem) {
2968
3010
  throw new TonConnectError('ton_addr connection item was not found');
2969
3011
  }
3012
+ const hasRequiredFeatures = checkRequiredWalletFeatures(connectEvent.device.features, (_a = this.walletsRequiredFeatures) !== null && _a !== void 0 ? _a : []);
3013
+ if (!hasRequiredFeatures) {
3014
+ (_b = this.provider) === null || _b === void 0 ? void 0 : _b.disconnect();
3015
+ this.onWalletConnectError(new WalletMissingRequiredFeaturesError('Wallet does not support required features', { cause: { connectEvent } }));
3016
+ return;
3017
+ }
2970
3018
  const wallet = {
2971
3019
  device: connectEvent.device,
2972
3020
  provider: this.provider.type,
@@ -2985,11 +3033,9 @@ class TonConnect {
2985
3033
  this.wallet = wallet;
2986
3034
  this.tracker.trackConnectionCompleted(wallet);
2987
3035
  }
2988
- onWalletConnectError(connectEventError) {
2989
- const error = connectErrorsParser.parseError(connectEventError);
3036
+ onWalletConnectError(error) {
2990
3037
  this.statusChangeErrorSubscriptions.forEach(errorsHandler => errorsHandler(error));
2991
3038
  logDebug(error);
2992
- this.tracker.trackConnectionError(connectEventError.message, connectEventError.code);
2993
3039
  if (error instanceof ManifestNotFoundError || error instanceof ManifestContentErrorError) {
2994
3040
  logError(error);
2995
3041
  throw error;
@@ -3153,10 +3199,13 @@ exports.UnknownAppError = UnknownAppError;
3153
3199
  exports.UnknownError = UnknownError;
3154
3200
  exports.UserRejectsError = UserRejectsError;
3155
3201
  exports.WalletAlreadyConnectedError = WalletAlreadyConnectedError;
3202
+ exports.WalletMissingRequiredFeaturesError = WalletMissingRequiredFeaturesError;
3156
3203
  exports.WalletNotConnectedError = WalletNotConnectedError;
3157
3204
  exports.WalletNotInjectedError = WalletNotInjectedError;
3205
+ exports.WalletNotSupportFeatureError = WalletNotSupportFeatureError;
3158
3206
  exports.WalletsListManager = WalletsListManager;
3159
3207
  exports.WrongAddressError = WrongAddressError;
3208
+ exports.checkRequiredWalletFeatures = checkRequiredWalletFeatures;
3160
3209
  exports.createConnectionCompletedEvent = createConnectionCompletedEvent;
3161
3210
  exports.createConnectionErrorEvent = createConnectionErrorEvent;
3162
3211
  exports.createConnectionRestoringCompletedEvent = createConnectionRestoringCompletedEvent;