@tonconnect/sdk 3.0.2 → 3.0.3-beta.1

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.
@@ -18,6 +18,13 @@ export declare interface Account {
18
18
  publicKey?: string;
19
19
  }
20
20
 
21
+ export declare type AddTonConnectPrefix<T extends string> = `ton-connect-${T}` | `ton-connect-ui-${T}`;
22
+
23
+ /**
24
+ * Requested authentication type: 'ton_addr' or 'ton_proof'.
25
+ */
26
+ export declare type AuthType = ConnectItem['name'];
27
+
21
28
  /**
22
29
  * Thrown when request to the wallet contains errors.
23
30
  */
@@ -26,11 +33,53 @@ export declare class BadRequestError extends TonConnectError {
26
33
  constructor(...args: ConstructorParameters<typeof TonConnectError>);
27
34
  }
28
35
 
36
+ /**
37
+ * A concrete implementation of EventDispatcher that dispatches events to the browser window.
38
+ */
39
+ export declare class BrowserEventDispatcher<T extends {
40
+ type: string;
41
+ }> implements EventDispatcher<T> {
42
+ /**
43
+ * The window object, possibly undefined in a server environment.
44
+ * @private
45
+ */
46
+ private readonly window;
47
+ /**
48
+ * Dispatches an event with the given name and details to the browser window.
49
+ * @param eventName - The name of the event to dispatch.
50
+ * @param eventDetails - The details of the event to dispatch.
51
+ * @returns A promise that resolves when the event has been dispatched.
52
+ */
53
+ dispatchEvent<P extends AddTonConnectPrefix<T['type']>>(eventName: P, eventDetails: T & {
54
+ type: RemoveTonConnectPrefix<P>;
55
+ }): Promise<void>;
56
+ /**
57
+ * Adds an event listener to the browser window.
58
+ * @param eventName - The name of the event to listen for.
59
+ * @param listener - The listener to add.
60
+ * @param options - The options for the listener.
61
+ * @returns A function that removes the listener.
62
+ */
63
+ addEventListener<P extends AddTonConnectPrefix<T['type']>>(eventName: P, listener: (event: CustomEvent<T & {
64
+ type: RemoveTonConnectPrefix<P>;
65
+ }>) => void, options?: AddEventListenerOptions): Promise<() => void>;
66
+ }
67
+
29
68
  export declare enum CHAIN {
30
69
  MAINNET = "-239",
31
70
  TESTNET = "-3"
32
71
  }
33
72
 
73
+ export declare enum CONNECT_EVENT_ERROR_CODES {
74
+ UNKNOWN_ERROR = 0,
75
+ BAD_REQUEST_ERROR = 1,
76
+ MANIFEST_NOT_FOUND_ERROR = 2,
77
+ MANIFEST_CONTENT_ERROR = 3,
78
+ UNKNOWN_APP_ERROR = 100,
79
+ USER_REJECTS_ERROR = 300,
80
+ METHOD_NOT_SUPPORTED = 400
81
+ }
82
+
34
83
  export declare enum CONNECT_ITEM_ERROR_CODES {
35
84
  UNKNOWN_ERROR = 0,
36
85
  METHOD_NOT_SUPPORTED = 400
@@ -43,6 +92,157 @@ export declare interface ConnectAdditionalRequest {
43
92
  tonProof?: string;
44
93
  }
45
94
 
95
+ /**
96
+ * Successful connection event when a user successfully connected a wallet.
97
+ */
98
+ export declare type ConnectionCompletedEvent = {
99
+ /**
100
+ * Event type.
101
+ */
102
+ type: 'connection-completed';
103
+ /**
104
+ * Connection success flag.
105
+ */
106
+ is_success: true;
107
+ } & ConnectionInfo;
108
+
109
+ /**
110
+ * Connection error event when a user cancels a connection or there is an error during the connection process.
111
+ */
112
+ export declare type ConnectionErrorEvent = {
113
+ /**
114
+ * Event type.
115
+ */
116
+ type: 'connection-error';
117
+ /**
118
+ * Connection success flag.
119
+ */
120
+ is_success: false;
121
+ /**
122
+ * Reason for the error.
123
+ */
124
+ error_message: string;
125
+ /**
126
+ * Error code.
127
+ */
128
+ error_code: CONNECT_EVENT_ERROR_CODES | null;
129
+ /**
130
+ * Custom data for the connection.
131
+ */
132
+ custom_data: Version;
133
+ };
134
+
135
+ /**
136
+ * Connection events.
137
+ */
138
+ export declare type ConnectionEvent = ConnectionStartedEvent | ConnectionCompletedEvent | ConnectionErrorEvent;
139
+
140
+ /**
141
+ * Information about a connected wallet.
142
+ */
143
+ export declare type ConnectionInfo = {
144
+ /**
145
+ * Connected wallet address.
146
+ */
147
+ wallet_address: string | null;
148
+ /**
149
+ * Wallet type: 'tonkeeper', 'tonhub', etc.
150
+ */
151
+ wallet_type: string | null;
152
+ /**
153
+ * Wallet version.
154
+ */
155
+ wallet_version: string | null;
156
+ /**
157
+ * Requested authentication types.
158
+ */
159
+ auth_type: AuthType;
160
+ /**
161
+ * Custom data for the connection.
162
+ */
163
+ custom_data: {
164
+ /**
165
+ * Connected chain ID.
166
+ */
167
+ chain_id: string | null;
168
+ /**
169
+ * Wallet provider.
170
+ */
171
+ provider: 'http' | 'injected' | null;
172
+ } & Version;
173
+ };
174
+
175
+ /**
176
+ * Connection restoring completed event when successfully restored a connection.
177
+ */
178
+ export declare type ConnectionRestoringCompletedEvent = {
179
+ /**
180
+ * Event type.
181
+ */
182
+ type: 'connection-restoring-completed';
183
+ /**
184
+ * Connection success flag.
185
+ */
186
+ is_success: true;
187
+ } & ConnectionInfo;
188
+
189
+ /**
190
+ * Connection restoring error event when there is an error during the connection restoring process.
191
+ */
192
+ export declare type ConnectionRestoringErrorEvent = {
193
+ /**
194
+ * Event type.
195
+ */
196
+ type: 'connection-restoring-error';
197
+ /**
198
+ * Connection success flag.
199
+ */
200
+ is_success: false;
201
+ /**
202
+ * Reason for the error.
203
+ */
204
+ error_message: string;
205
+ /**
206
+ * Custom data for the connection.
207
+ */
208
+ custom_data: Version;
209
+ };
210
+
211
+ /**
212
+ * Connection restoring events.
213
+ */
214
+ export declare type ConnectionRestoringEvent = ConnectionRestoringStartedEvent | ConnectionRestoringCompletedEvent | ConnectionRestoringErrorEvent;
215
+
216
+ /**
217
+ * Connection restoring started event when initiates a connection restoring process.
218
+ */
219
+ export declare type ConnectionRestoringStartedEvent = {
220
+ /**
221
+ * Event type.
222
+ */
223
+ type: 'connection-restoring-started';
224
+ /**
225
+ * Custom data for the connection.
226
+ */
227
+ custom_data: Version;
228
+ };
229
+
230
+ /**
231
+ * Initial connection event when a user initiates a connection.
232
+ */
233
+ export declare type ConnectionStartedEvent = {
234
+ /**
235
+ * Event type.
236
+ */
237
+ type: 'connection-started';
238
+ /**
239
+ * Custom data for the connection.
240
+ */
241
+ custom_data: Version;
242
+ };
243
+
244
+ export declare type ConnectItem = TonAddressItem | TonProofItem;
245
+
46
246
  export declare type ConnectItemReplyError<T> = {
47
247
  name: T;
48
248
  error: {
@@ -51,6 +251,98 @@ export declare type ConnectItemReplyError<T> = {
51
251
  };
52
252
  };
53
253
 
254
+ /**
255
+ * Create a connection completed event.
256
+ * @param version
257
+ * @param wallet
258
+ */
259
+ export declare function createConnectionCompletedEvent(version: Version, wallet: Wallet | null): ConnectionCompletedEvent;
260
+
261
+ /**
262
+ * Create a connection error event.
263
+ * @param version
264
+ * @param error_message
265
+ * @param errorCode
266
+ */
267
+ export declare function createConnectionErrorEvent(version: Version, error_message: string, errorCode: CONNECT_EVENT_ERROR_CODES | void): ConnectionErrorEvent;
268
+
269
+ /**
270
+ * Create a connection restoring completed event.
271
+ * @param version
272
+ * @param wallet
273
+ */
274
+ export declare function createConnectionRestoringCompletedEvent(version: Version, wallet: Wallet | null): ConnectionRestoringCompletedEvent;
275
+
276
+ /**
277
+ * Create a connection restoring error event.
278
+ * @param version
279
+ * @param errorMessage
280
+ */
281
+ export declare function createConnectionRestoringErrorEvent(version: Version, errorMessage: string): ConnectionRestoringErrorEvent;
282
+
283
+ /**
284
+ * Create a connection restoring started event.
285
+ */
286
+ export declare function createConnectionRestoringStartedEvent(version: Version): ConnectionRestoringStartedEvent;
287
+
288
+ /**
289
+ * Create a connection init event.
290
+ */
291
+ export declare function createConnectionStartedEvent(version: Version): ConnectionStartedEvent;
292
+
293
+ /**
294
+ * Create a disconnect event.
295
+ * @param version
296
+ * @param wallet
297
+ * @param scope
298
+ * @returns
299
+ */
300
+ export declare function createDisconnectionEvent(version: Version, wallet: Wallet | null, scope: 'dapp' | 'wallet'): DisconnectionEvent;
301
+
302
+ /**
303
+ * Create a request version event.
304
+ */
305
+ export declare function createRequestVersionEvent(): RequestVersionEvent;
306
+
307
+ /**
308
+ * Create a response version event.
309
+ * @param version
310
+ */
311
+ export declare function createResponseVersionEvent(version: string): ResponseVersionEvent;
312
+
313
+ /**
314
+ * Create a transaction init event.
315
+ * @param version
316
+ * @param wallet
317
+ * @param transaction
318
+ */
319
+ export declare function createTransactionSentForSignatureEvent(version: Version, wallet: Wallet | null, transaction: SendTransactionRequest): TransactionSentForSignatureEvent;
320
+
321
+ /**
322
+ * Create a transaction signed event.
323
+ * @param version
324
+ * @param wallet
325
+ * @param transaction
326
+ * @param signedTransaction
327
+ */
328
+ export declare function createTransactionSignedEvent(version: Version, wallet: Wallet | null, transaction: SendTransactionRequest, signedTransaction: SendTransactionResponse): TransactionSignedEvent;
329
+
330
+ /**
331
+ * Create a transaction error event.
332
+ * @param version
333
+ * @param wallet
334
+ * @param transaction
335
+ * @param errorMessage
336
+ * @param errorCode
337
+ */
338
+ export declare function createTransactionSigningFailedEvent(version: Version, wallet: Wallet | null, transaction: SendTransactionRequest, errorMessage: string, errorCode: SEND_TRANSACTION_ERROR_CODES | void): TransactionSigningFailedEvent;
339
+
340
+ /**
341
+ * Create a version info.
342
+ * @param version
343
+ */
344
+ export declare function createVersionInfo(version: Version): Version;
345
+
54
346
  export declare interface DappMetadata {
55
347
  /**
56
348
  * Dapp name. Might be simple, will not be used as identifier.
@@ -78,8 +370,48 @@ export declare interface DeviceInfo {
78
370
  features: Feature[];
79
371
  }
80
372
 
373
+ /**
374
+ * Disconnect event when a user initiates a disconnection.
375
+ */
376
+ export declare type DisconnectionEvent = {
377
+ /**
378
+ * Event type.
379
+ */
380
+ type: 'disconnection';
381
+ /**
382
+ * Disconnect scope: 'dapp' or 'wallet'.
383
+ */
384
+ scope: 'dapp' | 'wallet';
385
+ } & ConnectionInfo;
386
+
81
387
  export declare function encodeTelegramUrlParameters(parameters: string): string;
82
388
 
389
+ /**
390
+ * Interface for an event dispatcher that sends events.
391
+ */
392
+ export declare interface EventDispatcher<T extends {
393
+ type: string;
394
+ }> {
395
+ /**
396
+ * Dispatches an event with the given name and details.
397
+ * @param eventName - The name of the event to dispatch.
398
+ * @param eventDetails - The details of the event to dispatch.
399
+ */
400
+ dispatchEvent<P extends AddTonConnectPrefix<T['type']>>(eventName: P, eventDetails: T & {
401
+ type: RemoveTonConnectPrefix<P>;
402
+ }): Promise<void>;
403
+ /**
404
+ * Adds an event listener.
405
+ * @param eventName - The name of the event to listen for.
406
+ * @param listener - The listener to add.
407
+ * @param options - The options for the listener.
408
+ * @returns A function that removes the listener.
409
+ */
410
+ addEventListener<P extends AddTonConnectPrefix<T['type']>>(eventName: P, listener: (event: CustomEvent<T & {
411
+ type: RemoveTonConnectPrefix<P>;
412
+ }>) => void, options?: AddEventListenerOptions): Promise<() => void>;
413
+ }
414
+
83
415
  export declare type Feature = SendTransactionFeatureDeprecated | SendTransactionFeature | SignDataFeature;
84
416
 
85
417
  /**
@@ -228,6 +560,48 @@ export declare class ParseHexError extends TonConnectError {
228
560
  constructor(...args: ConstructorParameters<typeof TonConnectError>);
229
561
  }
230
562
 
563
+ /**
564
+ * Removes the `ton-connect-` and `ton-connect-ui-` prefixes from the given string.
565
+ */
566
+ export declare type RemoveTonConnectPrefix<T> = T extends `ton-connect-ui-${infer Rest}` ? Rest : T extends `ton-connect-${infer Rest}` ? Rest : T;
567
+
568
+ /**
569
+ * Request TON Connect UI version.
570
+ */
571
+ export declare type RequestVersionEvent = {
572
+ /**
573
+ * Event type.
574
+ */
575
+ type: 'request-version';
576
+ };
577
+
578
+ /**
579
+ * Response TON Connect UI version.
580
+ */
581
+ export declare type ResponseVersionEvent = {
582
+ /**
583
+ * Event type.
584
+ */
585
+ type: 'response-version';
586
+ /**
587
+ * TON Connect UI version.
588
+ */
589
+ version: string;
590
+ };
591
+
592
+ /**
593
+ * User action events.
594
+ */
595
+ export declare type SdkActionEvent = VersionEvent | ConnectionEvent | ConnectionRestoringEvent | DisconnectionEvent | TransactionSigningEvent;
596
+
597
+ export declare enum SEND_TRANSACTION_ERROR_CODES {
598
+ UNKNOWN_ERROR = 0,
599
+ BAD_REQUEST_ERROR = 1,
600
+ UNKNOWN_APP_ERROR = 100,
601
+ USER_REJECTS_ERROR = 300,
602
+ METHOD_NOT_SUPPORTED = 400
603
+ }
604
+
231
605
  export declare type SendTransactionFeature = {
232
606
  name: 'SendTransaction';
233
607
  maxMessages: number;
@@ -282,6 +656,10 @@ export declare type SignDataFeature = {
282
656
  name: 'SignData';
283
657
  };
284
658
 
659
+ export declare interface TonAddressItem {
660
+ name: 'ton_addr';
661
+ }
662
+
285
663
  declare class TonConnect implements ITonConnect {
286
664
  private static readonly walletsList;
287
665
  /**
@@ -298,6 +676,11 @@ declare class TonConnect implements ITonConnect {
298
676
  * Returns available wallets list.
299
677
  */
300
678
  static getWallets(): Promise<WalletInfo[]>;
679
+ /**
680
+ * Emits user action event to the EventDispatcher. By default, it uses `window.dispatchEvent` for browser environment.
681
+ * @private
682
+ */
683
+ private readonly tracker;
301
684
  private readonly walletsList;
302
685
  private readonly dappSettings;
303
686
  private readonly bridgeConnectionStorage;
@@ -419,6 +802,10 @@ export declare interface TonConnectOptions {
419
802
  * Storage to save protocol data. For browser default is `localStorage`. If you use SDK with nodeJS, you have to specify this field.
420
803
  */
421
804
  storage?: IStorage;
805
+ /**
806
+ * Event dispatcher to track user actions. By default, it uses `window.dispatchEvent` for browser environment.
807
+ */
808
+ eventDispatcher?: EventDispatcher<SdkActionEvent>;
422
809
  /**
423
810
  * Redefine wallets list source URL. Must be a link to a json file with [following structure]{@link https://github.com/ton-connect/wallets-list}
424
811
  * @default https://raw.githubusercontent.com/ton-connect/wallets-list/main/wallets.json
@@ -436,6 +823,11 @@ export declare interface TonConnectOptions {
436
823
  disableAutoPauseConnection?: boolean;
437
824
  }
438
825
 
826
+ export declare interface TonProofItem {
827
+ name: 'ton_proof';
828
+ payload: string;
829
+ }
830
+
439
831
  export declare type TonProofItemReply = TonProofItemReplySuccess | TonProofItemReplyError;
440
832
 
441
833
  export declare type TonProofItemReplyError = ConnectItemReplyError<TonProofItemReplySuccess['name']>;
@@ -460,6 +852,93 @@ export declare interface TonProofItemReplySuccess {
460
852
  */
461
853
  export declare function toUserFriendlyAddress(hexAddress: string, testOnly?: boolean): string;
462
854
 
855
+ /**
856
+ * Transaction information.
857
+ */
858
+ export declare type TransactionInfo = {
859
+ /**
860
+ * Transaction validity time in unix timestamp.
861
+ */
862
+ valid_until: string | null;
863
+ /**
864
+ * Sender address.
865
+ */
866
+ from: string | null;
867
+ /**
868
+ * Transaction messages.
869
+ */
870
+ messages: TransactionMessage[];
871
+ };
872
+
873
+ /**
874
+ * Transaction message.
875
+ */
876
+ export declare type TransactionMessage = {
877
+ /**
878
+ * Recipient address.
879
+ */
880
+ address: string | null;
881
+ /**
882
+ * Transfer amount.
883
+ */
884
+ amount: string | null;
885
+ };
886
+
887
+ /**
888
+ * Initial transaction event when a user initiates a transaction.
889
+ */
890
+ export declare type TransactionSentForSignatureEvent = {
891
+ /**
892
+ * Event type.
893
+ */
894
+ type: 'transaction-sent-for-signature';
895
+ } & ConnectionInfo & TransactionInfo;
896
+
897
+ /**
898
+ * Transaction signed event when a user successfully signed a transaction.
899
+ */
900
+ export declare type TransactionSignedEvent = {
901
+ /**
902
+ * Event type.
903
+ */
904
+ type: 'transaction-signed';
905
+ /**
906
+ * Connection success flag.
907
+ */
908
+ is_success: true;
909
+ /**
910
+ * Signed transaction.
911
+ */
912
+ signed_transaction: string;
913
+ } & ConnectionInfo & TransactionInfo;
914
+
915
+ /**
916
+ * Transaction events.
917
+ */
918
+ export declare type TransactionSigningEvent = TransactionSentForSignatureEvent | TransactionSignedEvent | TransactionSigningFailedEvent;
919
+
920
+ /**
921
+ * Transaction error event when a user cancels a transaction or there is an error during the transaction process.
922
+ */
923
+ export declare type TransactionSigningFailedEvent = {
924
+ /**
925
+ * Event type.
926
+ */
927
+ type: 'transaction-signing-failed';
928
+ /**
929
+ * Connection success flag.
930
+ */
931
+ is_success: false;
932
+ /**
933
+ * Reason for the error.
934
+ */
935
+ error_message: string;
936
+ /**
937
+ * Error code.
938
+ */
939
+ error_code: SEND_TRANSACTION_ERROR_CODES | null;
940
+ } & ConnectionInfo & TransactionInfo;
941
+
463
942
  /**
464
943
  * Thrown when app tries to send rpc request to the injected wallet while not connected.
465
944
  */
@@ -483,6 +962,25 @@ export declare class UserRejectsError extends TonConnectError {
483
962
  constructor(...args: ConstructorParameters<typeof TonConnectError>);
484
963
  }
485
964
 
965
+ /**
966
+ * Version of the TON Connect SDK and TON Connect UI.
967
+ */
968
+ export declare type Version = {
969
+ /**
970
+ * TON Connect SDK version.
971
+ */
972
+ ton_connect_sdk_lib: string | null;
973
+ /**
974
+ * TON Connect UI version.
975
+ */
976
+ ton_connect_ui_lib: string | null;
977
+ };
978
+
979
+ /**
980
+ * Version events.
981
+ */
982
+ export declare type VersionEvent = RequestVersionEvent | ResponseVersionEvent;
983
+
486
984
  export declare interface Wallet {
487
985
  /**
488
986
  * Information about user's wallet's device.
@@ -657,6 +1155,11 @@ export declare class WalletsListManager {
657
1155
  private isCorrectWalletConfigDTO;
658
1156
  }
659
1157
 
1158
+ /**
1159
+ * Parameters without version field.
1160
+ */
1161
+ export declare type WithoutVersion<T> = T extends [Version, ...infer Rest] ? [...Rest] : never;
1162
+
660
1163
  /**
661
1164
  * Thrown when passed address is in incorrect format.
662
1165
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonconnect/sdk",
3
- "version": "3.0.2",
3
+ "version": "3.0.3-beta.1",
4
4
  "scripts": {
5
5
  "build": "npx rimraf types-dist && npx rimraf lib && npx rollup -c rollup.config.mjs && ttsc --project tsconfig.declarations.json && api-extractor run && npx rimraf types-dist && npx rimraf dist && npx webpack --config webpack.config.js",
6
6
  "test": "vitest run"
@@ -56,6 +56,7 @@
56
56
  "webpack-cli": "^5.0.0",
57
57
  "rollup": "^3.18.0",
58
58
  "@rollup/plugin-typescript": "^11.0.0",
59
+ "@rollup/plugin-replace": "5.0.5",
59
60
  "vite": "^4.2.1",
60
61
  "vite-tsconfig-paths": "^4.0.5",
61
62
  "vitest": "^0.29.2",