Connect to App or Mini Wallet

UI#

In addition to the SDK, we also provide a UI interface. If you choose to integrate through the UI, and if the DApp is running inside Telegram, users can choose to stay within Telegram and invoke the mobile App wallet or the OKX Mini Wallet.

Installation and Initialization#

Make sure to update the OKX App to version 6.90.1 or later to begin access:

To integrate OKX Connect into your DApp, you can use npm:

npm install @okxconnect/ui
npm install @okxconnect/solana-provider

Before connecting to a wallet, you need to create an object that can provide a UI interface for subsequent operations such as connecting to the wallet and sending transactions.

OKXUniversalConnectUI.init(dappMetaData, actionsConfiguration, uiPreferences, language)

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 best to pass a url pointing to a 180x180px PNG icon.
  • actionsConfiguration - object
    • modals - ('before' | 'success' | 'error')[] | 'all' The modes of displaying alerts during transaction, defaults to 'before'.
    • returnStrategy -string 'none' | ${string}://${string}; for app wallet, specify the return strategy for the deep link when the user signs/rejects the request, if it is in tg, you can configure tg://resolve
    • tmaReturnUrl -string 'back' | 'none' | ${string}://${string}; For Telegram Mini Wallet, specify the return policy of deep link when user signs/rejects the request, if it's in tg, you can configure tg://resolve none means no processing after signing; default is back;
  • uiPreferences -object
    • theme - Theme can be: THEME.DARK, THEME.LIGHT, 'SYSTEM'.
  • language - 'en_US' | 'ru_RU' | 'zh_CN' | 'ar_AE' | 'cs_CZ' | 'de_DE' | 'es_ES' | 'es_LAT' | 'fr_FR' | 'id_ID' | 'it_IT' | 'nl_NL' | 'pl_PL' | 'pt_BR' | 'pt_PT' | 'ro_RO' | 'tr_TR' | 'uk_UA' | 'vi_VN'. , defaults to en_US

Return value

  • OKXUniversalConnectUI

Example

import { OKXUniversalConnectUI } from "@okxconnect/ui";

const universalUi = await OKXUniversalConnectUI.init({
    dappMetaData: {
        icon: "https://static.okx.com/cdn/assets/imgs/247/58E63FEA47A2B7D7.png",
        name: "OKX Connect Demo"
    },
    actionsConfiguration: {
        returnStrategy: 'tg://resolve',
        modals:"all",
        tmaReturnUrl:'back'
    },
    language: "en_US",
    uiPreferences: {
        theme: THEME.LIGHT
    },
});

Connect to Wallet#

Connecting to a wallet goes to get the wallet address as an identifier and the necessary parameters used to sign the transaction.

universalUi.openModal(connectParams: ConnectParams);

Request Parameters

  • connectParams - ConnectParams
    • namespaces - [namespace: string]: ConnectNamespace ; necessary information for the requested connection, the key is 'eip155' for EVM system and 'solana' for Solana system. If any of the requested chains are not supported by the wallet, the wallet will reject the connection;
      • chains: string[]; chain id information, the
      • defaultChain?: string; default chain
    • optionalNamespaces - [namespace: string]: ConnectNamespace; optional information for requesting a connection, the key for EVM is 'eip155' and 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, chain information, chain information, chain information.
        • defaultChain?: string; defaultChain

Return Value

  • Promise<SessionTypes.Struct | undefined>
    • topic: string; session identifier;
    • namespaces: Record<string, Namespace>; namespace information for successful connections;
      • 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; The default chain for the current session.
    • sessionConfig?: SessionConfig
      • dappInfo: object DApp information;
        • name:string
        • dappInfo: object DApp info; name:string

Example

var session = await universalUi.openModal({
    namespaces: {
        solana: {
            chains: ["solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", //solana mainnet
                "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",//solana testnet
                "sonic:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z"// sonic testnet
            ],
        }
    }
})

Sending Signature and Transactions#

Methods to send messages to the wallet, support signature, transaction.

First create an OKXSolanaProvider object, pass OKXUniversalProviderUI into the constructor, when calling OKXSolanaProvider related methods, the actionsConfiguration.mode configuration will be handled according to the values passed during init;

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

Signature#

okxSolanaProvider.signMessage(message, chain);

Request parameters

  • message - string, the message to be signed
  • chain: string, the chain to be signed, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains; if a chain is not passed, it will be handled according to solan:mainnet or sonic:testnet;

Return value

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

Sign a 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 solan: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 solan:mainnet or sonic:testnet;

Return value

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

Signs a transaction and broadcasts it for uploading#

okxSolanaProvider.signAndSendTransaction(transaction, chain);

Request parameters

  • transactions - Transaction | VersionedTransaction transaction data object.
  • chain: string, request signature execution of the chain, it is recommended to pass this parameter; when connecting multiple chains is a mandatory parameter; connecting a chain if it is not passed, it will be processed according to solan: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: public key

Example

//Signing a transfer transaction on solana mainnet
let provider = new OKXSolanaProvider(universalUi)
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 the connected wallet and delete the current session. If you want to switch the connected wallet, please disconnect the current wallet first.

universalUi.disconnect();