Skip to main content

Establishing a Connection

First of all, in order to start using Nightly extension, an app needs to establish a connection. Once the connection request is initiated on the application side, it will prompt the user to provide permission on sharing their public key and accepting further interaction with Nightly.

All methods are listed and available Nightly repo Solana.


Connecting

We recommend calling connect() function, in order to establish connection with Nightly.

  async connect(onDisconnect?: () => void) {
try {
const pk = await this._provider.connect(onDisconnect)
this._publicKey = pk
this._connected = true
return pk
} catch (error) {
throw new Error('User refused connection')
}
}

As soon as the application is connected to Nightly, it will be able to access the connected account's public key, using publicKey() method and prompt the user for additional transactions. Also you can obtain information on connection status by calling connected().

  get publicKey() {
return this._publicKey || DEFAULT_PUBLICKEY
}

get connected() {
return this._connected
}

Disconnecting

If you are willing to terminate connection, the process mirrors the connecting one. Though please keep in mind, that wallet may also initiate disconnection, not only application itself. Due to this fact, it's crucial for applications to gracefully handle a disconnect event.

  async disconnect() {
if (this._publicKey) {
await this._provider.disconnect()
this._publicKey = DEFAULT_PUBLICKEY
this._connected = false
}
}