SDK#
Installation and Initialization#
Make sure to update to version 6.94.0 or later to start integrating OKX Connect into your DApp can be done using npm:
npm install @okxconnect/universal-provider
Before connecting to a wallet, you need to create an object for subsequent operations such as connecting to the wallet and sending transactions.
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 Value
- OKXUniversalProvider
Examples
import { OKXUniversalProvider } from "@okxconnect/universal-provider";
const okxUniversalProvider = await OKXUniversalProvider.init({
dappMetaData: {
name: "application name",
icon: "application icon url"
},
})
Connecting to a wallet#
Connect to the wallet to get the wallet address as an identifier and the necessary parameters for signing transactions.
okxUniversalProvider.connect(connectParams: ConnectParams);
Request Parameters
- connectParams - ConnectParams
- namespaces - [namespace: string]: ConnectNamespace ; Optional information about the requested connection, the key of the COSMOS system is ‘cosmos’, the wallet will reject the connection if any of the requested chains are not supported by the wallet;
- chains: string[]; chain id information, defaultChain?
- defaultChain?: string; defaultChain
- optionalNamespaces - [namespace: string]: ConnectNamespace; optional information of the requested connection, the key of COSMOS is ‘cosmos’, if the corresponding chain is not supported by the wallet, it can still be connected;
- chains: string[]; chain id information, defaultChain?
- defaultChain?: string; default chain
- chains: string[]; chain id information, defaultChain?
- sessionConfig: object
- redirect: string The jump parameter after successful connection, if it is Mini App in Telegram, here can be set to Telegram's deeplink: ‘tg://resolve’
- namespaces - [namespace: string]: ConnectNamespace ; Optional information about the requested connection, the key of the COSMOS system is ‘cosmos’, the wallet will reject the connection if any of the requested chains are not supported by the wallet;
Returns Value
- Promise
<SessionTypes.Struct | undefined>
- topic: string; 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
- redirect?: string, the redirect parameter after successful connection;
- dappInfo: object DApp information;
Example
var session = await okxUniversalProvider.connect({
namespaces: {
cosmos: {
chains: [
"cosmos:cosmoshub-4",
// "cosmos:osmosis-1"
],
}
},
sessionConfig: {
redirect: "tg://resolve"
}
})
Determine if the wallet is connected#
Gets whether the wallet is currently connected.
Return Value
- boolean
Example
okxUniversalProvider.connected();
Prepare the transaction#
First create an OKXCosmosProvider object and pass OKXUniversalProvider into the constructor.
import { OKXCosmosProvider } from "@okxconnect/universal-provider";
let okxCosmosProvider = new OKXCosmosProvider(okxUniversalProvider)
Get account information#
okxCosmosProvider.getAccount(chainId);
Request parameters
- chainId: the requested chain, e.g. cosmos:cosmoshub-4, cosmos:osmosis-1
Return Value
- Object
- algo: ‘secp256k1’,
- address: string wallet-address, bech32Address: string wallet-address, bech32Address
- bech32Address: string walletAddress, pubKey: Uint8Address, pubKey: Uint8Address
- pubKey: Uint8Array publicKey, pubKey: Uint8Array publicKey, pubKey: Uint8Array publicKey
Example
let result = okxCosmosProvider.getAccount("cosmos:cosmoshub-4")
//Return structure
{
"algo": "secp256k1",
"address": "cosmos1u6lts9ng4etxj0zdaxsada6zgl8dudpg3ygvjw",
"bech32Address": "cosmos1u6lts9ng4etxj0zdaxsada6zgl8dudpg3ygvjw",
"pubKey": {
"0": 2,
"1": 68,
"2": 110,
...
"32": 144
}
}
Sign the message#
okxCosmosProvider.signArbitrary(chain, signerAddress, message);
Request Parameters
- chain - string, chain of requested execution methods
- signerAddress - string The address of the signature wallet.
- message - string The message to be signed.
Return Value
- Promise - object
- pub_key : object
- type:string Public key type
- value: string Public key
- signature: string Signature result
- pub_key : object
Example
let chain = "cosmos:cosmoshub-4"
let signStr = "data need to sign ..."
let result = okxCosmosProvider.signArbitrary(chain, signStr)
//Return structure: {"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AkRuGelKwOg+qJbScSUHV36zn73S1q6fD8C5dZ8furqQ"},"signature":"YSyndEFlHYTWpSXsn28oolZpKim/BnmCVD0hZfvPQHQV3Bc0B0EU77CKE6LpV+PUJn19d1skAQy/bXyzppnuxw=="}
SignAmino#
okxCosmosProvider.signAmino(chainId: string, signerAddress: string, signDoc: StdSignDoc, signOptions?: object);
Request Parameters
-
chainId - string, the chain for which the signature execution is requested, mandatory parameter
-
signerAddress - string, the address of the wallet.
-
signDoc - object, the transaction information to be signed in a fixed format, similar to cosmjs OfflineSigner signAmino method, the parameters are objects, signDoc is a fixed format. ***signDoc is a fixed format.
-
Promise - Object
- signed - object,transaction information
- signature -object, the result of the signature.
Example
let signDoc = {
"chain_id": "osmosis-1",
"account_number": "630104",
"sequence": "480",
"fee": {"gas": "683300", "amount": [{"denom": "uosmo", "amount": "2818"}]},
"msgs": [{
"type": "osmosis/poolmanager/swap-exact-amount-in",
"value": {
"sender": "osmo1u6lts9ng4etxj0zdaxsada6zgl8dudpgelmuyu",
"routes": [{
"pool_id": "1096",
"token_out_denom": "ibc/987C17B11ABC2B20019178ACE62929FE9840202CE79498E29FE8E5CB02B7C0A4"
}, {
"pool_id": "611",
"token_out_denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
}],
"token_in": {"denom": "uosmo", "amount": "100"},
"token_out_min_amount": "8"
}
}],
"memo": "FE",
"timeout_height": "23603788",
"signOptions": {
"useOneClickTrading": false,
"preferNoSetFee": true,
"fee": {"gas": "683300", "amount": [{"denom": "uosmo", "amount": "2818"}]}
}
}
let res = await provider.signAmino("cosmos:osmosis-1", provider.getAccount("cosmos:osmosis-1").address, signDoc)
/**
Return structure:
{
"signed": {
"chain_id": "osmosis-1",
"account_number": "630104",
"sequence": "480",
"fee": {
"amount": [
{
"amount": "12500",
"denom": "uosmo"
}
],
"gas": "500000"
},
"msgs": [
{
"type": "osmosis/poolmanager/swap-exact-amount-in",
"value": {
"sender": "osmo1u6lts9ng4etxj0zdaxsada6zgl8dudpgelmuyu",
"routes": [
{
"pool_id": "1096",
"token_out_denom": "ibc/987C17B11ABC2B20019178ACE62929FE9840202CE79498E29FE8E5CB02B7C0A4"
},
{
"pool_id": "611",
"token_out_denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
}
],
"token_in": {
"denom": "uosmo",
"amount": "100"
},
"token_out_min_amount": "8"
}
}
],
"memo": "FE",
"timeout_height": "23603788",
"signOptions": {
"useOneClickTrading": false,
"preferNoSetFee": true,
"fee": {
"gas": "683300",
"amount": [
{
"denom": "uosmo",
"amount": "2818"
}
]
}
}
},
"signature": {
"pub_key": {
"type": "tendermint/PubKeySecp256k1",
"value": "AkRuGelKwOg+qJbScSUHV36zn73S1q6fD8C5dZ8furqQ"
},
"signature": "2Brt/w+1U3C+tIbsI//pv9zTYca9WlBd1eKm/Gde5MFaRagmxtsn6h2beP7+4R4MDav7r1G+0Nxd5arB0qVfUw=="
}
}
*/
SignDirect#
okxCosmosProvider.signDirect(chainId, signerAddress, signDoc, signOptions?);
Request Parameters
- chainId - string, the chain where the signature execution is requested, mandatory parameter.
- signerAddress - string, wallet address
- signDoc - object transaction data
- bodyBytes ,Uint8Array
- authInfoBytes, Uint8Array
- chainId, string
- accountNumber, string
Return Value
- Promise - Object
- signed - object,transaction information
- signature -object, the result of the signature.
Example
let signDoc = {
"bodyBytes": Uint8Array,
"authInfoBytes": Uint8Array,
"chainId": "osmosis-1",
"accountNumber": "630104",
}
let res = await provider.signDirect("cosmos:osmosis-1", provider.getAccount("cosmos:osmosis-1").address, signDoc)
/**
The return structure is the same as
{
"signed": {
"bodyBytes": {
"type": "Buffer",
"data": [
10, 193, 1, 10, 41, ...]
},
"authInfoBytes": {
"0": 10,
"1": 81,
...
},
"chainId": "osmosis-1",
"accountNumber": "630104"
},
"signature": {
"pub_key": {
"type": "tendermint/PubKeySecp256k1",
"value": "AkRuGelKwOg+qJbScSUHV36zn73S1q6fD8C5dZ8furqQ"
},
"signature": "YpX2kGmbZYVxUqK8y9OCweJNgZkS4WaS79nBDfOJaTgowPfY0gSbXSQeRLlif2SIkBqcwTNSItBqb5M7a6K30g=="
}
}
*/
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.
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 Code | Description |
---|---|
OKX_CONNECT_ERROR_CODES.UNKNOWN_ERROR | Unknown Error |
OKX_CONNECT_ERROR_CODES.ALREADY_CONNECTED_ERROR | Wallet Already Connected |
OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERROR | Wallet Not Connected |
OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR | User Rejected |
OKX_CONNECT_ERROR_CODES.METHOD_NOT_SUPPORTED | Method Not Supported |
OKX_CONNECT_ERROR_CODES.CHAIN_NOT_SUPPORTED | Chain Not Supported |
OKX_CONNECT_ERROR_CODES.WALLET_NOT_SUPPORTED | Wallet Not Supported |
OKX_CONNECT_ERROR_CODES.CONNECTION_ERROR | Connection 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
}