@tonconnect/sdk 3.4.0-beta.0 → 3.4.0-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
@@ -166,6 +166,14 @@ class WalletMissingRequiredFeaturesError extends TonConnectError {
166
166
  }
167
167
  }
168
168
 
169
+ class WalletWrongNetworkError extends TonConnectError {
170
+ constructor(message, options) {
171
+ super(message, options);
172
+ this.name = 'WalletWrongNetworkError';
173
+ Object.setPrototypeOf(this, WalletWrongNetworkError.prototype);
174
+ }
175
+ }
176
+
169
177
  function isWalletConnectionSourceJS(value) {
170
178
  return 'jsBridgeKey' in value;
171
179
  }
@@ -3783,7 +3791,7 @@ class TonConnectTracker {
3783
3791
  }
3784
3792
  }
3785
3793
 
3786
- const tonConnectSdkVersion = "3.4.0-beta.0";
3794
+ const tonConnectSdkVersion = "3.4.0-beta.1";
3787
3795
 
3788
3796
  const bounceableTag = 0x11;
3789
3797
  const noBounceableTag = 0x51;
@@ -4781,10 +4789,13 @@ class TonConnect {
4781
4789
  var _a, _b, _c;
4782
4790
  // TODO: remove deprecated method
4783
4791
  const options = Object.assign({}, additionalOptions);
4784
- if (typeof requestOrOptions === 'object' && 'tonProof' in requestOrOptions) {
4792
+ if (typeof requestOrOptions === 'object' &&
4793
+ requestOrOptions !== null &&
4794
+ 'tonProof' in requestOrOptions) {
4785
4795
  options.request = requestOrOptions;
4786
4796
  }
4787
4797
  if (typeof requestOrOptions === 'object' &&
4798
+ requestOrOptions !== null &&
4788
4799
  ('openingDeadlineMS' in requestOrOptions ||
4789
4800
  'signal' in requestOrOptions ||
4790
4801
  'request' in requestOrOptions ||
@@ -4922,7 +4933,7 @@ class TonConnect {
4922
4933
  }
4923
4934
  sendTransaction(transaction, optionsOrOnRequestSent) {
4924
4935
  return __awaiter(this, void 0, void 0, function* () {
4925
- var _a;
4936
+ var _a, _b, _c;
4926
4937
  // TODO: remove deprecated method
4927
4938
  const options = {};
4928
4939
  if (typeof optionsOrOnRequestSent === 'function') {
@@ -4960,6 +4971,14 @@ class TonConnect {
4960
4971
  const { validUntil, messages } = transaction, tx = __rest(transaction, ["validUntil", "messages"]);
4961
4972
  const from = transaction.from || this.account.address;
4962
4973
  const network = transaction.network || this.account.chain;
4974
+ if (((_b = this.wallet) === null || _b === void 0 ? void 0 : _b.account.chain) && network !== this.wallet.account.chain) {
4975
+ throw new WalletWrongNetworkError('Wallet connected to a wrong network', {
4976
+ cause: {
4977
+ expectedChainId: (_c = this.wallet) === null || _c === void 0 ? void 0 : _c.account.chain,
4978
+ actualChainId: network
4979
+ }
4980
+ });
4981
+ }
4963
4982
  const response = yield this.provider.sendRequest(sendTransactionParser.convertToRpcRequest(Object.assign(Object.assign({}, tx), { from,
4964
4983
  network, valid_until: validUntil, messages: messages.map((_a) => {
4965
4984
  var { extraCurrency, payload, stateInit } = _a, msg = __rest(_a, ["extraCurrency", "payload", "stateInit"]);
@@ -4980,7 +4999,7 @@ class TonConnect {
4980
4999
  }
4981
5000
  signData(data, options) {
4982
5001
  return __awaiter(this, void 0, void 0, function* () {
4983
- var _a;
5002
+ var _a, _b, _c;
4984
5003
  const abortController = createAbortController(options === null || options === void 0 ? void 0 : options.signal);
4985
5004
  if (abortController.signal.aborted) {
4986
5005
  throw new TonConnectError('data sending was aborted');
@@ -5002,6 +5021,14 @@ class TonConnect {
5002
5021
  this.tracker.trackDataSentForSignature(this.wallet, data, sessionInfo, traceId);
5003
5022
  const from = data.from || this.account.address;
5004
5023
  const network = data.network || this.account.chain;
5024
+ if (((_b = this.wallet) === null || _b === void 0 ? void 0 : _b.account.chain) && network !== this.wallet.account.chain) {
5025
+ throw new WalletWrongNetworkError('Wallet connected to a wrong network', {
5026
+ cause: {
5027
+ expectedChainId: (_c = this.wallet) === null || _c === void 0 ? void 0 : _c.account.chain,
5028
+ actualChainId: network
5029
+ }
5030
+ });
5031
+ }
5005
5032
  const response = yield this.provider.sendRequest(signDataParser.convertToRpcRequest(Object.assign(Object.assign(Object.assign({}, data), (data.type === 'cell' ? { cell: normalizeBase64(data.cell) } : {})), { from,
5006
5033
  network })), { onRequestSent: options === null || options === void 0 ? void 0 : options.onRequestSent, signal: abortController.signal, traceId });
5007
5034
  if (signDataParser.isError(response)) {
@@ -5013,6 +5040,17 @@ class TonConnect {
5013
5040
  return Object.assign(Object.assign({}, result), { traceId });
5014
5041
  });
5015
5042
  }
5043
+ /**
5044
+ * Set desired network for the connection. Can only be set before connecting.
5045
+ * If wallet connects with a different chain, the SDK will throw an error and abort connection.
5046
+ * @param network desired network id (e.g., '-239', '-3', or custom). Pass undefined to allow any network.
5047
+ */
5048
+ setConnectionNetwork(network) {
5049
+ if (this.connected) {
5050
+ throw new TonConnectError('Cannot change network while wallet is connected');
5051
+ }
5052
+ this.desiredChainId = network;
5053
+ }
5016
5054
  /**
5017
5055
  * Disconnect form thw connected wallet and drop current session.
5018
5056
  */
@@ -5155,7 +5193,7 @@ class TonConnect {
5155
5193
  }
5156
5194
  }
5157
5195
  onWalletConnected(connectEvent, options) {
5158
- var _a;
5196
+ var _a, _b;
5159
5197
  const tonAccountItem = connectEvent.items.find(item => item.name === 'ton_addr');
5160
5198
  const tonProofItem = connectEvent.items.find(item => item.name === 'ton_proof');
5161
5199
  if (!tonAccountItem) {
@@ -5177,6 +5215,15 @@ class TonConnect {
5177
5215
  publicKey: tonAccountItem.publicKey
5178
5216
  }
5179
5217
  };
5218
+ if (this.desiredChainId && wallet.account.chain !== this.desiredChainId) {
5219
+ const expectedChainId = this.desiredChainId;
5220
+ const actualChainId = wallet.account.chain;
5221
+ (_b = this.provider) === null || _b === void 0 ? void 0 : _b.disconnect();
5222
+ this.onWalletConnectError(new WalletWrongNetworkError('Wallet connected to a wrong network', {
5223
+ cause: { expectedChainId, actualChainId }
5224
+ }));
5225
+ return;
5226
+ }
5180
5227
  if (tonProofItem) {
5181
5228
  const validationError = validateTonProofItemReply(tonProofItem);
5182
5229
  let tonProof = undefined;
@@ -5257,9 +5304,7 @@ class TonConnect {
5257
5304
  }
5258
5305
  createConnectRequest(request) {
5259
5306
  const items = [
5260
- {
5261
- name: 'ton_addr'
5262
- }
5307
+ Object.assign({ name: 'ton_addr' }, (this.desiredChainId ? { network: this.desiredChainId } : {}))
5263
5308
  ];
5264
5309
  if (request === null || request === void 0 ? void 0 : request.tonProof) {
5265
5310
  items.push({
@@ -5285,5 +5330,5 @@ TonConnect.isWalletInjected = (walletJSKey) => InjectedProvider.isWalletInjected
5285
5330
  */
5286
5331
  TonConnect.isInsideWalletBrowser = (walletJSKey) => InjectedProvider.isInsideWalletBrowser(walletJSKey);
5287
5332
 
5288
- export { BadRequestError, BrowserEventDispatcher, FetchWalletsError, LocalstorageNotFoundError, ParseHexError, TonConnect, TonConnectError, UUIDv7, UnknownAppError, UnknownError, UserRejectsError, WalletAlreadyConnectedError, WalletMissingRequiredFeaturesError, WalletNotConnectedError, WalletNotInjectedError, WalletNotSupportFeatureError, WalletsListManager, WrongAddressError, checkRequiredWalletFeatures, createConnectionCompletedEvent, createConnectionErrorEvent, createConnectionRestoringCompletedEvent, createConnectionRestoringErrorEvent, createConnectionRestoringStartedEvent, createConnectionStartedEvent, createDataSentForSignatureEvent, createDataSignedEvent, createDataSigningFailedEvent, createDisconnectionEvent, createRequestVersionEvent, createResponseVersionEvent, createSelectedWalletEvent, createTransactionSentForSignatureEvent, createTransactionSignedEvent, createTransactionSigningFailedEvent, createVersionInfo, createWalletModalOpenedEvent, decodeTelegramUrlParameters, TonConnect as default, enableQaMode, encodeTelegramUrlParameters, isConnectUrl, isQaModeEnabled, isTelegramUrl, isWalletInfoCurrentlyEmbedded, isWalletInfoCurrentlyInjected, isWalletInfoInjectable, isWalletInfoInjected, isWalletInfoRemote, toUserFriendlyAddress };
5333
+ export { BadRequestError, BrowserEventDispatcher, FetchWalletsError, LocalstorageNotFoundError, ParseHexError, TonConnect, TonConnectError, UUIDv7, UnknownAppError, UnknownError, UserRejectsError, WalletAlreadyConnectedError, WalletMissingRequiredFeaturesError, WalletNotConnectedError, WalletNotInjectedError, WalletNotSupportFeatureError, WalletWrongNetworkError, WalletsListManager, WrongAddressError, checkRequiredWalletFeatures, createConnectionCompletedEvent, createConnectionErrorEvent, createConnectionRestoringCompletedEvent, createConnectionRestoringErrorEvent, createConnectionRestoringStartedEvent, createConnectionStartedEvent, createDataSentForSignatureEvent, createDataSignedEvent, createDataSigningFailedEvent, createDisconnectionEvent, createRequestVersionEvent, createResponseVersionEvent, createSelectedWalletEvent, createTransactionSentForSignatureEvent, createTransactionSignedEvent, createTransactionSigningFailedEvent, createVersionInfo, createWalletModalOpenedEvent, decodeTelegramUrlParameters, TonConnect as default, enableQaMode, encodeTelegramUrlParameters, isConnectUrl, isQaModeEnabled, isTelegramUrl, isWalletInfoCurrentlyEmbedded, isWalletInfoCurrentlyInjected, isWalletInfoInjectable, isWalletInfoInjected, isWalletInfoRemote, toUserFriendlyAddress };
5289
5334
  //# sourceMappingURL=index.mjs.map