Connect to App or Mini Wallet

UI#

In addition to SDK, we also provide a UI interface. If using the UI connection, and the DApp is operating in Telegram, users can choose to open the mobile App Wallet or stay in Telegram and launch the OKX Mini Wallet.

Installation and Initialization#

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

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

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

Before connecting to the 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 telegram, 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 telegram, 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

Examples

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

const okxUniversalConnectUI = 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"
    },
    language: "en_US",
    uiPreferences: {
        theme: THEME.LIGHT
    },
});

Connecting to a wallet#

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

okxUniversalConnectUI.openModal(connectParams: ConnectParams);

Request parameters

  • connectParams - ConnectParams
    • namespaces - [namespace: string]: ConnectNamespace ; Necessary information for the requested connection, the key is ‘eip155’ for EVM systems and ‘aptos’ for Aptos systems. If any of the requested chains are not supported by the wallet, the wallet will reject the connection;
      • chains: string[]; chain id information, chain name, chain name, chain name, chain name, chain name, chain name, chain name, chain name, chain name, chain name
      • defaultChain?: string; default chain
    • optionalNamespaces - [namespace: string]: ConnectNamespace; optional information for requesting a connection, the key for EVM is ‘eip155’ and for Aptos is ‘aptos’. 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; default chain
    • sessionConfig: object
      • redirect: string Jump parameter after successful connection, if it is Mini App in Telegram, here can be set to Telegram's deeplink: ‘tg://resolve’

Return value

  • 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; The 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 okxUniversalConnectUI.openModal({
    namespaces: {
        aptos: {
            chains: ["aptos:mainnet", // aptos mainnet
                  // "movement:testnet",// movement testnet
            ],
        }
    },
    sessionConfig: {
        redirect: "tg://resolve"
    }
})

Connect to wallet and sign#

Connect to the wallet to get the wallet address and sign the data; the result will be called back in the event ‘connect_signResponse’;

await okxUniversalConnectUI.openModalAndSign(connectParams: ConnectParams, signRequest: RequestParams[]);

Request Parameters

  • connectParams - ConnectParams
    • namespaces - [namespace: string]: ConnectNamespace ; information necessary to request a connection, the key for Aptos is ‘aptos’. If any of the requested chains are not supported by the wallet, the wallet will reject the connection;
      • chains: string[]; chain id information, defaultChain?
      • defaultChain?: string; default chain
    • optionalNamespaces - [namespace: string]: ConnectNamespace; optional information about the requested connection, the key for Aptos is ‘aptos’. If the corresponding chain information is not supported by the wallet, the connection can still be made;
      • chains: string[]; chain id information, defaultChain?
      • defaultChain?: string; default chain
    • sessionConfig: object
      • redirect: string Jump parameter after successful connection, if it is a Mini App in Telegram, here can be set to Telegram's deeplink: ‘tg://resolve’
  • signRequest - RequestParams[]; the method to request the connection and sign the request, at most one method can be supported at the same time;
    • method: string; the name of the requested method, Aptos supports the method: ‘aptos_signMessage’;
    • chainId: string; the ID of the chain in which the method is executed, this chainId must be included in the namespaces above;
    • params: unknown[] | Record<string, unknown> | object | undefined; Parameters corresponding to the requested method;

Return Value

  • 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 in the current namespace;
      • defaultChain?: string; The default chain for the current session.
    • sessionConfig?: SessionConfig
      • dappInfo: object DApp information;
        • name: string
        • icon:string

Example

// Add the signature result monitor first
okxUniversalConnectUI.on("connect_signResponse", (signResponse) => {
  console.log(signResponse);
});
var session = await okxUniversalConnectUI.openModalAndSign({
        namespaces: {
            aptos: {
                chains: ["aptos:mainnet", // aptos mainnet
                 // "movement:testnet",// movement testnet
                ],
            }
        },
        sessionConfig: {
            redirect: "tg://resolve"
        }
    },
    [
        {
            chainId: "aptos:mainnet",
            method: "aptos_signMessage",
            params: {
                address: true,
                application: true,
                chainId: true,
                message: "Hello Aptos",
                nonce: "1234"
            }
        }
])

Determine if the wallet is connected#

Check if wallet is connected.

Return Value

  • boolean

Example

okxUniversalConnectUI.connected();

Prepare the transaction#

First create an OKXAptosProvider object, with the constructor passing in OKXUniversalProvider

import { OKXAptosProvider } from '@okxconnect/aptos-provider' ;
let okxAptosProvider = new OKXAptosProvider(okxUniversalConnectUI)

Get the wallet address and publicKey

okxAptosProvider.getAccount(chain);

Request Parameters

  • chain: string, the id of the chain to get the wallet address, if you don't pass it, it will take the first connected aptos system address.

Return Value

  • Object
    • address: string The address of the wallet.
    • publicKey: string Public key

Signature#

okxAptosProvider.signMessage(message, chain);

Request parameters

  • message - object
    • address?: boolean; // Should we include the address of the account in the message
    • application?: boolean; // Should we include the domain of the DApp
    • chainId?: boolean; // Should we include the current chain id the wallet is connected to
    • message: string; // The message to be signed and displayed to the user
    • nonce: string; // A nonce the DApp should generate
  • chain: string, the chain to be executed by the request signature, it is recommended to pass this parameter; it is mandatory to pass this parameter when connecting multiple chains;

Return value

  • Promise - object
    • address: string;
    • application: string;
    • chainId: number;
    • fullMessage: string; // The message that was generated to sign
    • message: string; // The message passed in by the user
    • nonce: string;
    • prefix: string; // Should always be APTOS
    • signature: string; // The signed full message

sign single transaction#

okxAptosProvider.signTransaction(transaction, chain);

Request Parameters

  • transaction - object | SimpleTransaction transaction data object
  • chain: string, the chain for which the signature execution is requested, this parameter is recommended; mandatory when connecting multiple chains;

Return Value

  • Promise - Buffer signed result.

Signs multiple transactions and chains them up

okxAptosProvider.signAndSubmitTransaction(transactions, chain);

Request Parameters

  • transactions - object | SimpleTransaction transaction data object
  • chain: string, the chain for which the signature execution is requested, this parameter is recommended; mandatory when connecting multiple chains;

Return Value

  • Promise - string transaction hash.

Example

// Signature message
let data = {
    address:true,
    application:true,
    chainId:true,
    message:"Hello OKX",
    nonce:"1234"
}

let provider = new OKXAptosProvider(okxUniversalConnectUI)
let message = await provider.signMessage(data, "aptos:mainnet")

//return value {‘address’: ‘0x2acddad65c27c6e5b568b398f0d1d01ebb8b55466461bbd51c1e42763a92fdfe’, ‘application’: ‘http://192.168.101.13’,"’ chainId": “aptos:mainnet”, “fullMessage”: ’APTOS\naddress: 0x2acddad65c27c6e5b568b398f0d1d01ebb8b55466461bbd51c1e42763a92fdfe\ napplication: http://192.168.101.13\nchainId: aptos:mainnet\nmessage: 123 Signature Test! \nnonce: 1234’, “message”: ’123 Signature test!’ , ‘nonce’: ‘1234’, ‘prefix’: ‘APTOS’, ‘signature’:’ 0xef4e587f537b80a2f4e424079984b80e130c92d939a92225764be00ed36486521e8857b8a222de4023c5f4d2e9fd2f62c26ca8a43694660583c8a5d4328da303 ’, ‘verified’:true}

// Sign the transaction and upload it
const config = new AptosConfig({ network: Network.MAINNET });
const aptos = new Aptos(config);
// Support for transactions created via @aptos-labs/ts-sdk
const transaction = await aptos.transaction.build.simple({
    sender:0x07897a0496703c27954fa3cc8310f134dd1f7621edf5e88b5bf436e4af70cfc6,
    data: {
        function:0x80273859084bc47f92a6c2d3e9257ebb2349668a1b0fb3db1d759a04c7628855::router::swap_exact_coin_for_coin_x1’,
        typeArguments: [0x1::aptos_coin::AptosCoin’,0x111ae3e5bc816a5e63c2da97d0aa3886519e0cd5e4b046659fa35796bd11542a::stapt_token:. StakedApt’,0x0163df34fccbf003ce219d3f1d9e70d140b60622cb9dd47599c25fb2f797ba6e::curves::Uncorrelated”,0x80273859084bc47f92a6c2d3e9257ebb2349668a1b0fb3db1d759a04c7628855::router::BinStepV0V05"],
        functionArguments: [10000, [9104], [5], [true]],
    }
}).
let result1 = await provider.signAndSubmitTransaction(transaction, ‘aptos:mainnet’)

// Transactions that also support the following data formats
let transactionData = {
  "arguments": ["100000",["0","0","10533"],["10","5","5"],["false","false","true"]],
  "function": "0x80273859084bc47f92a6c2d3e9257ebb2349668a1b0fb3db1d759a04c7628855::router::swap_exact_coin_for_coin_x3",
  "type": "entry_function_payload",
  "type_arguments": ["0x1::aptos_coin::AptosCoin","0x73eb84966be67e4697fc5ae75173ca6c35089e802650f75422ab49a8729704ec::coin::DooDoo","0x53a30a6e5936c0a4c5140daed34de39d17ca7fcae08f947c02e979cef98a3719::coin::LSD","0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC","0x80273859084bc47f92a6c2d3e9257ebb2349668a1b0fb3db1d759a04c7628855::router::CurveV1","0x0163df34fccbf003ce219d3f1d9e70d140b60622cb9dd47599c25fb2f797ba6e::curves::Uncorrelated","0x0163df34fccbf003ce219d3f1d9e70d140b60622cb9dd47599c25fb2f797ba6e::curves::Uncorrelated","0x54cb0bb2c18564b86e34539b9f89cfe1186e39d89fce54e1cd007b8e61673a85::bin_steps::X80","0x80273859084bc47f92a6c2d3e9257ebb2349668a1b0fb3db1d759a04c7628855::router::BinStepV0V05","0x80273859084bc47f92a6c2d3e9257ebb2349668a1b0fb3db1d759a04c7628855::router::BinStepV0V05"]
}
let result2 = await provider.signAndSubmitTransaction(transactionData, "movement:testnet")

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.

okxUniversalConnectUI.disconnect();

Event#

// Generate universalLink
okxUniversalConnectUI.on("display_uri", (uri) => {
    console.log(uri);
}).
// Session information changes will trigger this event;
okxUniversalConnectUI.on("session_update", (session) => {
  console.log(JSON.stringify(session));
});
// Disconnecting triggers this event;
okxUniversalConnectUI.on("session_delete", ({topic}) => {
    console.log(topic);
});
// This event is triggered when a connection is made and the signature is signed.
okxUniversalConnectUI.on("connect_signResponse", (signResponse) => {
  console.log(signResponse);
});

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
}