@tonconnect/sdk 0.0.13 → 0.0.15

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 (33) 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/index.js +3 -0
  10. package/lib/models/wallet/wallet-connection-source.d.ts +7 -2
  11. package/lib/models/wallet/wallet-connection-source.js +5 -0
  12. package/lib/models/wallet/wallet-info.d.ts +31 -0
  13. package/lib/models/wallet/wallet-info.js +7 -0
  14. package/lib/models/wallet/wallet.d.ts +1 -1
  15. package/lib/provider/bridge/bridge-gateway.js +2 -2
  16. package/lib/provider/bridge/bridge-provider.d.ts +4 -3
  17. package/lib/provider/bridge/bridge-provider.js +26 -6
  18. package/lib/provider/bridge/models/bridge-connection.d.ts +19 -7
  19. package/lib/provider/bridge/models/bridge-session.d.ts +2 -2
  20. package/lib/provider/injected/injected-provider.d.ts +10 -6
  21. package/lib/provider/injected/injected-provider.js +44 -18
  22. package/lib/provider/injected/models/injected-wallet-api.d.ts +3 -1
  23. package/lib/provider/provider.d.ts +1 -1
  24. package/lib/storage/bridge-connection-storage.d.ts +4 -2
  25. package/lib/storage/bridge-connection-storage.js +63 -12
  26. package/lib/storage/{bridge-gateway-storage.d.ts → http-bridge-gateway-storage.d.ts} +1 -1
  27. package/lib/storage/{bridge-gateway-storage.js → http-bridge-gateway-storage.js} +8 -8
  28. package/lib/ton-connect.d.ts +8 -8
  29. package/lib/ton-connect.interface.d.ts +7 -5
  30. package/lib/ton-connect.js +55 -35
  31. package/lib/wallets-list-manager.d.ts +10 -0
  32. package/lib/wallets-list-manager.js +109 -0
  33. package/package.json +2 -2
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 { WalletInfo, WalletInfoInjected, WalletInfoRemote, isWalletInfoInjected } from './wallet-info';
@@ -1,2 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isWalletInfoInjected = void 0;
4
+ var wallet_info_1 = require("./wallet-info");
5
+ Object.defineProperty(exports, "isWalletInfoInjected", { enumerable: true, get: function () { return wallet_info_1.isWalletInfoInjected; } });
@@ -1,4 +1,9 @@
1
- export interface WalletConnectionSource {
2
- universalLinkBase: string;
1
+ export declare type WalletConnectionSource = WalletConnectionSourceHTTP | WalletConnectionSourceJS;
2
+ export interface WalletConnectionSourceHTTP {
3
+ universalLink: 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;
@@ -0,0 +1,31 @@
1
+ export interface WalletInfoBase {
2
+ name: string;
3
+ imageUrl: string;
4
+ tondns?: string;
5
+ aboutUrl: string;
6
+ }
7
+ export interface WalletInfoRemote extends WalletInfoBase {
8
+ universalLink: string;
9
+ bridgeUrl: string;
10
+ }
11
+ export interface WalletInfoInjected extends WalletInfoBase {
12
+ jsBridgeKey: string;
13
+ injected: boolean;
14
+ embedded: boolean;
15
+ }
16
+ export declare type WalletInfo = WalletInfoRemote | WalletInfoInjected | (WalletInfoRemote & WalletInfoInjected);
17
+ export interface WalletInfoDTOBase {
18
+ name: string;
19
+ image: string;
20
+ tondns?: string;
21
+ about_url: string;
22
+ }
23
+ export interface WalletInfoRemoteDTO extends WalletInfoDTOBase {
24
+ universal_url: string;
25
+ bridge_url: string;
26
+ }
27
+ export interface WalletInfoInjectedDTO extends WalletInfoDTOBase {
28
+ js_bridge_key: string;
29
+ }
30
+ export declare type WalletInfoDTO = WalletInfoRemoteDTO | WalletInfoInjectedDTO | (WalletInfoRemoteDTO & WalletInfoInjectedDTO);
31
+ export declare function isWalletInfoInjected(value: WalletInfo): value is WalletInfoInjected;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isWalletInfoInjected = void 0;
4
+ function isWalletInfoInjected(value) {
5
+ return 'jsBridgeKey' in value;
6
+ }
7
+ exports.isWalletInfoInjected = isWalletInfoInjected;
@@ -5,6 +5,6 @@ export interface Wallet {
5
5
  provider: 'http' | 'injected';
6
6
  account: Account;
7
7
  connectItems?: {
8
- tonProof?: TonProofItemReply['signature'];
8
+ tonProof?: TonProofItemReply['proof'];
9
9
  };
10
10
  }
@@ -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>;
@@ -7,7 +7,6 @@ var ton_connect_error_1 = require("../../errors/ton-connect.error");
7
7
  var bridge_gateway_1 = require("./bridge-gateway");
8
8
  var bridge_connection_storage_1 = require("../../storage/bridge-connection-storage");
9
9
  var protocol = tslib_1.__importStar(require("../../resources/protocol.json"));
10
- var url_1 = require("../../utils/url");
11
10
  var BridgeProvider = /** @class */ (function () {
12
11
  function BridgeProvider(storage, walletConnectionSource) {
13
12
  this.storage = storage;
@@ -21,6 +20,21 @@ var BridgeProvider = /** @class */ (function () {
21
20
  this.listeners = [];
22
21
  this.connectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
23
22
  }
23
+ BridgeProvider.fromStorage = function (storage) {
24
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
25
+ var bridgeConnectionStorage, connection;
26
+ return tslib_1.__generator(this, function (_a) {
27
+ switch (_a.label) {
28
+ case 0:
29
+ bridgeConnectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
30
+ return [4 /*yield*/, bridgeConnectionStorage.getHttpConnection()];
31
+ case 1:
32
+ connection = _a.sent();
33
+ return [2 /*return*/, new BridgeProvider(storage, connection.session.walletConnectionSource)];
34
+ }
35
+ });
36
+ });
37
+ };
24
38
  BridgeProvider.prototype.connect = function (message) {
25
39
  var _a;
26
40
  (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
@@ -33,7 +47,7 @@ var BridgeProvider = /** @class */ (function () {
33
47
  this.bridge.registerSession();
34
48
  return this.generateUniversalLink(message);
35
49
  };
36
- BridgeProvider.prototype.autoConnect = function () {
50
+ BridgeProvider.prototype.restoreConnection = function () {
37
51
  var _a;
38
52
  return tslib_1.__awaiter(this, void 0, void 0, function () {
39
53
  var storedConnection;
@@ -41,7 +55,7 @@ var BridgeProvider = /** @class */ (function () {
41
55
  switch (_b.label) {
42
56
  case 0:
43
57
  (_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
44
- return [4 /*yield*/, this.connectionStorage.getConnection()];
58
+ return [4 /*yield*/, this.connectionStorage.getHttpConnection()];
45
59
  case 1:
46
60
  storedConnection = _b.sent();
47
61
  if (!storedConnection) {
@@ -125,11 +139,18 @@ var BridgeProvider = /** @class */ (function () {
125
139
  };
126
140
  BridgeProvider.prototype.updateSession = function (connectEvent, walletPublicKey) {
127
141
  return tslib_1.__awaiter(this, void 0, void 0, function () {
142
+ var tonAddrItem, connectEventToSave;
128
143
  return tslib_1.__generator(this, function (_a) {
129
144
  switch (_a.label) {
130
145
  case 0:
131
146
  this.session = tslib_1.__assign(tslib_1.__assign({}, this.session), { walletPublicKey: walletPublicKey });
132
- return [4 /*yield*/, this.connectionStorage.storeConnection({ session: this.session, connectEvent: connectEvent })];
147
+ tonAddrItem = connectEvent.payload.items.find(function (item) { return item.name === 'ton_addr'; });
148
+ connectEventToSave = tslib_1.__assign(tslib_1.__assign({}, connectEvent), { payload: tslib_1.__assign(tslib_1.__assign({}, connectEvent.payload), { items: [tonAddrItem] }) });
149
+ return [4 /*yield*/, this.connectionStorage.storeConnection({
150
+ type: 'http',
151
+ session: this.session,
152
+ connectEvent: connectEventToSave
153
+ })];
133
154
  case 1:
134
155
  _a.sent();
135
156
  return [2 /*return*/];
@@ -153,8 +174,7 @@ var BridgeProvider = /** @class */ (function () {
153
174
  });
154
175
  };
155
176
  BridgeProvider.prototype.generateUniversalLink = function (message) {
156
- var urlString = (0, url_1.addPathToUrl)(this.walletConnectionSource.universalLinkBase, this.universalLinkPath);
157
- var url = new URL(urlString);
177
+ var url = new URL(this.walletConnectionSource.universalLink);
158
178
  url.searchParams.append('v', protocol.version.toString());
159
179
  url.searchParams.append('id', this.session.sessionCrypto.sessionId);
160
180
  url.searchParams.append('r', protocol_1.Base64.encode(JSON.stringify(message), true));
@@ -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,20 +1,24 @@
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>;
21
+ private closeAllListeners;
18
22
  listen(eventsCallback: (e: WalletEvent) => void): () => void;
19
23
  sendRequest<T extends RpcMethod>(request: WithoutId<AppRequest<T>>): Promise<WithoutId<WalletResponse<T>>>;
20
24
  private makeSubscriptions;
@@ -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
+ });
22
34
  };
23
- InjectedProvider.deviceInfo = function () {
24
- return InjectedProvider.isWalletInjected()
25
- ? InjectedProvider.window.tonconnect.deviceInfo
26
- : undefined;
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;
43
+ };
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') {
@@ -74,17 +96,21 @@ var InjectedProvider = /** @class */ (function () {
74
96
  });
75
97
  };
76
98
  InjectedProvider.prototype.closeConnection = function () {
77
- var _a;
78
- this.listenSubscriptions = false;
79
- this.listeners = [];
80
- (_a = this.unsubscribeCallback) === null || _a === void 0 ? void 0 : _a.call(this);
99
+ if (this.listenSubscriptions) {
100
+ this.injectedWallet.disconnect();
101
+ }
102
+ this.closeAllListeners();
81
103
  };
82
104
  InjectedProvider.prototype.disconnect = function () {
105
+ this.closeAllListeners();
106
+ this.injectedWallet.disconnect();
107
+ return Promise.resolve();
108
+ };
109
+ InjectedProvider.prototype.closeAllListeners = function () {
83
110
  var _a;
84
111
  this.listenSubscriptions = false;
85
112
  this.listeners = [];
86
113
  (_a = this.unsubscribeCallback) === null || _a === void 0 ? void 0 : _a.call(this);
87
- return Promise.resolve();
88
114
  };
89
115
  InjectedProvider.prototype.listen = function (eventsCallback) {
90
116
  var _this = this;
@@ -2,8 +2,10 @@ 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;
10
+ disconnect(): void;
9
11
  }
@@ -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;
@@ -1,11 +1,12 @@
1
1
  import { TonConnectError } from "./errors/ton-connect.error";
2
- import { Account, DappMetadata, Wallet, WalletConnectionSource } from "./models";
2
+ import { Account, DappMetadata, Wallet, WalletConnectionSource, WalletInfo } 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 { WalletConnectionSourceJS } from "./models/wallet/wallet-connection-source";
6
6
  import { IStorage } from "./storage/models/storage.interface";
7
7
  import { ITonConnect } from "./ton-connect.interface";
8
8
  export declare class TonConnect implements ITonConnect {
9
+ private readonly walletsList;
9
10
  private readonly dappSettings;
10
11
  private readonly bridgeConnectionStorage;
11
12
  private _wallet;
@@ -30,9 +31,9 @@ export declare class TonConnect implements ITonConnect {
30
31
  storage?: IStorage;
31
32
  });
32
33
  /**
33
- * Indicates if the injected wallet is available.
34
+ * Returns available wallets list.
34
35
  */
35
- isInjectedProviderAvailable: typeof InjectedProvider.isWalletInjected;
36
+ getWallets(): Promise<WalletInfo[]>;
36
37
  /**
37
38
  * Allows to subscribe to connection status changes and handle connection errors.
38
39
  * @param callback will be called after connections status changes with actual wallet or null.
@@ -42,15 +43,15 @@ export declare class TonConnect implements ITonConnect {
42
43
  onStatusChange(callback: (wallet: Wallet | null) => void, errorsHandler?: (err: TonConnectError) => void): () => void;
43
44
  /**
44
45
  * 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.
46
+ * @param wallet wallet's bridge url and universal link for an external wallet or jsBridge key for the injected wallet.
46
47
  * @param request (optional) additional request to pass to the wallet while connect (currently only ton_proof is available).
47
48
  * @returns universal link if external wallet was passed or void for the injected wallet.
48
49
  */
49
- connect<T extends WalletConnectionSource | 'injected'>(wallet: T, request?: ConnectAdditionalRequest): T extends 'injected' ? void : string;
50
+ connect<T extends WalletConnectionSource>(wallet: T, request?: ConnectAdditionalRequest): T extends WalletConnectionSourceJS ? void : string;
50
51
  /**
51
52
  * Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
52
53
  */
53
- autoConnect(): void;
54
+ restoreConnection(): Promise<void>;
54
55
  /**
55
56
  * Asks connected wallet to sign and send the transaction.
56
57
  * @param transaction transaction to send.
@@ -62,7 +63,6 @@ export declare class TonConnect implements ITonConnect {
62
63
  * Disconnect form thw connected wallet and drop current session.
63
64
  */
64
65
  disconnect(): Promise<void>;
65
- private _autoConnect;
66
66
  private createProvider;
67
67
  private walletEventsListener;
68
68
  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 { WalletInfo } from "./models/wallet/wallet-info";
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,9 @@ export interface ITonConnect {
16
18
  */
17
19
  wallet: Wallet | null;
18
20
  /**
19
- * Indicates if the injected wallet is available.
21
+ * Returns available wallets list.
20
22
  */
21
- isInjectedProviderAvailable(): boolean;
23
+ getWallets(): Promise<WalletInfo[]>;
22
24
  /**
23
25
  * Allows to subscribe to connection status changes and handle connection errors.
24
26
  * @param callback will be called after connections status changes with actual wallet or null.
@@ -28,15 +30,15 @@ export interface ITonConnect {
28
30
  onStatusChange(callback: (walletInfo: Wallet | null) => void, errorsHandler?: (err: TonConnectError) => void): () => void;
29
31
  /**
30
32
  * 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.
33
+ * @param wallet wallet's bridge url and universal link for an external wallet or jsBridge key for the injected wallet.
32
34
  * @param request (optional) additional request to pass to the wallet while connect (currently only ton_proof is available).
33
35
  * @returns universal link if external wallet was passed or void for the injected wallet.
34
36
  */
35
- connect<T extends WalletConnectionSource | 'injected'>(wallet: T, request?: ConnectAdditionalRequest): T extends 'injected' ? void : string;
37
+ connect<T extends WalletConnectionSource>(wallet: T, request?: ConnectAdditionalRequest): T extends WalletConnectionSourceJS ? void : string;
36
38
  /**
37
39
  * Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
38
40
  */
39
- autoConnect(): void;
41
+ restoreConnection(): Promise<void>;
40
42
  /**
41
43
  * Disconnect form thw connected wallet and drop current session.
42
44
  */
@@ -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,12 @@ var TonConnect = /** @class */ (function () {
65
64
  enumerable: false,
66
65
  configurable: true
67
66
  });
67
+ /**
68
+ * Returns available wallets list.
69
+ */
70
+ TonConnect.prototype.getWallets = function () {
71
+ return this.walletsList.getWallets();
72
+ };
68
73
  /**
69
74
  * Allows to subscribe to connection status changes and handle connection errors.
70
75
  * @param callback will be called after connections status changes with actual wallet or null.
@@ -96,8 +101,49 @@ var TonConnect = /** @class */ (function () {
96
101
  /**
97
102
  * Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
98
103
  */
99
- TonConnect.prototype.autoConnect = function () {
100
- this._autoConnect();
104
+ TonConnect.prototype.restoreConnection = function () {
105
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
106
+ var _a, bridgeConnectionType, embeddedWallet, _b, _c, _d, _e;
107
+ return tslib_1.__generator(this, function (_f) {
108
+ switch (_f.label) {
109
+ case 0: return [4 /*yield*/, Promise.all([
110
+ this.bridgeConnectionStorage.storedConnectionType(),
111
+ this.walletsList.getEmbeddedWallet()
112
+ ])];
113
+ case 1:
114
+ _a = _f.sent(), bridgeConnectionType = _a[0], embeddedWallet = _a[1];
115
+ _b = bridgeConnectionType;
116
+ switch (_b) {
117
+ case 'http': return [3 /*break*/, 2];
118
+ case 'injected': return [3 /*break*/, 4];
119
+ }
120
+ return [3 /*break*/, 6];
121
+ case 2:
122
+ _c = this;
123
+ return [4 /*yield*/, bridge_provider_1.BridgeProvider.fromStorage(this.dappSettings.storage)];
124
+ case 3:
125
+ _c.provider = _f.sent();
126
+ return [3 /*break*/, 9];
127
+ case 4:
128
+ _d = this;
129
+ return [4 /*yield*/, injected_provider_1.InjectedProvider.fromStorage(this.dappSettings.storage)];
130
+ case 5:
131
+ _d.provider = _f.sent();
132
+ return [3 /*break*/, 9];
133
+ case 6:
134
+ if (!embeddedWallet) return [3 /*break*/, 8];
135
+ _e = this;
136
+ return [4 /*yield*/, this.createProvider(embeddedWallet)];
137
+ case 7:
138
+ _e.provider = _f.sent();
139
+ return [3 /*break*/, 9];
140
+ case 8: return [2 /*return*/];
141
+ case 9:
142
+ this.provider.listen(this.walletEventsListener.bind(this));
143
+ return [2 /*return*/, this.provider.restoreConnection()];
144
+ }
145
+ });
146
+ });
101
147
  };
102
148
  /**
103
149
  * Asks connected wallet to sign and send the transaction.
@@ -143,36 +189,10 @@ var TonConnect = /** @class */ (function () {
143
189
  });
144
190
  });
145
191
  };
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
192
  TonConnect.prototype.createProvider = function (wallet) {
173
193
  var provider;
174
- if (wallet === 'injected') {
175
- provider = new injected_provider_1.InjectedProvider();
194
+ if ((0, wallet_connection_source_1.isWalletConnectionSourceJS)(wallet)) {
195
+ provider = new injected_provider_1.InjectedProvider(wallet.jsBridgeKey);
176
196
  }
177
197
  else {
178
198
  provider = new bridge_provider_1.BridgeProvider(this.dappSettings.storage, wallet);
@@ -208,7 +228,7 @@ var TonConnect = /** @class */ (function () {
208
228
  };
209
229
  if (tonProofItem) {
210
230
  wallet.connectItems = {
211
- tonProof: tonProofItem.signature
231
+ tonProof: tonProofItem.proof
212
232
  };
213
233
  }
214
234
  this.wallet = wallet;
@@ -0,0 +1,10 @@
1
+ import { WalletInfoInjected, WalletInfo } from "./models/wallet/wallet-info";
2
+ export declare class WalletsListManager {
3
+ private walletsListCache;
4
+ private readonly walletsListSource;
5
+ getWallets(): Promise<WalletInfo[]>;
6
+ getEmbeddedWallet(): Promise<WalletInfoInjected | null>;
7
+ private fetchWalletsList;
8
+ private walletConfigDTOListToWalletConfigList;
9
+ private isCorrectWalletConfigDTO;
10
+ }
@@ -0,0 +1,109 @@
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 wallet_info_1 = require("./models/wallet/wallet-info");
7
+ var injected_provider_1 = require("./provider/injected/injected-provider");
8
+ var WalletsListManager = /** @class */ (function () {
9
+ function WalletsListManager() {
10
+ this.walletsListCache = null;
11
+ this.walletsListSource = 'https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json';
12
+ }
13
+ WalletsListManager.prototype.getWallets = function () {
14
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
15
+ var _this = this;
16
+ return tslib_1.__generator(this, function (_a) {
17
+ if (!this.walletsListCache) {
18
+ this.walletsListCache = this.fetchWalletsList();
19
+ this.walletsListCache.catch(function () { return (_this.walletsListCache = null); });
20
+ }
21
+ return [2 /*return*/, this.walletsListCache];
22
+ });
23
+ });
24
+ };
25
+ WalletsListManager.prototype.getEmbeddedWallet = function () {
26
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
27
+ var walletsList, injectedWallets;
28
+ return tslib_1.__generator(this, function (_a) {
29
+ switch (_a.label) {
30
+ case 0: return [4 /*yield*/, this.getWallets()];
31
+ case 1:
32
+ walletsList = _a.sent();
33
+ injectedWallets = walletsList.filter(wallet_info_1.isWalletInfoInjected);
34
+ if (injectedWallets.length !== 1) {
35
+ return [2 /*return*/, null];
36
+ }
37
+ return [2 /*return*/, injectedWallets[0]];
38
+ }
39
+ });
40
+ });
41
+ };
42
+ WalletsListManager.prototype.fetchWalletsList = function () {
43
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
44
+ var walletsResponse, walletsList, e_1;
45
+ var _this = this;
46
+ return tslib_1.__generator(this, function (_a) {
47
+ switch (_a.label) {
48
+ case 0:
49
+ _a.trys.push([0, 3, , 4]);
50
+ return [4 /*yield*/, fetch(this.walletsListSource)];
51
+ case 1:
52
+ walletsResponse = _a.sent();
53
+ return [4 /*yield*/, walletsResponse.json()];
54
+ case 2:
55
+ walletsList = _a.sent();
56
+ if (!Array.isArray(walletsList) ||
57
+ !walletsList.every(function (wallet) { return _this.isCorrectWalletConfigDTO(wallet); })) {
58
+ throw new fetch_wallets_error_1.FetchWalletsError('Wrong wallets list format');
59
+ }
60
+ return [2 /*return*/, this.walletConfigDTOListToWalletConfigList(walletsList)];
61
+ case 3:
62
+ e_1 = _a.sent();
63
+ throw new fetch_wallets_error_1.FetchWalletsError(e_1);
64
+ case 4: return [2 /*return*/];
65
+ }
66
+ });
67
+ });
68
+ };
69
+ WalletsListManager.prototype.walletConfigDTOListToWalletConfigList = function (walletConfigDTO) {
70
+ return walletConfigDTO.map(function (walletConfigDTO) {
71
+ var walletConfig = {
72
+ name: walletConfigDTO.name,
73
+ imageUrl: walletConfigDTO.image,
74
+ aboutUrl: walletConfigDTO.about_url,
75
+ tondns: walletConfigDTO.tondns
76
+ };
77
+ if ('bridge_url' in walletConfigDTO) {
78
+ walletConfig.bridgeUrl = walletConfigDTO.bridge_url;
79
+ walletConfig.universalLink = walletConfigDTO.universal_url;
80
+ }
81
+ if ('js_bridge_key' in walletConfigDTO) {
82
+ var jsBridgeKey = walletConfigDTO.js_bridge_key;
83
+ walletConfig.jsBridgeKey = jsBridgeKey;
84
+ walletConfig.injected =
85
+ injected_provider_1.InjectedProvider.isWalletInjected(jsBridgeKey);
86
+ walletConfig.embedded =
87
+ injected_provider_1.InjectedProvider.isInsideWalletBrowser(jsBridgeKey);
88
+ }
89
+ return walletConfig;
90
+ });
91
+ };
92
+ WalletsListManager.prototype.isCorrectWalletConfigDTO = function (value) {
93
+ if (!value || !(typeof value === 'object')) {
94
+ return false;
95
+ }
96
+ var containsName = 'name' in value;
97
+ var containsImage = 'image' in value;
98
+ var containsAbout = 'about_url' in value;
99
+ if (!containsName || !containsImage || !containsAbout) {
100
+ return false;
101
+ }
102
+ var containsUniversalUrl = 'universal_url' in value;
103
+ var containsHttpBridge = 'bridge_url' in value;
104
+ var containsJsBridge = 'js_bridge_key' in value;
105
+ return (containsHttpBridge && containsUniversalUrl) || containsJsBridge;
106
+ };
107
+ return WalletsListManager;
108
+ }());
109
+ 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.15",
4
4
  "scripts": {
5
5
  "build": "npx rimraf lib && ttsc",
6
6
  "build:production": "npx rimraf lib && ttsc --sourceMap false"
@@ -26,7 +26,7 @@
26
26
  "dependencies": {
27
27
  "deepmerge": "^4.2.2",
28
28
  "tweetnacl": "^1.0.3",
29
- "@tonconnect/protocol": "^0.0.6"
29
+ "@tonconnect/protocol": "^0.0.8"
30
30
  },
31
31
  "files": [
32
32
  "lib"