Connect to Extension Wallet
Provider API

Provider API#

What is injected provider API?#

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.

The injected object#

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.

The properties and methods of the injected object#

  1. name - string: Name of the wallet with a value of 'OKX Wallet'.
  2. icon - string: Wallet icon.
  3. apiVersion - string: The version.
  4. 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.
  5. 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.

Simple example of connecting to a wallet#

try {
  const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
} catch (error) {
  console.log(error);
}

Get networkId#

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();

Get utxos#

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();

Get balance#

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 wallet
const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getBalance();

Get used addresses#

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 addresses
const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const utxos = await okxwalletCardanoApi.getUsedAddresses();

Get unused addresses#

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();

Get change address#

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();

Sign transaction#

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);

Sign data#

api.signData: (addr: Cbor<address>, payload: HexString) => Promise<DataSignature>

Description

Sign data. Read more about message signing in CIP-0030.

Return value

  • dataSignature - object
    • signature - string
    • key - string
const okxwalletCardanoApi = await window.okxwallet.cardano.enable();
const addresses = await okxwalletCardanoApi.getUsedAddresses();
const payload = '';
const result = await okxwalletCardanoApi.signedData(addresses[0], payload);

Submit transaction#

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);