@tonconnect/ui-react 0.0.10 → 0.0.12

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/README.md CHANGED
@@ -153,9 +153,9 @@ You can use it to detect when connection restoring process if finished.
153
153
  import { useIsConnectionRestored } from '@tonconnect/ui-react';
154
154
 
155
155
  export const EntrypointPage = () => {
156
- const connectionRestoring = useIsConnectionRestored();
156
+ const connectionRestored = useIsConnectionRestored();
157
157
 
158
- if (connectionRestoring) {
158
+ if (!connectionRestored) {
159
159
  return <Loader>Please wait...</Loader>;
160
160
  }
161
161
 
package/lib/index.js CHANGED
@@ -4053,10 +4053,12 @@ class BridgeProvider {
4053
4053
  this.storage = storage;
4054
4054
  this.walletConnectionSource = walletConnectionSource;
4055
4055
  this.type = "http";
4056
+ this.standardUniversalLink = "https://connect.ton.org";
4056
4057
  this.pendingRequests = /* @__PURE__ */ new Map();
4057
4058
  this.nextRequestId = 0;
4058
4059
  this.session = null;
4059
4060
  this.bridge = null;
4061
+ this.pendingBridges = [];
4060
4062
  this.listeners = [];
4061
4063
  this.connectionStorage = new BridgeConnectionStorage(storage);
4062
4064
  }
@@ -4068,21 +4070,32 @@ class BridgeProvider {
4068
4070
  });
4069
4071
  }
4070
4072
  connect(message) {
4071
- var _a;
4072
- (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
4073
+ this.closeBridges();
4073
4074
  const sessionCrypto = new SessionCrypto();
4075
+ let walletConnectionSource = {
4076
+ universalLink: this.standardUniversalLink,
4077
+ bridgeUrl: ""
4078
+ };
4079
+ if (Array.isArray(this.walletConnectionSource)) {
4080
+ this.pendingBridges = this.walletConnectionSource.map((source) => new BridgeGateway(this.storage, source, sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this)));
4081
+ this.pendingBridges.forEach((bridge) => bridge.registerSession());
4082
+ } else {
4083
+ walletConnectionSource = this.walletConnectionSource;
4084
+ this.bridge = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
4085
+ this.bridge.registerSession();
4086
+ }
4074
4087
  this.session = {
4075
4088
  sessionCrypto,
4076
- walletConnectionSource: this.walletConnectionSource
4089
+ walletConnectionSource
4077
4090
  };
4078
- this.bridge = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
4079
- this.bridge.registerSession();
4080
- return this.generateUniversalLink(message);
4091
+ return this.generateUniversalLink(walletConnectionSource.universalLink, message);
4081
4092
  }
4082
4093
  restoreConnection() {
4083
- var _a;
4084
4094
  return __awaiter$4(this, void 0, void 0, function* () {
4085
- (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
4095
+ if (Array.isArray(this.walletConnectionSource)) {
4096
+ throw new TonConnectError$1("Internal error. Connection source is array while WalletConnectionSourceHTTP was expected.");
4097
+ }
4098
+ this.closeBridges();
4086
4099
  const storedConnection = yield this.connectionStorage.getHttpConnection();
4087
4100
  if (!storedConnection) {
4088
4101
  return;
@@ -4106,15 +4119,13 @@ class BridgeProvider {
4106
4119
  });
4107
4120
  }
4108
4121
  closeConnection() {
4109
- var _a;
4110
- (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
4122
+ this.closeBridges();
4111
4123
  this.listeners = [];
4112
4124
  this.session = null;
4113
4125
  this.bridge = null;
4114
4126
  }
4115
4127
  disconnect() {
4116
- var _a;
4117
- (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
4128
+ this.closeBridges();
4118
4129
  this.listeners = [];
4119
4130
  return this.removeBridgeAndSession();
4120
4131
  }
@@ -4122,6 +4133,18 @@ class BridgeProvider {
4122
4133
  this.listeners.push(callback);
4123
4134
  return () => this.listeners = this.listeners.filter((listener) => listener !== callback);
4124
4135
  }
4136
+ pendingGatewaysListener(gateway, bridgeIncomingMessage) {
4137
+ return __awaiter$4(this, void 0, void 0, function* () {
4138
+ if (!this.pendingBridges.includes(gateway)) {
4139
+ gateway.close();
4140
+ return;
4141
+ }
4142
+ this.closeBridges();
4143
+ this.session.walletConnectionSource.bridgeUrl = gateway.bridgeUrl;
4144
+ this.bridge = gateway;
4145
+ return this.gatewayListener(bridgeIncomingMessage);
4146
+ });
4147
+ }
4125
4148
  gatewayListener(bridgeIncomingMessage) {
4126
4149
  return __awaiter$4(this, void 0, void 0, function* () {
4127
4150
  const walletMessage = JSON.parse(this.session.sessionCrypto.decrypt(Base64$1.decode(bridgeIncomingMessage.message).toUint8Array(), hexToByteArray(bridgeIncomingMessage.from)));
@@ -4168,13 +4191,19 @@ class BridgeProvider {
4168
4191
  yield this.connectionStorage.removeConnection();
4169
4192
  });
4170
4193
  }
4171
- generateUniversalLink(message) {
4172
- const url = new URL(this.walletConnectionSource.universalLink);
4194
+ generateUniversalLink(universalLink, message) {
4195
+ const url = new URL(universalLink);
4173
4196
  url.searchParams.append("v", PROTOCOL_VERSION.toString());
4174
4197
  url.searchParams.append("id", this.session.sessionCrypto.sessionId);
4175
4198
  url.searchParams.append("r", JSON.stringify(message));
4176
4199
  return url.toString();
4177
4200
  }
4201
+ closeBridges(except) {
4202
+ var _a;
4203
+ (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
4204
+ this.pendingBridges.filter((item) => item !== except).forEach((bridge) => bridge.close());
4205
+ this.pendingBridges = [];
4206
+ }
4178
4207
  }
4179
4208
  function getWindow$1() {
4180
4209
  if (typeof window === "undefined") {
@@ -4658,7 +4687,7 @@ class TonConnect {
4658
4687
  }
4659
4688
  createProvider(wallet) {
4660
4689
  let provider;
4661
- if (isWalletConnectionSourceJS(wallet)) {
4690
+ if (!Array.isArray(wallet) && isWalletConnectionSourceJS(wallet)) {
4662
4691
  provider = new InjectedProvider(this.dappSettings.storage, wallet.jsBridgeKey);
4663
4692
  } else {
4664
4693
  provider = new BridgeProvider(this.dappSettings.storage, wallet);
@@ -5853,6 +5882,9 @@ function render(code, element, init, options = {}) {
5853
5882
  };
5854
5883
  }
5855
5884
  function template$1(html, check, isSVG) {
5885
+ if (typeof window === "undefined") {
5886
+ return null;
5887
+ }
5856
5888
  const t2 = document.createElement("template");
5857
5889
  t2.innerHTML = html;
5858
5890
  let node = t2.content.firstChild;
@@ -11832,7 +11864,7 @@ class WalletInfoStorage {
11832
11864
  constructor() {
11833
11865
  __publicField(this, "localStorage");
11834
11866
  __publicField(this, "storageKey", "ton-connect-ui_wallet-info");
11835
- if (!localStorage) {
11867
+ if (typeof localStorage === "undefined") {
11836
11868
  throw new TonConnectUIError(
11837
11869
  "window.localStorage is undefined. localStorage is required for TonConnectUI"
11838
11870
  );
@@ -11957,13 +11989,15 @@ class TonConnectUI {
11957
11989
  }
11958
11990
  connectWallet() {
11959
11991
  return __async(this, null, function* () {
11992
+ var _a;
11960
11993
  const walletsList = yield this.getWallets();
11961
11994
  const embeddedWallet = walletsList.find(
11962
11995
  (wallet) => "embedded" in wallet && wallet.embedded
11963
11996
  );
11964
11997
  if (embeddedWallet) {
11998
+ const additionalRequest = yield (_a = appState.getConnectParameters) == null ? void 0 : _a.call(appState);
11965
11999
  setLastSelectedWalletInfo(embeddedWallet);
11966
- this.connector.connect({ jsBridgeKey: embeddedWallet.jsBridgeKey });
12000
+ this.connector.connect({ jsBridgeKey: embeddedWallet.jsBridgeKey }, additionalRequest);
11967
12001
  } else {
11968
12002
  widgetController.openWalletsModal();
11969
12003
  }
@@ -12095,6 +12129,12 @@ class TonConnectUI {
12095
12129
  };
12096
12130
  }
12097
12131
  }
12132
+ function isClientSide() {
12133
+ return typeof window !== "undefined";
12134
+ }
12135
+ function isServerSide() {
12136
+ return !isClientSide();
12137
+ }
12098
12138
  const TonConnectUIContext = createContext$1(null);
12099
12139
  let tonConnectUI = null;
12100
12140
  const TonConnectUIProvider = (_a) => {
@@ -12103,7 +12143,7 @@ const TonConnectUIProvider = (_a) => {
12103
12143
  } = _b, options = __objRest(_b, [
12104
12144
  "children"
12105
12145
  ]);
12106
- if (!tonConnectUI) {
12146
+ if (isClientSide() && !tonConnectUI) {
12107
12147
  tonConnectUI = new TonConnectUI(options);
12108
12148
  }
12109
12149
  return /* @__PURE__ */ jsx(TonConnectUIContext.Provider, { value: tonConnectUI, children: children2 });
@@ -12130,6 +12170,10 @@ function checkProvider(provider) {
12130
12170
  return true;
12131
12171
  }
12132
12172
  function useTonConnectUI() {
12173
+ if (isServerSide()) {
12174
+ return [null, () => {
12175
+ }];
12176
+ }
12133
12177
  const tonConnectUI2 = useContext$1(TonConnectUIContext);
12134
12178
  checkProvider(tonConnectUI2);
12135
12179
  const setOptions = (options) => void (tonConnectUI2.uiOptions = options);
@@ -12145,6 +12189,9 @@ const TonConnectButton = ({ className: className2, style: style2 }) => {
12145
12189
  };
12146
12190
  const TonConnectButton$1 = memo(TonConnectButton);
12147
12191
  function useTonWallet() {
12192
+ if (isServerSide()) {
12193
+ return null;
12194
+ }
12148
12195
  const [tonConnectUI2] = useTonConnectUI();
12149
12196
  const [wallet, setWallet] = useState(
12150
12197
  () => tonConnectUI2.wallet && __spreadValues2(__spreadValues2({}, tonConnectUI2.wallet), tonConnectUI2.walletInfo)
@@ -15021,6 +15068,9 @@ function useTonAddress(userFriendly = true) {
15021
15068
  }
15022
15069
  }
15023
15070
  function useIsConnectionRestored() {
15071
+ if (isServerSide()) {
15072
+ return false;
15073
+ }
15024
15074
  const [restored, setRestored] = useState(false);
15025
15075
  const [tonConnectUI2] = useTonConnectUI();
15026
15076
  tonConnectUI2.connectionRestored.then(() => setRestored(true));