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 accessing it:

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

npm install @okxconnect/sui-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 app, 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 = OKXUniversalProvider.init({dappMetaData: {
        name: "application name",
        icon: "application icon url"
    }})

Connecting to Wallet#

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

okxUniversalProvider.connect(connectParams: ConnectParams);

Request parameters

  • connectParams - ConnectParams
    • namespaces - [namespace: string]: ConnectNamespace ; Necessary information for the requested connection, the key for the Sui line is 'sui' If any of the requested chains are not supported by the wallet, the wallet will reject the connection;
      • chains: string[]; chain id information, the chain's key is 'sui', the wallet will reject the connection if any of the requested chains is not supported.
    • optionalNamespaces - [namespace: string]: ConnectNamespace; optional information for requesting connection, the key for EVM is 'eip155', the key for Sui is 'sui'. If the corresponding chain information is not supported by the wallet, the connection can still be made;
      • chains: string[]; chain id information, if the corresponding chain is not supported by the wallet.
    • 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'.

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; 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: {
        sui: {
            chains: ["sui:mainnet"]
        }
    },
    sessionConfig: {
        redirect: "tg://resolve"
    }
})

Disconnect wallet#

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

Example

okxUniversalProvider.disconnect();

Send signatures and transactions#

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

First create an OKXSuiProvider object, pass OKXUniversalProvider into the constructor.

import { OKXSuiProvider } from '@okxconnect/sui-provider'
let suiProvider = new OKXSuiProvider(okxUniversalProvider)

Get the account#

suiProvider.getAccount();

Return Value

  • Object
    • address: string wallet address
    • publicKey: string public key (requires App 6.92.0 or later support)

Example

let result = suiProvider.getAccount()

// Return structure
{
    'address":0x7995ca23961fe06d8cea7da58ca751567ce820d7cba77b4a373249034eecca4a,
    'publicKey": “tUvCYrG22rHKR0c306MxgnhXOSf16Ot6H3GMO7btwDI=,
}

SignMessage#

suiProvider.signMessage(input: SuiSignMessageInput);

Request Parameters

  • SuiSignMessageInput - object
    • message: Uint8Array

Return Value

  • Promise - object
    • messageBytes: string
    • signature: string

SignPersonalMessage#

suiProvider.signPersonalMessage(input: SuiSignMessageInput);

Request Parameters

  • SuiSignMessageInput - object
    • message: Uint8Array

Return Value

  • Promise - object
    • bytes: string
    • signature: string

Example

const data = [76, 111, 103, 105, 110, 32, 119, 105, 116, 104, 32, 66, 108, 117, 101, 109, 111, 118, 101];
const uint8Array = new Uint8Array(data);
let input = {
    message: uint8Array
}
let signResult1 = await suiProvider.signMessage(input)
let signResult2 = await suiProvider.signPersonalMessage(input)

Sign Transaction#

suiProvider.signTransaction(input);

Request Parameters

// txBytes and txSerialize are the serialization of the transactionBlock.
// and transactionBlock can be passed in one way or the other, but not both.
interface SuiSignTransactionBlockInput {
    transactionBlock: TransactionBlock;
    chain: IdentifierString;
    txBytes: string?;
    txSerialize: string?
}

Return Value

  • Promise - object
    • signature: string,
    • transactionBlockBytes: string

Signs a transaction and broadcasts onchain#

suiProvider.signAndExecuteTransaction(input);

Request Parameters

// txBytes and txSerialize are the serialization of the transactionBlock.
// and transactionBlock can be passed in one way or the other, but not both.
interface SuiSignTransactionBlockInput {
    transactionBlock: TransactionBlock;
    chain: IdentifierString;
    txBytes: string?;
    txSerialize: string?;
}

Return Value

  • Promise - object
    • confirmedLocalExecution: bool,
    • digest: string,
    • txBytes: string

Example

// Define the amount to be transferred and the destination address
const amount = 109; // Amount to be transferred
const recipientAddress = '0x'; // destination address

/// Construct a transfer transaction
const tx = new Transaction();
const [coin] = tx.splitCoins(tx.gas, [amount]);
tx.transferObjects([coin], recipientAddress)
const input = {
    transactionBlock: tx,
    chain: 'sui:mainnet',
    options: {
        showEffects: true,
    }
}

let signResult1 = await suiProvider.signTransaction(input)
let signResult2 = await suiProvider.signAndExecuteTransaction(input)