The OKX injected provider API is a JavaScript API that OKX injects into websites visited by our users. Your DApp can use this API to request users' accounts, read data from blockchains users are connected to, and help users sign messages and transactions.
DApps can access the injected object with two methods, which are:
window.okxwallet.cardano
window.cardano.okxwallet
All two attributes point to the same object, and these two methods are provided for the convenience of DApp usage.
name
- string: Name of the wallet with a value of 'OKX Wallet'.icon
- string: Wallet icon.apiVersion
- string: The version.isEnabled
- () => Promise<bool>: Returns true if the dApp is already connected to the user's wallet, and false otherwise.If this function returns true, then any subsequent calls to wallet.enable() during the current session should succeed and return the API object.enable
- () => Promise<API>: The wallet should request the user's permission to connect the web page to the user's wallet, and if permission has been granted, the full API will be returned to the dApp to use.try {
const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
} catch (error) {
console.log(error);
}
api.getNetworkId(): Promise<number>
Description
Returns the network id of the currently connected account.
Return value
networkId
- string: The network id of the currently connected account.const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const networkId = await okxwalletCardanoApi.getNetworkId();
api.getUtxos(amount: cbor<value> = undefined): Promise<TransactionUnspentOutput[] | undefined>
Description
If amount is undefined, this shall return a list of all UTXOs (unspent transaction outputs) controlled by the wallet. If amount is not undefined, this request shall be limited to just the UTXOs that are required to reach the combined ADA/multiasset value target specified in amount, and if this cannot be attained, null shall be returned.
Return value
utxos
- string[]: List of utxos.const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getUtxos();
api.getBalance(): Promise<cbor<value>>
Description
Returns the total balance available of the wallet. This is the same as summing the results of api.getUtxos().
Return value
balance
- string: The total balance available of the walletconst okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getBalance();
api.getUsedAddresses(): Promise<cbor<address>[]>
Description
Returns a list of all used (included in some on-chain transaction) addresses controlled by the wallet.
Return value
addresses
- string[]: List of addressesconst okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getUsedAddresses();
api.getUnusedAddresses(): Promise<cbor<address>[]>
Description
Returns a list of unused addresses controlled by the wallet.
Return value
addresses
- string[]: List of unused addresses.const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getUnusedAddresses();
api.getChangeAddress(): Promise<cbor<address>>
Description
Returns an address owned by the wallet that should be used as a change address to return leftover assets during transaction creation back to the connected wallet. This can be used as a generic receive address as well.
Return value
changeAddress
- string: A change address.const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getChangeAddress();
api.signTx(tx: cbor<transaction>): Promise<cbor<transaction_witness_set>>
Description
Requests that a user sign the supplied transaction. The wallet should ask the user for permission, and if given, try to sign the supplied body and return a signed transaction.
Return value
signedTx
- string: Signed transaction.const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const rawTransaction = '';
const result = await okxwalletCardanoApi.signedTx(rawTransaction);
api.signData: (addr: Cbor<address>, payload: HexString) => Promise<DataSignature>
Description
Sign data. Read more about message signing in CIP-0030.
Return value
dataSignature
- object
const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const addresses = await okxwalletCardanoApi.getUsedAddresses();
const payload = '';
const result = await okxwalletCardanoApi.signedData(addresses[0], payload);
api.submitTx(tx: cbor<transaction>): Promise<hash32>
Description
Send the transaction and return the transaction id for the dApp to track.
Return value
txHash
- string: Transaction hash.const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const transaction = '';
const result = await okxwalletCardanoApi.submitTx(transaction);