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
Returns Value
Examples
import { OKXUniversalProvider } from "@okxconnect/universal-provider";
const okxUniversalProvider = await OKXUniversalProvider.init({
dappMetaData: {
name: "application name",
icon: "application icon url"
},
})
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
Returns Value
<SessionTypes.Struct | undefined>
Record<string, Namespace>
; namespace information for a successful connection;
Example
var session = await okxUniversalProvider.connect({
namespaces: {
cosmos: {
chains: [
"cosmos:cosmoshub-4",
// "cosmos:osmosis-1"
],
}
},
sessionConfig: {
redirect: "tg://resolve"
}
})
Gets whether the wallet is currently connected.
Return Value
Example
okxUniversalProvider.connected();
First create an OKXCosmosProvider object and pass OKXUniversalProvider into the constructor.
import { OKXCosmosProvider } from "@okxconnect/universal-provider";
let okxCosmosProvider = new OKXCosmosProvider(okxUniversalProvider)
okxCosmosProvider.getAccount(chainId);
Request parameters
Return Value
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
}
}
okxCosmosProvider.signArbitrary(chain, signerAddress, message);
Request Parameters
Return Value
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=="}
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
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=="
}
}
*/
okxCosmosProvider.signDirect(chainId, signerAddress, signDoc, signOptions?);
Request Parameters
Return Value
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 the connected wallet and delete the current session. If you want to switch the connected wallet, please disconnect the current wallet first.
okxUniversalProvider.disconnect();