@tonconnect/sdk 0.0.42 → 0.0.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tonconnect-sdk.min.js +1 -1
- package/dist/tonconnect-sdk.min.js.map +1 -1
- package/lib/{errors → cjs/errors}/dapp/dapp-metadata.error.js +0 -0
- package/lib/{errors → cjs/errors}/index.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/events/connect/index.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/events/connect/manifest-content-error.error.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/events/connect/manifest-not-found.error.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/events/connect/user-rejects.error.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/events/index.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/index.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/responses/bad-request.error.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/responses/index.js +0 -0
- package/lib/{errors → cjs/errors}/protocol/responses/unknown-app.error.js +0 -0
- package/lib/{errors → cjs/errors}/storage/index.js +0 -0
- package/lib/{errors → cjs/errors}/storage/localstorage-not-found.error.js +0 -0
- package/lib/{errors → cjs/errors}/ton-connect.error.js +0 -0
- package/lib/{errors → cjs/errors}/unknown.error.js +0 -0
- package/lib/{errors → cjs/errors}/wallet/index.js +0 -0
- package/lib/{errors → cjs/errors}/wallet/wallet-already-connected.error.js +0 -0
- package/lib/{errors → cjs/errors}/wallet/wallet-not-connected.error.js +0 -0
- package/lib/{errors → cjs/errors}/wallet/wallet-not-injected.error.js +0 -0
- package/lib/{errors → cjs/errors}/wallet/wallet-not-support-feature.error.js +0 -0
- package/lib/{errors → cjs/errors}/wallets-manager/fetch-wallets.error.js +0 -0
- package/lib/{errors → cjs/errors}/wallets-manager/index.js +0 -0
- package/lib/{index.js → cjs/index.js} +0 -0
- package/lib/{models → cjs/models}/dapp/dapp-metadata.js +0 -0
- package/lib/{models → cjs/models}/index.js +0 -0
- package/lib/{models → cjs/models}/methods/connect/connect-additional-request.js +0 -0
- package/lib/{models → cjs/models}/methods/connect/index.js +0 -0
- package/lib/{models → cjs/models}/methods/index.js +0 -0
- package/lib/{models → cjs/models}/methods/send-transaction/index.js +0 -0
- package/lib/{models → cjs/models}/methods/send-transaction/send-transaction-request.js +0 -0
- package/lib/{models → cjs/models}/methods/send-transaction/send-transaction-response.js +0 -0
- package/lib/{models → cjs/models}/ton-connect-options.js +0 -0
- package/lib/{models → cjs/models}/wallet/account.js +0 -0
- package/lib/{models → cjs/models}/wallet/index.js +0 -0
- package/lib/{models → cjs/models}/wallet/wallet-connection-source.js +0 -0
- package/lib/{models → cjs/models}/wallet/wallet-info.js +0 -0
- package/lib/{models → cjs/models}/wallet/wallet.js +0 -0
- package/lib/{parsers → cjs/parsers}/connect-errors-parser.js +0 -0
- package/lib/{parsers → cjs/parsers}/rpc-parser.js +0 -0
- package/lib/{parsers → cjs/parsers}/send-transaction-parser.js +0 -0
- package/lib/{provider → cjs/provider}/bridge/bridge-gateway.js +14 -0
- package/lib/{provider → cjs/provider}/bridge/bridge-provider.js +2 -25
- package/lib/{provider → cjs/provider}/bridge/models/bridge-connection.js +0 -0
- package/lib/{provider → cjs/provider}/bridge/models/bridge-incomming-message.js +0 -0
- package/lib/{provider → cjs/provider}/bridge/models/bridge-session-raw.js +0 -0
- package/lib/{provider → cjs/provider}/bridge/models/bridge-session.js +0 -0
- package/lib/{provider → cjs/provider}/injected/injected-provider.js +2 -25
- package/lib/{provider → cjs/provider}/injected/models/injected-wallet-api.js +0 -0
- package/lib/{provider → cjs/provider}/provider.js +0 -0
- package/lib/cjs/resources/protocol.js +4 -0
- package/lib/{storage → cjs/storage}/bridge-connection-storage.js +0 -0
- package/lib/{storage → cjs/storage}/default-storage.js +0 -0
- package/lib/{storage → cjs/storage}/http-bridge-gateway-storage.js +0 -0
- package/lib/{storage → cjs/storage}/models/storage.interface.js +0 -0
- package/lib/{ton-connect.interface.js → cjs/ton-connect.interface.js} +0 -0
- package/lib/{ton-connect.js → cjs/ton-connect.js} +1 -1
- package/lib/{utils → cjs/utils}/options.js +0 -0
- package/lib/{utils → cjs/utils}/types.js +0 -0
- package/lib/{utils → cjs/utils}/url.js +0 -0
- package/lib/{utils → cjs/utils}/web-api.js +0 -0
- package/lib/{wallets-list-manager.js → cjs/wallets-list-manager.js} +0 -0
- package/lib/esm/errors/dapp/dapp-metadata.error.js +10 -0
- package/lib/{errors/index.d.ts → esm/errors/index.js} +0 -0
- package/lib/{errors/protocol/events/connect/index.d.ts → esm/errors/protocol/events/connect/index.js} +0 -0
- package/lib/esm/errors/protocol/events/connect/manifest-content-error.error.js +11 -0
- package/lib/esm/errors/protocol/events/connect/manifest-not-found.error.js +11 -0
- package/lib/esm/errors/protocol/events/connect/user-rejects.error.js +10 -0
- package/lib/{errors/protocol/events/index.d.ts → esm/errors/protocol/events/index.js} +0 -0
- package/lib/{errors/protocol/index.d.ts → esm/errors/protocol/index.js} +0 -0
- package/lib/esm/errors/protocol/responses/bad-request.error.js +10 -0
- package/lib/{errors/protocol/responses/index.d.ts → esm/errors/protocol/responses/index.js} +0 -0
- package/lib/esm/errors/protocol/responses/unknown-app.error.js +10 -0
- package/lib/{errors/storage/index.d.ts → esm/errors/storage/index.js} +0 -0
- package/lib/esm/errors/storage/localstorage-not-found.error.js +10 -0
- package/lib/esm/errors/ton-connect.error.js +13 -0
- package/lib/esm/errors/unknown.error.js +10 -0
- package/lib/{errors/wallet/index.d.ts → esm/errors/wallet/index.js} +0 -0
- package/lib/esm/errors/wallet/wallet-already-connected.error.js +10 -0
- package/lib/esm/errors/wallet/wallet-not-connected.error.js +10 -0
- package/lib/esm/errors/wallet/wallet-not-injected.error.js +10 -0
- package/lib/esm/errors/wallet/wallet-not-support-feature.error.js +10 -0
- package/lib/esm/errors/wallets-manager/fetch-wallets.error.js +10 -0
- package/lib/{errors/wallets-manager/index.d.ts → esm/errors/wallets-manager/index.js} +0 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/models/dapp/dapp-metadata.js +1 -0
- package/lib/esm/models/index.js +2 -0
- package/lib/esm/models/methods/connect/connect-additional-request.js +1 -0
- package/lib/esm/models/methods/connect/index.js +1 -0
- package/lib/{models/methods/index.d.ts → esm/models/methods/index.js} +0 -0
- package/lib/esm/models/methods/send-transaction/index.js +1 -0
- package/lib/esm/models/methods/send-transaction/send-transaction-request.js +1 -0
- package/lib/esm/models/methods/send-transaction/send-transaction-response.js +1 -0
- package/lib/esm/models/ton-connect-options.js +1 -0
- package/lib/esm/models/wallet/account.js +1 -0
- package/lib/esm/models/wallet/index.js +1 -0
- package/lib/esm/models/wallet/wallet-connection-source.js +3 -0
- package/lib/esm/models/wallet/wallet-info.js +3 -0
- package/lib/esm/models/wallet/wallet.js +1 -0
- package/lib/esm/parsers/connect-errors-parser.js +23 -0
- package/lib/esm/parsers/rpc-parser.js +5 -0
- package/lib/esm/parsers/send-transaction-parser.js +31 -0
- package/lib/esm/provider/bridge/bridge-gateway.js +105 -0
- package/lib/esm/provider/bridge/bridge-provider.js +140 -0
- package/lib/esm/provider/bridge/models/bridge-connection.js +1 -0
- package/lib/esm/provider/bridge/models/bridge-incomming-message.js +1 -0
- package/lib/esm/provider/bridge/models/bridge-session-raw.js +1 -0
- package/lib/esm/provider/bridge/models/bridge-session.js +1 -0
- package/lib/esm/provider/injected/injected-provider.js +121 -0
- package/lib/esm/provider/injected/models/injected-wallet-api.js +1 -0
- package/lib/esm/provider/provider.js +1 -0
- package/lib/esm/resources/protocol.js +1 -0
- package/lib/esm/storage/bridge-connection-storage.js +96 -0
- package/lib/esm/storage/default-storage.js +35 -0
- package/lib/esm/storage/http-bridge-gateway-storage.js +34 -0
- package/lib/esm/storage/models/storage.interface.js +1 -0
- package/lib/esm/ton-connect.interface.js +1 -0
- package/lib/esm/ton-connect.js +252 -0
- package/lib/esm/utils/options.js +8 -0
- package/lib/esm/utils/types.js +1 -0
- package/lib/esm/utils/url.js +9 -0
- package/lib/esm/utils/web-api.js +20 -0
- package/lib/esm/wallets-list-manager.js +91 -0
- package/lib/{errors → types/errors}/dapp/dapp-metadata.error.d.ts +0 -0
- package/lib/types/errors/index.d.ts +6 -0
- package/lib/types/errors/protocol/events/connect/index.d.ts +1 -0
- package/lib/{errors → types/errors}/protocol/events/connect/manifest-content-error.error.d.ts +0 -0
- package/lib/{errors → types/errors}/protocol/events/connect/manifest-not-found.error.d.ts +0 -0
- package/lib/{errors → types/errors}/protocol/events/connect/user-rejects.error.d.ts +0 -0
- package/lib/types/errors/protocol/events/index.d.ts +1 -0
- package/lib/types/errors/protocol/index.d.ts +2 -0
- package/lib/{errors → types/errors}/protocol/responses/bad-request.error.d.ts +0 -0
- package/lib/types/errors/protocol/responses/index.d.ts +2 -0
- package/lib/{errors → types/errors}/protocol/responses/unknown-app.error.d.ts +0 -0
- package/lib/types/errors/storage/index.d.ts +1 -0
- package/lib/{errors → types/errors}/storage/localstorage-not-found.error.d.ts +0 -0
- package/lib/{errors → types/errors}/ton-connect.error.d.ts +0 -0
- package/lib/{errors → types/errors}/unknown.error.d.ts +0 -0
- package/lib/types/errors/wallet/index.d.ts +3 -0
- package/lib/{errors → types/errors}/wallet/wallet-already-connected.error.d.ts +0 -0
- package/lib/{errors → types/errors}/wallet/wallet-not-connected.error.d.ts +0 -0
- package/lib/{errors → types/errors}/wallet/wallet-not-injected.error.d.ts +0 -0
- package/lib/{errors → types/errors}/wallet/wallet-not-support-feature.error.d.ts +0 -0
- package/lib/{errors → types/errors}/wallets-manager/fetch-wallets.error.d.ts +0 -0
- package/lib/types/errors/wallets-manager/index.d.ts +1 -0
- package/lib/{index.d.ts → types/index.d.ts} +0 -0
- package/lib/{models → types/models}/dapp/dapp-metadata.d.ts +0 -0
- package/lib/{models → types/models}/index.d.ts +0 -0
- package/lib/{models → types/models}/methods/connect/connect-additional-request.d.ts +1 -1
- package/lib/{models → types/models}/methods/connect/index.d.ts +0 -0
- package/lib/types/models/methods/index.d.ts +2 -0
- package/lib/{models → types/models}/methods/send-transaction/index.d.ts +0 -0
- package/lib/{models → types/models}/methods/send-transaction/send-transaction-request.d.ts +0 -0
- package/lib/{models → types/models}/methods/send-transaction/send-transaction-response.d.ts +0 -0
- package/lib/{models → types/models}/ton-connect-options.d.ts +0 -0
- package/lib/{models → types/models}/wallet/account.d.ts +0 -0
- package/lib/{models → types/models}/wallet/index.d.ts +0 -0
- package/lib/{models → types/models}/wallet/wallet-connection-source.d.ts +0 -0
- package/lib/{models → types/models}/wallet/wallet-info.d.ts +0 -0
- package/lib/{models → types/models}/wallet/wallet.d.ts +0 -0
- package/lib/{parsers → types/parsers}/connect-errors-parser.d.ts +0 -0
- package/lib/{parsers → types/parsers}/rpc-parser.d.ts +0 -0
- package/lib/{parsers → types/parsers}/send-transaction-parser.d.ts +0 -0
- package/lib/{provider → types/provider}/bridge/bridge-gateway.d.ts +1 -0
- package/lib/{provider → types/provider}/bridge/bridge-provider.d.ts +0 -0
- package/lib/{provider → types/provider}/bridge/models/bridge-connection.d.ts +0 -0
- package/lib/{provider → types/provider}/bridge/models/bridge-incomming-message.d.ts +0 -0
- package/lib/{provider → types/provider}/bridge/models/bridge-session-raw.d.ts +0 -0
- package/lib/{provider → types/provider}/bridge/models/bridge-session.d.ts +0 -0
- package/lib/{provider → types/provider}/injected/injected-provider.d.ts +0 -0
- package/lib/{provider → types/provider}/injected/models/injected-wallet-api.d.ts +0 -0
- package/lib/{provider → types/provider}/provider.d.ts +0 -0
- package/lib/types/resources/protocol.d.ts +1 -0
- package/lib/{storage → types/storage}/bridge-connection-storage.d.ts +0 -0
- package/lib/{storage → types/storage}/default-storage.d.ts +0 -0
- package/lib/{storage → types/storage}/http-bridge-gateway-storage.d.ts +0 -0
- package/lib/{storage → types/storage}/models/storage.interface.d.ts +0 -0
- package/lib/{ton-connect.d.ts → types/ton-connect.d.ts} +0 -0
- package/lib/{ton-connect.interface.d.ts → types/ton-connect.interface.d.ts} +0 -0
- package/lib/{utils → types/utils}/options.d.ts +0 -0
- package/lib/{utils → types/utils}/types.d.ts +0 -0
- package/lib/{utils → types/utils}/url.d.ts +0 -0
- package/lib/{utils → types/utils}/web-api.d.ts +0 -0
- package/lib/{wallets-list-manager.d.ts → types/wallets-list-manager.d.ts} +0 -0
- package/package.json +15 -6
- package/lib/resources/protocol.json +0 -3
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Base64, isNode } from '@tonconnect/protocol';
|
|
11
|
+
import { TonConnectError } from "../../errors";
|
|
12
|
+
import { HttpBridgeGatewayStorage } from "../../storage/http-bridge-gateway-storage";
|
|
13
|
+
import { addPathToUrl } from "../../utils/url";
|
|
14
|
+
if (isNode()) {
|
|
15
|
+
try {
|
|
16
|
+
eval("global.EventSource = require('eventsource')");
|
|
17
|
+
eval("global.fetch = require('node-fetch')");
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
console.error(err);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class BridgeGateway {
|
|
24
|
+
constructor(storage, bridgeUrl, sessionId, listener, errorsListener) {
|
|
25
|
+
this.bridgeUrl = bridgeUrl;
|
|
26
|
+
this.sessionId = sessionId;
|
|
27
|
+
this.listener = listener;
|
|
28
|
+
this.errorsListener = errorsListener;
|
|
29
|
+
this.ssePath = 'events';
|
|
30
|
+
this.postPath = 'message';
|
|
31
|
+
this.heartbeatMessage = 'heartbeat';
|
|
32
|
+
this.defaultTtl = 300;
|
|
33
|
+
this.isClosed = false;
|
|
34
|
+
this.bridgeGatewayStorage = new HttpBridgeGatewayStorage(storage);
|
|
35
|
+
}
|
|
36
|
+
registerSession() {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const url = new URL(addPathToUrl(this.bridgeUrl, this.ssePath));
|
|
39
|
+
url.searchParams.append('client_id', this.sessionId);
|
|
40
|
+
const lastEventId = yield this.bridgeGatewayStorage.getLastEventId();
|
|
41
|
+
if (lastEventId) {
|
|
42
|
+
url.searchParams.append('last_event_id', lastEventId);
|
|
43
|
+
}
|
|
44
|
+
this.eventSource = new EventSource(url.toString());
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
this.eventSource.onerror = reject;
|
|
47
|
+
this.eventSource.onopen = () => {
|
|
48
|
+
this.eventSource.onerror = this.errorsHandler.bind(this);
|
|
49
|
+
this.eventSource.onmessage = this.messagesHandler.bind(this);
|
|
50
|
+
resolve();
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
send(message, receiver, ttl) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
const url = new URL(addPathToUrl(this.bridgeUrl, this.postPath));
|
|
58
|
+
url.searchParams.append('client_id', this.sessionId);
|
|
59
|
+
url.searchParams.append('to', receiver);
|
|
60
|
+
url.searchParams.append('ttl', (ttl || this.defaultTtl).toString());
|
|
61
|
+
yield fetch(url, {
|
|
62
|
+
method: 'post',
|
|
63
|
+
body: Base64.encode(message)
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
close() {
|
|
68
|
+
var _a;
|
|
69
|
+
this.isClosed = true;
|
|
70
|
+
(_a = this.eventSource) === null || _a === void 0 ? void 0 : _a.close();
|
|
71
|
+
}
|
|
72
|
+
errorsHandler(e) {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
if (!this.isClosed) {
|
|
75
|
+
if (((_a = this.eventSource) === null || _a === void 0 ? void 0 : _a.readyState) === EventSource.CLOSED) {
|
|
76
|
+
this.eventSource.close();
|
|
77
|
+
this.registerSession();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (((_b = this.eventSource) === null || _b === void 0 ? void 0 : _b.readyState) === EventSource.CONNECTING) {
|
|
81
|
+
console.debug('[TON_CONNET_SDK_ERROR]: Bridge error', JSON.stringify(e));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
this.errorsListener(e);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
messagesHandler(e) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (e.data === this.heartbeatMessage) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
yield this.bridgeGatewayStorage.storeLastEventId(e.lastEventId);
|
|
93
|
+
if (!this.isClosed) {
|
|
94
|
+
let bridgeIncomingMessage;
|
|
95
|
+
try {
|
|
96
|
+
bridgeIncomingMessage = JSON.parse(e.data);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw new TonConnectError(`Bridge message parse failed, message ${e.data}`);
|
|
100
|
+
}
|
|
101
|
+
this.listener(bridgeIncomingMessage);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Base64, SessionCrypto, hexToByteArray } from '@tonconnect/protocol';
|
|
11
|
+
import { TonConnectError } from "../../errors/ton-connect.error";
|
|
12
|
+
import { BridgeGateway } from "./bridge-gateway";
|
|
13
|
+
import { BridgeConnectionStorage } from "../../storage/bridge-connection-storage";
|
|
14
|
+
import { PROTOCOL_VERSION } from "../../resources/protocol";
|
|
15
|
+
export class BridgeProvider {
|
|
16
|
+
constructor(storage, walletConnectionSource) {
|
|
17
|
+
this.storage = storage;
|
|
18
|
+
this.walletConnectionSource = walletConnectionSource;
|
|
19
|
+
this.type = 'http';
|
|
20
|
+
this.pendingRequests = new Map();
|
|
21
|
+
this.nextRequestId = 0;
|
|
22
|
+
this.session = null;
|
|
23
|
+
this.bridge = null;
|
|
24
|
+
this.listeners = [];
|
|
25
|
+
this.connectionStorage = new BridgeConnectionStorage(storage);
|
|
26
|
+
}
|
|
27
|
+
static fromStorage(storage) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
|
|
30
|
+
const connection = yield bridgeConnectionStorage.getHttpConnection();
|
|
31
|
+
return new BridgeProvider(storage, connection.session.walletConnectionSource);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
connect(message) {
|
|
35
|
+
var _a;
|
|
36
|
+
(_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
|
|
37
|
+
const sessionCrypto = new SessionCrypto();
|
|
38
|
+
this.session = {
|
|
39
|
+
sessionCrypto,
|
|
40
|
+
walletConnectionSource: this.walletConnectionSource
|
|
41
|
+
};
|
|
42
|
+
this.bridge = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
|
|
43
|
+
this.bridge.registerSession();
|
|
44
|
+
return this.generateUniversalLink(message);
|
|
45
|
+
}
|
|
46
|
+
restoreConnection() {
|
|
47
|
+
var _a;
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
(_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
|
|
50
|
+
const storedConnection = yield this.connectionStorage.getHttpConnection();
|
|
51
|
+
if (!storedConnection) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
this.session = storedConnection.session;
|
|
55
|
+
this.bridge = new BridgeGateway(this.storage, this.walletConnectionSource.bridgeUrl, storedConnection.session.sessionCrypto.sessionId, this.gatewayListener.bind(this), this.gatewayErrorsListener.bind(this));
|
|
56
|
+
yield this.bridge.registerSession();
|
|
57
|
+
this.listeners.forEach(listener => listener(storedConnection.connectEvent));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
sendRequest(request) {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
const id = this.nextRequestId;
|
|
63
|
+
this.nextRequestId++;
|
|
64
|
+
if (!this.bridge || !this.session || !('walletPublicKey' in this.session)) {
|
|
65
|
+
throw new TonConnectError('Trying to send bridge request without session');
|
|
66
|
+
}
|
|
67
|
+
const encodedRequest = this.session.sessionCrypto.encrypt(JSON.stringify(Object.assign(Object.assign({}, request), { id })), hexToByteArray(this.session.walletPublicKey));
|
|
68
|
+
this.bridge.send(encodedRequest, this.session.walletPublicKey).catch(reject);
|
|
69
|
+
this.pendingRequests.set(id.toString(), resolve);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
closeConnection() {
|
|
73
|
+
var _a;
|
|
74
|
+
(_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
|
|
75
|
+
this.listeners = [];
|
|
76
|
+
}
|
|
77
|
+
disconnect() {
|
|
78
|
+
var _a;
|
|
79
|
+
(_a = this.bridge) === null || _a === void 0 ? void 0 : _a.close();
|
|
80
|
+
this.listeners = [];
|
|
81
|
+
return this.removeBridgeAndSession();
|
|
82
|
+
}
|
|
83
|
+
listen(callback) {
|
|
84
|
+
this.listeners.push(callback);
|
|
85
|
+
return () => (this.listeners = this.listeners.filter(listener => listener !== callback));
|
|
86
|
+
}
|
|
87
|
+
gatewayListener(bridgeIncomingMessage) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const walletMessage = JSON.parse(this.session.sessionCrypto.decrypt(Base64.decode(bridgeIncomingMessage.message).toUint8Array(), hexToByteArray(bridgeIncomingMessage.from)));
|
|
90
|
+
if (!('event' in walletMessage)) {
|
|
91
|
+
const id = walletMessage.id.toString();
|
|
92
|
+
const resolve = this.pendingRequests.get(id);
|
|
93
|
+
if (!resolve) {
|
|
94
|
+
throw new TonConnectError(`Response id ${id} doesn't match any request's id`);
|
|
95
|
+
}
|
|
96
|
+
resolve(walletMessage);
|
|
97
|
+
this.pendingRequests.delete(id);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (walletMessage.event === 'connect') {
|
|
101
|
+
yield this.updateSession(walletMessage, bridgeIncomingMessage.from);
|
|
102
|
+
}
|
|
103
|
+
if (walletMessage.event === 'disconnect') {
|
|
104
|
+
yield this.removeBridgeAndSession();
|
|
105
|
+
}
|
|
106
|
+
this.listeners.forEach(listener => listener(walletMessage));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
gatewayErrorsListener(e) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
throw new TonConnectError(`Bridge error ${JSON.stringify(e)}`);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
updateSession(connectEvent, walletPublicKey) {
|
|
115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
this.session = Object.assign(Object.assign({}, this.session), { walletPublicKey });
|
|
117
|
+
const tonAddrItem = connectEvent.payload.items.find(item => item.name === 'ton_addr');
|
|
118
|
+
const connectEventToSave = Object.assign(Object.assign({}, connectEvent), { payload: Object.assign(Object.assign({}, connectEvent.payload), { items: [tonAddrItem] }) });
|
|
119
|
+
yield this.connectionStorage.storeConnection({
|
|
120
|
+
type: 'http',
|
|
121
|
+
session: this.session,
|
|
122
|
+
connectEvent: connectEventToSave
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
removeBridgeAndSession() {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
this.session = null;
|
|
129
|
+
this.bridge = null;
|
|
130
|
+
yield this.connectionStorage.removeConnection();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
generateUniversalLink(message) {
|
|
134
|
+
const url = new URL(this.walletConnectionSource.universalLink);
|
|
135
|
+
url.searchParams.append('v', PROTOCOL_VERSION.toString());
|
|
136
|
+
url.searchParams.append('id', this.session.sessionCrypto.sessionId);
|
|
137
|
+
url.searchParams.append('r', JSON.stringify(message));
|
|
138
|
+
return url.toString();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { WalletNotInjectedError } from "../../errors/wallet/wallet-not-injected.error";
|
|
11
|
+
import { BridgeConnectionStorage } from "../../storage/bridge-connection-storage";
|
|
12
|
+
import { getWindow } from "../../utils/web-api";
|
|
13
|
+
import { PROTOCOL_VERSION } from "../../resources/protocol";
|
|
14
|
+
export class InjectedProvider {
|
|
15
|
+
constructor(injectedWalletKey) {
|
|
16
|
+
this.type = 'injected';
|
|
17
|
+
this.unsubscribeCallback = null;
|
|
18
|
+
this.listenSubscriptions = false;
|
|
19
|
+
this.listeners = [];
|
|
20
|
+
const window = InjectedProvider.window;
|
|
21
|
+
if (!InjectedProvider.isWindowContainsWallet(window, injectedWalletKey)) {
|
|
22
|
+
throw new WalletNotInjectedError();
|
|
23
|
+
}
|
|
24
|
+
this.injectedWallet = window[injectedWalletKey].tonconnect;
|
|
25
|
+
}
|
|
26
|
+
static fromStorage(storage) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const bridgeConnectionStorage = new BridgeConnectionStorage(storage);
|
|
29
|
+
const connection = yield bridgeConnectionStorage.getInjectedConnection();
|
|
30
|
+
return new InjectedProvider(connection.jsBridgeKey);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
static isWalletInjected(injectedWalletKey) {
|
|
34
|
+
return InjectedProvider.isWindowContainsWallet(this.window, injectedWalletKey);
|
|
35
|
+
}
|
|
36
|
+
static isInsideWalletBrowser(injectedWalletKey) {
|
|
37
|
+
if (InjectedProvider.isWindowContainsWallet(this.window, injectedWalletKey)) {
|
|
38
|
+
return this.window[injectedWalletKey].tonconnect.isWalletBrowser;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
static isWindowContainsWallet(window, injectedWalletKey) {
|
|
43
|
+
return (!!window &&
|
|
44
|
+
injectedWalletKey in window &&
|
|
45
|
+
typeof window[injectedWalletKey] === 'object' &&
|
|
46
|
+
'tonconnect' in window[injectedWalletKey]);
|
|
47
|
+
}
|
|
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
|
+
});
|
|
68
|
+
}
|
|
69
|
+
restoreConnection() {
|
|
70
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
try {
|
|
72
|
+
const connectEvent = yield this.injectedWallet.restoreConnection();
|
|
73
|
+
if (connectEvent.event === 'connect') {
|
|
74
|
+
this.makeSubscriptions();
|
|
75
|
+
this.listenSubscriptions = true;
|
|
76
|
+
this.listeners.forEach(listener => listener(connectEvent));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
console.error(e);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
closeConnection() {
|
|
85
|
+
if (this.listenSubscriptions) {
|
|
86
|
+
this.injectedWallet.disconnect();
|
|
87
|
+
}
|
|
88
|
+
this.closeAllListeners();
|
|
89
|
+
}
|
|
90
|
+
disconnect() {
|
|
91
|
+
this.closeAllListeners();
|
|
92
|
+
this.injectedWallet.disconnect();
|
|
93
|
+
return Promise.resolve();
|
|
94
|
+
}
|
|
95
|
+
closeAllListeners() {
|
|
96
|
+
var _a;
|
|
97
|
+
this.listenSubscriptions = false;
|
|
98
|
+
this.listeners = [];
|
|
99
|
+
(_a = this.unsubscribeCallback) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
100
|
+
}
|
|
101
|
+
listen(eventsCallback) {
|
|
102
|
+
this.listeners.push(eventsCallback);
|
|
103
|
+
return () => (this.listeners = this.listeners.filter(listener => listener !== eventsCallback));
|
|
104
|
+
}
|
|
105
|
+
sendRequest(request) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
return this.injectedWallet.send(Object.assign(Object.assign({}, request), { id: '0' }));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
makeSubscriptions() {
|
|
111
|
+
this.unsubscribeCallback = this.injectedWallet.listen(e => {
|
|
112
|
+
if (this.listenSubscriptions) {
|
|
113
|
+
this.listeners.forEach(listener => listener(e));
|
|
114
|
+
}
|
|
115
|
+
if (e.event === 'disconnect') {
|
|
116
|
+
this.disconnect();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
InjectedProvider.window = getWindow();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const PROTOCOL_VERSION = 2;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { SessionCrypto } from '@tonconnect/protocol';
|
|
11
|
+
import { TonConnectError } from "../errors";
|
|
12
|
+
export class BridgeConnectionStorage {
|
|
13
|
+
constructor(storage) {
|
|
14
|
+
this.storage = storage;
|
|
15
|
+
this.storeKey = 'ton-connect-storage_bridge-connection';
|
|
16
|
+
}
|
|
17
|
+
storeConnection(connection) {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
if (connection.type === 'injected') {
|
|
20
|
+
return this.storage.setItem(this.storeKey, JSON.stringify(connection));
|
|
21
|
+
}
|
|
22
|
+
const rawSession = {
|
|
23
|
+
sessionKeyPair: connection.session.sessionCrypto.stringifyKeypair(),
|
|
24
|
+
walletPublicKey: connection.session.walletPublicKey,
|
|
25
|
+
walletConnectionSource: connection.session.walletConnectionSource
|
|
26
|
+
};
|
|
27
|
+
const rawConnection = {
|
|
28
|
+
type: 'http',
|
|
29
|
+
connectEvent: connection.connectEvent,
|
|
30
|
+
session: rawSession
|
|
31
|
+
};
|
|
32
|
+
return this.storage.setItem(this.storeKey, JSON.stringify(rawConnection));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
removeConnection() {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
return this.storage.removeItem(this.storeKey);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
getConnection() {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
const stored = yield this.storage.getItem(this.storeKey);
|
|
43
|
+
if (!stored) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const connection = JSON.parse(stored);
|
|
47
|
+
if (connection.type === 'injected') {
|
|
48
|
+
return connection;
|
|
49
|
+
}
|
|
50
|
+
const sessionCrypto = new SessionCrypto(connection.session.sessionKeyPair);
|
|
51
|
+
return {
|
|
52
|
+
type: 'http',
|
|
53
|
+
connectEvent: connection.connectEvent,
|
|
54
|
+
session: {
|
|
55
|
+
sessionCrypto,
|
|
56
|
+
walletConnectionSource: connection.session.walletConnectionSource,
|
|
57
|
+
walletPublicKey: connection.session.walletPublicKey
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getHttpConnection() {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const connection = yield this.getConnection();
|
|
65
|
+
if (!connection) {
|
|
66
|
+
throw new TonConnectError('Trying to read HTTP connection source while nothing is stored');
|
|
67
|
+
}
|
|
68
|
+
if (connection.type === 'injected') {
|
|
69
|
+
throw new TonConnectError('Trying to read HTTP connection source while injected connection is stored');
|
|
70
|
+
}
|
|
71
|
+
return connection;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
getInjectedConnection() {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
const connection = yield this.getConnection();
|
|
77
|
+
if (!connection) {
|
|
78
|
+
throw new TonConnectError('Trying to read Injected bridge connection source while nothing is stored');
|
|
79
|
+
}
|
|
80
|
+
if ((connection === null || connection === void 0 ? void 0 : connection.type) === 'http') {
|
|
81
|
+
throw new TonConnectError('Trying to read Injected bridge connection source while HTTP connection is stored');
|
|
82
|
+
}
|
|
83
|
+
return connection;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
storedConnectionType() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
const stored = yield this.storage.getItem(this.storeKey);
|
|
89
|
+
if (!stored) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const connection = JSON.parse(stored);
|
|
93
|
+
return connection.type;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { LocalstorageNotFoundError } from "../errors/storage/localstorage-not-found.error";
|
|
11
|
+
import { getWindow } from "../utils/web-api";
|
|
12
|
+
export class DefaultStorage {
|
|
13
|
+
constructor() {
|
|
14
|
+
const window = getWindow();
|
|
15
|
+
if (!(window === null || window === void 0 ? void 0 : window.localStorage)) {
|
|
16
|
+
throw new LocalstorageNotFoundError();
|
|
17
|
+
}
|
|
18
|
+
this.window = window;
|
|
19
|
+
}
|
|
20
|
+
getItem(key) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
return Promise.resolve(this.window.localStorage.getItem(key));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
removeItem(key) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
this.window.localStorage.removeItem(key);
|
|
28
|
+
return Promise.resolve();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
setItem(key, value) {
|
|
32
|
+
this.window.localStorage.setItem(key, value);
|
|
33
|
+
return Promise.resolve();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export class HttpBridgeGatewayStorage {
|
|
11
|
+
constructor(storage) {
|
|
12
|
+
this.storage = storage;
|
|
13
|
+
this.storeKey = 'ton-connect-storage_http-bridge-gateway';
|
|
14
|
+
}
|
|
15
|
+
storeLastEventId(lastEventId) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
return this.storage.setItem(this.storeKey, lastEventId);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
removeLastEventId() {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
return this.storage.removeItem(this.storeKey);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
getLastEventId() {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const stored = yield this.storage.getItem(this.storeKey);
|
|
28
|
+
if (!stored) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return stored;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|