Connect to App or Mini Wallet

SDK#

Installation and Initialization#

Make sure to update the OKX App to version 6.90.1 or later to start integrating OKX Connect into your DApp can be done using npm:

npm install @okxconnect/solana-provider

Before connecting the wallet, you need to create an object for subsequent wallet connections, transaction submissions, and other operations.

OKXUniversalProvider.init({dappMetaData: {name, icon}})

Request Parameters

  • dappMetaData - object
    • name - string: the name of the application, will not be used as a unique representation.
    • icon - string: URL of the application icon, must be in PNG, ICO, etc. SVG icons are not supported. SVG icons are not supported. It is better to pass a url pointing to a 180x180px PNG icon.

Returns a value

  • OKXUniversalProvider

Example

import { OKXUniversalProvider } from "@okxconnect/universal-provider";

const okxUniversalProvider = await OKXUniversalProvider.init({
    dappMetaData: {
        name: "application name",
        icon: "application icon url"
    },
})

Connect to Wallet#

Connect the wallet to obtain the wallet address, which serves as an identifier and is necessary for signing transactions.

okxUniversalProvider.connect(connectParams: ConnectParams);

Request Parameters

  • connectParams - ConnectParams

    • namespaces - [namespace: string]: ConnectNamespace ; information necessary to request a connection, the key for Solana is “solana” . If any of the requested chains are not supported by the wallet, the wallet will reject the connection;
      • chains: string[]; Chain id information,
      • defaultChain?: string; default chain
    • optionalNamespaces - [namespace: string]: ConnectNamespace; optional information for requesting a connection, the key for Solana is “solana”. If the corresponding chain information is not supported by the wallet, the connection can still be made;
      • chains: string[]; Chain id information.
        • defaultChain?: string; default chain
    • sessionConfig: object
      • redirect: string Jump parameter after successful connection, if it is Mini App in Telegram, here can be set deeplink: "tg://resolve" in Telegram
  • Promise <SessionTypes.Struct | undefined>

    • topic: string; The session identifier;
    • namespaces: Record<string, Namespace>; namespace information for a successful connection;
      • chains: string[]; Chain information for the connection;
      • accounts: string[]; accounts information for the connection;
      • methods: string[]; methods supported by the wallet under the current namespace;
      • defaultChain?: string; default chain for the current session
    • sessionConfig?: SessionConfig
      • dappInfo: object DApp information;
        • name:string
        • icon:string
      • redirect?: string, the redirect parameter after successful connection;

Example

var session = await okxUniversalProvider.connect({
    namespaces: {
        solana: {
            chains: ["solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", // solana mainnet
            //  "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",// solana testnet
            //  "sonic:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",// sonic testnet ;
            ],
        }
    },
    sessionConfig: {
        redirect: "tg://resolve"
    }
})

Determine if the wallet is connected#

Gets whether the wallet is currently connected.

Return Value

  • boolean

Example

okxUniversalProvider.connected();

Sending Signature and Transactions#

This method allows sending messages to the wallet, supporting signatures, transactions.

First create an OKXSolanaProvider object, passing OKXUniversalProvider into the constructor.

import { OKXSolanaProvider } from "@okxconnect/solana-provider/OKXSolanaProvider";
let okxSolanaProvider = new OKXSolanaProvider(okxUniversalProvider)

Signature#

okxSolanaProvider.signMessage(message, chain);

Request Parameters

  • message - string, the message to be signed
  • chain: string, the chain for which the signature is requested, recommended to pass this parameter; mandatory when connecting multiple chains; if a chain is not passed, it will be treated as solana:mainnet or sonic:testnet;

Return Value

  • Promise - object
    • publicKey:string wallet address
    • signature:Uint8Array Signature result.

Signature for single transaction#

okxSolanaProvider.signTransaction(transaction, chain);

Request parameters

  • transaction - Transaction | VersionedTransaction transaction data object.
  • chain: string, the chain for which the signature execution is requested, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; connecting a chain without passing it will be handled according to solana:mainnet or sonic:testnet;

Return value

  • Promise - Transaction | VersionedTransaction signed transaction object.

Signs multiple transactions

okxSolanaProvider.signAllTransactions(transactions, chain);

Request parameters

  • transactions - [Transaction | VersionedTransaction] Array of transaction data objects.
  • chain: string, the chain for which the signature execution is requested, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; connecting a chain without passing it will be handled according to solana:mainnet or sonic:testnet;

Return value

  • Promise - [Transaction | VersionedTransaction] An array of signed transaction objects.

Signs a transaction and broadcasts onchain#

okxSolanaProvider.signAllTransactions(transactions, chain);

**Request Parameters

  • transactions - Transaction | VersionedTransaction transaction data object
  • chain: string, the chain for which the signature execution is requested, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; connecting a chain without passing it will be handled according to solana:mainnet or sonic:testnet;

**Return Value

  • Promise - string transactionhash.

get wallet address and pubKey#

okxSolanaProvider.getAccount(chain);

Request Parameters

  • chain: string, get the chain id of the wallet address, if not passed then the first connected svm address will be taken by default.

Return Value

  • Object
    • address: string wallet address
    • publicKey: PublicKey

Example

// Signing a transfer transaction on solana mainnet
let provider = new OKXSolanaProvider(okxUniversalProvider)
const transaction = new Transaction({
    feePayer: new PublicKey(provider.getAccount().address),
    recentBlockhash: "xNWbUfdEPktMsZQHY6Zk5RJqamWFcTKasekjr7c3wFX",
}).add(SystemProgram.transfer(
    {
        fromPubkey: new PublicKey(provider.getAccount().address),
        toPubkey: new PublicKey(provider.getAccount().address),
        lamports: 1000,
    }
))

let result = await provider.signTransaction(transaction, "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp")


Disconnect wallet#

Disconnect from a connected wallet and delete the current session. If you want to switch wallets, disconnect from the current wallet first.

okxUniversalProvider.disconnect();

Event#

// Generate universalLink
okxUniversalProvider.on('display_uri', (uri) => {
    console.log(uri);
}).
// Session information changes will trigger this event;
okxUniversalProvider.on('session_update', (session) => {
    console.log(JSON.stringify(session)); }); // Session information changes (e.g., adding a custom chain).

// Disconnecting triggers this event;
okxUniversalProvider.on('session_delete', ({topic}) => {
    console.log(topic);
});

Error codes#

Exceptions that may be thrown during connection, transaction, and disconnection.

Exception

Error CodeDescription
OKX_CONNECT_ERROR_CODES.UNKNOWN_ERRORUnknown Error
OKX_CONNECT_ERROR_CODES.ALREADY_CONNECTED_ERRORWallet Already Connected
OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERRORWallet Not Connected
OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERRORUser Rejected
OKX_CONNECT_ERROR_CODES.METHOD_NOT_SUPPORTEDMethod Not Supported
OKX_CONNECT_ERROR_CODES.CHAIN_NOT_SUPPORTEDChain Not Supported
OKX_CONNECT_ERROR_CODES.WALLET_NOT_SUPPORTEDWallet Not Supported
OKX_CONNECT_ERROR_CODES.CONNECTION_ERRORConnection Error
export enum OKX_CONNECT_ERROR_CODES {
    UNKNOWN_ERROR = 0,
    ALREADY_CONNECTED_ERROR = 11,
    NOT_CONNECTED_ERROR = 12,
    USER_REJECTS_ERROR = 300,
    METHOD_NOT_SUPPORTED = 400,
    CHAIN_NOT_SUPPORTED = 500,
    WALLET_NOT_SUPPORTED = 600,
    CONNECTION_ERROR = 700
}