Skip to content

mois-ilya/ton-sign-data-reference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TON Connect Sign Data

Reference implementation of TON Connect sign-data protocol in TypeScript.

Features

  • Sign and verify text messages
  • Sign and verify binary data
  • Sign and verify cell data with TL-B schemas

Usage

Text Data Signing

import { signData, verifySignData } from './src';
import { mnemonicToPrivateKey } from '@ton/crypto';

// Get key pair from your wallet
const keyPair = await mnemonicToPrivateKey(mnemonic);

const signedData = signData({
    payload: {
        type: 'text',
        text: 'Hello, TON!',
    },
    domain: 'app.example.com',
    privateKey: keyPair.secretKey,
    address: 'UQC...fx',
});

const isValid = verifySignData({
    signedData,
    publicKey: keyPair.publicKey,
});

Binary Data Signing

const signedData = signData({
    payload: {
        type: 'binary',
        bytes: Buffer.from('Binary Data').toString('base64'),
    },
    domain: 'app.example.com',
    privateKey: keyPair.secretKey,
    address: 'UQC...fx',
});

TON Cell Signing

import { beginCell, Address } from '@ton/core';

const cell = beginCell()
    .storeUint(0x123, 32) // transaction op-code
    .storeCoins(1_000_000_000n) // amount: 1 TON
    .storeAddress(Address.parse(address)) // recipient address
    .storeStringTail('Transfer 1 TON') // transaction comment
    .endCell();

const signedData = signData({
    payload: {
        type: 'cell',
        schema: 'transfer#123 amount:Coins to:MsgAddress comment:string = Transfer',
        cell: cell.toBoc().toString('base64'),
    },
    domain: 'app.example.com',
    privateKey: keyPair.secretKey,
    address: 'UQC...fx',
});

Development

# Install dependencies
npm install

# Run example
npm run dev

# Run tests
npm test

Testing

The project uses Vitest for testing. Tests cover all three types of payloads and various edge cases:

  • Text message signing and verification
  • Binary data signing and verification
  • TON Cell signing and verification
  • Invalid signature cases
  • Message tampering detection

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

7 stars

Watchers

3 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors