@tonconnect/sdk 0.0.43 → 0.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/dist/tonconnect-sdk.min.js +1 -1
- package/dist/tonconnect-sdk.min.js.map +1 -1
- package/lib/cjs/errors/address/index.js +5 -0
- package/lib/cjs/errors/address/wrong-address.error.js +11 -0
- package/lib/cjs/errors/binary/base64-encode.error.js +11 -0
- package/lib/cjs/errors/binary/index.js +7 -0
- package/lib/cjs/errors/binary/parse-hex.error.js +11 -0
- package/lib/{errors → cjs/errors}/dapp/dapp-metadata.error.js +0 -0
- package/lib/{errors → cjs/errors}/index.js +2 -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} +3 -1
- 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 +2 -3
- 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} +3 -5
- package/lib/cjs/utils/address.js +87 -0
- 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} +40 -16
- package/lib/esm/errors/address/index.js +1 -0
- package/lib/esm/errors/address/wrong-address.error.js +7 -0
- package/lib/esm/errors/binary/base64-encode.error.js +7 -0
- package/lib/esm/errors/binary/index.js +2 -0
- package/lib/esm/errors/binary/parse-hex.error.js +7 -0
- package/lib/esm/errors/dapp/dapp-metadata.error.js +10 -0
- package/lib/esm/errors/index.js +8 -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 +6 -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/address.js +83 -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 +115 -0
- package/lib/types/errors/address/index.d.ts +1 -0
- package/lib/types/errors/address/wrong-address.error.d.ts +4 -0
- package/lib/types/errors/binary/base64-encode.error.d.ts +4 -0
- package/lib/types/errors/binary/index.d.ts +2 -0
- package/lib/types/errors/binary/parse-hex.error.d.ts +4 -0
- package/lib/{errors → types/errors}/dapp/dapp-metadata.error.d.ts +0 -0
- package/lib/{errors → types/errors}/index.d.ts +2 -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} +1 -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/types/models/methods/connect/connect-additional-request.d.ts +6 -0
- 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 -1
- 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 +14 -7
- 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 +2 -2
- 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} +1 -3
- package/lib/{ton-connect.interface.d.ts → types/ton-connect.interface.d.ts} +1 -3
- package/lib/types/utils/address.d.ts +1 -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/models/methods/connect/connect-additional-request.d.ts +0 -15
- package/lib/models/methods/send-transaction/send-transaction-options.d.ts +0 -11
- package/lib/models/methods/send-transaction/send-transaction-options.js +0 -2
- package/lib/resources/protocol.json +0 -3
|
@@ -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 {};
|
|
@@ -0,0 +1,252 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
import { DappMetadataError } from "./errors/dapp/dapp-metadata.error";
|
|
22
|
+
import { ManifestContentErrorError } from "./errors/protocol/events/connect/manifest-content-error.error";
|
|
23
|
+
import { ManifestNotFoundError } from "./errors/protocol/events/connect/manifest-not-found.error";
|
|
24
|
+
import { TonConnectError } from "./errors/ton-connect.error";
|
|
25
|
+
import { WalletAlreadyConnectedError } from "./errors/wallet/wallet-already-connected.error";
|
|
26
|
+
import { WalletNotConnectedError } from "./errors/wallet/wallet-not-connected.error";
|
|
27
|
+
import { WalletNotSupportFeatureError } from "./errors/wallet/wallet-not-support-feature.error";
|
|
28
|
+
import { isWalletConnectionSourceJS } from "./models/wallet/wallet-connection-source";
|
|
29
|
+
import { connectErrorsParser } from "./parsers/connect-errors-parser";
|
|
30
|
+
import { sendTransactionParser } from "./parsers/send-transaction-parser";
|
|
31
|
+
import { BridgeProvider } from "./provider/bridge/bridge-provider";
|
|
32
|
+
import { InjectedProvider } from "./provider/injected/injected-provider";
|
|
33
|
+
import { BridgeConnectionStorage } from "./storage/bridge-connection-storage";
|
|
34
|
+
import { DefaultStorage } from "./storage/default-storage";
|
|
35
|
+
import { getWebPageManifest } from "./utils/web-api";
|
|
36
|
+
import { WalletsListManager } from "./wallets-list-manager";
|
|
37
|
+
export class TonConnect {
|
|
38
|
+
constructor(options) {
|
|
39
|
+
this.walletsList = new WalletsListManager();
|
|
40
|
+
this._wallet = null;
|
|
41
|
+
this.provider = null;
|
|
42
|
+
this.statusChangeSubscriptions = [];
|
|
43
|
+
this.statusChangeErrorSubscriptions = [];
|
|
44
|
+
this.dappSettings = {
|
|
45
|
+
manifestUrl: (options === null || options === void 0 ? void 0 : options.manifestUrl) || getWebPageManifest(),
|
|
46
|
+
storage: (options === null || options === void 0 ? void 0 : options.storage) || new DefaultStorage()
|
|
47
|
+
};
|
|
48
|
+
if (!this.dappSettings.manifestUrl) {
|
|
49
|
+
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
|
+
this.bridgeConnectionStorage = new BridgeConnectionStorage(this.dappSettings.storage);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Shows if the wallet is connected right now.
|
|
55
|
+
*/
|
|
56
|
+
get connected() {
|
|
57
|
+
return this._wallet !== null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Current connected account or null if no account is connected.
|
|
61
|
+
*/
|
|
62
|
+
get account() {
|
|
63
|
+
var _a;
|
|
64
|
+
return ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.account) || null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Current connected wallet or null if no account is connected.
|
|
68
|
+
*/
|
|
69
|
+
get wallet() {
|
|
70
|
+
return this._wallet;
|
|
71
|
+
}
|
|
72
|
+
set wallet(value) {
|
|
73
|
+
this._wallet = value;
|
|
74
|
+
this.statusChangeSubscriptions.forEach(callback => callback(this._wallet));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Returns available wallets list.
|
|
78
|
+
*/
|
|
79
|
+
getWallets() {
|
|
80
|
+
return this.walletsList.getWallets();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Allows to subscribe to connection status changes and handle connection errors.
|
|
84
|
+
* @param callback will be called after connections status changes with actual wallet or null.
|
|
85
|
+
* @param errorsHandler (optional) will be called with some instance of TonConnectError when connect error is received.
|
|
86
|
+
* @returns unsubscribe callback.
|
|
87
|
+
*/
|
|
88
|
+
onStatusChange(callback, errorsHandler) {
|
|
89
|
+
this.statusChangeSubscriptions.push(callback);
|
|
90
|
+
if (errorsHandler) {
|
|
91
|
+
this.statusChangeErrorSubscriptions.push(errorsHandler);
|
|
92
|
+
}
|
|
93
|
+
return () => {
|
|
94
|
+
this.statusChangeSubscriptions = this.statusChangeSubscriptions.filter(item => item !== callback);
|
|
95
|
+
if (errorsHandler) {
|
|
96
|
+
this.statusChangeErrorSubscriptions = this.statusChangeErrorSubscriptions.filter(item => item !== errorsHandler);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
connect(wallet, request) {
|
|
101
|
+
var _a;
|
|
102
|
+
if (this.connected) {
|
|
103
|
+
throw new WalletAlreadyConnectedError();
|
|
104
|
+
}
|
|
105
|
+
(_a = this.provider) === null || _a === void 0 ? void 0 : _a.closeConnection();
|
|
106
|
+
this.provider = this.createProvider(wallet);
|
|
107
|
+
return this.provider.connect(this.createConnectRequest(request));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
|
|
111
|
+
*/
|
|
112
|
+
restoreConnection() {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
const [bridgeConnectionType, embeddedWallet] = yield Promise.all([
|
|
115
|
+
this.bridgeConnectionStorage.storedConnectionType(),
|
|
116
|
+
this.walletsList.getEmbeddedWallet()
|
|
117
|
+
]);
|
|
118
|
+
switch (bridgeConnectionType) {
|
|
119
|
+
case 'http':
|
|
120
|
+
this.provider = yield BridgeProvider.fromStorage(this.dappSettings.storage);
|
|
121
|
+
break;
|
|
122
|
+
case 'injected':
|
|
123
|
+
this.provider = yield InjectedProvider.fromStorage(this.dappSettings.storage);
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
if (embeddedWallet) {
|
|
127
|
+
this.provider = yield this.createProvider(embeddedWallet);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
this.provider.listen(this.walletEventsListener.bind(this));
|
|
134
|
+
return this.provider.restoreConnection();
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Asks connected wallet to sign and send the transaction.
|
|
139
|
+
* @param transaction transaction to send.
|
|
140
|
+
* @returns signed transaction boc that allows you to find the transaction in the blockchain.
|
|
141
|
+
* If user rejects transaction, method will throw the corresponding error.
|
|
142
|
+
*/
|
|
143
|
+
sendTransaction(transaction) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
this.checkConnection();
|
|
146
|
+
this.checkFeatureSupport('SendTransaction');
|
|
147
|
+
const { validUntil } = transaction, tx = __rest(transaction, ["validUntil"]);
|
|
148
|
+
const response = yield this.provider.sendRequest(sendTransactionParser.convertToRpcRequest(Object.assign(Object.assign({}, tx), { valid_until: validUntil })));
|
|
149
|
+
if (sendTransactionParser.isError(response)) {
|
|
150
|
+
return sendTransactionParser.parseAndThrowError(response);
|
|
151
|
+
}
|
|
152
|
+
return sendTransactionParser.convertFromRpcResponse(response);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Disconnect form thw connected wallet and drop current session.
|
|
157
|
+
*/
|
|
158
|
+
disconnect() {
|
|
159
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
if (!this.connected) {
|
|
161
|
+
throw new WalletNotConnectedError();
|
|
162
|
+
}
|
|
163
|
+
yield this.provider.disconnect();
|
|
164
|
+
this.onWalletDisconnected();
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
createProvider(wallet) {
|
|
168
|
+
let provider;
|
|
169
|
+
if (isWalletConnectionSourceJS(wallet)) {
|
|
170
|
+
provider = new InjectedProvider(wallet.jsBridgeKey);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
provider = new BridgeProvider(this.dappSettings.storage, wallet);
|
|
174
|
+
}
|
|
175
|
+
provider.listen(this.walletEventsListener.bind(this));
|
|
176
|
+
return provider;
|
|
177
|
+
}
|
|
178
|
+
walletEventsListener(e) {
|
|
179
|
+
switch (e.event) {
|
|
180
|
+
case 'connect':
|
|
181
|
+
this.onWalletConnected(e.payload);
|
|
182
|
+
break;
|
|
183
|
+
case 'connect_error':
|
|
184
|
+
this.onWalletConnectError(e.payload);
|
|
185
|
+
break;
|
|
186
|
+
case 'disconnect':
|
|
187
|
+
this.onWalletDisconnected();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
onWalletConnected(connectEvent) {
|
|
191
|
+
const tonAccountItem = connectEvent.items.find(item => item.name === 'ton_addr');
|
|
192
|
+
const tonProofItem = connectEvent.items.find(item => item.name === 'ton_proof');
|
|
193
|
+
if (!tonAccountItem) {
|
|
194
|
+
throw new TonConnectError('ton_addr connection item was not found');
|
|
195
|
+
}
|
|
196
|
+
const wallet = {
|
|
197
|
+
device: connectEvent.device,
|
|
198
|
+
provider: this.provider.type,
|
|
199
|
+
account: {
|
|
200
|
+
address: tonAccountItem.address,
|
|
201
|
+
chain: tonAccountItem.network,
|
|
202
|
+
walletStateInit: tonAccountItem.walletStateInit
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
if (tonProofItem) {
|
|
206
|
+
wallet.connectItems = {
|
|
207
|
+
tonProof: tonProofItem
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
this.wallet = wallet;
|
|
211
|
+
}
|
|
212
|
+
onWalletConnectError(connectEventError) {
|
|
213
|
+
const error = connectErrorsParser.parseError(connectEventError);
|
|
214
|
+
this.statusChangeErrorSubscriptions.forEach(errorsHandler => errorsHandler(error));
|
|
215
|
+
console.debug(error);
|
|
216
|
+
if (error instanceof ManifestNotFoundError || error instanceof ManifestContentErrorError) {
|
|
217
|
+
console.error(error);
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
onWalletDisconnected() {
|
|
222
|
+
this.wallet = null;
|
|
223
|
+
}
|
|
224
|
+
checkConnection() {
|
|
225
|
+
if (!this.connected) {
|
|
226
|
+
throw new WalletNotConnectedError();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
checkFeatureSupport(feature) {
|
|
230
|
+
var _a;
|
|
231
|
+
if (!((_a = this.wallet) === null || _a === void 0 ? void 0 : _a.device.features.includes(feature))) {
|
|
232
|
+
throw new WalletNotSupportFeatureError();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
createConnectRequest(request) {
|
|
236
|
+
const items = [
|
|
237
|
+
{
|
|
238
|
+
name: 'ton_addr'
|
|
239
|
+
}
|
|
240
|
+
];
|
|
241
|
+
if (request === null || request === void 0 ? void 0 : request.tonProof) {
|
|
242
|
+
items.push({
|
|
243
|
+
name: 'ton_proof',
|
|
244
|
+
payload: request.tonProof
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
manifestUrl: this.dappSettings.manifestUrl,
|
|
249
|
+
items
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { WrongAddressError, ParseHexError } from "../errors";
|
|
2
|
+
import { Base64 } from '@tonconnect/protocol';
|
|
3
|
+
export function toUserFriendlyAddress(hexAddress) {
|
|
4
|
+
const { wc, hex } = parseHexAddress(hexAddress);
|
|
5
|
+
const bounceableTag = 0x11;
|
|
6
|
+
const addr = new Int8Array(34);
|
|
7
|
+
addr[0] = bounceableTag;
|
|
8
|
+
addr[1] = wc;
|
|
9
|
+
addr.set(hex, 2);
|
|
10
|
+
const addressWithChecksum = new Uint8Array(36);
|
|
11
|
+
addressWithChecksum.set(addr);
|
|
12
|
+
addressWithChecksum.set(crc16(addr), 34);
|
|
13
|
+
let addressBase64 = Base64.encode(addressWithChecksum);
|
|
14
|
+
return addressBase64.replace(/\+/g, '-').replace(/\//g, '_');
|
|
15
|
+
}
|
|
16
|
+
function parseHexAddress(hexAddress) {
|
|
17
|
+
if (!hexAddress.includes(':')) {
|
|
18
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. Address must include ":".`);
|
|
19
|
+
}
|
|
20
|
+
const parts = hexAddress.split(':');
|
|
21
|
+
if (parts.length !== 2) {
|
|
22
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. Address must include ":" only once.`);
|
|
23
|
+
}
|
|
24
|
+
const wc = parseInt(parts[0]);
|
|
25
|
+
if (wc !== 0 && wc !== -1) {
|
|
26
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. WC must be eq 0 or -1, but ${wc} received.`);
|
|
27
|
+
}
|
|
28
|
+
const hex = parts[1];
|
|
29
|
+
if ((hex === null || hex === void 0 ? void 0 : hex.length) !== 64) {
|
|
30
|
+
throw new WrongAddressError(`Wrong address ${hexAddress}. Hex part must be 64bytes length, but ${hex === null || hex === void 0 ? void 0 : hex.length} received.`);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
wc,
|
|
34
|
+
hex: hexToBytes(hex)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function crc16(data) {
|
|
38
|
+
const poly = 0x1021;
|
|
39
|
+
let reg = 0;
|
|
40
|
+
const message = new Uint8Array(data.length + 2);
|
|
41
|
+
message.set(data);
|
|
42
|
+
for (let byte of message) {
|
|
43
|
+
let mask = 0x80;
|
|
44
|
+
while (mask > 0) {
|
|
45
|
+
reg <<= 1;
|
|
46
|
+
if (byte & mask) {
|
|
47
|
+
reg += 1;
|
|
48
|
+
}
|
|
49
|
+
mask >>= 1;
|
|
50
|
+
if (reg > 0xffff) {
|
|
51
|
+
reg &= 0xffff;
|
|
52
|
+
reg ^= poly;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return new Uint8Array([Math.floor(reg / 256), reg % 256]);
|
|
57
|
+
}
|
|
58
|
+
const toByteMap = {};
|
|
59
|
+
for (let ord = 0; ord <= 0xff; ord++) {
|
|
60
|
+
let s = ord.toString(16);
|
|
61
|
+
if (s.length < 2) {
|
|
62
|
+
s = '0' + s;
|
|
63
|
+
}
|
|
64
|
+
toByteMap[s] = ord;
|
|
65
|
+
}
|
|
66
|
+
function hexToBytes(hex) {
|
|
67
|
+
hex = hex.toLowerCase();
|
|
68
|
+
const length2 = hex.length;
|
|
69
|
+
if (length2 % 2 !== 0) {
|
|
70
|
+
throw new ParseHexError('Hex string must have length a multiple of 2: ' + hex);
|
|
71
|
+
}
|
|
72
|
+
const length = length2 / 2;
|
|
73
|
+
const result = new Uint8Array(length);
|
|
74
|
+
for (let i = 0; i < length; i++) {
|
|
75
|
+
const doubled = i * 2;
|
|
76
|
+
const hexSubstring = hex.substring(doubled, doubled + 2);
|
|
77
|
+
if (!toByteMap.hasOwnProperty(hexSubstring)) {
|
|
78
|
+
throw new ParseHexError('Invalid hex character: ' + hexSubstring);
|
|
79
|
+
}
|
|
80
|
+
result[i] = toByteMap[hexSubstring];
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import deepmerge from 'deepmerge';
|
|
2
|
+
export function mergeOptions(options, defaultOptions) {
|
|
3
|
+
if (!options) {
|
|
4
|
+
return defaultOptions;
|
|
5
|
+
}
|
|
6
|
+
const overwriteMerge = (_, sourceArray, __) => sourceArray;
|
|
7
|
+
return deepmerge(defaultOptions, options, { arrayMerge: overwriteMerge });
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|