@tonconnect/sdk 0.0.13 → 0.0.14

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.
Files changed (31) hide show
  1. package/README.md +55 -5
  2. package/lib/errors/index.d.ts +1 -0
  3. package/lib/errors/index.js +1 -0
  4. package/lib/errors/wallets-manager/fetch-wallets.error.d.ts +4 -0
  5. package/lib/errors/wallets-manager/fetch-wallets.error.js +19 -0
  6. package/lib/errors/wallets-manager/index.d.ts +1 -0
  7. package/lib/errors/wallets-manager/index.js +5 -0
  8. package/lib/models/wallet/index.d.ts +1 -0
  9. package/lib/models/wallet/wallet-config.d.ts +28 -0
  10. package/lib/models/wallet/wallet-config.js +2 -0
  11. package/lib/models/wallet/wallet-connection-source.d.ts +6 -1
  12. package/lib/models/wallet/wallet-connection-source.js +5 -0
  13. package/lib/provider/bridge/bridge-gateway.js +2 -2
  14. package/lib/provider/bridge/bridge-provider.d.ts +4 -3
  15. package/lib/provider/bridge/bridge-provider.js +25 -3
  16. package/lib/provider/bridge/models/bridge-connection.d.ts +19 -7
  17. package/lib/provider/bridge/models/bridge-session.d.ts +2 -2
  18. package/lib/provider/injected/injected-provider.d.ts +9 -6
  19. package/lib/provider/injected/injected-provider.js +35 -13
  20. package/lib/provider/injected/models/injected-wallet-api.d.ts +2 -1
  21. package/lib/provider/provider.d.ts +1 -1
  22. package/lib/storage/bridge-connection-storage.d.ts +4 -2
  23. package/lib/storage/bridge-connection-storage.js +63 -12
  24. package/lib/storage/{bridge-gateway-storage.d.ts → http-bridge-gateway-storage.d.ts} +1 -1
  25. package/lib/storage/{bridge-gateway-storage.js → http-bridge-gateway-storage.js} +8 -8
  26. package/lib/ton-connect.d.ts +8 -6
  27. package/lib/ton-connect.interface.d.ts +14 -5
  28. package/lib/ton-connect.js +72 -34
  29. package/lib/wallets-list-manager.d.ts +17 -0
  30. package/lib/wallets-list-manager.js +136 -0
  31. package/package.json +1 -1
package/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  Use it to connect your app to TON wallets via TonConnect protocol.
6
6
  You can find more details and the protocol specification in the [docs](https://github.com/ton-connect/docs).
7
+ See the example of sdk usage [here](https://github.com/ton-connect/demo-dapp).
7
8
 
8
9
  ## Get started
9
10
  `npm i @tonconnect/sdk`
@@ -15,7 +16,7 @@ import TonConnect from '@tonconnect/sdk';
15
16
 
16
17
  const connector = new TonConnect();
17
18
 
18
- connector.autoConnect();
19
+ connector.restoreConnection();
19
20
  ```
20
21
 
21
22
  ## Subscribe to the connection status changes
@@ -27,6 +28,53 @@ connector.onStatusChange(
27
28
  );
28
29
  ```
29
30
 
31
+ ## Fetch wallets list
32
+
33
+ TonConnect is build to support different wallets. You can fetch all supported wallets list and show a custom wallet selection dialog for user
34
+
35
+ ```ts
36
+ const { allWalletsList, injectedWalletsList, remoteConnectionWalletsList } = await connector.walletsList.getWalletsConfig();
37
+
38
+ /* allWalletsList is
39
+ {
40
+ name: string;
41
+ imageUrl: string;
42
+ tondns?: string;
43
+ aboutUrl: string;
44
+ universalLinkBase?: string;
45
+ bridgeUrl?: string;
46
+ jsBridgeKey?: string;
47
+ }[]
48
+
49
+ injectedWalletsList is (all injected to the page available wallets)
50
+ {
51
+ name: string;
52
+ imageUrl: string;
53
+ tondns?: string;
54
+ aboutUrl: string;
55
+ jsBridgeKey: string;
56
+ }[]
57
+
58
+ remoteConnectionWalletsList is (all wallets available via http bridge (QR code))
59
+ {
60
+ name: string;
61
+ imageUrl: string;
62
+ tondns?: string;
63
+ aboutUrl: string;
64
+ universalLinkBase: string;
65
+ bridgeUrl: string;
66
+ }[]
67
+ */
68
+ ```
69
+
70
+ ### Check if your app is opened inside some wallet's browser
71
+
72
+ If your app is opened inside some wallet's browser you shouldn't show a wallet selection dialog. Just connect dapp to the host-wallet when 'connect' button is clicked
73
+
74
+ ```ts
75
+ const walletConnectionSourceOrNull = await connector.inWhichWalletBrowser();
76
+ ```
77
+
30
78
 
31
79
  ## Initialize a wallet connection when user clicks to 'connect' button in your app
32
80
  ### Initialize a remote wallet connection via universal link
@@ -44,9 +92,11 @@ Then you have to show this link to user as QR code, or use it as a deeplink. You
44
92
 
45
93
  ### Initialize injected wallet connection
46
94
  ```ts
47
- if (connector.isInjectedProviderAvailable()) {
48
- connector.connect('injected');
95
+ const walletConnectionSource = {
96
+ jsBridgeKey: 'tonkeeper'
49
97
  }
98
+
99
+ connector.connect(walletConnectionSource);
50
100
  ```
51
101
 
52
102
  You will receive an update in `connector.onStatusChange` when user approves connection in the wallet
@@ -63,12 +113,12 @@ const transaction = {
63
113
  {
64
114
  address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
65
115
  amount: "20000000",
66
- initState: "base64bocblahblahblah=="
116
+ initState: "base64bocblahblahblah==" // just for instance. Replace with your transaction initState or remove
67
117
  },
68
118
  {
69
119
  address: "0:E69F10CC84877ABF539F83F879291E5CA169451BA7BCE91A37A5CED3AB8080D3",
70
120
  amount: "60000000",
71
- payload: "base64bocblahblahblah=="
121
+ payload: "base64bocblahblahblah==" // just for instance. Replace with your transaction payload or remove
72
122
  }
73
123
  ]
74
124
  }
@@ -1,5 +1,6 @@
1
1
  export * from './protocol';
2
2
  export * from './wallet';
3
3
  export * from './storage';
4
+ export * from './wallets-manager';
4
5
  export { TonConnectError } from './ton-connect.error';
5
6
  export { UnknownError } from './unknown.error';
@@ -5,6 +5,7 @@ var tslib_1 = require("tslib");
5
5
  tslib_1.__exportStar(require("./protocol"), exports);
6
6
  tslib_1.__exportStar(require("./wallet"), exports);
7
7
  tslib_1.__exportStar(require("./storage"), exports);
8
+ tslib_1.__exportStar(require("./wallets-manager"), exports);
8
9
  var ton_connect_error_1 = require("./ton-connect.error");
9
10
  Object.defineProperty(exports, "TonConnectError", { enumerable: true, get: function () { return ton_connect_error_1.TonConnectError; } });
10
11
  var unknown_error_1 = require("./unknown.error");
@@ -0,0 +1,4 @@
1
+ import { TonConnectError } from "../ton-connect.error";
2
+ export declare class FetchWalletsError extends TonConnectError {
3
+ constructor(...args: ConstructorParameters<typeof TonConnectError>);
4
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FetchWalletsError = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var ton_connect_error_1 = require("../ton-connect.error");
6
+ var FetchWalletsError = /** @class */ (function (_super) {
7
+ tslib_1.__extends(FetchWalletsError, _super);
8
+ function FetchWalletsError() {
9
+ var args = [];
10
+ for (var _i = 0; _i < arguments.length; _i++) {
11
+ args[_i] = arguments[_i];
12
+ }
13
+ var _this = _super.apply(this, args) || this;
14
+ Object.setPrototypeOf(_this, FetchWalletsError.prototype);
15
+ return _this;
16
+ }
17
+ return FetchWalletsError;
18
+ }(ton_connect_error_1.TonConnectError));
19
+ exports.FetchWalletsError = FetchWalletsError;
@@ -0,0 +1 @@
1
+ export { FetchWalletsError } from './fetch-wallets.error';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FetchWalletsError = void 0;
4
+ var fetch_wallets_error_1 = require("./fetch-wallets.error");
5
+ Object.defineProperty(exports, "FetchWalletsError", { enumerable: true, get: function () { return fetch_wallets_error_1.FetchWalletsError; } });
@@ -1,3 +1,4 @@
1
1
  export type { Account } from './account';
2
2
  export type { Wallet } from './wallet';
3
3
  export type { WalletConnectionSource } from './wallet-connection-source';
4
+ export type { WalletConfig, JSBridgeWalletConfig, HTTPBridgeWalletConfig } from './wallet-config';
@@ -0,0 +1,28 @@
1
+ export interface WalletConfigBase {
2
+ name: string;
3
+ imageUrl: string;
4
+ tondns?: string;
5
+ aboutUrl: string;
6
+ }
7
+ export interface HTTPBridgeWalletConfig extends WalletConfigBase {
8
+ universalLinkBase: string;
9
+ bridgeUrl: string;
10
+ }
11
+ export interface JSBridgeWalletConfig extends WalletConfigBase {
12
+ jsBridgeKey: string;
13
+ }
14
+ export declare type WalletConfig = HTTPBridgeWalletConfig | JSBridgeWalletConfig | (HTTPBridgeWalletConfig & JSBridgeWalletConfig);
15
+ export interface WalletConfigDTOBase {
16
+ name: string;
17
+ image: string;
18
+ tondns?: string;
19
+ about_url: string;
20
+ }
21
+ export interface HTTPBridgeWalletConfigDTO extends WalletConfigDTOBase {
22
+ universal_url: string;
23
+ bridge_url: string;
24
+ }
25
+ export interface JSBridgeWalletConfigDTO extends WalletConfigDTOBase {
26
+ js_bridge_key: string;
27
+ }
28
+ export declare type WalletConfigDTO = HTTPBridgeWalletConfigDTO | JSBridgeWalletConfigDTO | (HTTPBridgeWalletConfigDTO & JSBridgeWalletConfigDTO);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +1,9 @@
1
- export interface WalletConnectionSource {
1
+ export declare type WalletConnectionSource = WalletConnectionSourceHTTP | WalletConnectionSourceJS;
2
+ export interface WalletConnectionSourceHTTP {
2
3
  universalLinkBase: string;
3
4
  bridgeUrl: string;
4
5
  }
6
+ export interface WalletConnectionSourceJS {
7
+ jsBridgeKey: string;
8
+ }
9
+ export declare function isWalletConnectionSourceJS(value: WalletConnectionSource): value is WalletConnectionSourceJS;
@@ -1,2 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isWalletConnectionSourceJS = void 0;
4
+ function isWalletConnectionSourceJS(value) {
5
+ return 'jsBridgeKey' in value;
6
+ }
7
+ exports.isWalletConnectionSourceJS = isWalletConnectionSourceJS;
@@ -4,7 +4,7 @@ exports.BridgeGateway = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var protocol_1 = require("@tonconnect/protocol");
6
6
  var errors_1 = require("../../errors");
7
- var bridge_gateway_storage_1 = require("../../storage/bridge-gateway-storage");
7
+ var http_bridge_gateway_storage_1 = require("../../storage/http-bridge-gateway-storage");
8
8
  var url_1 = require("../../utils/url");
9
9
  var BridgeGateway = /** @class */ (function () {
10
10
  function BridgeGateway(storage, bridgeUrl, sessionId, listener, errorsListener) {
@@ -16,7 +16,7 @@ var BridgeGateway = /** @class */ (function () {
16
16
  this.postPath = 'message';
17
17
  this.defaultTtl = 300;
18
18
  this.isClosed = false;
19
- this.bridgeGatewayStorage = new bridge_gateway_storage_1.BridgeGatewayStorage(storage);
19
+ this.bridgeGatewayStorage = new http_bridge_gateway_storage_1.HttpBridgeGatewayStorage(storage);
20
20
  }
21
21
  BridgeGateway.prototype.registerSession = function () {
22
22
  return tslib_1.__awaiter(this, void 0, void 0, function () {
@@ -1,11 +1,12 @@
1
1
  import { AppRequest, ConnectRequest, RpcMethod, WalletEvent, WalletResponse } from '@tonconnect/protocol';
2
- import { WalletConnectionSource } from "../../models";
2
+ import { WalletConnectionSourceHTTP } from "../../models/wallet/wallet-connection-source";
3
3
  import { HTTPProvider } from "../provider";
4
4
  import { IStorage } from "../../storage/models/storage.interface";
5
5
  import { WithoutId } from "../../utils/types";
6
6
  export declare class BridgeProvider implements HTTPProvider {
7
7
  private readonly storage;
8
8
  private readonly walletConnectionSource;
9
+ static fromStorage(storage: IStorage): Promise<BridgeProvider>;
9
10
  readonly type = "http";
10
11
  private readonly universalLinkPath;
11
12
  private readonly connectionStorage;
@@ -14,9 +15,9 @@ export declare class BridgeProvider implements HTTPProvider {
14
15
  private session;
15
16
  private bridge;
16
17
  private listeners;
17
- constructor(storage: IStorage, walletConnectionSource: WalletConnectionSource);
18
+ constructor(storage: IStorage, walletConnectionSource: WalletConnectionSourceHTTP);
18
19
  connect(message: ConnectRequest): string;
19
- autoConnect(): Promise<void>;
20
+ restoreConnection(): Promise<void>;
20
21
  sendRequest<T extends RpcMethod>(request: WithoutId<AppRequest<T>>): Promise<WithoutId<WalletResponse<T>>>;
21
22
  closeConnection(): void;
22
23
  disconnect(): Promise<void>;
@@ -21,6 +21,21 @@ var BridgeProvider = /** @class */ (function () {
21
21
  this.listeners = [];
22
22
  this.connectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
23
23
  }
24
+ BridgeProvider.fromStorage = function (storage) {
25
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
26
+ var bridgeConnectionStorage, connection;
27
+ return tslib_1.__generator(this, function (_a) {
28
+ switch (_a.label) {
29
+ case 0:
30
+ bridgeConnectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
31
+ return [4 /*yield*/, bridgeConnectionStorage.getHttpConnection()];
32
+ case 1:
33
+ connection = _a.sent();
34
+ return [2 /*return*/, new BridgeProvider(storage, connection.session.walletConnectionSource)];
35
+ }
36
+ });
37
+ });
38
+ };
24
39
  BridgeProvider.prototype.connect = function (message) {
25
40
  var _a;
26
41
  (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
@@ -33,7 +48,7 @@ var BridgeProvider = /** @class */ (function () {
33
48
  this.bridge.registerSession();
34
49
  return this.generateUniversalLink(message);
35
50
  };
36
- BridgeProvider.prototype.autoConnect = function () {
51
+ BridgeProvider.prototype.restoreConnection = function () {
37
52
  var _a;
38
53
  return tslib_1.__awaiter(this, void 0, void 0, function () {
39
54
  var storedConnection;
@@ -41,7 +56,7 @@ var BridgeProvider = /** @class */ (function () {
41
56
  switch (_b.label) {
42
57
  case 0:
43
58
  (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
44
- return [4 /*yield*/, this.connectionStorage.getConnection()];
59
+ return [4 /*yield*/, this.connectionStorage.getHttpConnection()];
45
60
  case 1:
46
61
  storedConnection = _b.sent();
47
62
  if (!storedConnection) {
@@ -125,11 +140,18 @@ var BridgeProvider = /** @class */ (function () {
125
140
  };
126
141
  BridgeProvider.prototype.updateSession = function (connectEvent, walletPublicKey) {
127
142
  return tslib_1.__awaiter(this, void 0, void 0, function () {
143
+ var tonAddrItem, connectEventToSave;
128
144
  return tslib_1.__generator(this, function (_a) {
129
145
  switch (_a.label) {
130
146
  case 0:
131
147
  this.session = tslib_1.__assign(tslib_1.__assign({}, this.session), { walletPublicKey: walletPublicKey });
132
- return [4 /*yield*/, this.connectionStorage.storeConnection({ session: this.session, connectEvent: connectEvent })];
148
+ tonAddrItem = connectEvent.payload.items.find(function (item) { return item.name === 'ton_addr'; });
149
+ connectEventToSave = tslib_1.__assign(tslib_1.__assign({}, connectEvent), { payload: tslib_1.__assign(tslib_1.__assign({}, connectEvent.payload), { items: [tonAddrItem] }) });
150
+ return [4 /*yield*/, this.connectionStorage.storeConnection({
151
+ type: 'http',
152
+ session: this.session,
153
+ connectEvent: connectEventToSave
154
+ })];
133
155
  case 1:
134
156
  _a.sent();
135
157
  return [2 /*return*/];
@@ -1,11 +1,23 @@
1
- import { ConnectEventSuccess } from '@tonconnect/protocol';
2
- import { BridgeSessionRaw } from './bridge-session-raw';
1
+ import { DeviceInfo, TonAddressItemReply } from '@tonconnect/protocol';
2
+ import { BridgeSessionRaw } from "./bridge-session-raw";
3
3
  import { BridgeSession } from './bridge-session';
4
- export interface BridgeConnection {
5
- connectEvent: ConnectEventSuccess;
4
+ export declare type BridgeConnection = BridgeConnectionHttp | BridgeConnectionInjected;
5
+ export interface BridgeConnectionInjected {
6
+ type: 'injected';
7
+ jsBridgeKey: string;
8
+ }
9
+ export interface BridgeConnectionHttp {
10
+ type: 'http';
11
+ connectEvent: {
12
+ event: 'connect';
13
+ payload: {
14
+ items: [TonAddressItemReply];
15
+ device: DeviceInfo;
16
+ };
17
+ };
6
18
  session: BridgeSession;
7
19
  }
8
- export interface BridgeConnectionRaw {
9
- connectEvent: ConnectEventSuccess;
20
+ export declare type BridgeConnectionHttpRaw = Omit<BridgeConnectionHttp, 'session'> & {
10
21
  session: BridgeSessionRaw;
11
- }
22
+ };
23
+ export declare type BridgeConnectionRaw = BridgeConnectionHttpRaw | BridgeConnectionInjected;
@@ -1,8 +1,8 @@
1
1
  import { SessionCrypto } from '@tonconnect/protocol';
2
- import { WalletConnectionSource } from "../../../models";
2
+ import { WalletConnectionSourceHTTP } from "../../../models/wallet/wallet-connection-source";
3
3
  export interface BridgeSession {
4
4
  sessionCrypto: SessionCrypto;
5
5
  walletPublicKey: string;
6
- walletConnectionSource: WalletConnectionSource;
6
+ walletConnectionSource: WalletConnectionSourceHTTP;
7
7
  }
8
8
  export declare type BridgePartialSession = Omit<BridgeSession, 'walletPublicKey'>;
@@ -1,18 +1,21 @@
1
- import { AppRequest, RpcMethod, WalletResponse, DeviceInfo, ConnectRequest, WalletEvent } from '@tonconnect/protocol';
1
+ import { AppRequest, RpcMethod, WalletResponse, ConnectRequest, WalletEvent } from '@tonconnect/protocol';
2
2
  import { InternalProvider } from "../provider";
3
+ import { IStorage } from "../../storage/models/storage.interface";
3
4
  import { WithoutId } from "../../utils/types";
4
- export declare class InjectedProvider implements InternalProvider {
5
+ export declare class InjectedProvider<T extends string = string> implements InternalProvider {
5
6
  private static window;
6
- static isWalletInjected(): boolean;
7
- static deviceInfo(): DeviceInfo | undefined;
7
+ static fromStorage(storage: IStorage): Promise<InjectedProvider>;
8
+ static isWalletInjected(injectedWalletKey: string): boolean;
9
+ static isInsideWalletBrowser(injectedWalletKey: string): boolean;
10
+ private static isWindowContainsWallet;
8
11
  readonly type = "injected";
9
12
  private unsubscribeCallback;
10
13
  private injectedWallet;
11
14
  private listenSubscriptions;
12
15
  private listeners;
13
- constructor();
16
+ constructor(injectedWalletKey: T);
14
17
  connect(message: ConnectRequest, auto?: boolean): void;
15
- autoConnect(): Promise<void>;
18
+ restoreConnection(): Promise<void>;
16
19
  closeConnection(): void;
17
20
  disconnect(): Promise<void>;
18
21
  listen(eventsCallback: (e: WalletEvent) => void): () => void;
@@ -4,26 +4,48 @@ exports.InjectedProvider = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var wallet_not_injected_error_1 = require("../../errors/wallet/wallet-not-injected.error");
6
6
  var protocol = tslib_1.__importStar(require("../../resources/protocol.json"));
7
+ var bridge_connection_storage_1 = require("../../storage/bridge-connection-storage");
7
8
  var InjectedProvider = /** @class */ (function () {
8
- function InjectedProvider() {
9
+ function InjectedProvider(injectedWalletKey) {
9
10
  this.type = 'injected';
10
11
  this.unsubscribeCallback = null;
11
12
  this.listenSubscriptions = false;
12
13
  this.listeners = [];
13
- if (!InjectedProvider.isWalletInjected()) {
14
+ var window = InjectedProvider.window;
15
+ if (!InjectedProvider.isWindowContainsWallet(window, injectedWalletKey)) {
14
16
  throw new wallet_not_injected_error_1.WalletNotInjectedError();
15
17
  }
16
- this.injectedWallet = InjectedProvider.window.tonconnect;
18
+ this.injectedWallet = window[injectedWalletKey].tonconnect;
17
19
  }
18
- InjectedProvider.isWalletInjected = function () {
19
- return (InjectedProvider.window &&
20
- 'tonconnect' in InjectedProvider.window &&
21
- typeof InjectedProvider.window.tonconnect === 'object');
20
+ InjectedProvider.fromStorage = function (storage) {
21
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
22
+ var bridgeConnectionStorage, connection;
23
+ return tslib_1.__generator(this, function (_a) {
24
+ switch (_a.label) {
25
+ case 0:
26
+ bridgeConnectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
27
+ return [4 /*yield*/, bridgeConnectionStorage.getInjectedConnection()];
28
+ case 1:
29
+ connection = _a.sent();
30
+ return [2 /*return*/, new InjectedProvider(connection.jsBridgeKey)];
31
+ }
32
+ });
33
+ });
34
+ };
35
+ InjectedProvider.isWalletInjected = function (injectedWalletKey) {
36
+ return InjectedProvider.isWindowContainsWallet(this.window, injectedWalletKey);
37
+ };
38
+ InjectedProvider.isInsideWalletBrowser = function (injectedWalletKey) {
39
+ if (InjectedProvider.isWindowContainsWallet(this.window, injectedWalletKey)) {
40
+ return this.window[injectedWalletKey].tonconnect.isWalletBrowser;
41
+ }
42
+ return false;
22
43
  };
23
- InjectedProvider.deviceInfo = function () {
24
- return InjectedProvider.isWalletInjected()
25
- ? InjectedProvider.window.tonconnect.deviceInfo
26
- : undefined;
44
+ InjectedProvider.isWindowContainsWallet = function (window, injectedWalletKey) {
45
+ return (window &&
46
+ injectedWalletKey in window &&
47
+ typeof window[injectedWalletKey] === 'object' &&
48
+ 'tonconnect' in window[injectedWalletKey]);
27
49
  };
28
50
  InjectedProvider.prototype.connect = function (message, auto) {
29
51
  var _this = this;
@@ -48,14 +70,14 @@ var InjectedProvider = /** @class */ (function () {
48
70
  _this.listeners.forEach(function (listener) { return listener(connectEventError); });
49
71
  });
50
72
  };
51
- InjectedProvider.prototype.autoConnect = function () {
73
+ InjectedProvider.prototype.restoreConnection = function () {
52
74
  return tslib_1.__awaiter(this, void 0, void 0, function () {
53
75
  var connectEvent_1, e_1;
54
76
  return tslib_1.__generator(this, function (_a) {
55
77
  switch (_a.label) {
56
78
  case 0:
57
79
  _a.trys.push([0, 2, , 3]);
58
- return [4 /*yield*/, this.injectedWallet.autoConnect()];
80
+ return [4 /*yield*/, this.injectedWallet.restoreConnection()];
59
81
  case 1:
60
82
  connectEvent_1 = _a.sent();
61
83
  if (connectEvent_1.event === 'connect') {
@@ -2,8 +2,9 @@ import { AppRequest, ConnectEvent, ConnectRequest, DeviceInfo, RpcMethod, Wallet
2
2
  export interface InjectedWalletApi {
3
3
  deviceInfo: DeviceInfo;
4
4
  protocolVersion: number;
5
+ isWalletBrowser: boolean;
5
6
  connect(protocolVersion: number, message: ConnectRequest, auto: boolean): Promise<ConnectEvent>;
6
- autoConnect(): Promise<ConnectEvent>;
7
+ restoreConnection(): Promise<ConnectEvent>;
7
8
  send<T extends RpcMethod>(message: AppRequest<T>): Promise<WalletResponse<T>>;
8
9
  listen(callback: (event: WalletEvent) => void): () => void;
9
10
  }
@@ -11,7 +11,7 @@ export interface HTTPProvider extends BaseProvider {
11
11
  connect(message: ConnectRequest): string;
12
12
  }
13
13
  interface BaseProvider {
14
- autoConnect(): Promise<void>;
14
+ restoreConnection(): Promise<void>;
15
15
  closeConnection(): void;
16
16
  disconnect(): Promise<void>;
17
17
  sendRequest<T extends RpcMethod>(request: WithoutId<AppRequest<T>>): Promise<WithoutId<WalletResponse<T>>>;
@@ -1,4 +1,4 @@
1
- import { BridgeConnection } from "../provider/bridge/models/bridge-connection";
1
+ import { BridgeConnection, BridgeConnectionHttp, BridgeConnectionInjected } from "../provider/bridge/models/bridge-connection";
2
2
  import { IStorage } from "./models/storage.interface";
3
3
  export declare class BridgeConnectionStorage {
4
4
  private readonly storage;
@@ -7,5 +7,7 @@ export declare class BridgeConnectionStorage {
7
7
  storeConnection(connection: BridgeConnection): Promise<void>;
8
8
  removeConnection(): Promise<void>;
9
9
  getConnection(): Promise<BridgeConnection | null>;
10
- storedConnectionExists(): Promise<boolean>;
10
+ getHttpConnection(): Promise<BridgeConnectionHttp>;
11
+ getInjectedConnection(): Promise<BridgeConnectionInjected>;
12
+ storedConnectionType(): Promise<BridgeConnection['type'] | null>;
11
13
  }
@@ -3,23 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BridgeConnectionStorage = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var protocol_1 = require("@tonconnect/protocol");
6
+ var errors_1 = require("../errors");
6
7
  var BridgeConnectionStorage = /** @class */ (function () {
7
8
  function BridgeConnectionStorage(storage) {
8
9
  this.storage = storage;
9
- this.storeKey = 'ton-connect-storage_http-bridge-connection';
10
+ this.storeKey = 'ton-connect-storage_bridge-connection';
10
11
  }
11
12
  BridgeConnectionStorage.prototype.storeConnection = function (connection) {
12
13
  return tslib_1.__awaiter(this, void 0, void 0, function () {
13
14
  var rawSession, rawConnection;
14
15
  return tslib_1.__generator(this, function (_a) {
16
+ if (connection.type === 'injected') {
17
+ return [2 /*return*/, this.storage.setItem(this.storeKey, JSON.stringify(connection))];
18
+ }
15
19
  rawSession = {
16
20
  sessionKeyPair: connection.session.sessionCrypto.stringifyKeypair(),
17
21
  walletPublicKey: connection.session.walletPublicKey,
18
22
  walletConnectionSource: connection.session.walletConnectionSource
19
23
  };
20
24
  rawConnection = {
21
- session: rawSession,
22
- connectEvent: connection.connectEvent
25
+ type: 'http',
26
+ connectEvent: connection.connectEvent,
27
+ session: rawSession
23
28
  };
24
29
  return [2 /*return*/, this.storage.setItem(this.storeKey, JSON.stringify(rawConnection))];
25
30
  });
@@ -34,7 +39,7 @@ var BridgeConnectionStorage = /** @class */ (function () {
34
39
  };
35
40
  BridgeConnectionStorage.prototype.getConnection = function () {
36
41
  return tslib_1.__awaiter(this, void 0, void 0, function () {
37
- var stored, rawConnection, sessionCrypto;
42
+ var stored, connection, sessionCrypto;
38
43
  return tslib_1.__generator(this, function (_a) {
39
44
  switch (_a.label) {
40
45
  case 0: return [4 /*yield*/, this.storage.getItem(this.storeKey)];
@@ -43,29 +48,75 @@ var BridgeConnectionStorage = /** @class */ (function () {
43
48
  if (!stored) {
44
49
  return [2 /*return*/, null];
45
50
  }
46
- rawConnection = JSON.parse(stored);
47
- sessionCrypto = new protocol_1.SessionCrypto(rawConnection.session.sessionKeyPair);
51
+ connection = JSON.parse(stored);
52
+ if (connection.type === 'injected') {
53
+ return [2 /*return*/, connection];
54
+ }
55
+ sessionCrypto = new protocol_1.SessionCrypto(connection.session.sessionKeyPair);
48
56
  return [2 /*return*/, {
49
- connectEvent: rawConnection.connectEvent,
57
+ type: 'http',
58
+ connectEvent: connection.connectEvent,
50
59
  session: {
51
60
  sessionCrypto: sessionCrypto,
52
- walletConnectionSource: rawConnection.session.walletConnectionSource,
53
- walletPublicKey: rawConnection.session.walletPublicKey
61
+ walletConnectionSource: connection.session.walletConnectionSource,
62
+ walletPublicKey: connection.session.walletPublicKey
54
63
  }
55
64
  }];
56
65
  }
57
66
  });
58
67
  });
59
68
  };
60
- BridgeConnectionStorage.prototype.storedConnectionExists = function () {
69
+ BridgeConnectionStorage.prototype.getHttpConnection = function () {
61
70
  return tslib_1.__awaiter(this, void 0, void 0, function () {
62
- var stored;
71
+ var connection;
72
+ return tslib_1.__generator(this, function (_a) {
73
+ switch (_a.label) {
74
+ case 0: return [4 /*yield*/, this.getConnection()];
75
+ case 1:
76
+ connection = _a.sent();
77
+ if (!connection) {
78
+ throw new errors_1.TonConnectError('Trying to read HTTP connection source while nothing is stored');
79
+ }
80
+ if (connection.type === 'injected') {
81
+ throw new errors_1.TonConnectError('Trying to read HTTP connection source while injected connection is stored');
82
+ }
83
+ return [2 /*return*/, connection];
84
+ }
85
+ });
86
+ });
87
+ };
88
+ BridgeConnectionStorage.prototype.getInjectedConnection = function () {
89
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
90
+ var connection;
91
+ return tslib_1.__generator(this, function (_a) {
92
+ switch (_a.label) {
93
+ case 0: return [4 /*yield*/, this.getConnection()];
94
+ case 1:
95
+ connection = _a.sent();
96
+ if (!connection) {
97
+ throw new errors_1.TonConnectError('Trying to read Injected bridge connection source while nothing is stored');
98
+ }
99
+ if ((connection === null || connection === void 0 ? void 0 : connection.type) === 'http') {
100
+ throw new errors_1.TonConnectError('Trying to read Injected bridge connection source while HTTP connection is stored');
101
+ }
102
+ return [2 /*return*/, connection];
103
+ }
104
+ });
105
+ });
106
+ };
107
+ BridgeConnectionStorage.prototype.storedConnectionType = function () {
108
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
109
+ var stored, connection;
63
110
  return tslib_1.__generator(this, function (_a) {
64
111
  switch (_a.label) {
65
112
  case 0: return [4 /*yield*/, this.storage.getItem(this.storeKey)];
66
113
  case 1:
67
114
  stored = _a.sent();
68
- return [2 /*return*/, !!stored];
115
+ if (!stored) {
116
+ return [2 /*return*/, null];
117
+ }
118
+ connection = JSON.parse(stored);
119
+ return [2 /*return*/, connection.type];
69
120
  }
70
121
  });
71
122
  });
@@ -1,5 +1,5 @@
1
1
  import { IStorage } from "./models/storage.interface";
2
- export declare class BridgeGatewayStorage {
2
+ export declare class HttpBridgeGatewayStorage {
3
3
  private readonly storage;
4
4
  private readonly storeKey;
5
5
  constructor(storage: IStorage);
@@ -1,27 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BridgeGatewayStorage = void 0;
3
+ exports.HttpBridgeGatewayStorage = void 0;
4
4
  var tslib_1 = require("tslib");
5
- var BridgeGatewayStorage = /** @class */ (function () {
6
- function BridgeGatewayStorage(storage) {
5
+ var HttpBridgeGatewayStorage = /** @class */ (function () {
6
+ function HttpBridgeGatewayStorage(storage) {
7
7
  this.storage = storage;
8
8
  this.storeKey = 'ton-connect-storage_http-bridge-gateway';
9
9
  }
10
- BridgeGatewayStorage.prototype.storeLastEventId = function (lastEventId) {
10
+ HttpBridgeGatewayStorage.prototype.storeLastEventId = function (lastEventId) {
11
11
  return tslib_1.__awaiter(this, void 0, void 0, function () {
12
12
  return tslib_1.__generator(this, function (_a) {
13
13
  return [2 /*return*/, this.storage.setItem(this.storeKey, lastEventId)];
14
14
  });
15
15
  });
16
16
  };
17
- BridgeGatewayStorage.prototype.removeLastEventId = function () {
17
+ HttpBridgeGatewayStorage.prototype.removeLastEventId = function () {
18
18
  return tslib_1.__awaiter(this, void 0, void 0, function () {
19
19
  return tslib_1.__generator(this, function (_a) {
20
20
  return [2 /*return*/, this.storage.removeItem(this.storeKey)];
21
21
  });
22
22
  });
23
23
  };
24
- BridgeGatewayStorage.prototype.getLastEventId = function () {
24
+ HttpBridgeGatewayStorage.prototype.getLastEventId = function () {
25
25
  return tslib_1.__awaiter(this, void 0, void 0, function () {
26
26
  var stored;
27
27
  return tslib_1.__generator(this, function (_a) {
@@ -37,6 +37,6 @@ var BridgeGatewayStorage = /** @class */ (function () {
37
37
  });
38
38
  });
39
39
  };
40
- return BridgeGatewayStorage;
40
+ return HttpBridgeGatewayStorage;
41
41
  }());
42
- exports.BridgeGatewayStorage = BridgeGatewayStorage;
42
+ exports.HttpBridgeGatewayStorage = HttpBridgeGatewayStorage;
@@ -2,10 +2,13 @@ import { TonConnectError } from "./errors/ton-connect.error";
2
2
  import { Account, DappMetadata, Wallet, WalletConnectionSource } from "./models";
3
3
  import { SendTransactionRequest, SendTransactionResponse } from "./models/methods";
4
4
  import { ConnectAdditionalRequest } from "./models/methods/connect/connect-additional-request";
5
- import { InjectedProvider } from "./provider/injected/injected-provider";
5
+ import { JSBridgeWalletConfig } from "./models/wallet/wallet-config";
6
+ import { WalletConnectionSourceJS } from "./models/wallet/wallet-connection-source";
6
7
  import { IStorage } from "./storage/models/storage.interface";
7
8
  import { ITonConnect } from "./ton-connect.interface";
9
+ import { WalletsListManager } from "./wallets-list-manager";
8
10
  export declare class TonConnect implements ITonConnect {
11
+ walletsList: WalletsListManager;
9
12
  private readonly dappSettings;
10
13
  private readonly bridgeConnectionStorage;
11
14
  private _wallet;
@@ -32,7 +35,7 @@ export declare class TonConnect implements ITonConnect {
32
35
  /**
33
36
  * Indicates if the injected wallet is available.
34
37
  */
35
- isInjectedProviderAvailable: typeof InjectedProvider.isWalletInjected;
38
+ inWhichWalletBrowser(): Promise<JSBridgeWalletConfig | null>;
36
39
  /**
37
40
  * Allows to subscribe to connection status changes and handle connection errors.
38
41
  * @param callback will be called after connections status changes with actual wallet or null.
@@ -42,15 +45,15 @@ export declare class TonConnect implements ITonConnect {
42
45
  onStatusChange(callback: (wallet: Wallet | null) => void, errorsHandler?: (err: TonConnectError) => void): () => void;
43
46
  /**
44
47
  * Generates universal link for an external wallet and subscribes to the wallet's bridge, or sends connect request to the injected wallet.
45
- * @param wallet wallet's bridge url and universal link for an external wallet or 'injected' for the injected wallet.
48
+ * @param wallet wallet's bridge url and universal link for an external wallet or jsBridge key for the injected wallet.
46
49
  * @param request (optional) additional request to pass to the wallet while connect (currently only ton_proof is available).
47
50
  * @returns universal link if external wallet was passed or void for the injected wallet.
48
51
  */
49
- connect<T extends WalletConnectionSource | 'injected'>(wallet: T, request?: ConnectAdditionalRequest): T extends 'injected' ? void : string;
52
+ connect<T extends WalletConnectionSource>(wallet: T, request?: ConnectAdditionalRequest): T extends WalletConnectionSourceJS ? void : string;
50
53
  /**
51
54
  * Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
52
55
  */
53
- autoConnect(): void;
56
+ restoreConnection(): Promise<void>;
54
57
  /**
55
58
  * Asks connected wallet to sign and send the transaction.
56
59
  * @param transaction transaction to send.
@@ -62,7 +65,6 @@ export declare class TonConnect implements ITonConnect {
62
65
  * Disconnect form thw connected wallet and drop current session.
63
66
  */
64
67
  disconnect(): Promise<void>;
65
- private _autoConnect;
66
68
  private createProvider;
67
69
  private walletEventsListener;
68
70
  private onWalletConnected;
@@ -2,6 +2,8 @@ import { TonConnectError } from "./errors";
2
2
  import { Account, WalletConnectionSource, Wallet } from "./models";
3
3
  import { SendTransactionRequest, SendTransactionResponse } from "./models/methods";
4
4
  import { ConnectAdditionalRequest } from "./models/methods/connect/connect-additional-request";
5
+ import { JSBridgeWalletConfig, WalletConfig } from "./models/wallet/wallet-config";
6
+ import { WalletConnectionSourceJS } from "./models/wallet/wallet-connection-source";
5
7
  export interface ITonConnect {
6
8
  /**
7
9
  * Shows if the wallet is connected right now.
@@ -16,9 +18,16 @@ export interface ITonConnect {
16
18
  */
17
19
  wallet: Wallet | null;
18
20
  /**
19
- * Indicates if the injected wallet is available.
21
+ * Allows to get information about supported wallets
20
22
  */
21
- isInjectedProviderAvailable(): boolean;
23
+ walletsList: {
24
+ getWalletsList: () => Promise<WalletConfig[]>;
25
+ getInjectedWalletsList: () => Promise<JSBridgeWalletConfig[]>;
26
+ };
27
+ /**
28
+ * If app is opened in some wallet's browser returns that wallet config. Else returns null;
29
+ */
30
+ inWhichWalletBrowser(): Promise<JSBridgeWalletConfig | null>;
22
31
  /**
23
32
  * Allows to subscribe to connection status changes and handle connection errors.
24
33
  * @param callback will be called after connections status changes with actual wallet or null.
@@ -28,15 +37,15 @@ export interface ITonConnect {
28
37
  onStatusChange(callback: (walletInfo: Wallet | null) => void, errorsHandler?: (err: TonConnectError) => void): () => void;
29
38
  /**
30
39
  * Generates universal link for an external wallet and subscribes to the wallet's bridge, or sends connect request to the injected wallet.
31
- * @param wallet wallet's bridge url and universal link for an external wallet or 'injected' for the injected wallet.
40
+ * @param wallet wallet's bridge url and universal link for an external wallet or jsBridge key for the injected wallet.
32
41
  * @param request (optional) additional request to pass to the wallet while connect (currently only ton_proof is available).
33
42
  * @returns universal link if external wallet was passed or void for the injected wallet.
34
43
  */
35
- connect<T extends WalletConnectionSource | 'injected'>(wallet: T, request?: ConnectAdditionalRequest): T extends 'injected' ? void : string;
44
+ connect<T extends WalletConnectionSource>(wallet: T, request?: ConnectAdditionalRequest): T extends WalletConnectionSourceJS ? void : string;
36
45
  /**
37
46
  * Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
38
47
  */
39
- autoConnect(): void;
48
+ restoreConnection(): Promise<void>;
40
49
  /**
41
50
  * Disconnect form thw connected wallet and drop current session.
42
51
  */
@@ -5,6 +5,7 @@ var tslib_1 = require("tslib");
5
5
  var ton_connect_error_1 = require("./errors/ton-connect.error");
6
6
  var wallet_already_connected_error_1 = require("./errors/wallet/wallet-already-connected.error");
7
7
  var wallet_not_connected_error_1 = require("./errors/wallet/wallet-not-connected.error");
8
+ var wallet_connection_source_1 = require("./models/wallet/wallet-connection-source");
8
9
  var connect_errors_parser_1 = require("./parsers/connect-errors-parser");
9
10
  var send_transaction_parser_1 = require("./parsers/send-transaction-parser");
10
11
  var bridge_provider_1 = require("./provider/bridge/bridge-provider");
@@ -13,16 +14,14 @@ var bridge_connection_storage_1 = require("./storage/bridge-connection-storage")
13
14
  var default_storage_1 = require("./storage/default-storage");
14
15
  var options_1 = require("./utils/options");
15
16
  var web_api_1 = require("./utils/web-api");
17
+ var wallets_list_manager_1 = require("./wallets-list-manager");
16
18
  var TonConnect = /** @class */ (function () {
17
19
  function TonConnect(options) {
20
+ this.walletsList = new wallets_list_manager_1.WalletsListManager();
18
21
  this._wallet = null;
19
22
  this.provider = null;
20
23
  this.statusChangeSubscriptions = [];
21
24
  this.statusChangeErrorSubscriptions = [];
22
- /**
23
- * Indicates if the injected wallet is available.
24
- */
25
- this.isInjectedProviderAvailable = injected_provider_1.InjectedProvider.isWalletInjected;
26
25
  this.dappSettings = {
27
26
  metadata: (0, options_1.mergeOptions)(options === null || options === void 0 ? void 0 : options.dappMetedata, (0, web_api_1.getWebPageMetadata)()),
28
27
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new default_storage_1.DefaultStorage()
@@ -65,6 +64,28 @@ var TonConnect = /** @class */ (function () {
65
64
  enumerable: false,
66
65
  configurable: true
67
66
  });
67
+ /**
68
+ * Indicates if the injected wallet is available.
69
+ */
70
+ TonConnect.prototype.inWhichWalletBrowser = function () {
71
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
72
+ var injectedWalletsList;
73
+ return tslib_1.__generator(this, function (_a) {
74
+ switch (_a.label) {
75
+ case 0: return [4 /*yield*/, this.walletsList.getInjectedWalletsList()];
76
+ case 1:
77
+ injectedWalletsList = _a.sent();
78
+ if (injectedWalletsList.length !== 1) {
79
+ return [2 /*return*/, null];
80
+ }
81
+ if (!injected_provider_1.InjectedProvider.isInsideWalletBrowser(injectedWalletsList[0].jsBridgeKey)) {
82
+ return [2 /*return*/, null];
83
+ }
84
+ return [2 /*return*/, injectedWalletsList[0]];
85
+ }
86
+ });
87
+ });
88
+ };
68
89
  /**
69
90
  * Allows to subscribe to connection status changes and handle connection errors.
70
91
  * @param callback will be called after connections status changes with actual wallet or null.
@@ -96,8 +117,51 @@ var TonConnect = /** @class */ (function () {
96
117
  /**
97
118
  * Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
98
119
  */
99
- TonConnect.prototype.autoConnect = function () {
100
- this._autoConnect();
120
+ TonConnect.prototype.restoreConnection = function () {
121
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
122
+ var _a, bridgeConnectionType, injectedWalletsList, _b, _c, _d, _e;
123
+ return tslib_1.__generator(this, function (_f) {
124
+ switch (_f.label) {
125
+ case 0: return [4 /*yield*/, Promise.all([
126
+ this.bridgeConnectionStorage.storedConnectionType(),
127
+ this.walletsList.getInjectedWalletsList()
128
+ ])];
129
+ case 1:
130
+ _a = _f.sent(), bridgeConnectionType = _a[0], injectedWalletsList = _a[1];
131
+ _b = bridgeConnectionType;
132
+ switch (_b) {
133
+ case 'http': return [3 /*break*/, 2];
134
+ case 'injected': return [3 /*break*/, 4];
135
+ }
136
+ return [3 /*break*/, 6];
137
+ case 2:
138
+ _c = this;
139
+ return [4 /*yield*/, bridge_provider_1.BridgeProvider.fromStorage(this.dappSettings.storage)];
140
+ case 3:
141
+ _c.provider = _f.sent();
142
+ return [3 /*break*/, 9];
143
+ case 4:
144
+ _d = this;
145
+ return [4 /*yield*/, injected_provider_1.InjectedProvider.fromStorage(this.dappSettings.storage)];
146
+ case 5:
147
+ _d.provider = _f.sent();
148
+ return [3 /*break*/, 9];
149
+ case 6:
150
+ if (!(injectedWalletsList.length === 1)) return [3 /*break*/, 8];
151
+ // DApp probably opened in the wallet's browser. Should try smart auto connect
152
+ _e = this;
153
+ return [4 /*yield*/, this.createProvider(injectedWalletsList[0])];
154
+ case 7:
155
+ // DApp probably opened in the wallet's browser. Should try smart auto connect
156
+ _e.provider = _f.sent();
157
+ return [3 /*break*/, 9];
158
+ case 8: return [2 /*return*/];
159
+ case 9:
160
+ this.provider.listen(this.walletEventsListener.bind(this));
161
+ return [2 /*return*/, this.provider.restoreConnection()];
162
+ }
163
+ });
164
+ });
101
165
  };
102
166
  /**
103
167
  * Asks connected wallet to sign and send the transaction.
@@ -143,36 +207,10 @@ var TonConnect = /** @class */ (function () {
143
207
  });
144
208
  });
145
209
  };
146
- TonConnect.prototype._autoConnect = function () {
147
- return tslib_1.__awaiter(this, void 0, void 0, function () {
148
- var bridgeConnection, _a, _b;
149
- return tslib_1.__generator(this, function (_c) {
150
- switch (_c.label) {
151
- case 0: return [4 /*yield*/, this.bridgeConnectionStorage.getConnection()];
152
- case 1:
153
- bridgeConnection = _c.sent();
154
- if (!bridgeConnection) return [3 /*break*/, 3];
155
- _a = this;
156
- return [4 /*yield*/, this.createProvider(bridgeConnection.session.walletConnectionSource)];
157
- case 2:
158
- _a.provider = _c.sent();
159
- return [2 /*return*/, this.provider.autoConnect()];
160
- case 3:
161
- if (!injected_provider_1.InjectedProvider.isWalletInjected()) return [3 /*break*/, 5];
162
- _b = this;
163
- return [4 /*yield*/, this.createProvider('injected')];
164
- case 4:
165
- _b.provider = _c.sent();
166
- return [2 /*return*/, this.provider.autoConnect()];
167
- case 5: return [2 /*return*/];
168
- }
169
- });
170
- });
171
- };
172
210
  TonConnect.prototype.createProvider = function (wallet) {
173
211
  var provider;
174
- if (wallet === 'injected') {
175
- provider = new injected_provider_1.InjectedProvider();
212
+ if ((0, wallet_connection_source_1.isWalletConnectionSourceJS)(wallet)) {
213
+ provider = new injected_provider_1.InjectedProvider(wallet.jsBridgeKey);
176
214
  }
177
215
  else {
178
216
  provider = new bridge_provider_1.BridgeProvider(this.dappSettings.storage, wallet);
@@ -0,0 +1,17 @@
1
+ import { HTTPBridgeWalletConfig, JSBridgeWalletConfig, WalletConfig } from "./models/wallet/wallet-config";
2
+ export declare class WalletsListManager {
3
+ private walletsListCache;
4
+ private readonly walletsListSource;
5
+ getWalletsList(): Promise<WalletConfig[]>;
6
+ getInjectedWalletsList(): Promise<JSBridgeWalletConfig[]>;
7
+ getRemoteConnectionWalletsList(): Promise<HTTPBridgeWalletConfig[]>;
8
+ getWalletsConfig(): Promise<{
9
+ allWalletsList: WalletConfig[];
10
+ injectedWalletsList: JSBridgeWalletConfig[];
11
+ remoteConnectionWalletsList: HTTPBridgeWalletConfig[];
12
+ }>;
13
+ private walletConfigDTOListToWalletConfigList;
14
+ private isCorrectWalletConfigDTO;
15
+ private isInjectedWallet;
16
+ private isRemoteConnectionWallet;
17
+ }
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WalletsListManager = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var fetch_wallets_error_1 = require("./errors/wallets-manager/fetch-wallets.error");
6
+ var injected_provider_1 = require("./provider/injected/injected-provider");
7
+ var WalletsListManager = /** @class */ (function () {
8
+ function WalletsListManager() {
9
+ this.walletsListCache = null;
10
+ this.walletsListSource = 'https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json';
11
+ }
12
+ WalletsListManager.prototype.getWalletsList = function () {
13
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
14
+ var walletsResponse, walletsList, e_1;
15
+ var _this = this;
16
+ return tslib_1.__generator(this, function (_a) {
17
+ switch (_a.label) {
18
+ case 0:
19
+ if (this.walletsListCache) {
20
+ return [2 /*return*/, this.walletsListCache];
21
+ }
22
+ _a.label = 1;
23
+ case 1:
24
+ _a.trys.push([1, 4, , 5]);
25
+ return [4 /*yield*/, fetch(this.walletsListSource)];
26
+ case 2:
27
+ walletsResponse = _a.sent();
28
+ return [4 /*yield*/, walletsResponse.json()];
29
+ case 3:
30
+ walletsList = _a.sent();
31
+ if (!Array.isArray(walletsList) ||
32
+ !walletsList.every(function (wallet) { return _this.isCorrectWalletConfigDTO(wallet); })) {
33
+ throw new fetch_wallets_error_1.FetchWalletsError('Wrong wallets list format');
34
+ }
35
+ this.walletsListCache = this.walletConfigDTOListToWalletConfigList(walletsList);
36
+ return [2 /*return*/, this.walletsListCache];
37
+ case 4:
38
+ e_1 = _a.sent();
39
+ throw new fetch_wallets_error_1.FetchWalletsError(e_1);
40
+ case 5: return [2 /*return*/];
41
+ }
42
+ });
43
+ });
44
+ };
45
+ WalletsListManager.prototype.getInjectedWalletsList = function () {
46
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
47
+ var walletsList;
48
+ return tslib_1.__generator(this, function (_a) {
49
+ switch (_a.label) {
50
+ case 0: return [4 /*yield*/, this.getWalletsList()];
51
+ case 1:
52
+ walletsList = (_a.sent()).filter(this.isInjectedWallet);
53
+ return [2 /*return*/, walletsList.filter(function (wallet) { return injected_provider_1.InjectedProvider.isWalletInjected(wallet.jsBridgeKey); })];
54
+ }
55
+ });
56
+ });
57
+ };
58
+ WalletsListManager.prototype.getRemoteConnectionWalletsList = function () {
59
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
60
+ return tslib_1.__generator(this, function (_a) {
61
+ switch (_a.label) {
62
+ case 0: return [4 /*yield*/, this.getWalletsList()];
63
+ case 1: return [2 /*return*/, (_a.sent()).filter(this.isRemoteConnectionWallet)];
64
+ }
65
+ });
66
+ });
67
+ };
68
+ WalletsListManager.prototype.getWalletsConfig = function () {
69
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
70
+ var allWalletsList, _a, injectedWalletsList, remoteConnectionWalletsList;
71
+ return tslib_1.__generator(this, function (_b) {
72
+ switch (_b.label) {
73
+ case 0: return [4 /*yield*/, this.getWalletsList()];
74
+ case 1:
75
+ allWalletsList = _b.sent();
76
+ return [4 /*yield*/, Promise.all([
77
+ this.getInjectedWalletsList(),
78
+ this.getRemoteConnectionWalletsList()
79
+ ])];
80
+ case 2:
81
+ _a = _b.sent(), injectedWalletsList = _a[0], remoteConnectionWalletsList = _a[1];
82
+ return [2 /*return*/, {
83
+ allWalletsList: allWalletsList,
84
+ injectedWalletsList: injectedWalletsList,
85
+ remoteConnectionWalletsList: remoteConnectionWalletsList
86
+ }];
87
+ }
88
+ });
89
+ });
90
+ };
91
+ WalletsListManager.prototype.walletConfigDTOListToWalletConfigList = function (walletConfigDTO) {
92
+ return walletConfigDTO.map(function (walletConfigDTO) {
93
+ var walletConfig = {
94
+ name: walletConfigDTO.name,
95
+ imageUrl: walletConfigDTO.image,
96
+ aboutUrl: walletConfigDTO.about_url,
97
+ tondns: walletConfigDTO.tondns
98
+ };
99
+ if ('bridge_url' in walletConfigDTO) {
100
+ walletConfig.bridgeUrl = walletConfigDTO.bridge_url;
101
+ walletConfig.universalLinkBase =
102
+ walletConfigDTO.universal_url;
103
+ }
104
+ if ('js_bridge_key' in walletConfigDTO) {
105
+ walletConfig.jsBridgeKey = walletConfigDTO.js_bridge_key;
106
+ }
107
+ return walletConfig;
108
+ });
109
+ };
110
+ WalletsListManager.prototype.isCorrectWalletConfigDTO = function (value) {
111
+ if (!value || !(typeof value === 'object')) {
112
+ return false;
113
+ }
114
+ var containsName = 'name' in value;
115
+ var containsImage = 'image' in value;
116
+ var containsAbout = 'about_url' in value;
117
+ if (!containsName || !containsImage || !containsAbout) {
118
+ return false;
119
+ }
120
+ var containsUniversalUrl = 'universal_url' in value;
121
+ var containsHttpBridge = 'bridge_url' in value;
122
+ var containsJsBridge = 'js_bridge_key' in value;
123
+ if (!(containsHttpBridge && containsUniversalUrl) && !containsJsBridge) {
124
+ return false;
125
+ }
126
+ return true;
127
+ };
128
+ WalletsListManager.prototype.isInjectedWallet = function (wallet) {
129
+ return 'jsBridgeKey' in wallet;
130
+ };
131
+ WalletsListManager.prototype.isRemoteConnectionWallet = function (wallet) {
132
+ return 'bridgeUrl' in wallet;
133
+ };
134
+ return WalletsListManager;
135
+ }());
136
+ exports.WalletsListManager = WalletsListManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonconnect/sdk",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "scripts": {
5
5
  "build": "npx rimraf lib && ttsc",
6
6
  "build:production": "npx rimraf lib && ttsc --sourceMap false"