Git Product home page Git Product logo

ton-connect's Introduction

TON Connect v1

⚠️ Migrate to version 2

Please go to TON Connect v2 for a newer protocol that offers a seamless bridge API and does not require the app to receive callbacks from the wallet.


Deprecated TON Connect v1 spec

📄 TON Connect v1 Specification

How to run the demo

cd server-example
yarn install
yarn start

Install Ton Connect Server

$ yarn add @tonapps/tonconnect-server

Generate Static Secret

$ npx tonconnect-generate-sk

Put generated static secret to env vars or config

How to use the server API

import { TonConnectServer, AuthRequestTypes } from '@tonapps/tonconnect-server';

// Create a TonConnectServer instance configured with a static secret.
const tonconnect = new TonConnectServer({ 
    staticSecret: process.env.TONCONNECT_SECRET 
});

// When we need to authenticate the user, create an authentication request:
const request = tonconnect.createRequest({
    image_url: 'https://ddejfvww7sqtk.cloudfront.net/images/landing/ton-nft-tegro-dog/avatar/image_d0315e1461.jpg',
    callback_url: `${hostname}/tonconnect`,
    items: [{
        type: AuthRequestTypes.ADDRESS,
        required: true
    }, {
        type: AuthRequestTypes.OWNERSHIP,
        required: true
    }],
});


res.send(request);
 
// Example: Tonkeeper deeplink:
// Provide the user with the URL to download that request.
const requestURL = `example.com/myrequest`;
const deeplinkURL = `https://app.tonkeeper.com/ton-login/${requestURL}`;

Decode Auth Response

try {
    const response = tonconnect.decodeResponse(req.query.tonlogin);
    
    console.log('response', response);
    
    for (let payload of response.payload) {
        switch (payload.type) {
            case AuthRequestTypes.OWNERSHIP: 
                const isVerified = await tonconnect.verifyTonOwnership(payload, response.client_id);

                if (isVerified) {
                    console.log(`ton-ownership is verified for ${payload.address}`);
                } else {
                    console.log(`ton-ownership is NOT verified`);
                }

                break;
            case AuthRequestTypes.ADDRESS: 
                console.log(`ton-address ${payload.address}`);
                break;
        }
    }
} catch (err) {
    console.log(err);
}

AuthPayload specification

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.