@tonconnect/sdk 3.1.1-beta.0 → 3.2.0-beta.0
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 +3 -0
- package/dist/tonconnect-sdk.min.js +1 -1
- package/dist/tonconnect-sdk.min.js.map +1 -1
- package/lib/cjs/index.cjs +151 -7
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/index.mjs +147 -10
- package/lib/esm/index.mjs.map +1 -1
- package/lib/types/index.d.ts +101 -2
- package/package.json +2 -2
package/lib/cjs/index.cjs
CHANGED
|
@@ -143,8 +143,8 @@ class WalletNotSupportFeatureError extends TonConnectError {
|
|
|
143
143
|
get info() {
|
|
144
144
|
return "Wallet doesn't support requested feature method.";
|
|
145
145
|
}
|
|
146
|
-
constructor(
|
|
147
|
-
super(
|
|
146
|
+
constructor(message, options) {
|
|
147
|
+
super(message, options);
|
|
148
148
|
Object.setPrototypeOf(this, WalletNotSupportFeatureError.prototype);
|
|
149
149
|
}
|
|
150
150
|
}
|
|
@@ -320,6 +320,32 @@ class SendTransactionParser extends RpcParser {
|
|
|
320
320
|
}
|
|
321
321
|
const sendTransactionParser = new SendTransactionParser();
|
|
322
322
|
|
|
323
|
+
const signDataErrors = {
|
|
324
|
+
[protocol.SIGN_DATA_ERROR_CODES.UNKNOWN_ERROR]: UnknownError,
|
|
325
|
+
[protocol.SIGN_DATA_ERROR_CODES.USER_REJECTS_ERROR]: UserRejectsError,
|
|
326
|
+
[protocol.SIGN_DATA_ERROR_CODES.BAD_REQUEST_ERROR]: BadRequestError,
|
|
327
|
+
[protocol.SIGN_DATA_ERROR_CODES.UNKNOWN_APP_ERROR]: UnknownAppError
|
|
328
|
+
};
|
|
329
|
+
class SignDataParser extends RpcParser {
|
|
330
|
+
convertToRpcRequest(payload) {
|
|
331
|
+
return {
|
|
332
|
+
method: 'signData',
|
|
333
|
+
params: [JSON.stringify(payload)]
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
parseAndThrowError(response) {
|
|
337
|
+
let ErrorConstructor = UnknownError;
|
|
338
|
+
if (response.error.code in signDataErrors) {
|
|
339
|
+
ErrorConstructor = signDataErrors[response.error.code] || UnknownError;
|
|
340
|
+
}
|
|
341
|
+
throw new ErrorConstructor(response.error.message);
|
|
342
|
+
}
|
|
343
|
+
convertFromRpcResponse(rpcResponse) {
|
|
344
|
+
return rpcResponse.result;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const signDataParser = new SignDataParser();
|
|
348
|
+
|
|
323
349
|
class HttpBridgeGatewayStorage {
|
|
324
350
|
constructor(storage, bridgeUrl) {
|
|
325
351
|
this.storage = storage;
|
|
@@ -2218,27 +2244,62 @@ class WalletsListManager {
|
|
|
2218
2244
|
function checkSendTransactionSupport(features, options) {
|
|
2219
2245
|
const supportsDeprecatedSendTransactionFeature = features.includes('SendTransaction');
|
|
2220
2246
|
const sendTransactionFeature = findFeature(features, 'SendTransaction');
|
|
2247
|
+
const requiredFeature = {
|
|
2248
|
+
minMessages: options.requiredMessagesNumber,
|
|
2249
|
+
extraCurrencyRequired: options.requireExtraCurrencies
|
|
2250
|
+
};
|
|
2221
2251
|
if (!supportsDeprecatedSendTransactionFeature && !sendTransactionFeature) {
|
|
2222
|
-
throw new WalletNotSupportFeatureError("Wallet doesn't support SendTransaction feature."
|
|
2252
|
+
throw new WalletNotSupportFeatureError("Wallet doesn't support SendTransaction feature.", {
|
|
2253
|
+
cause: { requiredFeature: { featureName: 'SendTransaction', value: requiredFeature } }
|
|
2254
|
+
});
|
|
2223
2255
|
}
|
|
2224
2256
|
if (options.requireExtraCurrencies) {
|
|
2225
2257
|
if (!sendTransactionFeature || !sendTransactionFeature.extraCurrencySupported) {
|
|
2226
|
-
throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Extra currencies support is required
|
|
2258
|
+
throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Extra currencies support is required.`, {
|
|
2259
|
+
cause: {
|
|
2260
|
+
requiredFeature: { featureName: 'SendTransaction', value: requiredFeature }
|
|
2261
|
+
}
|
|
2262
|
+
});
|
|
2227
2263
|
}
|
|
2228
2264
|
}
|
|
2229
2265
|
if (sendTransactionFeature && sendTransactionFeature.maxMessages !== undefined) {
|
|
2230
2266
|
if (sendTransactionFeature.maxMessages < options.requiredMessagesNumber) {
|
|
2231
|
-
throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Max support messages number is ${sendTransactionFeature.maxMessages}, but ${options.requiredMessagesNumber} is required
|
|
2267
|
+
throw new WalletNotSupportFeatureError(`Wallet is not able to handle such SendTransaction request. Max support messages number is ${sendTransactionFeature.maxMessages}, but ${options.requiredMessagesNumber} is required.`, {
|
|
2268
|
+
cause: {
|
|
2269
|
+
requiredFeature: { featureName: 'SendTransaction', value: requiredFeature }
|
|
2270
|
+
}
|
|
2271
|
+
});
|
|
2232
2272
|
}
|
|
2233
2273
|
return;
|
|
2234
2274
|
}
|
|
2235
2275
|
logWarning("Connected wallet didn't provide information about max allowed messages in the SendTransaction request. Request may be rejected by the wallet.");
|
|
2236
2276
|
}
|
|
2277
|
+
function checkSignDataSupport(features, options) {
|
|
2278
|
+
const signDataFeature = features.find(feature => feature && typeof feature === 'object' && feature.name === 'SignData');
|
|
2279
|
+
if (!signDataFeature) {
|
|
2280
|
+
throw new WalletNotSupportFeatureError("Wallet doesn't support SignData feature.", {
|
|
2281
|
+
cause: {
|
|
2282
|
+
requiredFeature: {
|
|
2283
|
+
featureName: 'SignData',
|
|
2284
|
+
value: { types: options.requiredTypes }
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
const unsupportedTypes = options.requiredTypes.filter(requiredType => !signDataFeature.types.includes(requiredType));
|
|
2290
|
+
if (unsupportedTypes.length) {
|
|
2291
|
+
throw new WalletNotSupportFeatureError(`Wallet doesn't support required SignData types: ${unsupportedTypes.join(', ')}.`, {
|
|
2292
|
+
cause: {
|
|
2293
|
+
requiredFeature: { featureName: 'SignData', value: { types: unsupportedTypes } }
|
|
2294
|
+
}
|
|
2295
|
+
});
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2237
2298
|
function checkRequiredWalletFeatures(features, walletsRequiredFeatures) {
|
|
2238
2299
|
if (typeof walletsRequiredFeatures !== 'object') {
|
|
2239
2300
|
return true;
|
|
2240
2301
|
}
|
|
2241
|
-
const { sendTransaction } = walletsRequiredFeatures;
|
|
2302
|
+
const { sendTransaction, signData } = walletsRequiredFeatures;
|
|
2242
2303
|
if (sendTransaction) {
|
|
2243
2304
|
const feature = findFeature(features, 'SendTransaction');
|
|
2244
2305
|
if (!feature) {
|
|
@@ -2248,6 +2309,15 @@ function checkRequiredWalletFeatures(features, walletsRequiredFeatures) {
|
|
|
2248
2309
|
return false;
|
|
2249
2310
|
}
|
|
2250
2311
|
}
|
|
2312
|
+
if (signData) {
|
|
2313
|
+
const feature = findFeature(features, 'SignData');
|
|
2314
|
+
if (!feature) {
|
|
2315
|
+
return false;
|
|
2316
|
+
}
|
|
2317
|
+
if (!checkSignData(feature, signData)) {
|
|
2318
|
+
return false;
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2251
2321
|
return true;
|
|
2252
2322
|
}
|
|
2253
2323
|
function findFeature(features, requiredFeatureName) {
|
|
@@ -2259,6 +2329,9 @@ function checkSendTransaction(feature, requiredFeature) {
|
|
|
2259
2329
|
const correctExtraCurrency = !requiredFeature.extraCurrencyRequired || feature.extraCurrencySupported;
|
|
2260
2330
|
return !!(correctMessagesNumber && correctExtraCurrency);
|
|
2261
2331
|
}
|
|
2332
|
+
function checkSignData(feature, requiredFeature) {
|
|
2333
|
+
return requiredFeature.types.every(requiredType => feature.types.includes(requiredType));
|
|
2334
|
+
}
|
|
2262
2335
|
|
|
2263
2336
|
/**
|
|
2264
2337
|
* Create a request version event.
|
|
@@ -2406,6 +2479,15 @@ function createTransactionSignedEvent(version, wallet, transaction, signedTransa
|
|
|
2406
2479
|
function createTransactionSigningFailedEvent(version, wallet, transaction, errorMessage, errorCode) {
|
|
2407
2480
|
return Object.assign(Object.assign({ type: 'transaction-signing-failed', is_success: false, error_message: errorMessage, error_code: errorCode !== null && errorCode !== void 0 ? errorCode : null }, createConnectionInfo(version, wallet)), createTransactionInfo(wallet, transaction));
|
|
2408
2481
|
}
|
|
2482
|
+
function createDataSentForSignatureEvent(version, wallet, data) {
|
|
2483
|
+
return Object.assign({ type: 'sign-data-request-initiated', data }, createConnectionInfo(version, wallet));
|
|
2484
|
+
}
|
|
2485
|
+
function createDataSignedEvent(version, wallet, data, signedData) {
|
|
2486
|
+
return Object.assign({ type: 'sign-data-request-completed', is_success: true, data, signed_data: signedData }, createConnectionInfo(version, wallet));
|
|
2487
|
+
}
|
|
2488
|
+
function createDataSigningFailedEvent(version, wallet, data, errorMessage, errorCode) {
|
|
2489
|
+
return Object.assign({ type: 'sign-data-request-failed', is_success: false, data, error_message: errorMessage, error_code: errorCode !== null && errorCode !== void 0 ? errorCode : null }, createConnectionInfo(version, wallet));
|
|
2490
|
+
}
|
|
2409
2491
|
/**
|
|
2410
2492
|
* Create a disconnect event.
|
|
2411
2493
|
* @param version
|
|
@@ -2474,6 +2556,9 @@ class BrowserEventDispatcher {
|
|
|
2474
2556
|
* * `transaction-sent-for-signature`: when a user sends a transaction for signature.
|
|
2475
2557
|
* * `transaction-signed`: when a user successfully signs a transaction.
|
|
2476
2558
|
* * `transaction-signing-failed`: when a user cancels transaction signing or there is an error during the signing process.
|
|
2559
|
+
* * `sign-data-request-initiated`: when a user sends data for signature.
|
|
2560
|
+
* * `sign-data-request-completed`: when a user successfully signs data.
|
|
2561
|
+
* * `sign-data-request-failed`: when a user cancels data signing or there is an error during the signing process.
|
|
2477
2562
|
*
|
|
2478
2563
|
* If you want to track user actions, you can subscribe to the window events with prefix `ton-connect-`:
|
|
2479
2564
|
*
|
|
@@ -2674,9 +2759,42 @@ class TonConnectTracker {
|
|
|
2674
2759
|
}
|
|
2675
2760
|
catch (e) { }
|
|
2676
2761
|
}
|
|
2762
|
+
/**
|
|
2763
|
+
* Track sign data init event.
|
|
2764
|
+
* @param args
|
|
2765
|
+
*/
|
|
2766
|
+
trackDataSentForSignature(...args) {
|
|
2767
|
+
try {
|
|
2768
|
+
const event = createDataSentForSignatureEvent(this.version, ...args);
|
|
2769
|
+
this.dispatchUserActionEvent(event);
|
|
2770
|
+
}
|
|
2771
|
+
catch (e) { }
|
|
2772
|
+
}
|
|
2773
|
+
/**
|
|
2774
|
+
* Track sign data success event.
|
|
2775
|
+
* @param args
|
|
2776
|
+
*/
|
|
2777
|
+
trackDataSigned(...args) {
|
|
2778
|
+
try {
|
|
2779
|
+
const event = createDataSignedEvent(this.version, ...args);
|
|
2780
|
+
this.dispatchUserActionEvent(event);
|
|
2781
|
+
}
|
|
2782
|
+
catch (e) { }
|
|
2783
|
+
}
|
|
2784
|
+
/**
|
|
2785
|
+
* Track sign data error event.
|
|
2786
|
+
* @param args
|
|
2787
|
+
*/
|
|
2788
|
+
trackDataSigningFailed(...args) {
|
|
2789
|
+
try {
|
|
2790
|
+
const event = createDataSigningFailedEvent(this.version, ...args);
|
|
2791
|
+
this.dispatchUserActionEvent(event);
|
|
2792
|
+
}
|
|
2793
|
+
catch (e) { }
|
|
2794
|
+
}
|
|
2677
2795
|
}
|
|
2678
2796
|
|
|
2679
|
-
const tonConnectSdkVersion = "3.
|
|
2797
|
+
const tonConnectSdkVersion = "3.2.0-beta.0";
|
|
2680
2798
|
|
|
2681
2799
|
class TonConnect {
|
|
2682
2800
|
constructor(options) {
|
|
@@ -2925,6 +3043,25 @@ class TonConnect {
|
|
|
2925
3043
|
return result;
|
|
2926
3044
|
});
|
|
2927
3045
|
}
|
|
3046
|
+
signData(data, options) {
|
|
3047
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3048
|
+
const abortController = createAbortController(options === null || options === void 0 ? void 0 : options.signal);
|
|
3049
|
+
if (abortController.signal.aborted) {
|
|
3050
|
+
throw new TonConnectError('data sending was aborted');
|
|
3051
|
+
}
|
|
3052
|
+
this.checkConnection();
|
|
3053
|
+
checkSignDataSupport(this.wallet.device.features, { requiredTypes: [data.type] });
|
|
3054
|
+
this.tracker.trackDataSentForSignature(this.wallet, data);
|
|
3055
|
+
const response = yield this.provider.sendRequest(signDataParser.convertToRpcRequest(data));
|
|
3056
|
+
if (signDataParser.isError(response)) {
|
|
3057
|
+
this.tracker.trackDataSigningFailed(this.wallet, data, response.error.message, response.error.code);
|
|
3058
|
+
return signDataParser.parseAndThrowError(response);
|
|
3059
|
+
}
|
|
3060
|
+
const result = signDataParser.convertFromRpcResponse(response);
|
|
3061
|
+
this.tracker.trackDataSigned(this.wallet, data, result);
|
|
3062
|
+
return result;
|
|
3063
|
+
});
|
|
3064
|
+
}
|
|
2928
3065
|
/**
|
|
2929
3066
|
* Disconnect form thw connected wallet and drop current session.
|
|
2930
3067
|
*/
|
|
@@ -3198,6 +3335,10 @@ Object.defineProperty(exports, 'SEND_TRANSACTION_ERROR_CODES', {
|
|
|
3198
3335
|
enumerable: true,
|
|
3199
3336
|
get: function () { return protocol.SEND_TRANSACTION_ERROR_CODES; }
|
|
3200
3337
|
});
|
|
3338
|
+
Object.defineProperty(exports, 'SIGN_DATA_ERROR_CODES', {
|
|
3339
|
+
enumerable: true,
|
|
3340
|
+
get: function () { return protocol.SIGN_DATA_ERROR_CODES; }
|
|
3341
|
+
});
|
|
3201
3342
|
exports.BadRequestError = BadRequestError;
|
|
3202
3343
|
exports.BrowserEventDispatcher = BrowserEventDispatcher;
|
|
3203
3344
|
exports.FetchWalletsError = FetchWalletsError;
|
|
@@ -3222,6 +3363,9 @@ exports.createConnectionRestoringCompletedEvent = createConnectionRestoringCompl
|
|
|
3222
3363
|
exports.createConnectionRestoringErrorEvent = createConnectionRestoringErrorEvent;
|
|
3223
3364
|
exports.createConnectionRestoringStartedEvent = createConnectionRestoringStartedEvent;
|
|
3224
3365
|
exports.createConnectionStartedEvent = createConnectionStartedEvent;
|
|
3366
|
+
exports.createDataSentForSignatureEvent = createDataSentForSignatureEvent;
|
|
3367
|
+
exports.createDataSignedEvent = createDataSignedEvent;
|
|
3368
|
+
exports.createDataSigningFailedEvent = createDataSigningFailedEvent;
|
|
3225
3369
|
exports.createDisconnectionEvent = createDisconnectionEvent;
|
|
3226
3370
|
exports.createRequestVersionEvent = createRequestVersionEvent;
|
|
3227
3371
|
exports.createResponseVersionEvent = createResponseVersionEvent;
|