The world of decentralized applications (DApps) is rapidly evolving, and seamless wallet integration is at the heart of user experience. With the rise of Nostr-based protocols and Web3 infrastructure, developers need reliable, secure, and easy-to-implement APIs to connect users directly from their browser wallets. This guide dives into the Injected Provider API offered via a leading Web3-enabled platform, focusing on nostr integration, browser wallet connectivity, and essential functions for building powerful decentralized exchanges (DEXs) and social platforms.
Whether you're building a DEX, a messaging app, or a social blockchain client, understanding how to leverage this API will empower your application with secure authentication, message encryption, event signing, and real-time account monitoring.
What Is the Injected Provider API?
The Injected Provider API is a JavaScript interface embedded directly into websites when accessed by users with a compatible Web3 browser wallet. It allows your DApp to securely interact with the user's connected blockchain identity—requesting accounts, reading blockchain data, and facilitating digital signatures for transactions and messages—all without exposing private keys.
This API operates through a globally available object injected into the browser’s window context, enabling direct communication between your frontend and the user's wallet.
👉 Discover how easy it is to integrate Web3 wallet functionality into your DApp.
How to Access the Injected Object
To begin using the API, your DApp must detect and access the injected provider object. The entry point is:
window.okxwallet.nostrThis object serves as the foundation for all subsequent interactions. Before making calls, always verify its presence to ensure the user has an active wallet session:
if (window.okxwallet && window.okxwallet.nostr) {
console.log("Wallet is ready!");
} else {
console.log("Please install a compatible Web3 wallet.");
}Connecting a Wallet: Simple Implementation Example
Establishing a connection starts with requesting access to the user's public identity. While the API doesn’t require explicit “connect” calls like some wallets, retrieving the public key serves as the de facto handshake.
Here’s a basic example:
async function getPublicKey() {
try {
const publicKey = await window.okxwallet.nostr.getPublicKey();
console.log("Connected account:", publicKey);
return publicKey;
} catch (error) {
console.error("User rejected request or connection failed:", error);
}
}Once the public key is retrieved, your DApp can proceed with personalized features such as profile loading, message decryption, or transaction signing.
Retrieve the User’s Public Key
Method: window.okxwallet.nostr.getPublicKey(): Promise<string>
Description
Returns the public key of the currently connected account. This key acts as the user’s blockchain identifier within the Nostr network.
Return Value
publicKey– string: The hexadecimal-encoded public key of the authenticated user.
This method prompts the user for permission if not already granted, ensuring privacy and control remain in the user’s hands.
Sign a Nostr Event
Method: window.okxwallet.nostr.signEvent(event: Event): Promise<SignedEvent>
Description
Securely signs a Nostr event using the user’s private key inside the wallet environment. The private key never leaves the wallet, preserving security.
Parameters
event– objectcreated_at– number: Unix timestamp (seconds) of event creationkind– number: Event type (e.g., 1 for text note, 4 for encrypted DM)tags– string[][]: Array of tag arrays (e.g.,[["p", "<pubkey>"]])content– string: Message or data payload
Return Value
SignedEvent– Includes all original fields plus:id– string: SHA-256 hash of serialized event datapubkey– string: Public key of signersig– string: ECDSA signature over the event ID
👉 Start building secure Nostr-powered apps with event signing capabilities today.
Encrypt Messages Using NIP-04
Method: window.okxwallet.nostr.nip04.encrypt(pubkey: string, message: string): Promise<string>
Description
Encrypts a message to a specific recipient using the NIP-04 standard, which leverages ECDH + AES encryption for private messaging.
Parameters
pubkey– string: Recipient’s public keymessage– string: Plaintext message to encrypt
Return Value
encryptMsg– string: Base64-encoded encrypted output
Use this method to enable end-to-end encrypted direct messages in your DApp.
Decrypt Incoming Messages
Method: window.okxwallet.nostr.nip04.decrypt(pubkey: string, message: string): Promise<string>
Description
Decrypts a message sent by another user using NIP-04 encryption. The wallet handles key derivation and decryption internally.
Parameters
pubkey– string: Sender’s public keymessage– string: Encrypted Base64 string
Return Value
decryptMsg– string: Decrypted plaintext message
This ensures that only the intended recipient can read sensitive communications.
Add or Remove Event Listeners
Methods:
window.okxwallet.nostr.on(event: string, callback: Function): voidwindow.okxwallet.nostr.off(event: string, callback: Function): void
Description
Register or unregister listeners for wallet-emitted events. Real-time updates enhance UX by responding instantly to user actions.
Supported Events
accountChanged– Triggered when the user switches accounts in their wallet
Example usage:
function handleAccountChange(newPubKey) {
console.log("Account switched to:", newPubKey);
refreshUserProfile();
}
// Attach listener
window.okxwallet.nostr.on('accountChanged', handleAccountChange);
// Later, remove if needed
window.okxwallet.nostr.off('accountChanged', handleAccountChange);👉 Enhance your app’s interactivity with real-time wallet event tracking.
Frequently Asked Questions (FAQ)
Q: Is this API compatible with all Nostr clients?
A: This Injected Provider API follows standard Nostr specifications (like NIP-07 and NIP-04), making it interoperable with most Nostr-based applications and services that support provider injection.
Q: Does the wallet expose private keys to my DApp?
A: No. All cryptographic operations—including signing and decryption—occur securely within the wallet environment. Private keys are never exposed to the website or DApp frontend.
Q: Can I use this API for non-Nostr blockchain interactions?
A: While optimized for Nostr, similar provider patterns exist for other blockchains. For multi-chain support, consider combining this with other Web3 providers like those for Ethereum or Bitcoin-based systems.
Q: What happens if the user denies a signature request?
A: The promise will reject with an error. Your application should handle this gracefully by notifying the user or retrying after adjustments.
Q: How do I test this locally during development?
A: Install a compatible browser wallet extension, then run your DApp on localhost. The provider will be injected automatically in supported environments.
Q: Is there rate limiting or usage caps on these API calls?
A: There are no inherent limits imposed by the provider itself. However, responsible usage is encouraged to maintain performance and user trust.
Core Keywords
- Injected Provider API
- Nostr wallet integration
- Web3 browser wallet
- DEX API documentation
- Connect wallet JavaScript
- Sign Nostr event
- Encrypt message NIP-04
- Wallet event listener
By integrating these functionalities, developers can create robust, secure, and interactive decentralized applications aligned with modern Web3 standards. From secure messaging to dynamic account management, this API provides everything needed to build next-generation Nostr-enabled experiences.