@tonconnect/ui-react 0.0.4 → 0.0.5

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/index.umd.js CHANGED
@@ -4221,7 +4221,8 @@ var __objRest = (source, exclude) => {
4221
4221
  });
4222
4222
  };
4223
4223
  class InjectedProvider {
4224
- constructor(injectedWalletKey) {
4224
+ constructor(storage, injectedWalletKey) {
4225
+ this.injectedWalletKey = injectedWalletKey;
4225
4226
  this.type = "injected";
4226
4227
  this.unsubscribeCallback = null;
4227
4228
  this.listenSubscriptions = false;
@@ -4230,13 +4231,14 @@ var __objRest = (source, exclude) => {
4230
4231
  if (!InjectedProvider.isWindowContainsWallet(window2, injectedWalletKey)) {
4231
4232
  throw new WalletNotInjectedError();
4232
4233
  }
4234
+ this.connectionStorage = new BridgeConnectionStorage(storage);
4233
4235
  this.injectedWallet = window2[injectedWalletKey].tonconnect;
4234
4236
  }
4235
4237
  static fromStorage(storage) {
4236
4238
  return __awaiter$3(this, void 0, void 0, function* () {
4237
4239
  const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
4238
4240
  const connection = yield bridgeConnectionStorage.getInjectedConnection();
4239
- return new InjectedProvider(connection.jsBridgeKey);
4241
+ return new InjectedProvider(storage, connection.jsBridgeKey);
4240
4242
  });
4241
4243
  }
4242
4244
  static isWalletInjected(injectedWalletKey) {
@@ -4251,23 +4253,8 @@ var __objRest = (source, exclude) => {
4251
4253
  static isWindowContainsWallet(window2, injectedWalletKey) {
4252
4254
  return !!window2 && injectedWalletKey in window2 && typeof window2[injectedWalletKey] === "object" && "tonconnect" in window2[injectedWalletKey];
4253
4255
  }
4254
- connect(message, auto = false) {
4255
- this.injectedWallet.connect(PROTOCOL_VERSION, message, auto).then((connectEvent) => {
4256
- if (connectEvent.event === "connect") {
4257
- this.makeSubscriptions();
4258
- this.listenSubscriptions = true;
4259
- }
4260
- this.listeners.forEach((listener) => listener(connectEvent));
4261
- }).catch((e2) => {
4262
- const connectEventError = {
4263
- event: "connect_error",
4264
- payload: {
4265
- code: 0,
4266
- message: e2 === null || e2 === void 0 ? void 0 : e2.toString()
4267
- }
4268
- };
4269
- this.listeners.forEach((listener) => listener(connectEventError));
4270
- });
4256
+ connect(message) {
4257
+ this._connect(PROTOCOL_VERSION, message);
4271
4258
  }
4272
4259
  restoreConnection() {
4273
4260
  return __awaiter$3(this, void 0, void 0, function* () {
@@ -4275,10 +4262,12 @@ var __objRest = (source, exclude) => {
4275
4262
  const connectEvent = yield this.injectedWallet.restoreConnection();
4276
4263
  if (connectEvent.event === "connect") {
4277
4264
  this.makeSubscriptions();
4278
- this.listenSubscriptions = true;
4279
4265
  this.listeners.forEach((listener) => listener(connectEvent));
4266
+ } else {
4267
+ yield this.connectionStorage.removeConnection();
4280
4268
  }
4281
4269
  } catch (e2) {
4270
+ yield this.connectionStorage.removeConnection();
4282
4271
  console.error(e2);
4283
4272
  }
4284
4273
  });
@@ -4292,7 +4281,7 @@ var __objRest = (source, exclude) => {
4292
4281
  disconnect() {
4293
4282
  this.closeAllListeners();
4294
4283
  this.injectedWallet.disconnect();
4295
- return Promise.resolve();
4284
+ return this.connectionStorage.removeConnection();
4296
4285
  }
4297
4286
  closeAllListeners() {
4298
4287
  var _a;
@@ -4309,7 +4298,30 @@ var __objRest = (source, exclude) => {
4309
4298
  return this.injectedWallet.send(Object.assign(Object.assign({}, request), { id: "0" }));
4310
4299
  });
4311
4300
  }
4301
+ _connect(protocolVersion, message) {
4302
+ return __awaiter$3(this, void 0, void 0, function* () {
4303
+ try {
4304
+ const connectEvent = yield this.injectedWallet.connect(protocolVersion, message);
4305
+ if (connectEvent.event === "connect") {
4306
+ yield this.updateSession();
4307
+ this.makeSubscriptions();
4308
+ }
4309
+ this.listeners.forEach((listener) => listener(connectEvent));
4310
+ } catch (e2) {
4311
+ console.debug(e2);
4312
+ const connectEventError = {
4313
+ event: "connect_error",
4314
+ payload: {
4315
+ code: 0,
4316
+ message: e2 === null || e2 === void 0 ? void 0 : e2.toString()
4317
+ }
4318
+ };
4319
+ this.listeners.forEach((listener) => listener(connectEventError));
4320
+ }
4321
+ });
4322
+ }
4312
4323
  makeSubscriptions() {
4324
+ this.listenSubscriptions = true;
4313
4325
  this.unsubscribeCallback = this.injectedWallet.listen((e2) => {
4314
4326
  if (this.listenSubscriptions) {
4315
4327
  this.listeners.forEach((listener) => listener(e2));
@@ -4319,6 +4331,12 @@ var __objRest = (source, exclude) => {
4319
4331
  }
4320
4332
  });
4321
4333
  }
4334
+ updateSession() {
4335
+ return this.connectionStorage.storeConnection({
4336
+ type: "injected",
4337
+ jsBridgeKey: this.injectedWalletKey
4338
+ });
4339
+ }
4322
4340
  }
4323
4341
  InjectedProvider.window = getWindow$1();
4324
4342
  var __awaiter$2 = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P2, generator) {
@@ -4403,9 +4421,12 @@ var __objRest = (source, exclude) => {
4403
4421
  });
4404
4422
  };
4405
4423
  class WalletsListManager {
4406
- constructor() {
4424
+ constructor(walletsListSource) {
4407
4425
  this.walletsListCache = null;
4408
4426
  this.walletsListSource = "https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json";
4427
+ if (walletsListSource) {
4428
+ this.walletsListSource = walletsListSource;
4429
+ }
4409
4430
  }
4410
4431
  getWallets() {
4411
4432
  return __awaiter$1(this, void 0, void 0, function* () {
@@ -4419,11 +4440,11 @@ var __objRest = (source, exclude) => {
4419
4440
  getEmbeddedWallet() {
4420
4441
  return __awaiter$1(this, void 0, void 0, function* () {
4421
4442
  const walletsList = yield this.getWallets();
4422
- const injectedWallets = walletsList.filter(isWalletInfoInjected);
4423
- if (injectedWallets.length !== 1) {
4443
+ const embeddedWallets = walletsList.filter((item) => isWalletInfoInjected(item) && item.embedded);
4444
+ if (embeddedWallets.length !== 1) {
4424
4445
  return null;
4425
4446
  }
4426
- return injectedWallets[0].embedded ? injectedWallets[0] : null;
4447
+ return embeddedWallets[0];
4427
4448
  });
4428
4449
  }
4429
4450
  fetchWalletsList() {
@@ -4546,6 +4567,7 @@ var __objRest = (source, exclude) => {
4546
4567
  manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
4547
4568
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
4548
4569
  };
4570
+ this.walletsList = new WalletsListManager(options === null || options === void 0 ? void 0 : options.walletsListSource);
4549
4571
  if (!this.dappSettings.manifestUrl) {
4550
4572
  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");
4551
4573
  }
@@ -4640,7 +4662,7 @@ var __objRest = (source, exclude) => {
4640
4662
  createProvider(wallet) {
4641
4663
  let provider;
4642
4664
  if (isWalletConnectionSourceJS(wallet)) {
4643
- provider = new InjectedProvider(wallet.jsBridgeKey);
4665
+ provider = new InjectedProvider(this.dappSettings.storage, wallet.jsBridgeKey);
4644
4666
  } else {
4645
4667
  provider = new BridgeProvider(this.dappSettings.storage, wallet);
4646
4668
  }
@@ -4723,6 +4745,8 @@ var __objRest = (source, exclude) => {
4723
4745
  }
4724
4746
  }
4725
4747
  TonConnect.walletsList = new WalletsListManager();
4748
+ TonConnect.isWalletInjected = (walletJSKey) => InjectedProvider.isWalletInjected(walletJSKey);
4749
+ TonConnect.isInsideWalletBrowser = (walletJSKey) => InjectedProvider.isInsideWalletBrowser(walletJSKey);
4726
4750
  function toUserFriendlyAddress$1(hexAddress) {
4727
4751
  const { wc, hex } = parseHexAddress$1(hexAddress);
4728
4752
  const bounceableTag = 17;
@@ -11042,8 +11066,64 @@ var __objRest = (source, exclude) => {
11042
11066
  `;
11043
11067
  const [appState, setAppState] = createStore({
11044
11068
  buttonRootId: null,
11045
- language: "en"
11069
+ language: "en",
11070
+ buttonConfiguration: {},
11071
+ widgetConfiguration: {}
11046
11072
  });
11073
+ class TonConnectUIError extends TonConnectError$1 {
11074
+ constructor(...args) {
11075
+ super(...args);
11076
+ Object.setPrototypeOf(this, TonConnectUIError.prototype);
11077
+ }
11078
+ }
11079
+ class WalletNotFoundError extends TonConnectUIError {
11080
+ constructor(...args) {
11081
+ super(...args);
11082
+ Object.setPrototypeOf(this, WalletNotFoundError.prototype);
11083
+ }
11084
+ }
11085
+ function uiWalletToWalletInfo(uiWallet) {
11086
+ if ("jsBridgeKey" in uiWallet) {
11087
+ return __spreadProps(__spreadValues({}, uiWallet), {
11088
+ injected: TonConnect.isWalletInjected(uiWallet.jsBridgeKey),
11089
+ embedded: TonConnect.isInsideWalletBrowser(uiWallet.jsBridgeKey)
11090
+ });
11091
+ }
11092
+ return uiWallet;
11093
+ }
11094
+ function applyWalletsListConfiguration(walletsList, configuration) {
11095
+ var _a;
11096
+ if (!configuration) {
11097
+ return walletsList;
11098
+ }
11099
+ if ("wallets" in configuration) {
11100
+ return configuration.wallets.map((wallet) => {
11101
+ if (typeof wallet === "string") {
11102
+ const walletInfo = walletsList.find((item) => item.name === wallet);
11103
+ if (!walletInfo) {
11104
+ throw new WalletNotFoundError(
11105
+ `Wallet with name === '${wallet}' wasn't found in the wallets list. Check ${wallet} correctness.`
11106
+ );
11107
+ }
11108
+ return walletInfo;
11109
+ }
11110
+ return uiWalletToWalletInfo(wallet);
11111
+ });
11112
+ }
11113
+ const filteredWalletsList = ((_a = configuration.excludeWallets) == null ? void 0 : _a.length) ? walletsList.filter(
11114
+ (wallet) => {
11115
+ var _a2;
11116
+ return (_a2 = configuration.excludeWallets) == null ? void 0 : _a2.every((item) => item !== wallet.name);
11117
+ }
11118
+ ) : walletsList;
11119
+ if (!configuration.includeWallets) {
11120
+ return filteredWalletsList;
11121
+ }
11122
+ if (configuration.includeWalletsOrder === "start") {
11123
+ return configuration.includeWallets.map(uiWalletToWalletInfo).concat(walletsList);
11124
+ }
11125
+ return walletsList.concat(configuration.includeWallets.map(uiWalletToWalletInfo));
11126
+ }
11047
11127
  const WalletsModal = () => {
11048
11128
  const {
11049
11129
  locale
@@ -11051,8 +11131,14 @@ var __objRest = (source, exclude) => {
11051
11131
  createEffect(() => locale(appState.language));
11052
11132
  const connector = useContext(ConnectorContext);
11053
11133
  const tonConnectUI2 = useContext(TonConnectUiContext);
11054
- const [walletsList] = createResource(() => tonConnectUI2.getWallets());
11134
+ const [fetchedWalletsList] = createResource(() => tonConnectUI2.getWallets());
11055
11135
  const [selectedWalletInfo, setSelectedWalletInfo] = createSignal(null);
11136
+ const walletsList = createMemo(() => {
11137
+ if (fetchedWalletsList.state !== "ready") {
11138
+ return null;
11139
+ }
11140
+ return applyWalletsListConfiguration(fetchedWalletsList(), appState.widgetConfiguration.wallets);
11141
+ });
11056
11142
  const onClose = () => {
11057
11143
  setWalletsModalOpen(false);
11058
11144
  setSelectedWalletInfo(null);
@@ -11065,7 +11151,11 @@ var __objRest = (source, exclude) => {
11065
11151
  if (isWalletInfoInjected(walletInfo) && walletInfo.injected) {
11066
11152
  return onSelectIfInjected(walletInfo);
11067
11153
  }
11068
- setSelectedWalletInfo(walletInfo);
11154
+ if ("bridgeUrl" in walletInfo) {
11155
+ setSelectedWalletInfo(walletInfo);
11156
+ return;
11157
+ }
11158
+ openLink(walletInfo.aboutUrl, "_blank");
11069
11159
  };
11070
11160
  const onSelectIfMobile = (walletInfo) => {
11071
11161
  const universalLink = connector.connect({
@@ -11095,7 +11185,7 @@ var __objRest = (source, exclude) => {
11095
11185
  get children() {
11096
11186
  return [createComponent(Show, {
11097
11187
  get when() {
11098
- return walletsList.state !== "ready";
11188
+ return !walletsList();
11099
11189
  },
11100
11190
  get children() {
11101
11191
  return [createComponent(H1Styled$1, {
@@ -11111,7 +11201,7 @@ var __objRest = (source, exclude) => {
11111
11201
  }
11112
11202
  }), createComponent(Show, {
11113
11203
  get when() {
11114
- return walletsList.state === "ready";
11204
+ return walletsList();
11115
11205
  },
11116
11206
  get children() {
11117
11207
  return [createComponent(Show, {
@@ -11341,12 +11431,6 @@ var __objRest = (source, exclude) => {
11341
11431
  tonConnectUI: tonConnectUI2
11342
11432
  }), document.getElementById(root))
11343
11433
  };
11344
- class TonConnectUIError extends TonConnectError$1 {
11345
- constructor(...args) {
11346
- super(...args);
11347
- Object.setPrototypeOf(this, TonConnectUIError.prototype);
11348
- }
11349
- }
11350
11434
  class WalletInfoStorage {
11351
11435
  constructor() {
11352
11436
  __publicField(this, "localStorage");
@@ -11511,7 +11595,10 @@ var __objRest = (source, exclude) => {
11511
11595
  if (options && "connector" in options && options.connector) {
11512
11596
  this.connector = options.connector;
11513
11597
  } else if (options && "manifestUrl" in options && options.manifestUrl) {
11514
- this.connector = new TonConnect({ manifestUrl: options.manifestUrl });
11598
+ this.connector = new TonConnect({
11599
+ manifestUrl: options.manifestUrl,
11600
+ walletsListSource: options.walletsListSource
11601
+ });
11515
11602
  } else {
11516
11603
  throw new TonConnectUIError(
11517
11604
  "You have to specify a `manifestUrl` or a `connector` in the options."
@@ -11521,7 +11608,11 @@ var __objRest = (source, exclude) => {
11521
11608
  const rootId = this.normalizeWidgetRoot(options == null ? void 0 : options.widgetRootId);
11522
11609
  this.subscribeToWalletChange();
11523
11610
  if ((options == null ? void 0 : options.restoreConnection) !== false) {
11524
- this.connector.restoreConnection();
11611
+ this.connector.restoreConnection().then(() => {
11612
+ if (!this.connector.connected) {
11613
+ this.walletInfoStorage.removeWalletInfo();
11614
+ }
11615
+ });
11525
11616
  }
11526
11617
  this.uiOptions = options || {};
11527
11618
  setAppState({ connector: this.connector });
@@ -11560,11 +11651,11 @@ var __objRest = (source, exclude) => {
11560
11651
  }
11561
11652
  setAppState((state) => {
11562
11653
  const merged = mergeOptions(
11563
- {
11564
- language: options.language,
11565
- buttonConfiguration: options.buttonConfiguration,
11654
+ __spreadValues(__spreadValues(__spreadValues({}, options.language && { language: options.language }), options.buttonConfiguration && {
11655
+ buttonConfiguration: options.buttonConfiguration
11656
+ }), options.widgetConfiguration && {
11566
11657
  widgetConfiguration: options.widgetConfiguration
11567
- },
11658
+ }),
11568
11659
  unwrap(state)
11569
11660
  );
11570
11661
  if (options.buttonRootId !== void 0) {