@tonconnect/ui-react 0.0.3 → 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.js CHANGED
@@ -4218,7 +4218,8 @@ var __awaiter$3 = globalThis && globalThis.__awaiter || function(thisArg, _argum
4218
4218
  });
4219
4219
  };
4220
4220
  class InjectedProvider {
4221
- constructor(injectedWalletKey) {
4221
+ constructor(storage, injectedWalletKey) {
4222
+ this.injectedWalletKey = injectedWalletKey;
4222
4223
  this.type = "injected";
4223
4224
  this.unsubscribeCallback = null;
4224
4225
  this.listenSubscriptions = false;
@@ -4227,13 +4228,14 @@ class InjectedProvider {
4227
4228
  if (!InjectedProvider.isWindowContainsWallet(window2, injectedWalletKey)) {
4228
4229
  throw new WalletNotInjectedError();
4229
4230
  }
4231
+ this.connectionStorage = new BridgeConnectionStorage(storage);
4230
4232
  this.injectedWallet = window2[injectedWalletKey].tonconnect;
4231
4233
  }
4232
4234
  static fromStorage(storage) {
4233
4235
  return __awaiter$3(this, void 0, void 0, function* () {
4234
4236
  const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
4235
4237
  const connection = yield bridgeConnectionStorage.getInjectedConnection();
4236
- return new InjectedProvider(connection.jsBridgeKey);
4238
+ return new InjectedProvider(storage, connection.jsBridgeKey);
4237
4239
  });
4238
4240
  }
4239
4241
  static isWalletInjected(injectedWalletKey) {
@@ -4248,23 +4250,8 @@ class InjectedProvider {
4248
4250
  static isWindowContainsWallet(window2, injectedWalletKey) {
4249
4251
  return !!window2 && injectedWalletKey in window2 && typeof window2[injectedWalletKey] === "object" && "tonconnect" in window2[injectedWalletKey];
4250
4252
  }
4251
- connect(message, auto = false) {
4252
- this.injectedWallet.connect(PROTOCOL_VERSION, message, auto).then((connectEvent) => {
4253
- if (connectEvent.event === "connect") {
4254
- this.makeSubscriptions();
4255
- this.listenSubscriptions = true;
4256
- }
4257
- this.listeners.forEach((listener) => listener(connectEvent));
4258
- }).catch((e2) => {
4259
- const connectEventError = {
4260
- event: "connect_error",
4261
- payload: {
4262
- code: 0,
4263
- message: e2 === null || e2 === void 0 ? void 0 : e2.toString()
4264
- }
4265
- };
4266
- this.listeners.forEach((listener) => listener(connectEventError));
4267
- });
4253
+ connect(message) {
4254
+ this._connect(PROTOCOL_VERSION, message);
4268
4255
  }
4269
4256
  restoreConnection() {
4270
4257
  return __awaiter$3(this, void 0, void 0, function* () {
@@ -4272,10 +4259,12 @@ class InjectedProvider {
4272
4259
  const connectEvent = yield this.injectedWallet.restoreConnection();
4273
4260
  if (connectEvent.event === "connect") {
4274
4261
  this.makeSubscriptions();
4275
- this.listenSubscriptions = true;
4276
4262
  this.listeners.forEach((listener) => listener(connectEvent));
4263
+ } else {
4264
+ yield this.connectionStorage.removeConnection();
4277
4265
  }
4278
4266
  } catch (e2) {
4267
+ yield this.connectionStorage.removeConnection();
4279
4268
  console.error(e2);
4280
4269
  }
4281
4270
  });
@@ -4289,7 +4278,7 @@ class InjectedProvider {
4289
4278
  disconnect() {
4290
4279
  this.closeAllListeners();
4291
4280
  this.injectedWallet.disconnect();
4292
- return Promise.resolve();
4281
+ return this.connectionStorage.removeConnection();
4293
4282
  }
4294
4283
  closeAllListeners() {
4295
4284
  var _a;
@@ -4306,7 +4295,30 @@ class InjectedProvider {
4306
4295
  return this.injectedWallet.send(Object.assign(Object.assign({}, request), { id: "0" }));
4307
4296
  });
4308
4297
  }
4298
+ _connect(protocolVersion, message) {
4299
+ return __awaiter$3(this, void 0, void 0, function* () {
4300
+ try {
4301
+ const connectEvent = yield this.injectedWallet.connect(protocolVersion, message);
4302
+ if (connectEvent.event === "connect") {
4303
+ yield this.updateSession();
4304
+ this.makeSubscriptions();
4305
+ }
4306
+ this.listeners.forEach((listener) => listener(connectEvent));
4307
+ } catch (e2) {
4308
+ console.debug(e2);
4309
+ const connectEventError = {
4310
+ event: "connect_error",
4311
+ payload: {
4312
+ code: 0,
4313
+ message: e2 === null || e2 === void 0 ? void 0 : e2.toString()
4314
+ }
4315
+ };
4316
+ this.listeners.forEach((listener) => listener(connectEventError));
4317
+ }
4318
+ });
4319
+ }
4309
4320
  makeSubscriptions() {
4321
+ this.listenSubscriptions = true;
4310
4322
  this.unsubscribeCallback = this.injectedWallet.listen((e2) => {
4311
4323
  if (this.listenSubscriptions) {
4312
4324
  this.listeners.forEach((listener) => listener(e2));
@@ -4316,6 +4328,12 @@ class InjectedProvider {
4316
4328
  }
4317
4329
  });
4318
4330
  }
4331
+ updateSession() {
4332
+ return this.connectionStorage.storeConnection({
4333
+ type: "injected",
4334
+ jsBridgeKey: this.injectedWalletKey
4335
+ });
4336
+ }
4319
4337
  }
4320
4338
  InjectedProvider.window = getWindow$1();
4321
4339
  var __awaiter$2 = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P2, generator) {
@@ -4400,9 +4418,12 @@ var __awaiter$1 = globalThis && globalThis.__awaiter || function(thisArg, _argum
4400
4418
  });
4401
4419
  };
4402
4420
  class WalletsListManager {
4403
- constructor() {
4421
+ constructor(walletsListSource) {
4404
4422
  this.walletsListCache = null;
4405
4423
  this.walletsListSource = "https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json";
4424
+ if (walletsListSource) {
4425
+ this.walletsListSource = walletsListSource;
4426
+ }
4406
4427
  }
4407
4428
  getWallets() {
4408
4429
  return __awaiter$1(this, void 0, void 0, function* () {
@@ -4416,11 +4437,11 @@ class WalletsListManager {
4416
4437
  getEmbeddedWallet() {
4417
4438
  return __awaiter$1(this, void 0, void 0, function* () {
4418
4439
  const walletsList = yield this.getWallets();
4419
- const injectedWallets = walletsList.filter(isWalletInfoInjected);
4420
- if (injectedWallets.length !== 1) {
4440
+ const embeddedWallets = walletsList.filter((item) => isWalletInfoInjected(item) && item.embedded);
4441
+ if (embeddedWallets.length !== 1) {
4421
4442
  return null;
4422
4443
  }
4423
- return injectedWallets[0].embedded ? injectedWallets[0] : null;
4444
+ return embeddedWallets[0];
4424
4445
  });
4425
4446
  }
4426
4447
  fetchWalletsList() {
@@ -4543,6 +4564,7 @@ class TonConnect {
4543
4564
  manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
4544
4565
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
4545
4566
  };
4567
+ this.walletsList = new WalletsListManager(options === null || options === void 0 ? void 0 : options.walletsListSource);
4546
4568
  if (!this.dappSettings.manifestUrl) {
4547
4569
  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");
4548
4570
  }
@@ -4637,7 +4659,7 @@ class TonConnect {
4637
4659
  createProvider(wallet) {
4638
4660
  let provider;
4639
4661
  if (isWalletConnectionSourceJS(wallet)) {
4640
- provider = new InjectedProvider(wallet.jsBridgeKey);
4662
+ provider = new InjectedProvider(this.dappSettings.storage, wallet.jsBridgeKey);
4641
4663
  } else {
4642
4664
  provider = new BridgeProvider(this.dappSettings.storage, wallet);
4643
4665
  }
@@ -4720,6 +4742,8 @@ class TonConnect {
4720
4742
  }
4721
4743
  }
4722
4744
  TonConnect.walletsList = new WalletsListManager();
4745
+ TonConnect.isWalletInjected = (walletJSKey) => InjectedProvider.isWalletInjected(walletJSKey);
4746
+ TonConnect.isInsideWalletBrowser = (walletJSKey) => InjectedProvider.isInsideWalletBrowser(walletJSKey);
4723
4747
  function toUserFriendlyAddress$1(hexAddress) {
4724
4748
  const { wc, hex } = parseHexAddress$1(hexAddress);
4725
4749
  const bounceableTag = 17;
@@ -7776,7 +7800,6 @@ const AccountButtonDropdown = (props) => {
7776
7800
  const connector = useContext(ConnectorContext);
7777
7801
  const [isCopiedShown, setIsCopiedShown] = createSignal(false);
7778
7802
  const onCopy = () => __async(void 0, null, function* () {
7779
- console.log(tonConnectUi.account.address);
7780
7803
  const userFriendlyAddress = toUserFriendlyAddress$1(tonConnectUi.account.address);
7781
7804
  yield copyToClipboard(userFriendlyAddress);
7782
7805
  setIsCopiedShown(true);
@@ -8826,7 +8849,7 @@ function P(l2, u2, e2) {
8826
8849
  const _tmpl$$3 = /* @__PURE__ */ template$1(`<div></div>`);
8827
8850
  const AccountButton = () => {
8828
8851
  const connector = useContext(ConnectorContext);
8829
- const tonConnectUI = useContext(TonConnectUiContext);
8852
+ const tonConnectUI2 = useContext(TonConnectUiContext);
8830
8853
  const [isOpened, setIsOpened] = createSignal(false);
8831
8854
  const [address, setAddress] = createSignal("");
8832
8855
  let dropDownRef;
@@ -8875,7 +8898,7 @@ const AccountButton = () => {
8875
8898
  get children() {
8876
8899
  return createComponent(AccountButtonStyled, {
8877
8900
  appearance: "flat",
8878
- onClick: () => tonConnectUI.connectWallet(),
8901
+ onClick: () => tonConnectUI2.connectWallet(),
8879
8902
  get children() {
8880
8903
  return [createComponent(TonIcon, {}), createComponent(Text, {
8881
8904
  translationKey: "button.connectWallet",
@@ -11040,17 +11063,79 @@ const LoaderContainerStyled = styled.div`
11040
11063
  `;
11041
11064
  const [appState, setAppState] = createStore({
11042
11065
  buttonRootId: null,
11043
- language: "en"
11066
+ language: "en",
11067
+ buttonConfiguration: {},
11068
+ widgetConfiguration: {}
11044
11069
  });
11070
+ class TonConnectUIError extends TonConnectError$1 {
11071
+ constructor(...args) {
11072
+ super(...args);
11073
+ Object.setPrototypeOf(this, TonConnectUIError.prototype);
11074
+ }
11075
+ }
11076
+ class WalletNotFoundError extends TonConnectUIError {
11077
+ constructor(...args) {
11078
+ super(...args);
11079
+ Object.setPrototypeOf(this, WalletNotFoundError.prototype);
11080
+ }
11081
+ }
11082
+ function uiWalletToWalletInfo(uiWallet) {
11083
+ if ("jsBridgeKey" in uiWallet) {
11084
+ return __spreadProps(__spreadValues({}, uiWallet), {
11085
+ injected: TonConnect.isWalletInjected(uiWallet.jsBridgeKey),
11086
+ embedded: TonConnect.isInsideWalletBrowser(uiWallet.jsBridgeKey)
11087
+ });
11088
+ }
11089
+ return uiWallet;
11090
+ }
11091
+ function applyWalletsListConfiguration(walletsList, configuration) {
11092
+ var _a;
11093
+ if (!configuration) {
11094
+ return walletsList;
11095
+ }
11096
+ if ("wallets" in configuration) {
11097
+ return configuration.wallets.map((wallet) => {
11098
+ if (typeof wallet === "string") {
11099
+ const walletInfo = walletsList.find((item) => item.name === wallet);
11100
+ if (!walletInfo) {
11101
+ throw new WalletNotFoundError(
11102
+ `Wallet with name === '${wallet}' wasn't found in the wallets list. Check ${wallet} correctness.`
11103
+ );
11104
+ }
11105
+ return walletInfo;
11106
+ }
11107
+ return uiWalletToWalletInfo(wallet);
11108
+ });
11109
+ }
11110
+ const filteredWalletsList = ((_a = configuration.excludeWallets) == null ? void 0 : _a.length) ? walletsList.filter(
11111
+ (wallet) => {
11112
+ var _a2;
11113
+ return (_a2 = configuration.excludeWallets) == null ? void 0 : _a2.every((item) => item !== wallet.name);
11114
+ }
11115
+ ) : walletsList;
11116
+ if (!configuration.includeWallets) {
11117
+ return filteredWalletsList;
11118
+ }
11119
+ if (configuration.includeWalletsOrder === "start") {
11120
+ return configuration.includeWallets.map(uiWalletToWalletInfo).concat(walletsList);
11121
+ }
11122
+ return walletsList.concat(configuration.includeWallets.map(uiWalletToWalletInfo));
11123
+ }
11045
11124
  const WalletsModal = () => {
11046
11125
  const {
11047
11126
  locale
11048
11127
  } = useI18n()[1];
11049
11128
  createEffect(() => locale(appState.language));
11050
11129
  const connector = useContext(ConnectorContext);
11051
- const tonConnectUI = useContext(TonConnectUiContext);
11052
- const [walletsList] = createResource(() => tonConnectUI.getWallets());
11130
+ const tonConnectUI2 = useContext(TonConnectUiContext);
11131
+ const [fetchedWalletsList] = createResource(() => tonConnectUI2.getWallets());
11053
11132
  const [selectedWalletInfo, setSelectedWalletInfo] = createSignal(null);
11133
+ const walletsList = createMemo(() => {
11134
+ if (fetchedWalletsList.state !== "ready") {
11135
+ return null;
11136
+ }
11137
+ return applyWalletsListConfiguration(fetchedWalletsList(), appState.widgetConfiguration.wallets);
11138
+ });
11054
11139
  const onClose = () => {
11055
11140
  setWalletsModalOpen(false);
11056
11141
  setSelectedWalletInfo(null);
@@ -11063,7 +11148,11 @@ const WalletsModal = () => {
11063
11148
  if (isWalletInfoInjected(walletInfo) && walletInfo.injected) {
11064
11149
  return onSelectIfInjected(walletInfo);
11065
11150
  }
11066
- setSelectedWalletInfo(walletInfo);
11151
+ if ("bridgeUrl" in walletInfo) {
11152
+ setSelectedWalletInfo(walletInfo);
11153
+ return;
11154
+ }
11155
+ openLink(walletInfo.aboutUrl, "_blank");
11067
11156
  };
11068
11157
  const onSelectIfMobile = (walletInfo) => {
11069
11158
  const universalLink = connector.connect({
@@ -11093,7 +11182,7 @@ const WalletsModal = () => {
11093
11182
  get children() {
11094
11183
  return [createComponent(Show, {
11095
11184
  get when() {
11096
- return walletsList.state !== "ready";
11185
+ return !walletsList();
11097
11186
  },
11098
11187
  get children() {
11099
11188
  return [createComponent(H1Styled$1, {
@@ -11109,7 +11198,7 @@ const WalletsModal = () => {
11109
11198
  }
11110
11199
  }), createComponent(Show, {
11111
11200
  get when() {
11112
- return walletsList.state === "ready";
11201
+ return walletsList();
11113
11202
  },
11114
11203
  get children() {
11115
11204
  return [createComponent(Show, {
@@ -11335,16 +11424,10 @@ const widgetController = {
11335
11424
  setAction: (action2) => void setTimeout(() => setAction(action2)),
11336
11425
  clearAction: () => void setTimeout(() => setAction(null)),
11337
11426
  getSelectedWalletInfo: () => lastSelectedWalletInfo(),
11338
- renderApp: (root, tonConnectUI) => render(() => createComponent(App, {
11339
- tonConnectUI
11427
+ renderApp: (root, tonConnectUI2) => render(() => createComponent(App, {
11428
+ tonConnectUI: tonConnectUI2
11340
11429
  }), document.getElementById(root))
11341
11430
  };
11342
- class TonConnectUIError extends TonConnectError$1 {
11343
- constructor(...args) {
11344
- super(...args);
11345
- Object.setPrototypeOf(this, TonConnectUIError.prototype);
11346
- }
11347
- }
11348
11431
  class WalletInfoStorage {
11349
11432
  constructor() {
11350
11433
  __publicField(this, "localStorage");
@@ -11509,7 +11592,10 @@ class TonConnectUI {
11509
11592
  if (options && "connector" in options && options.connector) {
11510
11593
  this.connector = options.connector;
11511
11594
  } else if (options && "manifestUrl" in options && options.manifestUrl) {
11512
- this.connector = new TonConnect({ manifestUrl: options.manifestUrl });
11595
+ this.connector = new TonConnect({
11596
+ manifestUrl: options.manifestUrl,
11597
+ walletsListSource: options.walletsListSource
11598
+ });
11513
11599
  } else {
11514
11600
  throw new TonConnectUIError(
11515
11601
  "You have to specify a `manifestUrl` or a `connector` in the options."
@@ -11519,7 +11605,11 @@ class TonConnectUI {
11519
11605
  const rootId = this.normalizeWidgetRoot(options == null ? void 0 : options.widgetRootId);
11520
11606
  this.subscribeToWalletChange();
11521
11607
  if ((options == null ? void 0 : options.restoreConnection) !== false) {
11522
- this.connector.restoreConnection();
11608
+ this.connector.restoreConnection().then(() => {
11609
+ if (!this.connector.connected) {
11610
+ this.walletInfoStorage.removeWalletInfo();
11611
+ }
11612
+ });
11523
11613
  }
11524
11614
  this.uiOptions = options || {};
11525
11615
  setAppState({ connector: this.connector });
@@ -11558,11 +11648,11 @@ class TonConnectUI {
11558
11648
  }
11559
11649
  setAppState((state) => {
11560
11650
  const merged = mergeOptions(
11561
- {
11562
- language: options.language,
11563
- buttonConfiguration: options.buttonConfiguration,
11651
+ __spreadValues(__spreadValues(__spreadValues({}, options.language && { language: options.language }), options.buttonConfiguration && {
11652
+ buttonConfiguration: options.buttonConfiguration
11653
+ }), options.widgetConfiguration && {
11564
11654
  widgetConfiguration: options.widgetConfiguration
11565
- },
11655
+ }),
11566
11656
  unwrap(state)
11567
11657
  );
11568
11658
  if (options.buttonRootId !== void 0) {
@@ -11650,6 +11740,8 @@ class TonConnectUI {
11650
11740
  console.error(e2);
11651
11741
  throw new TonConnectUIError("Unhandled error:" + e2);
11652
11742
  }
11743
+ } finally {
11744
+ widgetController.clearAction();
11653
11745
  }
11654
11746
  });
11655
11747
  }
@@ -11690,23 +11782,26 @@ class TonConnectUI {
11690
11782
  }
11691
11783
  }
11692
11784
  const TonConnectUIContext = createContext$1(null);
11785
+ let tonConnectUI = null;
11693
11786
  const TonConnectUIProvider = (_a) => {
11694
11787
  var _b = _a, {
11695
11788
  children: children2
11696
11789
  } = _b, options = __objRest(_b, [
11697
11790
  "children"
11698
11791
  ]);
11699
- const tonConnectUI = new TonConnectUI(options);
11792
+ if (!tonConnectUI) {
11793
+ tonConnectUI = new TonConnectUI(options);
11794
+ }
11700
11795
  return /* @__PURE__ */ jsx(TonConnectUIContext.Provider, { value: tonConnectUI, children: children2 });
11701
11796
  };
11702
11797
  const TonConnectUIProvider$1 = memo$1(TonConnectUIProvider);
11703
- class TonConnectUiReactError extends TonConnectUIError {
11798
+ class TonConnectUIReactError extends TonConnectUIError {
11704
11799
  constructor(...args) {
11705
11800
  super(...args);
11706
- Object.setPrototypeOf(this, TonConnectUiReactError.prototype);
11801
+ Object.setPrototypeOf(this, TonConnectUIReactError.prototype);
11707
11802
  }
11708
11803
  }
11709
- class TonConnectProviderNotSetError extends TonConnectUiReactError {
11804
+ class TonConnectProviderNotSetError extends TonConnectUIReactError {
11710
11805
  constructor(...args) {
11711
11806
  super(...args);
11712
11807
  Object.setPrototypeOf(this, TonConnectProviderNotSetError.prototype);
@@ -11721,10 +11816,10 @@ function checkProvider(provider) {
11721
11816
  return true;
11722
11817
  }
11723
11818
  function useTonConnectUI() {
11724
- const tonConnectUI = useContext$1(TonConnectUIContext);
11725
- checkProvider(tonConnectUI);
11726
- const setOptions = (options) => void (tonConnectUI.uiOptions = options);
11727
- return [tonConnectUI, setOptions];
11819
+ const tonConnectUI2 = useContext$1(TonConnectUIContext);
11820
+ checkProvider(tonConnectUI2);
11821
+ const setOptions = (options) => void (tonConnectUI2.uiOptions = options);
11822
+ return [tonConnectUI2, setOptions];
11728
11823
  }
11729
11824
  const buttonRootId = "ton-connect-button";
11730
11825
  const TonConnectButton = ({ className: className2, style: style2 }) => {
@@ -11736,15 +11831,15 @@ const TonConnectButton = ({ className: className2, style: style2 }) => {
11736
11831
  };
11737
11832
  const TonConnectButton$1 = memo$1(TonConnectButton);
11738
11833
  function useTonWallet() {
11739
- const [tonConnectUI] = useTonConnectUI();
11834
+ const [tonConnectUI2] = useTonConnectUI();
11740
11835
  const [wallet, setWallet] = useState(
11741
- () => tonConnectUI.wallet && __spreadValues2(__spreadValues2({}, tonConnectUI.wallet), tonConnectUI.walletInfo)
11836
+ () => tonConnectUI2.wallet && __spreadValues2(__spreadValues2({}, tonConnectUI2.wallet), tonConnectUI2.walletInfo)
11742
11837
  );
11743
11838
  useEffect(
11744
- () => tonConnectUI.onStatusChange((value) => {
11839
+ () => tonConnectUI2.onStatusChange((value) => {
11745
11840
  setWallet(value);
11746
11841
  }),
11747
- [tonConnectUI]
11842
+ [tonConnectUI2]
11748
11843
  );
11749
11844
  return wallet;
11750
11845
  }
@@ -14613,7 +14708,7 @@ export {
14613
14708
  TonConnectUI,
14614
14709
  TonConnectUIError,
14615
14710
  TonConnectUIProvider$1 as TonConnectUIProvider,
14616
- TonConnectUiReactError,
14711
+ TonConnectUIReactError,
14617
14712
  useTonAddress,
14618
14713
  useTonConnectUI,
14619
14714
  useTonWallet