Skip to main content

Connect Nightly

This part of the documentation assumes you already have access to the Nightly Canton

provider. :::


To view the implementation of the Canton adapter used below, visit our source code.

Source code: https://github.com/nightly-labs/canton-web3-template/blob/main/app/misc/adapter.ts​


Nightly exposes the Canton provider at window.nightly.canton. We recommend using the adapter pattern from the Canton template to handle connection lifecycle callbacks and reuse the same instance across your app.


Connect​

First, initialize the adapter with your app metadata and callbacks, then call connect().

Types​

export interface CantonAccount {
partyId: string
/** Base64-encoded public key */
publicKey: string
}

export type SignRequestResponse =
| { type: 'sign_request_approved'; data: { /** Base64-encoded signature */ signature?: string } }
| { type: 'sign_request_rejected'; data: { reason: string } }
| { type: 'sign_request_error'; data: { error: string } }

export interface CantonWallet {
partyId: string
/** Base64-encoded public key */
publicKey: string
signMessage: (message: string, onResponse: (response: SignRequestResponse) => void) => void
getPendingTransactions: () => Promise<unknown[] | null>
getHoldingUtxos: () => Promise<unknown[] | null>
}

export type CantonConnectResult = CantonWallet | null
import adapter from '../misc/adapter'
import type { CantonWallet } from '../misc/adapter'

adapter.init({
appName: 'My Canton dApp',
iconUrl: 'https://docs.nightly.app/img/logo.png',
network: 'mainnet',
onAccept: (wallet: CantonWallet) => {
console.log('Connected party:', wallet.partyId)
},
onReject: () => {
console.log('Connection rejected')
},
onDisconnect: () => {
console.log('Disconnected')
}
})

const wallet: CantonWallet | null = await adapter.connect()

The wallet object contains partyId, publicKey, signMessage() and RPC helpers like getPendingTransactions() and getHoldingUtxos().


Direct connect (without adapter)​

type CantonAccount = {
partyId: string
/** Base64-encoded public key */
publicKey: string
}

const nightly = window.nightly?.canton

const account: CantonAccount | null = nightly ? await nightly.connect() : null

Disconnect​

await adapter.disconnect()