@tonconnect/sdk 0.0.4 → 0.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.
- package/README.md +22 -16
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/ton-connect.d.ts +39 -3
- package/lib/ton-connect.interface.d.ts +40 -3
- package/lib/ton-connect.js +58 -28
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
# TON Connect SDK
|
|
1
|
+
# TON Connect SDK
|
|
2
2
|
|
|
3
|
-
⚠️
|
|
3
|
+
⚠️ TonConnect is currently in beta testing. Use it at your own risk.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Use it to connect your app to TON wallets via TonConnect protocol.
|
|
6
|
+
You can find more details and the protocol specification in the [docs](https://github.com/ton-connect/docs).
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
## Get started
|
|
9
|
+
`npm i @tonconnect/sdk`
|
|
10
|
+
|
|
11
|
+
## Init connector and call autoConnect. If user connected his wallet before, connector will restore the connection
|
|
12
|
+
|
|
13
|
+
```ts
|
|
8
14
|
import TonConnect from '@tonconnect/sdk';
|
|
9
15
|
|
|
10
16
|
const connector = new TonConnect();
|
|
@@ -25,28 +31,28 @@ connector.onStatusChange(
|
|
|
25
31
|
## Initialize a wallet connection when user clicks to 'connect' button in your app
|
|
26
32
|
### Initialize a remote wallet connection via universal link
|
|
27
33
|
|
|
28
|
-
```
|
|
34
|
+
```ts
|
|
29
35
|
const walletConnectionSource = {
|
|
30
36
|
universalLinkBase: 'https://app.mycooltonwallet.com',
|
|
31
|
-
bridgeURL: 'https://bridge.mycooltonwallet.
|
|
37
|
+
bridgeURL: 'https://bridge.mycooltonwallet.com,'
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
const uniwersalLink = connector.connect(walletConnectionSource);
|
|
35
41
|
```
|
|
36
42
|
|
|
37
|
-
Then you have to show this link to user as QR code, or use it as a deeplink. You will receive update in `connector.onStatusChange` when user approves connection in the wallet
|
|
38
|
-
|
|
39
|
-
### Initialize injected wallet connection
|
|
43
|
+
Then you have to show this link to user as QR code, or use it as a deeplink. You will receive an update in `connector.onStatusChange` when user approves connection in the wallet
|
|
40
44
|
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
### Initialize injected wallet connection
|
|
46
|
+
```ts
|
|
47
|
+
if (connector.isInjectedProviderAvailable()) {
|
|
48
|
+
connector.connect('injected');
|
|
49
|
+
}
|
|
43
50
|
```
|
|
44
51
|
|
|
45
|
-
|
|
46
|
-
You will receive update in `connector.onStatusChange` when user approves connection in the wallet
|
|
52
|
+
You will receive an update in `connector.onStatusChange` when user approves connection in the wallet
|
|
47
53
|
|
|
48
54
|
## Send transaction
|
|
49
|
-
```
|
|
55
|
+
```ts
|
|
50
56
|
if (!connetor.connected) {
|
|
51
57
|
alert('Please connect wallet to send the transaction!');
|
|
52
58
|
}
|
|
@@ -57,12 +63,12 @@ const transaction = {
|
|
|
57
63
|
{
|
|
58
64
|
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
|
|
59
65
|
amount: "20000000",
|
|
60
|
-
initState: "base64bocblahblahblah=="
|
|
66
|
+
initState: "base64bocblahblahblah=="
|
|
61
67
|
},
|
|
62
68
|
{
|
|
63
69
|
address: "0:E69F10CC84877ABF539F83F879291E5CA169451BA7BCE91A37A5CED3AB8080D3",
|
|
64
70
|
amount: "60000000",
|
|
65
|
-
payload: "base64bocblahblahblah=="
|
|
71
|
+
payload: "base64bocblahblahblah=="
|
|
66
72
|
}
|
|
67
73
|
]
|
|
68
74
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = void 0;
|
|
3
|
+
exports.CHAIN = exports.default = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
tslib_1.__exportStar(require("./ton-connect"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./models"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./errors"), exports);
|
|
8
8
|
var ton_connect_1 = require("./ton-connect");
|
|
9
9
|
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return ton_connect_1.TonConnect; } });
|
|
10
|
+
var protocol_1 = require("@tonconnect/protocol");
|
|
11
|
+
Object.defineProperty(exports, "CHAIN", { enumerable: true, get: function () { return protocol_1.CHAIN; } });
|
package/lib/ton-connect.d.ts
CHANGED
|
@@ -12,21 +12,57 @@ export declare class TonConnect implements ITonConnect {
|
|
|
12
12
|
private provider;
|
|
13
13
|
private statusChangeSubscriptions;
|
|
14
14
|
private statusChangeErrorSubscriptions;
|
|
15
|
+
/**
|
|
16
|
+
* Shows if the wallet is connected right now.
|
|
17
|
+
*/
|
|
15
18
|
get connected(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Current connected account or null if no account is connected.
|
|
21
|
+
*/
|
|
16
22
|
get account(): Account | null;
|
|
23
|
+
/**
|
|
24
|
+
* Current connected wallet or null if no account is connected.
|
|
25
|
+
*/
|
|
17
26
|
get wallet(): Wallet | null;
|
|
18
27
|
private set wallet(value);
|
|
19
28
|
constructor(options?: {
|
|
20
29
|
dappMetedata?: DappMetadata;
|
|
21
30
|
storage?: IStorage;
|
|
22
31
|
});
|
|
32
|
+
/**
|
|
33
|
+
* Indicates if the injected wallet is available.
|
|
34
|
+
*/
|
|
35
|
+
isInjectedProviderAvailable: typeof InjectedProvider.isWalletInjected;
|
|
36
|
+
/**
|
|
37
|
+
* Allows to subscribe to connection status changes and handle connection errors.
|
|
38
|
+
* @param callback will be called after connections status changes with actual wallet or null.
|
|
39
|
+
* @param errorsHandler (optional) will be called with some instance of TonConnectError when connect error is received.
|
|
40
|
+
* @returns unsubscribe callback.
|
|
41
|
+
*/
|
|
23
42
|
onStatusChange(callback: (wallet: Wallet | null) => void, errorsHandler?: (err: TonConnectError) => void): () => void;
|
|
43
|
+
/**
|
|
44
|
+
* Generates universal link for an external wallet and subscribes to the wallet's bridge, or sends connect request to the injected wallet.
|
|
45
|
+
* @param wallet wallet's bridge url and universal link for an external wallet or 'injected' for the injected wallet.
|
|
46
|
+
* @param request (optional) additional request to pass to the wallet while connect (currently only ton_proof is available).
|
|
47
|
+
* @returns universal link if external wallet was passed or void for the injected wallet.
|
|
48
|
+
*/
|
|
24
49
|
connect<T extends WalletConnectionSource | 'injected'>(wallet: T, request?: ConnectAdditionalRequest): T extends 'injected' ? void : string;
|
|
50
|
+
/**
|
|
51
|
+
* Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
|
|
52
|
+
*/
|
|
25
53
|
autoConnect(): void;
|
|
26
|
-
|
|
27
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Asks connected wallet to sign and send the transaction.
|
|
56
|
+
* @param transaction transaction to send.
|
|
57
|
+
* @returns signed transaction boc that allows you to find the transaction in the blockchain.
|
|
58
|
+
* If user rejects transaction, method will throw the corresponding error.
|
|
59
|
+
*/
|
|
60
|
+
sendTransaction(transaction: SendTransactionRequest): Promise<SendTransactionResponse>;
|
|
61
|
+
/**
|
|
62
|
+
* Disconnect form thw connected wallet and drop current session.
|
|
63
|
+
*/
|
|
28
64
|
disconnect(): Promise<void>;
|
|
29
|
-
|
|
65
|
+
private _autoConnect;
|
|
30
66
|
private createProvider;
|
|
31
67
|
private walletEventsListener;
|
|
32
68
|
private onWalletConnected;
|
|
@@ -1,14 +1,51 @@
|
|
|
1
|
+
import { TonConnectError } from "./errors";
|
|
1
2
|
import { Account, WalletConnectionSource, Wallet } from "./models";
|
|
2
3
|
import { SendTransactionRequest, SendTransactionResponse } from "./models/methods";
|
|
3
4
|
import { ConnectAdditionalRequest } from "./models/methods/connect/connect-additional-request";
|
|
4
5
|
export interface ITonConnect {
|
|
6
|
+
/**
|
|
7
|
+
* Shows if the wallet is connected right now.
|
|
8
|
+
*/
|
|
5
9
|
connected: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Current connected account or null if no account is connected.
|
|
12
|
+
*/
|
|
6
13
|
account: Account | null;
|
|
14
|
+
/**
|
|
15
|
+
* Current connected wallet or null if no account is connected.
|
|
16
|
+
*/
|
|
7
17
|
wallet: Wallet | null;
|
|
8
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Indicates if the injected wallet is available.
|
|
20
|
+
*/
|
|
21
|
+
isInjectedProviderAvailable(): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Allows to subscribe to connection status changes and handle connection errors.
|
|
24
|
+
* @param callback will be called after connections status changes with actual wallet or null.
|
|
25
|
+
* @param errorsHandler (optional) will be called with some instance of TonConnectError when connect error is received.
|
|
26
|
+
* @returns unsubscribe callback.
|
|
27
|
+
*/
|
|
28
|
+
onStatusChange(callback: (walletInfo: Wallet | null) => void, errorsHandler?: (err: TonConnectError) => void): () => void;
|
|
29
|
+
/**
|
|
30
|
+
* Generates universal link for an external wallet and subscribes to the wallet's bridge, or sends connect request to the injected wallet.
|
|
31
|
+
* @param wallet wallet's bridge url and universal link for an external wallet or 'injected' for the injected wallet.
|
|
32
|
+
* @param request (optional) additional request to pass to the wallet while connect (currently only ton_proof is available).
|
|
33
|
+
* @returns universal link if external wallet was passed or void for the injected wallet.
|
|
34
|
+
*/
|
|
9
35
|
connect<T extends WalletConnectionSource | 'injected'>(wallet: T, request?: ConnectAdditionalRequest): T extends 'injected' ? void : string;
|
|
36
|
+
/**
|
|
37
|
+
* Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
|
|
38
|
+
*/
|
|
10
39
|
autoConnect(): void;
|
|
11
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Disconnect form thw connected wallet and drop current session.
|
|
42
|
+
*/
|
|
12
43
|
disconnect(): Promise<void>;
|
|
13
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Asks connected wallet to sign and send the transaction.
|
|
46
|
+
* @param transaction transaction to send.
|
|
47
|
+
* @returns signed transaction boc that allows you to find the transaction in the blockchain.
|
|
48
|
+
* If user rejects transaction, method will throw the corresponding error.
|
|
49
|
+
*/
|
|
50
|
+
sendTransaction(transaction: SendTransactionRequest): Promise<SendTransactionResponse>;
|
|
14
51
|
}
|
package/lib/ton-connect.js
CHANGED
|
@@ -19,6 +19,9 @@ var TonConnect = /** @class */ (function () {
|
|
|
19
19
|
this.provider = null;
|
|
20
20
|
this.statusChangeSubscriptions = [];
|
|
21
21
|
this.statusChangeErrorSubscriptions = [];
|
|
22
|
+
/**
|
|
23
|
+
* Indicates if the injected wallet is available.
|
|
24
|
+
*/
|
|
22
25
|
this.isInjectedProviderAvailable = injected_provider_1.InjectedProvider.isWalletInjected;
|
|
23
26
|
this.dappSettings = {
|
|
24
27
|
metadata: (options === null || options === void 0 ? void 0 : options.dappMetedata) || (0, web_api_1.getWebPageMetadata)(),
|
|
@@ -27,6 +30,9 @@ var TonConnect = /** @class */ (function () {
|
|
|
27
30
|
this.bridgeConnectionStorage = new bridge_connection_storage_1.BridgeConnectionStorage(this.dappSettings.storage);
|
|
28
31
|
}
|
|
29
32
|
Object.defineProperty(TonConnect.prototype, "connected", {
|
|
33
|
+
/**
|
|
34
|
+
* Shows if the wallet is connected right now.
|
|
35
|
+
*/
|
|
30
36
|
get: function () {
|
|
31
37
|
return this._wallet !== null;
|
|
32
38
|
},
|
|
@@ -34,6 +40,9 @@ var TonConnect = /** @class */ (function () {
|
|
|
34
40
|
configurable: true
|
|
35
41
|
});
|
|
36
42
|
Object.defineProperty(TonConnect.prototype, "account", {
|
|
43
|
+
/**
|
|
44
|
+
* Current connected account or null if no account is connected.
|
|
45
|
+
*/
|
|
37
46
|
get: function () {
|
|
38
47
|
var _a;
|
|
39
48
|
return ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.account) || null;
|
|
@@ -42,6 +51,9 @@ var TonConnect = /** @class */ (function () {
|
|
|
42
51
|
configurable: true
|
|
43
52
|
});
|
|
44
53
|
Object.defineProperty(TonConnect.prototype, "wallet", {
|
|
54
|
+
/**
|
|
55
|
+
* Current connected wallet or null if no account is connected.
|
|
56
|
+
*/
|
|
45
57
|
get: function () {
|
|
46
58
|
return this._wallet;
|
|
47
59
|
},
|
|
@@ -53,6 +65,12 @@ var TonConnect = /** @class */ (function () {
|
|
|
53
65
|
enumerable: false,
|
|
54
66
|
configurable: true
|
|
55
67
|
});
|
|
68
|
+
/**
|
|
69
|
+
* Allows to subscribe to connection status changes and handle connection errors.
|
|
70
|
+
* @param callback will be called after connections status changes with actual wallet or null.
|
|
71
|
+
* @param errorsHandler (optional) will be called with some instance of TonConnectError when connect error is received.
|
|
72
|
+
* @returns unsubscribe callback.
|
|
73
|
+
*/
|
|
56
74
|
TonConnect.prototype.onStatusChange = function (callback, errorsHandler) {
|
|
57
75
|
var _this = this;
|
|
58
76
|
this.statusChangeSubscriptions.push(callback);
|
|
@@ -75,43 +93,26 @@ var TonConnect = /** @class */ (function () {
|
|
|
75
93
|
this.provider = this.createProvider(wallet);
|
|
76
94
|
return this.provider.connect(this.createConnectRequest(request));
|
|
77
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
* Try to restore existing session and reconnect to the corresponding wallet. Call it immediately when your app is loaded.
|
|
98
|
+
*/
|
|
78
99
|
TonConnect.prototype.autoConnect = function () {
|
|
79
100
|
this._autoConnect();
|
|
80
101
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
bridgeConnection = _c.sent();
|
|
89
|
-
if (!bridgeConnection) return [3 /*break*/, 3];
|
|
90
|
-
_a = this;
|
|
91
|
-
return [4 /*yield*/, this.createProvider(bridgeConnection.session.walletConnectionSource)];
|
|
92
|
-
case 2:
|
|
93
|
-
_a.provider = _c.sent();
|
|
94
|
-
return [2 /*return*/, this.provider.autoConnect()];
|
|
95
|
-
case 3:
|
|
96
|
-
if (!injected_provider_1.InjectedProvider.isWalletInjected()) return [3 /*break*/, 5];
|
|
97
|
-
_b = this;
|
|
98
|
-
return [4 /*yield*/, this.createProvider('injected')];
|
|
99
|
-
case 4:
|
|
100
|
-
_b.provider = _c.sent();
|
|
101
|
-
return [2 /*return*/, this.provider.autoConnect()];
|
|
102
|
-
case 5: return [2 /*return*/];
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
};
|
|
107
|
-
TonConnect.prototype.sendTransaction = function (tx) {
|
|
102
|
+
/**
|
|
103
|
+
* Asks connected wallet to sign and send the transaction.
|
|
104
|
+
* @param transaction transaction to send.
|
|
105
|
+
* @returns signed transaction boc that allows you to find the transaction in the blockchain.
|
|
106
|
+
* If user rejects transaction, method will throw the corresponding error.
|
|
107
|
+
*/
|
|
108
|
+
TonConnect.prototype.sendTransaction = function (transaction) {
|
|
108
109
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
109
110
|
var response;
|
|
110
111
|
return tslib_1.__generator(this, function (_a) {
|
|
111
112
|
switch (_a.label) {
|
|
112
113
|
case 0:
|
|
113
114
|
this.checkConnection();
|
|
114
|
-
return [4 /*yield*/, this.provider.sendRequest(send_transaction_parser_1.sendTransactionParser.convertToRpcRequest(
|
|
115
|
+
return [4 /*yield*/, this.provider.sendRequest(send_transaction_parser_1.sendTransactionParser.convertToRpcRequest(transaction))];
|
|
115
116
|
case 1:
|
|
116
117
|
response = _a.sent();
|
|
117
118
|
if (send_transaction_parser_1.sendTransactionParser.isError(response)) {
|
|
@@ -122,6 +123,9 @@ var TonConnect = /** @class */ (function () {
|
|
|
122
123
|
});
|
|
123
124
|
});
|
|
124
125
|
};
|
|
126
|
+
/**
|
|
127
|
+
* Disconnect form thw connected wallet and drop current session.
|
|
128
|
+
*/
|
|
125
129
|
TonConnect.prototype.disconnect = function () {
|
|
126
130
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
127
131
|
return tslib_1.__generator(this, function (_a) {
|
|
@@ -139,6 +143,32 @@ var TonConnect = /** @class */ (function () {
|
|
|
139
143
|
});
|
|
140
144
|
});
|
|
141
145
|
};
|
|
146
|
+
TonConnect.prototype._autoConnect = function () {
|
|
147
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
148
|
+
var bridgeConnection, _a, _b;
|
|
149
|
+
return tslib_1.__generator(this, function (_c) {
|
|
150
|
+
switch (_c.label) {
|
|
151
|
+
case 0: return [4 /*yield*/, this.bridgeConnectionStorage.getConnection()];
|
|
152
|
+
case 1:
|
|
153
|
+
bridgeConnection = _c.sent();
|
|
154
|
+
if (!bridgeConnection) return [3 /*break*/, 3];
|
|
155
|
+
_a = this;
|
|
156
|
+
return [4 /*yield*/, this.createProvider(bridgeConnection.session.walletConnectionSource)];
|
|
157
|
+
case 2:
|
|
158
|
+
_a.provider = _c.sent();
|
|
159
|
+
return [2 /*return*/, this.provider.autoConnect()];
|
|
160
|
+
case 3:
|
|
161
|
+
if (!injected_provider_1.InjectedProvider.isWalletInjected()) return [3 /*break*/, 5];
|
|
162
|
+
_b = this;
|
|
163
|
+
return [4 /*yield*/, this.createProvider('injected')];
|
|
164
|
+
case 4:
|
|
165
|
+
_b.provider = _c.sent();
|
|
166
|
+
return [2 /*return*/, this.provider.autoConnect()];
|
|
167
|
+
case 5: return [2 /*return*/];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
};
|
|
142
172
|
TonConnect.prototype.createProvider = function (wallet) {
|
|
143
173
|
var provider;
|
|
144
174
|
if (wallet === 'injected') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tonconnect/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "npx rimraf lib && ttsc",
|
|
6
6
|
"build:production": "npx rimraf lib && ttsc --sourceMap false"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"deepmerge": "^4.2.2",
|
|
28
28
|
"tweetnacl": "^1.0.3",
|
|
29
|
-
"@tonconnect/protocol": "^0.0.
|
|
29
|
+
"@tonconnect/protocol": "^0.0.5"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"lib"
|