Integrate with Slope Wallet on Solana (Extension)

Establishing a Connection

In order to start interacting with Slope you must first establish a connection. This connection request will prompt the user for permission to share their public key, and indicate that they are willing to interact further.

Once permission is established the first time, the web application's domain will be whitelisted for future connection requests. The user may also indicate that they are willing to auto-approve certain transactions from the application as well.

Similarly, it is possible to terminate the connection both on the application and the user side.

Connecting

The easiest way is to create an object of Slope: new window.Slope(), and then call the method connect().

The call will return a promise that will be resolved when the user accepts or declines the connection request, and when the popup is closed. When msg === 'ok' it means that the user accepted the request.

try {
    const slope = new window.Slope()
    const { msg, data } = await slope.connect()
    
    if (msg === 'ok') {
        console.log(data.publicKey)
        // EFf1fmV6RL6EC7Z45CtaG1iNCPhxsUzJEWsKJJqjjSm8
    } else {
        console.log(msg)
        // User rejected the request.
    }
} catch (error) {
    // Slope is not installed or other errors.
}

Alternatively, the more recommended way is to use Solana's official wallet adapter solana-labs/wallet-adapter. This adapter provides a much better API.

Disconnecting

Disconnecting is similar to connecting.

const slope = new window.Slope()
slope.disconnect()

Sending a Transaction

Once the web application is connected to Slope, it can send transactions on behalf of the user, with the user's permission.

In order to send a transaction, the web application must:

  • Create an unsigned transaction or transactions.

  • Have it be signed by the user's Slope wallet.

  • Send it to a Solana node for processing.

Signing a Transaction

Once a transaction is created, the web application may ask the user's Slope wallet to sign the transaction using their account's private key. By far the easiest way of doing this is by using the signTransaction method on the provider.

import { Transaction, Connection, PublicKey } from '@solana/web3.js'
import bs58 from 'bs58'

const tx = new Transaction()

const slope = new window.Slope()
const { msg, data } = await slope.signTransaction(
    bs58.encode(tx.serializeMessage())
)

if (msg === 'ok') {
    tx.addSignature(
        new PublicKey(data.publicKey),
        bs58.decode(data.signature)
    )
    
    const conn = new Connection('<NETWORK_URL>')
    const txid = await conn.sendRawTransaction(tx.serialize())
}

Signing Multiple Transactions

It is also possible to sign and send multiple transactions at once. This is exposed through the signAllTransactions method on the provider.

const messages = transactions.map(tx => bs58.encode(tx.serializeMessage()))
const { msg, data } = await slope.signAllTransactions(messages)

Signing a Message

When the web application is connected to Slope, it can also request that the user signs a given message.

In order to send a message for the user to sign, the web application must:

  • Provide a hex or UTF-8 encoded string as a Uint8Array.

  • Have it be signed by the user's Slope Wallet.

Example of signing a message.

const message = 'Some text'
const encodedMessage = new TextEncoder().encode(message)

const { msg, data } = await slope.signMessage(
    encodedMessage,
    'utf8' // 'utf8' or 'hex'
)
const signedMessage = data.signature

Last updated