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:
- Request user account access
- Read blockchain data
- Facilitate message and transaction signing
- Manage wallet connections seamlessly
Accessing the Injected Object
DApps can access the injected provider through two primary methods:
window.okxwallet.cardanowindow.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:
name(string): Identifies the wallet as 'OKX Wallet'icon(string): URL pointing to the wallet's logoapiVersion(string): Current API version numberisEnabled(): Returns a Promise indicating wallet-DApp connection statusenable(): 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
networkId: String representing the current network
๐ Learn more about Cardano network parameters
UTXO Management
Accessing UTXOs
api.getUtxos(amount: cbor = undefined): Promise<string[]>
Description
- Without parameters: Returns all wallet-controlled UTXOs
- With amount parameter: Returns UTXOs meeting specified ADA/multi-asset value
Return Value
utxos: Array of unspent transaction outputs
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
balance: String representing total available funds
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
signedTx: CBOR-encoded signed transaction
๐ 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:
signature: Cryptographic proofkey: Verification key
Broadcasting Transactions
api.submitTx(tx: cbor): Promise<string>
Description
Submits signed transactions to the network.
Return Value
txHash: Transaction identifier for tracking
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.