Skip to main content
Initialize WalletKit before adding an events handler. See the initialization guide for details.

Connection

Before receiving and handling wallet events, set up an events handler and establish a connection with a dApp.

Setting up events handler

Create your own implementation of an events handler and add it to the WalletKit instance:
import TONWalletKit

class YourCustomWalletEventsHandler: TONBridgeEventsHandler {

    func handle(event: TONWalletKitEvent) throws {
        // Process the event or throw an error
    }
}

let eventsHandler = YourCustomWalletEventsHandler()
try await walletKit.add(eventsHandler: eventsHandler)
If a handler is not needed anymore or needs to be replaced by some other handler, remove it:
try await walletKit.remove(eventsHandler: eventsHandler)

Establishing connection with dApp

To establish a connection with a dApp, one needs a connection request URL from any source: copy/paste, QR code, deep link, etc. Send it to the WalletKit to initiate a connection request.
try await walletKit.connect(url: /* connection url */)
Once the connection request is fired, the corresponding event will be sent to your event handler. The connection event contains an object you can use to display request information to your app’s users and to approve (or reject) the request to complete connection establishment.
class YourCustomWalletEventsHandler: TONBridgeEventsHandler {

    func handle(event: TONWalletKitEvent) throws {
        switch event {
        case .connectRequest(let request):
            // Send request object to appropriate screen
            // to display request information to app user
            break
        default: ()
        }
    }
}

// Call an appropriate method corresponding to the user's response

// To approve
try await request.approve(walletAddress: /* TON wallet address */)

// To reject
try await request.reject(reason: /* rejection reason */)
After approval, the connection will be established, and the wallet service will be able to receive and handle other events associated with the corresponding TON wallet.

Handling events

All event handling is made through a custom event handler. Received events carry a lot of useful information that can be displayed to the user to allow them to approve or reject the request based on their action. The only exception is a disconnection request. Even though it contains information you can show to a user, there is no need to approve or reject it on their end.

Transaction event handling

class YourCustomWalletEventsHandler: TONBridgeEventsHandler {

    func handle(event: TONWalletKitEvent) throws {
        switch event {
        case .transactionRequest(let request):
            // Send request object to appropriate screen
            // to display request information to app user
            break
        default: ()
        }
    }
}

// Call an appropriate method corresponding to the user's response

// To approve
try await request.approve()

// To reject
try await request.reject(reason: /* rejection reason */)

Sign data event handling

class YourCustomWalletEventsHandler: TONBridgeEventsHandler {

    func handle(event: TONWalletKitEvent) throws {
        switch event {
        case .signDataRequest(let request):
            // Send request object to appropriate screen
            // to display request information to app user
            break
        default: ()
        }
    }
}

// Call an appropriate method corresponding to the user's response

// To approve
try await request.approve()

// To reject
try await request.reject(reason: /* rejection reason */)

Disconnection event handling

class YourCustomWalletEventsHandler: TONBridgeEventsHandler {

    func handle(event: TONWalletKitEvent) throws {
        switch event {
        case .disconnect(let info):
            // Send info object to appropriate screen
            // to display request information to app user
            break
        default: ()
        }
    }
}
Alternatively, explore the complete demo wallet with WalletKit integration:

Demo wallet, GitHub