Cardano Provider API: Connecting Browser Extension Wallets & Web3 Integration Guide

ยท

What is the Injected Provider API?

The Injected Provider API is a JavaScript interface that enables decentralized applications (DApps) to interact with cryptocurrency wallets through browser extensions. This powerful API allows DApps to:

Accessing the Injected Object

DApps can access the injected provider through two primary methods:

  1. window.okxwallet.cardano
  2. window.cardano.okxwallet

Both properties reference the same object, providing flexibility for developers. This dual-access approach ensures compatibility with various implementation preferences.

Core Properties and Methods

The injected object contains these essential components:

  1. name (string): Identifies the wallet as 'OKX Wallet'
  2. icon (string): URL pointing to the wallet's logo
  3. apiVersion (string): Current API version number
  4. isEnabled(): Returns a Promise indicating wallet-DApp connection status
  5. enable(): Initiates wallet connection, returning the API object upon user approval

Wallet Connection Example

async function connectWallet() {
  try {
    const enabledWallet = await window.cardano.okxwallet.enable();
    console.log("Wallet connected successfully!");
    return enabledWallet;
  } catch (error) {
    console.error("Connection failed:", error);
  }
}

Network Operations

Retrieving Network ID

api.getNetworkId(): Promise<string>

Description
Returns the network ID of the currently connected account.

Return Value

๐Ÿ‘‰ Learn more about Cardano network parameters

UTXO Management

Accessing UTXOs

api.getUtxos(amount: cbor = undefined): Promise<string[]>

Description

Return Value

Asset Management

Checking Wallet Balance

api.getBalance(): Promise<string>

Description
Returns the total available balance in the wallet, equivalent to the sum of all UTXOs.

Return Value

Address Handling

Used Addresses

api.getUsedAddresses(): Promise<string[]>

Description
Returns all wallet-controlled addresses that have participated in transactions.

Unused Addresses

api.getUnusedAddresses(): Promise<string[]>

Description
Returns fresh addresses never used in transactions.

Change Address

api.getChangeAddress(): Promise<string>

Description
Provides the address used for transaction change outputs.

Transaction Operations

Signing Transactions

api.signTx(tx: cbor): Promise<string>

Description
Requests user approval to sign transactions.

Return Value

๐Ÿ‘‰ Advanced transaction signing techniques

Message Signing

api.signData(addr: Cbor, payload: HexString): Promise<object>

Description
Signs messages following CIP-0030 standards.

Return Value
Object containing:

Broadcasting Transactions

api.submitTx(tx: cbor): Promise<string>

Description
Submits signed transactions to the network.

Return Value

FAQ Section

Q: How do I handle different Cardano network versions?

A: Always check the apiVersion property and implement version-specific handling when necessary.

Q: What's the difference between used and unused addresses?

A: Used addresses appear on-chain, while unused addresses have never participated in transactions.

Q: Can I access the wallet without user interaction?

A: No, the enable() method requires explicit user consent for security reasons.

Q: How should I handle transaction failures?

A: Implement comprehensive error handling and provide clear user feedback about transaction status.

Q: Is message signing compatible with all Cardano wallets?

A: Most modern wallets support CIP-0030 standards, but always include fallback options.

Q: What's the recommended way to monitor transaction confirmation?

A: Use the returned txHash with Cardano blockchain explorers to track confirmation status.