@onekeyfe/onekey-ton-provider 2.2.8 → 2.2.10
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
|
|
2
2
|
import { ProviderTonBase } from './ProviderTonBase';
|
|
3
|
-
import { AppRequest, ConnectEvent, ConnectRequest, DeviceInfo, RpcMethod, WalletEvent, WalletResponse } from '@tonconnect/protocol';
|
|
3
|
+
import { AppRequest, ConnectEvent, ConnectRequest, DeviceInfo, RpcMethod, WalletEvent, WalletResponse, WalletResponseError } from '@tonconnect/protocol';
|
|
4
4
|
import { AccountInfo, SignDataRequest, SignDataResult, SignProofRequest, SignProofResult, TransactionRequest, WalletInfo } from './types';
|
|
5
5
|
declare const PROVIDER_EVENTS: {
|
|
6
6
|
readonly disconnect: "disconnect";
|
|
@@ -48,10 +48,10 @@ export declare class ProviderTon extends ProviderTonBase implements IProviderTon
|
|
|
48
48
|
private _handleAccountChange;
|
|
49
49
|
private _network;
|
|
50
50
|
isNetworkChanged(network: string): boolean;
|
|
51
|
-
private _id;
|
|
52
51
|
_connect(protocolVersion?: number, message?: ConnectRequest): Promise<ConnectEvent>;
|
|
53
52
|
connect(protocolVersion?: number, message?: ConnectRequest): Promise<ConnectEvent>;
|
|
54
53
|
restoreConnection(): Promise<ConnectEvent>;
|
|
54
|
+
convertError<T extends RpcMethod>(id: string, error: unknown): WalletResponseError<T>;
|
|
55
55
|
send<T extends RpcMethod>(message: AppRequest<T>): Promise<WalletResponse<T>>;
|
|
56
56
|
private _sendTransaction;
|
|
57
57
|
private _signData;
|
|
@@ -43,6 +43,7 @@ export function createTonProvider(originalProvider, customDeviceInfo, customWall
|
|
|
43
43
|
},
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
+
// Doc: https://github.com/ton-blockchain/ton-connect/blob/main/requests-responses.md
|
|
46
47
|
export class ProviderTon extends ProviderTonBase {
|
|
47
48
|
constructor(props) {
|
|
48
49
|
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
|
|
@@ -61,7 +62,6 @@ export class ProviderTon extends ProviderTonBase {
|
|
|
61
62
|
};
|
|
62
63
|
this.protocolVersion = 2;
|
|
63
64
|
this.isWalletBrowser = false;
|
|
64
|
-
this._id = 0;
|
|
65
65
|
// void this._getDeviceInfo();
|
|
66
66
|
this._registerEvents();
|
|
67
67
|
}
|
|
@@ -154,7 +154,7 @@ export class ProviderTon extends ProviderTonBase {
|
|
|
154
154
|
}
|
|
155
155
|
_connect(protocolVersion, message) {
|
|
156
156
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
const id =
|
|
157
|
+
const id = Date.now();
|
|
158
158
|
const isGetTonAddr = !message || (message && message.items.some((item) => item.name === 'ton_addr'));
|
|
159
159
|
const proofItem = message &&
|
|
160
160
|
message.items.find((item) => item.name === 'ton_proof');
|
|
@@ -164,10 +164,26 @@ export class ProviderTon extends ProviderTonBase {
|
|
|
164
164
|
items.push(Object.assign({ name: 'ton_addr' }, this._accountInfo));
|
|
165
165
|
}
|
|
166
166
|
else {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
167
|
+
let result;
|
|
168
|
+
try {
|
|
169
|
+
result = yield this._callBridge({
|
|
170
|
+
method: 'connect',
|
|
171
|
+
params: protocolVersion && message ? [protocolVersion, message] : [],
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
const { code } = error;
|
|
176
|
+
if (code === 4001) {
|
|
177
|
+
return {
|
|
178
|
+
event: 'connect_error',
|
|
179
|
+
id,
|
|
180
|
+
payload: {
|
|
181
|
+
code: CONNECT_EVENT_ERROR_CODES.USER_REJECTS_ERROR,
|
|
182
|
+
message: ConnectEventErrorMessage.USER_DECLINED,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
171
187
|
if (!result) {
|
|
172
188
|
return {
|
|
173
189
|
event: 'connect_error',
|
|
@@ -183,14 +199,30 @@ export class ProviderTon extends ProviderTonBase {
|
|
|
183
199
|
}
|
|
184
200
|
}
|
|
185
201
|
if (proofItem) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
202
|
+
let result;
|
|
203
|
+
try {
|
|
204
|
+
result = yield this._callBridge({
|
|
205
|
+
method: 'signProof',
|
|
206
|
+
params: [
|
|
207
|
+
{
|
|
208
|
+
payload: proofItem.payload,
|
|
209
|
+
},
|
|
210
|
+
],
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
const { code } = error;
|
|
215
|
+
if (code === 4001) {
|
|
216
|
+
return {
|
|
217
|
+
event: 'connect_error',
|
|
218
|
+
id,
|
|
219
|
+
payload: {
|
|
220
|
+
code: CONNECT_EVENT_ERROR_CODES.USER_REJECTS_ERROR,
|
|
221
|
+
message: ConnectEventErrorMessage.USER_DECLINED,
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
}
|
|
194
226
|
if (!result) {
|
|
195
227
|
return {
|
|
196
228
|
event: 'connect_error',
|
|
@@ -222,34 +254,61 @@ export class ProviderTon extends ProviderTonBase {
|
|
|
222
254
|
restoreConnection() {
|
|
223
255
|
return this._connect();
|
|
224
256
|
}
|
|
257
|
+
convertError(id, error) {
|
|
258
|
+
const { code, message } = error;
|
|
259
|
+
if (code === 4001) {
|
|
260
|
+
return {
|
|
261
|
+
id,
|
|
262
|
+
error: {
|
|
263
|
+
code: SEND_TRANSACTION_ERROR_CODES.USER_REJECTS_ERROR,
|
|
264
|
+
message: ConnectEventErrorMessage.USER_DECLINED,
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
return {
|
|
270
|
+
id,
|
|
271
|
+
error: {
|
|
272
|
+
code: SEND_TRANSACTION_ERROR_CODES.UNKNOWN_APP_ERROR,
|
|
273
|
+
message: message !== null && message !== void 0 ? message : '',
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
}
|
|
225
278
|
send(message) {
|
|
226
279
|
return __awaiter(this, void 0, void 0, function* () {
|
|
227
280
|
const id = message.id;
|
|
228
281
|
let res;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
282
|
+
try {
|
|
283
|
+
const params = message.params.map((p) => {
|
|
284
|
+
if (typeof p === 'string') {
|
|
285
|
+
return JSON.parse(p);
|
|
286
|
+
}
|
|
287
|
+
return p;
|
|
288
|
+
});
|
|
289
|
+
if (message.method === 'sendTransaction') {
|
|
290
|
+
res = yield this._sendTransaction(params[0]);
|
|
291
|
+
}
|
|
292
|
+
else if (message.method === 'signData') {
|
|
293
|
+
res = yield this._signData(params[0]);
|
|
294
|
+
}
|
|
295
|
+
else if (message.method === 'disconnect') {
|
|
296
|
+
yield this._disconnect();
|
|
297
|
+
// @ts-expect-error
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
return {
|
|
302
|
+
id,
|
|
303
|
+
error: {
|
|
304
|
+
code: SEND_TRANSACTION_ERROR_CODES.METHOD_NOT_SUPPORTED,
|
|
305
|
+
message: SendTransactionErrorMessage.METHOD_NOT_SUPPORTED,
|
|
306
|
+
},
|
|
307
|
+
};
|
|
232
308
|
}
|
|
233
|
-
return p;
|
|
234
|
-
});
|
|
235
|
-
if (message.method === 'sendTransaction') {
|
|
236
|
-
res = yield this._sendTransaction(params[0]);
|
|
237
|
-
}
|
|
238
|
-
else if (message.method === 'signData') {
|
|
239
|
-
res = yield this._signData(params[0]);
|
|
240
|
-
}
|
|
241
|
-
else if (message.method === 'disconnect') {
|
|
242
|
-
yield this._disconnect();
|
|
243
|
-
res = '';
|
|
244
309
|
}
|
|
245
|
-
|
|
246
|
-
return
|
|
247
|
-
id,
|
|
248
|
-
error: {
|
|
249
|
-
code: SEND_TRANSACTION_ERROR_CODES.METHOD_NOT_SUPPORTED,
|
|
250
|
-
message: SendTransactionErrorMessage.METHOD_NOT_SUPPORTED,
|
|
251
|
-
},
|
|
252
|
-
};
|
|
310
|
+
catch (error) {
|
|
311
|
+
return this.convertError(id, error);
|
|
253
312
|
}
|
|
254
313
|
if (res === undefined) {
|
|
255
314
|
return {
|
|
@@ -268,11 +327,10 @@ export class ProviderTon extends ProviderTonBase {
|
|
|
268
327
|
}
|
|
269
328
|
_sendTransaction(request) {
|
|
270
329
|
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
-
|
|
330
|
+
return yield this._callBridge({
|
|
272
331
|
method: 'sendTransaction',
|
|
273
332
|
params: [request],
|
|
274
333
|
});
|
|
275
|
-
return Buffer.from(txid, 'hex');
|
|
276
334
|
});
|
|
277
335
|
}
|
|
278
336
|
_signData(request) {
|
|
@@ -48,6 +48,7 @@ function createTonProvider(originalProvider, customDeviceInfo, customWalletInfo)
|
|
|
48
48
|
},
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
|
+
// Doc: https://github.com/ton-blockchain/ton-connect/blob/main/requests-responses.md
|
|
51
52
|
class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
52
53
|
constructor(props) {
|
|
53
54
|
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout: props.timeout }) }));
|
|
@@ -66,7 +67,6 @@ class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
|
66
67
|
};
|
|
67
68
|
this.protocolVersion = 2;
|
|
68
69
|
this.isWalletBrowser = false;
|
|
69
|
-
this._id = 0;
|
|
70
70
|
// void this._getDeviceInfo();
|
|
71
71
|
this._registerEvents();
|
|
72
72
|
}
|
|
@@ -159,7 +159,7 @@ class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
|
159
159
|
}
|
|
160
160
|
_connect(protocolVersion, message) {
|
|
161
161
|
return __awaiter(this, void 0, void 0, function* () {
|
|
162
|
-
const id =
|
|
162
|
+
const id = Date.now();
|
|
163
163
|
const isGetTonAddr = !message || (message && message.items.some((item) => item.name === 'ton_addr'));
|
|
164
164
|
const proofItem = message &&
|
|
165
165
|
message.items.find((item) => item.name === 'ton_proof');
|
|
@@ -169,10 +169,26 @@ class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
|
169
169
|
items.push(Object.assign({ name: 'ton_addr' }, this._accountInfo));
|
|
170
170
|
}
|
|
171
171
|
else {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
172
|
+
let result;
|
|
173
|
+
try {
|
|
174
|
+
result = yield this._callBridge({
|
|
175
|
+
method: 'connect',
|
|
176
|
+
params: protocolVersion && message ? [protocolVersion, message] : [],
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
const { code } = error;
|
|
181
|
+
if (code === 4001) {
|
|
182
|
+
return {
|
|
183
|
+
event: 'connect_error',
|
|
184
|
+
id,
|
|
185
|
+
payload: {
|
|
186
|
+
code: protocol_1.CONNECT_EVENT_ERROR_CODES.USER_REJECTS_ERROR,
|
|
187
|
+
message: types_1.ConnectEventErrorMessage.USER_DECLINED,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
176
192
|
if (!result) {
|
|
177
193
|
return {
|
|
178
194
|
event: 'connect_error',
|
|
@@ -188,14 +204,30 @@ class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
|
188
204
|
}
|
|
189
205
|
}
|
|
190
206
|
if (proofItem) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
207
|
+
let result;
|
|
208
|
+
try {
|
|
209
|
+
result = yield this._callBridge({
|
|
210
|
+
method: 'signProof',
|
|
211
|
+
params: [
|
|
212
|
+
{
|
|
213
|
+
payload: proofItem.payload,
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
const { code } = error;
|
|
220
|
+
if (code === 4001) {
|
|
221
|
+
return {
|
|
222
|
+
event: 'connect_error',
|
|
223
|
+
id,
|
|
224
|
+
payload: {
|
|
225
|
+
code: protocol_1.CONNECT_EVENT_ERROR_CODES.USER_REJECTS_ERROR,
|
|
226
|
+
message: types_1.ConnectEventErrorMessage.USER_DECLINED,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
199
231
|
if (!result) {
|
|
200
232
|
return {
|
|
201
233
|
event: 'connect_error',
|
|
@@ -227,34 +259,61 @@ class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
|
227
259
|
restoreConnection() {
|
|
228
260
|
return this._connect();
|
|
229
261
|
}
|
|
262
|
+
convertError(id, error) {
|
|
263
|
+
const { code, message } = error;
|
|
264
|
+
if (code === 4001) {
|
|
265
|
+
return {
|
|
266
|
+
id,
|
|
267
|
+
error: {
|
|
268
|
+
code: protocol_1.SEND_TRANSACTION_ERROR_CODES.USER_REJECTS_ERROR,
|
|
269
|
+
message: types_1.ConnectEventErrorMessage.USER_DECLINED,
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
return {
|
|
275
|
+
id,
|
|
276
|
+
error: {
|
|
277
|
+
code: protocol_1.SEND_TRANSACTION_ERROR_CODES.UNKNOWN_APP_ERROR,
|
|
278
|
+
message: message !== null && message !== void 0 ? message : '',
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
}
|
|
230
283
|
send(message) {
|
|
231
284
|
return __awaiter(this, void 0, void 0, function* () {
|
|
232
285
|
const id = message.id;
|
|
233
286
|
let res;
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
287
|
+
try {
|
|
288
|
+
const params = message.params.map((p) => {
|
|
289
|
+
if (typeof p === 'string') {
|
|
290
|
+
return JSON.parse(p);
|
|
291
|
+
}
|
|
292
|
+
return p;
|
|
293
|
+
});
|
|
294
|
+
if (message.method === 'sendTransaction') {
|
|
295
|
+
res = yield this._sendTransaction(params[0]);
|
|
296
|
+
}
|
|
297
|
+
else if (message.method === 'signData') {
|
|
298
|
+
res = yield this._signData(params[0]);
|
|
299
|
+
}
|
|
300
|
+
else if (message.method === 'disconnect') {
|
|
301
|
+
yield this._disconnect();
|
|
302
|
+
// @ts-expect-error
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
return {
|
|
307
|
+
id,
|
|
308
|
+
error: {
|
|
309
|
+
code: protocol_1.SEND_TRANSACTION_ERROR_CODES.METHOD_NOT_SUPPORTED,
|
|
310
|
+
message: types_1.SendTransactionErrorMessage.METHOD_NOT_SUPPORTED,
|
|
311
|
+
},
|
|
312
|
+
};
|
|
237
313
|
}
|
|
238
|
-
return p;
|
|
239
|
-
});
|
|
240
|
-
if (message.method === 'sendTransaction') {
|
|
241
|
-
res = yield this._sendTransaction(params[0]);
|
|
242
|
-
}
|
|
243
|
-
else if (message.method === 'signData') {
|
|
244
|
-
res = yield this._signData(params[0]);
|
|
245
|
-
}
|
|
246
|
-
else if (message.method === 'disconnect') {
|
|
247
|
-
yield this._disconnect();
|
|
248
|
-
res = '';
|
|
249
314
|
}
|
|
250
|
-
|
|
251
|
-
return
|
|
252
|
-
id,
|
|
253
|
-
error: {
|
|
254
|
-
code: protocol_1.SEND_TRANSACTION_ERROR_CODES.METHOD_NOT_SUPPORTED,
|
|
255
|
-
message: types_1.SendTransactionErrorMessage.METHOD_NOT_SUPPORTED,
|
|
256
|
-
},
|
|
257
|
-
};
|
|
315
|
+
catch (error) {
|
|
316
|
+
return this.convertError(id, error);
|
|
258
317
|
}
|
|
259
318
|
if (res === undefined) {
|
|
260
319
|
return {
|
|
@@ -273,11 +332,10 @@ class ProviderTon extends ProviderTonBase_1.ProviderTonBase {
|
|
|
273
332
|
}
|
|
274
333
|
_sendTransaction(request) {
|
|
275
334
|
return __awaiter(this, void 0, void 0, function* () {
|
|
276
|
-
|
|
335
|
+
return yield this._callBridge({
|
|
277
336
|
method: 'sendTransaction',
|
|
278
337
|
params: [request],
|
|
279
338
|
});
|
|
280
|
-
return Buffer.from(txid, 'hex');
|
|
281
339
|
});
|
|
282
340
|
}
|
|
283
341
|
_signData(request) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/onekey-ton-provider",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.10",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"cross-inpage-provider"
|
|
6
6
|
],
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"start": "tsc --watch"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@onekeyfe/cross-inpage-provider-core": "2.2.
|
|
32
|
-
"@onekeyfe/cross-inpage-provider-errors": "2.2.
|
|
33
|
-
"@onekeyfe/cross-inpage-provider-types": "2.2.
|
|
34
|
-
"@onekeyfe/extension-bridge-injected": "2.2.
|
|
31
|
+
"@onekeyfe/cross-inpage-provider-core": "2.2.10",
|
|
32
|
+
"@onekeyfe/cross-inpage-provider-errors": "2.2.10",
|
|
33
|
+
"@onekeyfe/cross-inpage-provider-types": "2.2.10",
|
|
34
|
+
"@onekeyfe/extension-bridge-injected": "2.2.10",
|
|
35
35
|
"@tonconnect/protocol": "^2.2.6"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "1a20b392cb23a7ed6704d11caffcf4f41c723960"
|
|
38
38
|
}
|