@tonconnect/sdk 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,8 @@ const bridge_connection_storage_1 = require("../../storage/bridge-connection-sto
15
15
  const web_api_1 = require("../../utils/web-api");
16
16
  const protocol_1 = require("../../resources/protocol");
17
17
  class InjectedProvider {
18
- constructor(injectedWalletKey) {
18
+ constructor(storage, injectedWalletKey) {
19
+ this.injectedWalletKey = injectedWalletKey;
19
20
  this.type = 'injected';
20
21
  this.unsubscribeCallback = null;
21
22
  this.listenSubscriptions = false;
@@ -24,13 +25,14 @@ class InjectedProvider {
24
25
  if (!InjectedProvider.isWindowContainsWallet(window, injectedWalletKey)) {
25
26
  throw new wallet_not_injected_error_1.WalletNotInjectedError();
26
27
  }
28
+ this.connectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
27
29
  this.injectedWallet = window[injectedWalletKey].tonconnect;
28
30
  }
29
31
  static fromStorage(storage) {
30
32
  return __awaiter(this, void 0, void 0, function* () {
31
33
  const bridgeConnectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(storage);
32
34
  const connection = yield bridgeConnectionStorage.getInjectedConnection();
33
- return new InjectedProvider(connection.jsBridgeKey);
35
+ return new InjectedProvider(storage, connection.jsBridgeKey);
34
36
  });
35
37
  }
36
38
  static isWalletInjected(injectedWalletKey) {
@@ -48,26 +50,8 @@ class InjectedProvider {
48
50
  typeof window[injectedWalletKey] === 'object' &&
49
51
  'tonconnect' in window[injectedWalletKey]);
50
52
  }
51
- connect(message, auto = false) {
52
- this.injectedWallet
53
- .connect(protocol_1.PROTOCOL_VERSION, message, auto)
54
- .then(connectEvent => {
55
- if (connectEvent.event === 'connect') {
56
- this.makeSubscriptions();
57
- this.listenSubscriptions = true;
58
- }
59
- this.listeners.forEach(listener => listener(connectEvent));
60
- })
61
- .catch(e => {
62
- const connectEventError = {
63
- event: 'connect_error',
64
- payload: {
65
- code: 0,
66
- message: e === null || e === void 0 ? void 0 : e.toString()
67
- }
68
- };
69
- this.listeners.forEach(listener => listener(connectEventError));
70
- });
53
+ connect(message) {
54
+ this._connect(protocol_1.PROTOCOL_VERSION, message);
71
55
  }
72
56
  restoreConnection() {
73
57
  return __awaiter(this, void 0, void 0, function* () {
@@ -75,11 +59,14 @@ class InjectedProvider {
75
59
  const connectEvent = yield this.injectedWallet.restoreConnection();
76
60
  if (connectEvent.event === 'connect') {
77
61
  this.makeSubscriptions();
78
- this.listenSubscriptions = true;
79
62
  this.listeners.forEach(listener => listener(connectEvent));
80
63
  }
64
+ else {
65
+ yield this.connectionStorage.removeConnection();
66
+ }
81
67
  }
82
68
  catch (e) {
69
+ yield this.connectionStorage.removeConnection();
83
70
  console.error(e);
84
71
  }
85
72
  });
@@ -93,7 +80,7 @@ class InjectedProvider {
93
80
  disconnect() {
94
81
  this.closeAllListeners();
95
82
  this.injectedWallet.disconnect();
96
- return Promise.resolve();
83
+ return this.connectionStorage.removeConnection();
97
84
  }
98
85
  closeAllListeners() {
99
86
  var _a;
@@ -110,7 +97,31 @@ class InjectedProvider {
110
97
  return this.injectedWallet.send(Object.assign(Object.assign({}, request), { id: '0' }));
111
98
  });
112
99
  }
100
+ _connect(protocolVersion, message) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ try {
103
+ const connectEvent = yield this.injectedWallet.connect(protocolVersion, message);
104
+ if (connectEvent.event === 'connect') {
105
+ yield this.updateSession();
106
+ this.makeSubscriptions();
107
+ }
108
+ this.listeners.forEach(listener => listener(connectEvent));
109
+ }
110
+ catch (e) {
111
+ console.debug(e);
112
+ const connectEventError = {
113
+ event: 'connect_error',
114
+ payload: {
115
+ code: 0,
116
+ message: e === null || e === void 0 ? void 0 : e.toString()
117
+ }
118
+ };
119
+ this.listeners.forEach(listener => listener(connectEventError));
120
+ }
121
+ });
122
+ }
113
123
  makeSubscriptions() {
124
+ this.listenSubscriptions = true;
114
125
  this.unsubscribeCallback = this.injectedWallet.listen(e => {
115
126
  if (this.listenSubscriptions) {
116
127
  this.listeners.forEach(listener => listener(e));
@@ -120,6 +131,12 @@ class InjectedProvider {
120
131
  }
121
132
  });
122
133
  }
134
+ updateSession() {
135
+ return this.connectionStorage.storeConnection({
136
+ type: 'injected',
137
+ jsBridgeKey: this.injectedWalletKey
138
+ });
139
+ }
123
140
  }
124
141
  exports.InjectedProvider = InjectedProvider;
125
142
  InjectedProvider.window = (0, web_api_1.getWindow)();
@@ -48,6 +48,7 @@ class TonConnect {
48
48
  manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || (0, web_api_1.getWebPageManifest)(),
49
49
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new default_storage_1.DefaultStorage()
50
50
  };
51
+ this.walletsList = new wallets_list_manager_1.WalletsListManager(options === null || options === void 0 ? void 0 : options.walletsListSource);
51
52
  if (!this.dappSettings.manifestUrl) {
52
53
  throw new dapp_metadata_error_1.DappMetadataError('Dapp tonconnect-manifest.json must be specified if window.location.origin is undefined. See more https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest');
53
54
  }
@@ -176,7 +177,7 @@ class TonConnect {
176
177
  createProvider(wallet) {
177
178
  let provider;
178
179
  if ((0, wallet_connection_source_1.isWalletConnectionSourceJS)(wallet)) {
179
- provider = new injected_provider_1.InjectedProvider(wallet.jsBridgeKey);
180
+ provider = new injected_provider_1.InjectedProvider(this.dappSettings.storage, wallet.jsBridgeKey);
180
181
  }
181
182
  else {
182
183
  provider = new bridge_provider_1.BridgeProvider(this.dappSettings.storage, wallet);
@@ -261,3 +262,13 @@ class TonConnect {
261
262
  }
262
263
  exports.TonConnect = TonConnect;
263
264
  TonConnect.walletsList = new wallets_list_manager_1.WalletsListManager();
265
+ /**
266
+ * Check if specified wallet is injected and available to use with the app.
267
+ * @param walletJSKey target wallet's js bridge key.
268
+ */
269
+ TonConnect.isWalletInjected = (walletJSKey) => injected_provider_1.InjectedProvider.isWalletInjected(walletJSKey);
270
+ /**
271
+ * Check if the app is opened inside specified wallet's browser.
272
+ * @param walletJSKey target wallet's js bridge key.
273
+ */
274
+ TonConnect.isInsideWalletBrowser = (walletJSKey) => injected_provider_1.InjectedProvider.isInsideWalletBrowser(walletJSKey);
@@ -14,9 +14,12 @@ const fetch_wallets_error_1 = require("./errors/wallets-manager/fetch-wallets.er
14
14
  const wallet_info_1 = require("./models/wallet/wallet-info");
15
15
  const injected_provider_1 = require("./provider/injected/injected-provider");
16
16
  class WalletsListManager {
17
- constructor() {
17
+ constructor(walletsListSource) {
18
18
  this.walletsListCache = null;
19
19
  this.walletsListSource = 'https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json';
20
+ if (walletsListSource) {
21
+ this.walletsListSource = walletsListSource;
22
+ }
20
23
  }
21
24
  getWallets() {
22
25
  return __awaiter(this, void 0, void 0, function* () {
@@ -30,11 +33,11 @@ class WalletsListManager {
30
33
  getEmbeddedWallet() {
31
34
  return __awaiter(this, void 0, void 0, function* () {
32
35
  const walletsList = yield this.getWallets();
33
- const injectedWallets = walletsList.filter(wallet_info_1.isWalletInfoInjected);
34
- if (injectedWallets.length !== 1) {
36
+ const embeddedWallets = walletsList.filter(item => (0, wallet_info_1.isWalletInfoInjected)(item) && item.embedded);
37
+ if (embeddedWallets.length !== 1) {
35
38
  return null;
36
39
  }
37
- return injectedWallets[0].embedded ? injectedWallets[0] : null;
40
+ return embeddedWallets[0];
38
41
  });
39
42
  }
40
43
  fetchWalletsList() {
@@ -12,7 +12,8 @@ import { BridgeConnectionStorage } from "../../storage/bridge-connection-storage
12
12
  import { getWindow } from "../../utils/web-api";
13
13
  import { PROTOCOL_VERSION } from "../../resources/protocol";
14
14
  export class InjectedProvider {
15
- constructor(injectedWalletKey) {
15
+ constructor(storage, injectedWalletKey) {
16
+ this.injectedWalletKey = injectedWalletKey;
16
17
  this.type = 'injected';
17
18
  this.unsubscribeCallback = null;
18
19
  this.listenSubscriptions = false;
@@ -21,13 +22,14 @@ export class InjectedProvider {
21
22
  if (!InjectedProvider.isWindowContainsWallet(window, injectedWalletKey)) {
22
23
  throw new WalletNotInjectedError();
23
24
  }
25
+ this.connectionStorage = new BridgeConnectionStorage(storage);
24
26
  this.injectedWallet = window[injectedWalletKey].tonconnect;
25
27
  }
26
28
  static fromStorage(storage) {
27
29
  return __awaiter(this, void 0, void 0, function* () {
28
30
  const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
29
31
  const connection = yield bridgeConnectionStorage.getInjectedConnection();
30
- return new InjectedProvider(connection.jsBridgeKey);
32
+ return new InjectedProvider(storage, connection.jsBridgeKey);
31
33
  });
32
34
  }
33
35
  static isWalletInjected(injectedWalletKey) {
@@ -45,26 +47,8 @@ export class InjectedProvider {
45
47
  typeof window[injectedWalletKey] === 'object' &&
46
48
  'tonconnect' in window[injectedWalletKey]);
47
49
  }
48
- connect(message, auto = false) {
49
- this.injectedWallet
50
- .connect(PROTOCOL_VERSION, message, auto)
51
- .then(connectEvent => {
52
- if (connectEvent.event === 'connect') {
53
- this.makeSubscriptions();
54
- this.listenSubscriptions = true;
55
- }
56
- this.listeners.forEach(listener => listener(connectEvent));
57
- })
58
- .catch(e => {
59
- const connectEventError = {
60
- event: 'connect_error',
61
- payload: {
62
- code: 0,
63
- message: e === null || e === void 0 ? void 0 : e.toString()
64
- }
65
- };
66
- this.listeners.forEach(listener => listener(connectEventError));
67
- });
50
+ connect(message) {
51
+ this._connect(PROTOCOL_VERSION, message);
68
52
  }
69
53
  restoreConnection() {
70
54
  return __awaiter(this, void 0, void 0, function* () {
@@ -72,11 +56,14 @@ export class InjectedProvider {
72
56
  const connectEvent = yield this.injectedWallet.restoreConnection();
73
57
  if (connectEvent.event === 'connect') {
74
58
  this.makeSubscriptions();
75
- this.listenSubscriptions = true;
76
59
  this.listeners.forEach(listener => listener(connectEvent));
77
60
  }
61
+ else {
62
+ yield this.connectionStorage.removeConnection();
63
+ }
78
64
  }
79
65
  catch (e) {
66
+ yield this.connectionStorage.removeConnection();
80
67
  console.error(e);
81
68
  }
82
69
  });
@@ -90,7 +77,7 @@ export class InjectedProvider {
90
77
  disconnect() {
91
78
  this.closeAllListeners();
92
79
  this.injectedWallet.disconnect();
93
- return Promise.resolve();
80
+ return this.connectionStorage.removeConnection();
94
81
  }
95
82
  closeAllListeners() {
96
83
  var _a;
@@ -107,7 +94,31 @@ export class InjectedProvider {
107
94
  return this.injectedWallet.send(Object.assign(Object.assign({}, request), { id: '0' }));
108
95
  });
109
96
  }
97
+ _connect(protocolVersion, message) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ try {
100
+ const connectEvent = yield this.injectedWallet.connect(protocolVersion, message);
101
+ if (connectEvent.event === 'connect') {
102
+ yield this.updateSession();
103
+ this.makeSubscriptions();
104
+ }
105
+ this.listeners.forEach(listener => listener(connectEvent));
106
+ }
107
+ catch (e) {
108
+ console.debug(e);
109
+ const connectEventError = {
110
+ event: 'connect_error',
111
+ payload: {
112
+ code: 0,
113
+ message: e === null || e === void 0 ? void 0 : e.toString()
114
+ }
115
+ };
116
+ this.listeners.forEach(listener => listener(connectEventError));
117
+ }
118
+ });
119
+ }
110
120
  makeSubscriptions() {
121
+ this.listenSubscriptions = true;
111
122
  this.unsubscribeCallback = this.injectedWallet.listen(e => {
112
123
  if (this.listenSubscriptions) {
113
124
  this.listeners.forEach(listener => listener(e));
@@ -117,5 +128,11 @@ export class InjectedProvider {
117
128
  }
118
129
  });
119
130
  }
131
+ updateSession() {
132
+ return this.connectionStorage.storeConnection({
133
+ type: 'injected',
134
+ jsBridgeKey: this.injectedWalletKey
135
+ });
136
+ }
120
137
  }
121
138
  InjectedProvider.window = getWindow();
@@ -45,6 +45,7 @@ export class TonConnect {
45
45
  manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
46
46
  storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
47
47
  };
48
+ this.walletsList = new WalletsListManager(options === null || options === void 0 ? void 0 : options.walletsListSource);
48
49
  if (!this.dappSettings.manifestUrl) {
49
50
  throw new DappMetadataError('Dapp tonconnect-manifest.json must be specified if window.location.origin is undefined. See more https://github.com/ton-connect/docs/blob/main/requests-responses.md#app-manifest');
50
51
  }
@@ -173,7 +174,7 @@ export class TonConnect {
173
174
  createProvider(wallet) {
174
175
  let provider;
175
176
  if (isWalletConnectionSourceJS(wallet)) {
176
- provider = new InjectedProvider(wallet.jsBridgeKey);
177
+ provider = new InjectedProvider(this.dappSettings.storage, wallet.jsBridgeKey);
177
178
  }
178
179
  else {
179
180
  provider = new BridgeProvider(this.dappSettings.storage, wallet);
@@ -257,3 +258,13 @@ export class TonConnect {
257
258
  }
258
259
  }
259
260
  TonConnect.walletsList = new WalletsListManager();
261
+ /**
262
+ * Check if specified wallet is injected and available to use with the app.
263
+ * @param walletJSKey target wallet's js bridge key.
264
+ */
265
+ TonConnect.isWalletInjected = (walletJSKey) => InjectedProvider.isWalletInjected(walletJSKey);
266
+ /**
267
+ * Check if the app is opened inside specified wallet's browser.
268
+ * @param walletJSKey target wallet's js bridge key.
269
+ */
270
+ TonConnect.isInsideWalletBrowser = (walletJSKey) => InjectedProvider.isInsideWalletBrowser(walletJSKey);
@@ -11,9 +11,12 @@ import { FetchWalletsError } from "./errors/wallets-manager/fetch-wallets.error"
11
11
  import { isWalletInfoInjected } from "./models/wallet/wallet-info";
12
12
  import { InjectedProvider } from "./provider/injected/injected-provider";
13
13
  export class WalletsListManager {
14
- constructor() {
14
+ constructor(walletsListSource) {
15
15
  this.walletsListCache = null;
16
16
  this.walletsListSource = 'https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json';
17
+ if (walletsListSource) {
18
+ this.walletsListSource = walletsListSource;
19
+ }
17
20
  }
18
21
  getWallets() {
19
22
  return __awaiter(this, void 0, void 0, function* () {
@@ -27,11 +30,11 @@ export class WalletsListManager {
27
30
  getEmbeddedWallet() {
28
31
  return __awaiter(this, void 0, void 0, function* () {
29
32
  const walletsList = yield this.getWallets();
30
- const injectedWallets = walletsList.filter(isWalletInfoInjected);
31
- if (injectedWallets.length !== 1) {
33
+ const embeddedWallets = walletsList.filter(item => isWalletInfoInjected(item) && item.embedded);
34
+ if (embeddedWallets.length !== 1) {
32
35
  return null;
33
36
  }
34
- return injectedWallets[0].embedded ? injectedWallets[0] : null;
37
+ return embeddedWallets[0];
35
38
  });
36
39
  }
37
40
  fetchWalletsList() {
@@ -12,4 +12,8 @@ export interface TonConnectOptions {
12
12
  * Storage to save protocol data. For browser default is `localStorage`. If you use SDK with nodeJS, you have to specify this field.
13
13
  */
14
14
  storage?: IStorage;
15
+ /**
16
+ * @deprecated Don't use it
17
+ */
18
+ walletsListSource?: string;
15
19
  }
@@ -1,4 +1,4 @@
1
1
  export type { Account } from './account';
2
2
  export type { Wallet } from './wallet';
3
3
  export type { WalletConnectionSource, WalletConnectionSourceHTTP, WalletConnectionSourceJS } from './wallet-connection-source';
4
- export { WalletInfo, WalletInfoInjected, WalletInfoRemote, isWalletInfoInjected } from './wallet-info';
4
+ export { WalletInfo, WalletInfoBase, WalletInfoInjected, WalletInfoRemote, isWalletInfoInjected } from './wallet-info';
@@ -3,6 +3,7 @@ import { InternalProvider } from "../provider";
3
3
  import { IStorage } from "../../storage/models/storage.interface";
4
4
  import { WithoutId } from "../../utils/types";
5
5
  export declare class InjectedProvider<T extends string = string> implements InternalProvider {
6
+ private readonly injectedWalletKey;
6
7
  private static window;
7
8
  static fromStorage(storage: IStorage): Promise<InjectedProvider>;
8
9
  static isWalletInjected(injectedWalletKey: string): boolean;
@@ -11,15 +12,18 @@ export declare class InjectedProvider<T extends string = string> implements Inte
11
12
  readonly type = "injected";
12
13
  private unsubscribeCallback;
13
14
  private injectedWallet;
15
+ private readonly connectionStorage;
14
16
  private listenSubscriptions;
15
17
  private listeners;
16
- constructor(injectedWalletKey: T);
17
- connect(message: ConnectRequest, auto?: boolean): void;
18
+ constructor(storage: IStorage, injectedWalletKey: T);
19
+ connect(message: ConnectRequest): void;
18
20
  restoreConnection(): Promise<void>;
19
21
  closeConnection(): void;
20
22
  disconnect(): Promise<void>;
21
23
  private closeAllListeners;
22
24
  listen(eventsCallback: (e: WalletEvent) => void): () => void;
23
25
  sendRequest<T extends RpcMethod>(request: WithoutId<AppRequest<T>>): Promise<WithoutId<WalletResponse<T>>>;
26
+ private _connect;
24
27
  private makeSubscriptions;
28
+ private updateSession;
25
29
  }
@@ -3,7 +3,7 @@ export interface InjectedWalletApi {
3
3
  deviceInfo: DeviceInfo;
4
4
  protocolVersion: number;
5
5
  isWalletBrowser: boolean;
6
- connect(protocolVersion: number, message: ConnectRequest, auto: boolean): Promise<ConnectEvent>;
6
+ connect(protocolVersion: number, message: ConnectRequest): Promise<ConnectEvent>;
7
7
  restoreConnection(): Promise<ConnectEvent>;
8
8
  send<T extends RpcMethod>(message: AppRequest<T>): Promise<WalletResponse<T>>;
9
9
  listen(callback: (event: WalletEvent) => void): () => void;
@@ -7,6 +7,16 @@ import { WalletConnectionSourceJS } from "./models/wallet/wallet-connection-sour
7
7
  import { ITonConnect } from "./ton-connect.interface";
8
8
  export declare class TonConnect implements ITonConnect {
9
9
  private static readonly walletsList;
10
+ /**
11
+ * Check if specified wallet is injected and available to use with the app.
12
+ * @param walletJSKey target wallet's js bridge key.
13
+ */
14
+ static isWalletInjected: (walletJSKey: string) => boolean;
15
+ /**
16
+ * Check if the app is opened inside specified wallet's browser.
17
+ * @param walletJSKey target wallet's js bridge key.
18
+ */
19
+ static isInsideWalletBrowser: (walletJSKey: string) => boolean;
10
20
  /**
11
21
  * Returns available wallets list.
12
22
  */
@@ -2,6 +2,7 @@ import { WalletInfoInjected, WalletInfo } from "./models/wallet/wallet-info";
2
2
  export declare class WalletsListManager {
3
3
  private walletsListCache;
4
4
  private readonly walletsListSource;
5
+ constructor(walletsListSource?: string);
5
6
  getWallets(): Promise<WalletInfo[]>;
6
7
  getEmbeddedWallet(): Promise<WalletInfoInjected | null>;
7
8
  private fetchWalletsList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonconnect/sdk",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
4
  "scripts": {
5
5
  "build": "npx rimraf lib && ttsc --outDir ./lib/cjs && ttsc --module esnext --outDir ./lib/esm && npx rimraf dist && webpack --mode development",
6
6
  "build:production": "npx rimraf lib && ttsc --project tsconfig.cjs.json --sourceMap false && ttsc --project tsconfig.esm.json --sourceMap false && npx rimraf dist && webpack --mode production"