@tonconnect/sdk 3.2.0 → 3.3.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 CHANGED
@@ -247,6 +247,134 @@ try {
247
247
  }
248
248
  ```
249
249
 
250
+ ## Sign data
251
+
252
+ Sign arbitrary data with the user's wallet. The wallet will display the data to the user for confirmation before signing.
253
+
254
+ ### Data Types
255
+
256
+ You can sign three types of data. Choose the right format based on your use case:
257
+
258
+ - **Text** - Use for human-readable text that users should see and understand
259
+ - **Cell** - Use for TON Blockchain data that should be used in smart contracts (wallet may show unknown content warning)
260
+ - **Binary** - For other arbitrary data (least preferred due to security warnings)
261
+
262
+ #### Text Format
263
+
264
+ Use when you need to sign human-readable text. The wallet displays the text to the user.
265
+
266
+ **Parameters:**
267
+ - `type` (string, required): Must be `"text"`
268
+ - `text` (string, required): UTF-8 text to sign
269
+ - `network` (string, optional): `"-239"` for mainnet, `"-3"` for testnet
270
+ - `from` (string, optional): Signer address in raw format `"0:<hex>"`
271
+
272
+ ```ts
273
+ const textData = {
274
+ type: "text",
275
+ text: "Confirm new 2fa number:\n+1 234 567 8901",
276
+ network: "-239", // MAINNET = '-239', TESTNET = '-3'
277
+ from: "0:348bcf827469c5fc38541c77fdd91d4e347eac200f6f2d9fd62dc08885f0415f"
278
+ };
279
+
280
+ try {
281
+ const result = await connector.signData(textData);
282
+ console.log('Signed:', result);
283
+ } catch (e) {
284
+ if (e instanceof UserRejectedError) {
285
+ console.log('User rejected signing');
286
+ } else {
287
+ console.error('Error:', e);
288
+ }
289
+ }
290
+ ```
291
+
292
+ #### Binary Format
293
+
294
+ Use for arbitrary binary data. The wallet shows a warning about unknown content.
295
+
296
+ **Parameters:**
297
+ - `type` (string, required): Must be `"binary"`
298
+ - `bytes` (string, required): Base64 encoded binary data (not url-safe)
299
+ - `network` (string, optional): `"-239"` for mainnet, `"-3"` for testnet
300
+ - `from` (string, optional): Signer address in raw format `"0:<hex>"`
301
+
302
+ ```ts
303
+ const binaryData = {
304
+ type: "binary",
305
+ bytes: "1Z/SGh+3HFMKlVHSkN91DpcCzT4C5jzHT3sA/24C5A==",
306
+ network: "-239", // MAINNET = '-239', TESTNET = '-3'
307
+ from: "0:348bcf827469c5fc38541c77fdd91d4e347eac200f6f2d9fd62dc08885f0415f"
308
+ };
309
+
310
+ try {
311
+ const result = await connector.signData(binaryData);
312
+ console.log('Signed:', result);
313
+ } catch (e) {
314
+ if (e instanceof UserRejectedError) {
315
+ console.log('User rejected signing');
316
+ } else {
317
+ console.error('Error:', e);
318
+ }
319
+ }
320
+ ```
321
+
322
+ #### Cell Format
323
+
324
+ Use for TON Blockchain data with TL-B schema. The wallet can parse and display the content if the schema is valid.
325
+
326
+ **Parameters:**
327
+ - `type` (string, required): Must be `"cell"`
328
+ - `schema` (string, required): TL-B schema of the cell payload
329
+ - `cell` (string, required): Base64 encoded BoC (not url-safe) with single-root cell
330
+ - `network` (string, optional): `"-239"` for mainnet, `"-3"` for testnet
331
+ - `from` (string, optional): Signer address in raw format `"0:<hex>"`
332
+
333
+ ```ts
334
+ const cellData = {
335
+ type: "cell",
336
+ schema: "transfer#0f8a7ea5 query_id:uint64 amount:(VarUInteger 16) destination:MsgAddress response_destination:MsgAddress custom_payload:(Maybe ^Cell) forward_ton_amount:(VarUInteger 16) forward_payload:(Either Cell ^Cell) = InternalMsgBody;",
337
+ cell: "te6ccgEBAQEAVwAAqg+KfqVUbeTvKqB4h0AcnDgIAZucsOi6TLrfP6FcuPKEeTI6oB3fF/NBjyqtdov/KtutACCLqvfmyV9kH+Pyo5lcsrJzJDzjBJK6fd+ZnbFQe4+XggI=",
338
+ network: "-239", // MAINNET = '-239', TESTNET = '-3'
339
+ from: "0:348bcf827469c5fc38541c77fdd91d4e347eac200f6f2d9fd62dc08885f0415f"
340
+ };
341
+
342
+ try {
343
+ const result = await connector.signData(cellData);
344
+ console.log('Signed:', result);
345
+ } catch (e) {
346
+ if (e instanceof UserRejectedError) {
347
+ console.log('User rejected signing');
348
+ } else {
349
+ console.error('Error:', e);
350
+ }
351
+ }
352
+ ```
353
+
354
+ ### Response
355
+
356
+ All signData calls return the same response structure:
357
+
358
+ ```ts
359
+ interface SignDataResult {
360
+ signature: string; // Base64 encoded signature
361
+ address: string; // Wallet address in raw format
362
+ timestamp: number; // UNIX timestamp in seconds (UTC)
363
+ domain: string; // App domain name
364
+ payload: object; // Original payload from the request
365
+ }
366
+ ```
367
+
368
+ ### Signature Verification
369
+
370
+ After receiving the signed data, you need to verify the signature to ensure it's authentic and was signed by the claimed wallet address.
371
+
372
+ **For backend verification:** See [TypeScript verification example](https://github.com/ton-connect/demo-dapp-with-react-ui/blob/master/src/server/services/sign-data-service.ts#L32-L109) showing how to verify signatures on your server.
373
+
374
+ **For smart contract verification:** See [FunC verification example](https://github.com/p0lunin/sign-data-contract-verify-example/blob/master/contracts/sign_data_example.fc) showing how to verify signatures in TON smart contracts.
375
+
376
+ **For complete technical details:** See the [Sign Data specification](https://github.com/ton-blockchain/ton-connect/blob/main/requests-responses.md#sign-data) for full signature verification requirements.
377
+
250
378
  ## Disconnect wallet
251
379
  Note that if you want to change connected wallet, you should disconnect current one firstly. Also make sure that you've waited for `restoreConnection()` promise resolved before call `disconnect()`;
252
380