The OKX Injected Provider API for Bitcoin Signet is a JavaScript-based interface embedded by OKX into web applications. It enables decentralized applications (DApps) to interact seamlessly with the OKX Wallet extension, allowing them to request user account details, read data from connected blockchains, and facilitate message and transaction signing.
This API is specifically designed for Bitcoin Signet, a test network for Bitcoin that enables developers to test and debug applications in a risk-free environment. It is compatible with Bitcoin-based chains and supports integration with extension wallets.
Note: The BTC Signet functionality is available only for OKX Wallet extension version 2.82.32 or higher.
Core Methods of the OKX Bitcoin Signet Provider
Connect to Wallet
Use the connect
method to establish a connection between the DApp and the user's OKX Wallet.
okxwallet.bitcoinSignet.connect()
- Parameters: None
Return Value: A Promise that resolves to an object containing:
address
: The Bitcoin address of the currently connected account.publicKey
: The public key associated with that address.
This method is essential for initiating user authentication and retrieving basic account information.
Sign a Message
The signMessage
method allows users to cryptographically sign a string of data using their private key.
okxwallet.bitcoinSignet.signMessage(signStr[, type])
Parameters:
signStr
(string): The data string that requires a signature.type
(string, optional): The signing algorithm. Options are"ecdsa"
or"bip322-simple"
. The default is"ecdsa"
.
- Return Value: A Promise that resolves to the signature string.
This is commonly used for verifying ownership of an address without conducting a transaction.
Sign a PSBT (Partially Signed Bitcoin Transaction)
The signPsbt
method is used to sign a PSBT, which is a standard for creating flexible Bitcoin transactions.
okxwallet.bitcoinSignet.signPsbt(psbtHex[, options])
Parameters:
psbtHex
(string): A hexadecimal string representing the PSBT to be signed.options
(object, optional): An object for advanced signing parameters.autoFinalized
(boolean): Iftrue
(default), the PSBT is finalized after signing.toSignInputs
(array): An array of objects specifying which inputs to sign.index
(number): The index of the input to sign.address
(string) orpublicKey
(string): Specify which key to use for signing by providing either the address or its public key.sighashTypes
(number[], optional): An array of signature hash types.disableTweakSigner
(boolean, optional): Disables the default tweakSigner for Taproot addresses, allowing signing with the original private key.
Important Note for Taproot: If any input in the PSBT uses a Taproot address, you must add the corresponding public key to each input during the PSBT creation process.
- Return Value: A Promise that resolves to the signed PSBT as a hexadecimal string.
๐ Explore advanced PSBT signing strategies
Sign Multiple PSBTs
For batch processing, the signPsbts
method allows you to sign multiple PSBTs in a single call.
okxwallet.bitcoinSignet.signPsbts(psbtHexs[, options])
Parameters:
psbtHexs
(string[]): An array of PSBT hex strings.options
(object[], optional): An array of option objects corresponding to each PSBT. The structure is identical to the options insignPsbt
.
- Return Value: A Promise that resolves to an array of the signed PSBT hex strings.
The same Taproot public key requirement applies to all PSBTs in the array.
Frequently Asked Questions
What is the OKX Injected Provider API?
It is a JavaScript API that OKX injects into web pages. It allows DApps to securely request user permissions, access wallet information, and execute signing operations directly through the OKX Wallet extension, facilitating a seamless Web3 experience.
What is Bitcoin Signet?
Signet is a Bitcoin test network. Unlike other testnets, it requires blocks to be signed by a private key, ensuring chain stability and predictability. This makes it an ideal environment for developers to test their DApps and smart contracts without using real funds.
Why do I need to add a public key for Taproot inputs?
Taproot (P2TR) addresses use a more complex cryptographic scheme (Schnorr signatures). Providing the public key for each input ensures the wallet can correctly generate the signature using the appropriate tweaked private key, which is essential for a successful and valid transaction.
Can I use this API on the Bitcoin mainnet?
No, the okxwallet.bitcoinSignet
namespace is specifically designed for the Signet test network. For mainnet operations, you would use a different provider API intended for production use.
What is the difference between ecdsa
and bip322-simple
for message signing?ecdsa
is the standard elliptic curve digital signature algorithm widely used in Bitcoin. bip322-simple
refers to a standardized message signing protocol defined in BIP322, which provides a more universal way to prove ownership of a Bitcoin address.
How do I handle errors from these API calls?
All methods return a Promise. You should use .catch()
or try/catch
blocks (with await
) to handle any potential errors, such as user rejection, connection issues, or incorrect parameters.
Key Considerations for Developers
Integrating the OKX Provider API into your DApp requires careful attention to detail. Always ensure your application:
- Checks for the existence of the
window.okxwallet.bitcoinSignet
object before calling methods to avoid runtime errors. - Clearly communicates to users what data is being requested and why, especially for signing operations.
- Handles both successful responses and potential errors gracefully to provide a smooth user experience.
- Thoroughly tests all functionality on the Signet testnet before considering a mainnet deployment.
By leveraging this API, developers can build powerful, secure, and user-friendly Bitcoin DApps that integrate directly with the OKX Wallet ecosystem. For a deeper dive into available methods and their latest specifications, always refer to the official API documentation.