Provider API#
What is injected provider API?#
The OKX Injected Providers API is based on a JavaScript model and is embedded by OKX into the websites users access. DApps projects can utilize this API to request user account information, retrieve data from the connected blockchain, and assist users in signing messages and transactions.
connect#
okxwallet.bitcoin.connect()
Description
Connect wallet
Parameters
None
Return value
- Promise - object
- address - string: address of current account
- publicKey - string: public key of current account
Example
const result = await okxwallet.bitcoin.connect()
// example
{
address: 'bc1pwqye6x35g2n6xpwalywhpsvsu39k3l6086cvdgqazlw9mz2meansz9knaq',
publicKey: '4a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497'
}
requestAccounts#
Description
okxwallet.bitcoin.requestAccounts()
Connect the current account
Parameters
None
Return value
- Promise - string[]: address of current account
Example
try {
let accounts = await okxwallet.bitcoin.requestAccounts();
console.log('connect success', accounts);
} catch (e) {
console.log('connect failed');
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
getAccounts#
okxwallet.bitcoin.getAccounts()
Description
Get the address of current account
Parameters
None
Return value
- Promise - string[]: address of current account
Example
try {
let res = await okxwallet.bitcoin.getAccounts();
console.log(res);
} catch (e) {
console.log(e);
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
getNetwork#
- Don't support Testnet.
- This field is only available for extension version 2.77.1 or above.
okxwallet.bitcoin.getNetwork()
Description
Get network
Parameters
None
Return value
- Promise - string: the network
Example
try {
let res = await okxwallet.bitcoin.getNetwork();
console.log(res);
} catch (e) {
console.log(e);
}
// example
livenet;
getPublicKey#
okxwallet.bitcoin.getPublicKey()
Description
Get the public key of current account
Parameters
None
Return value
- Promise - string: public key
Example
try {
let res = await okxwallet.bitcoin.getPublicKey();
console.log(res)
} catch (e) {
console.log(e);
}
// example
03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f
getBalance#
okxwallet.bitcoin.getBalance()
Description
Get BTC balance
Parameters
None
Return value
- Promise - object
- confirmed - number: the confirmed satoshis
- unconfirmed - number: the unconfirmed satoshis
- total - number: the total satoshis
Example
try {
let res = await okxwallet.bitcoin.getBalance();
console.log(res)
} catch (e) {
console.log(e);
}
// example
{
"confirmed":0,
"unconfirmed":100000,
"total":100000
}
getInscriptions#
okxwallet.bitcoin.getInscriptions()
Description
List inscriptions of current account
Parameters
- cursor - number: (optional) offset, starting from 0. The default value is 0.
- size - number: (optional) number per page. The default value is 20.
Return value
- Promise - object
- total - number: the total count
- list - object[]:
- inscriptionId - string: the ID of inscription
- inscriptionNumber - string: the number of inscription
- address - string: the address of inscription
- outputValue - string: the output value of inscription
- contentLength - string: the content length of inscription
- contentType - number: the content type of the inscription
- timestamp - number: the block time of the inscription
- offset - number: the offset of inscription
- output - string: the identification of the utxo where the current inscription is located
- genesisTransaction - string: the transaction ID of the genesis transaction
- location - string: the txid and vout of current location
Example
try {
let res = await okxwallet.bitcoin.getInscriptions(0, 20);
console.log(res)
} catch (e) {
console.log(e);
}
// example
{
"total":10,
"list":[
{
inscriptionId: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531i0',
inscriptionNumber: 55878989,
address: 'bc1q8h8s4zd9y0lkrx334aqnj4ykqs220ss735a3gh',
outputValue: 546,
contentLength: 53,
contentType: 'text/plain',
timestamp: 1705406294,
location: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0:0',
output: '6037b17df2f48cf87f6b6e6ff89af416f6f21dd3d3bc9f1832fb1ff560037531:0',
offset: 0,
genesisTransaction: '02c9eae52923fdb21fe16ee9eb873c7d66fe412a61b75147451d8a47d089def4'
}
]
}
sendBitcoin#
okxwallet.bitcoin.sendBitcoin(toAddress, satoshis, options)
Description
Send BTC
Parameters
- toAddress - string: the address to send
- satoshis - number: the satoshis to send
- options - object: (optional)
- feeRate - number: the network fee rate
Return value
- Promise - string: transaction hash
Example
try {
let txid = await okxwallet.bitcoin.sendBitcoin(
'tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz',
1000
);
console.log(txid);
} catch (e) {
console.log(e);
}
send#
okxwallet.bitcoin.send({ from, to, value, satBytes })
Description
Send BTC (supports memo parameter)
Parameters
- from - string: the BTC address of the currently connected wallet
- to - string: addresses that accept BTC
- value - string: amount of BTC sent
- satBytes - string (optional): custom rate
- memo - string: (optional) specify the content of outputs OP_RETURN. Example
- memoPos - number: (optional) specify the output position of outputs OP_RETURN. If a memo is provided, the memoPos must be specified, otherwise the memo will not take effect.
Return value
- Promise - object
- txhash: transaction hash
Example
const result = await window.okxwallet.bitcoin.send({
from: 'bc1p4k9ghlrynzuum080a4zk6e2my8kjzfhptr5747afzrn7xmmdtj6sgrhd0m',
to: 'bc1plklsxq4wtv44dv8nm49fj0gh0zm9zxewm6ayzahrxc8yqtennc2s9udmcd',
value: '0.000012',
});
// example
{
txhash: 'd153136cd74512b69d24c68b2d2c715c3629e607540c3f6cd3acc1140ca9bf57';
}
sendInscription#
okxwallet.bitcoin.sendInscription(address, inscriptionId, options)
Description
Send inscription
Parameters
-
address - string: the receiver address
-
inscriptionId - string: Inscriptions ID + protocol, default is Ordinals NFT if no transmission protocol, currently only support Ordinals and Atomicals Association
Protocol Description Ordinals Ordinals Protocol Atomicals Atomicals Protocol -
options - object: (optional)
- feeRate - number: the network fee rate
Return value
- Promise - string: transaction hash
Example
// send Ordinals NFT
try {
let txid = await okxwallet.bitcoin.sendInscription(
'tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny',
'e9b86a063d78cc8a1ed17d291703bcc95bcd521e087ab0c7f1621c9c607def1ai0',
{ feeRate: 15 }
);
console.log(
'send Ordinal NFT to tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny',
{ txid }
);
} catch (e) {
console.log(e);
}
// send Atomicals NFT
try {
let txid = await okxwallet.bitcoin.sendInscription(
'tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny',
'ab12349dca49643fcc55c8e6a685ad0481047139c5b1af5af85387973fc7ceafi0-Atomicals',
{ feeRate: 15 }
);
console.log(
'send Atomicals NFT to tb1q8h8s4zd9y0lkrx334aqnj4ykqs220ss7mjxzny',
{ txid }
);
} catch (e) {
console.log(e);
}
transferNft#
okxwallet.bitcoin.transferNft({ from, to, data })
Description
Send inscription
The transferNft
method supports batch transfers, while the sendInscription
method only supports individual transfers
Parameters
-
from - string: the BTC address of the currently connected wallet
-
to - string: addresses that accept NFTs or BRC-20 tokens
-
data-string | string[]: indicates the sent NFT tokenId + protocol. If the NFT is an array, multiple NFT are transferred in batches. If no transmission protocol is used, the default NFT is Ordinals NFT. Currently, only Ordinals and Atomicals are supported
Protocol Description Ordinals Ordinals Protocol Atomicals Atomicals Protocol
Return value
- Promise - object
- txhash - string: transaction hash
Example
// send Ordinals NFT
try {
let res = await window.okxwallet.bitcoin.transferNft({
from: 'bc1p8qfrmxdlmynr076uu28vlszxavwujwe7dus0r8y9thrnp5lgfh6qu2ctrr',
to: 'bc1p8qfrmxdlmynr076uu28vlszxavwujwe7dus0r8y9thrnp5lgfh6qu2ctrr',
data: [
'2f285ba4c457c98c35dcb008114b96cee7c957f00a6993690efb231f91ccc2d9i0-Ordinals',
'2f2532f59d6e46931bc84e496cc6b45f87966b149b85ed3199265cb845550d58i0-Ordinals',
],
});
console.log(res);
} catch (e) {
console.log(e);
}
// example
{
txhash: 'df409c3ce3c4d7d840b681fab8a3a5b8e32b1600636cc5409d84d2c06365a5fc';
}
// send Atomicals NFT
try {
let res = await window.okxwallet.bitcoin.transferNft({
from: 'bc1p8qfrmxdlmynr076uu28vlszxavwujwe7dus0r8y9thrnp5lgfh6qu2ctrr',
to: 'bc1p8qfrmxdlmynr076uu28vlszxavwujwe7dus0r8y9thrnp5lgfh6qu2ctrr',
data: [
'ab12349dca49643fcc55c8e6a685ad0481047139c5b1af5af85387973fc7ceafi0-Atomicals',
],
});
console.log(res);
} catch (e) {
console.log(e);
}
// example
{
txhash: 'df409c3ce3c4d7d840b681fab8a3a5b8e32b1600636cc5409d84d2c06365a5fc';
}
signMessage#
okxwallet.bitcoin.signMessage(signStr[, type])
Description
Sign message
Parameters
- signStr - string: requires signed data
- type - string: (optional) "ecdsa" | "bip322-simple". The default value is "ecdsa". (Note: For app versions below 6.51.0, only “ecdsa” is available. For app versions equal to or above 6.51.0, all parameter types are available.)
Return value
- Promise - string: the signing result
Example
const signStr = 'need sign string';
const result = await window.okxwallet.bitcoin.signMessage(signStr, 'ecdsa')
// example
INg2ZeG8b6GsiYLiWeQQpvmfFHqCt3zC6ocdlN9ZRQLhSFZdGhgYWF8ipar1wqJtYufxzSYiZm5kdlAcnxgZWQU=
pushTx#
okxwallet.bitcoin.pushTx(rawTx)
Description
Push transaction
Parameters
- rawTx - string: rawtx to push
Return value
- Promise - string: transaction hash
Example
try {
let txid = await okxwallet.bitcoin.pushTx('0200000000010135bd7d...');
console.log(txid);
} catch (e) {
console.log(e);
}
splitUtxo#
okxwallet.bitcoin.splitUtxo({ from, amount })
Description
Spliting UTXO and initializing OKX Wallet
Splitting is required by the signature algorithm
Parameters
- object
- from - string: the BTC address of the currently connected wallet
- amount - number: (optional) the amount of splits. The default value is 2.
Return value
- Promise -
{utxos: array}
: UTXOs and signatures
Example
try {
let { utxos } = await window.okxwallet.bitcoin.splitUtxo({
from: 'bc1pkrym02ck30phct287l0rktjjjnapavkl2qhsy78aeeeuk3qaaulqh90v6s',
});
console.log(utxos);
} catch (e) {
console.log(e);
}
// example
{
utxos: [
{
txId: '1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306',
vOut: 0,
amount: 546,
rawTransaction: 'xxxx',
},
{
txId: '1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306',
vOut: 1,
amount: 546,
rawTransaction: 'xxxx',
},
];
}
inscribe#
okxwallet.bitcoin.inscribe({ type, from, tick, tid })
Description
Inscribe transferable BRC-20
Parameters
-
type - number: transaction types. Details are shown in the table below.
Type Description 51 Default value. Inscription of a BRC-20 transfer -
from - string: the BTC address of the currently connected wallet
-
tick - string: BRC-20 token name (from on-chain)
Return value
- Promise - string: transaction hash that reveals the transaction
Example
try {
let txid = await okxwallet.bitcoin.inscribe({
from: 'bc1pkrym02ck30phct287l0rktjjjnapavkl2qhsy78aeeeuk3qaaulqh90v6s',
tick: 'ordi',
});
console.log(txid);
} catch (e) {
console.log(e);
}
mint#
okxwallet.bitcoin.mint({ type, from, inscriptions })
Description
Universal inscriptions that support the Ordinal protocol
This method supports batch inscribing
Parameters
-
type - number: the type of inscribed transaction to be sent. Please refer to the table below for details.
Type Description 60 BRC-20 deploy inscription 50 BRC-20 mint inscription 51 BRC-20 transfer inscription 62 Image inscription. The image needs to be converted into a hexadecimal string representation of the image byte stream. 61 Plain text -
from - string: the BTC address of the currently connected wallet
-
inscriptions - object[]: the array of inscriptions where each array item is an object type with its respective fields and meanings as shown in the table below:
Field Type Default Description contentType string "text/plain;charset=utf-8" The type of content to be inscribed, represented by a MIME type value. As for the Ordinals protocol specifications, please refer to: https://docs.ordinals.com/inscriptions.html for details. body string No The content being inscribed the contentType and body parameters passed in for different inscription types:
Inscription Type Content Type Body image inscription eg. "image/png", "image/jpeg" The image needs to be converted into a hexadecimal string representation of the image byte stream BRC-20 "text/plain;charset=utf-8" Simply convert it to a string using JSON.stringify plain text "text/plain;charset=utf-8" Directly pass in plain text
Return value
- Promise - object: tts fields and their meanings are shown below:
- commitTx - string: the hash value of the commit transaction during inscription
- revealTxs - string[]: the hash value of the reveal transaction during inscription. For batch inscriptions, it corresponds to the hash value of each reveal transaction separately.
- commitTxFee - number: the network fee spent on the commit transaction
- revealTxFees - number[]: the network fee spent on the reveal transaction. If it's a batch inscription, it corresponds to the network fee of each reveal transaction separately.
- commitAddrs - string[]: the "to" address of the commit transaction, i.e., the delegate address
- feeRate - number: network fee rate
- size - number: the size of the inscription
Example
okxwallet.bitcoin.mint({
type: 61,
from: 'bc1p4k9ghlrynzuum080a4zk6e2my8kjzfhptr5747afzrn7xmmdtj6sgrhd0m',
inscriptions: [{
contentType: 'text/plain;charset=utf-8',
body: 'hello'
}, {
contentType: 'text/plain;charset=utf-8',
body: 'world'
}]
})
// response
{
"commitAddrs": [
"bc1p9trqtf68gfeq3f3hlktaapp0eapufh02ly8dr6swfwffflvncncqwvtuen",
"bc1p5ttl7q2mpvfhjq3wqffka4c05sv5jcfphcl5qeuj0pmsx7evfhcqhm60rk"
],
"commitTx": "453e126346bbaaef0aaaa208acd3426cd14a39f825bd76cb8d9892957e2a5bda",
"revealTxs": [
"526ff04e4ba34617ee28826412bdc8e22484890635320f880c5ec50f10d6b189",
"0f65f79456a59b3e0cd4ef00e279d0d6da57582e114eafbada95b51759a845b2"
],
"commitTxFee": 1379,
"revealTxFees": [
973,
973
],
feeRate: 80,
size: 546,
}
signPsbt#
okxwallet.bitcoin.signPsbt(psbtHex[, options])
Description
Signing psbt: this will traverse all inputs that match the current address to sign.
Parameters
- psbtHex - string: hexadecimal string representation of the partially signed bitcoin transaction (PSBT) that needs to be signed.
When you generate the psbt (string) to be signed, you need to add a public key for every input of the psbt if the input uses a Taproot address.
Example: Refer to txInput and publicKey below.
const txInputs: utxoInput[] = [];
txInputs.push({
txId: "1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306",
vOut: 2,
amount: 341474,
address: "tb1q8h8....mjxzny",
privateKey: "0s79......ldjejke",
publicKey: "tb1q8h8....mjxzny",
bip32Derivation: [
{
"masterFingerprint": "a22e8e32",
"pubkey": "tb1q8h8....mjxzny",
"path": "m/49'/0'/0'/0/0",
},
],
});
- options
- autoFinalized - boolean: whether to finalize psbt after signing — the default is true
- toSignInputs - array:
- index - number: which input to sign
- address - string: (at least specify either an address or a public key) which corresponding private key to use for signing
- publicKey - string: (at least specify either an address or a public key) which corresponding private key to use for signing
- sighashTypes - number[]: (optional) sighashTypes
- disableTweakSigner - boolean :(optional) when signing and unlocking Taproot addresses, the tweakSigner is used by default for signature generation. Enabling this allows for signing with the original private key.
- For app versions below 6.51.0 and extension versions below 2.77.1, the options feature isn’t available, and autoFinalized is defaulted as false.
- For app version 6.51.0 or above and extension version 2.77.1 or above, the options feature is available, and autoFinalizedis a boolean, defaulted as true.
Return value
- Promise - string: the hex string of the signed psbt
Example
try {
let res = await okxwallet.bitcoin.signPsbt('70736274ff01007d....', {
autoFinalized: false,
toSignInputs: [
{
index: 0,
address: 'tb1q8h8....mjxzny',
},
{
index: 1,
publicKey: 'tb1q8h8....mjxzny',
sighashTypes: [1],
},
{
index: 2,
publicKey: '02062...8779693f',
},
],
});
console.log(res);
} catch (e) {
console.log(e);
}
okxwallet.bitcoin.signPsbt('xxxxxxxx', {
toSignInputs: [{ index: 0, publicKey: 'xxxxxx', disableTweakSigner: true }],
autoFinalized: false,
});
signPsbts#
okxwallet.bitcoin.signPsbts(psbtHexs[, options])
Description
Signing psbts: this will traverse all inputs that match the current address to sign.
Parameters
- psbtHexs - string[]: the hex strings of psbts to sign
When you generate the psbt (string) to be signed, you need to add a public key for every input of the psbt if the input uses a Taproot address.
Example: Refer to txInput and publicKey below.
const txInputs: utxoInput[] = [];
txInputs.push({
txId: "1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306",
vOut: 2,
amount: 341474,
address: "tb1q8h8....mjxzny",
privateKey: "0s79......ldjejke",
publicKey: "tb1q8h8....mjxzny",
bip32Derivation: [
{
"masterFingerprint": "a22e8e32",
"pubkey": "tb1q8h8....mjxzny",
"path": "m/49'/0'/0'/0/0",
},
],
});
- options - object[]: the options of signing psbts
- autoFinalized - boolean: whether to finalize psbts after signing — the default is true
- toSignInputs - array:
- index - number: which input to sign
- address - string: (at least specify either an address or a public key) which corresponding private key to use for signing
- publicKey - string: (at least specify either an address or a public key) which corresponding private key to use for signing
- sighashTypes - number[]: (optional) sighashTypes
Return value
- Promise - string[]: the hex strings of the signed psbts
Example
try {
let res = await okxwallet.bitcoin.signPsbts([
'70736274ff01007d...',
'70736274ff01007d...',
]);
console.log(res);
} catch (e) {
console.log(e);
}
pushPsbt#
okxwallet.bitcoin.pushPsbt(psbtHex)
Description
Push psbt transaction
Parameters
- psbtHex - string: the hex string of psbt to push
Return value
- Promise - string: transaction hash
Example
try {
let res = await okxwallet.bitcoin.pushPsbt('70736274ff01007d....');
console.log(res);
} catch (e) {
console.log(e);
}
sendPsbt#
okxwallet.bitcoin.sendPsbt(txs, from)
Description
Push psbt transaction
- The
sendPsbt
method supports batch on-chain transactions, while thepushPsbt
method only supports individual on-chain transactions. - The
sendPsbt
method supports the inclusion of thetype
parameter, providing more accurate representation of transaction history within the wallet. On the other hand, transactions pushed to the chain via thepushPsbt
method may have a simpler representation in the transaction history.
Parameters
- txs - array: tx of psbts to publish
- from - string: the BTC address of the currently connected wallet
Type | Description |
---|---|
52 | Send BRC-20 |
20 | Send NFT |
Return value
- Promise - array: transaction hash
Example
okxwallet.bitcoin.sendPsbt(
[
{
itemId: 'xxxxx0', //Batch unique identification, no duplicates within multiple transactions
signedTx: '70736274ff01007d....', // Signature string
type: 52, // BRC-20 52 or NFT 20
extJson: {
//Split utxo transactions, not transmitted
// NFTID
inscription:
'885441055c7bb5d1c54863e33f5c3a06e5a14cc4749cb61a9b3ff1dbe52a5bbbi0',
},
},
{
itemId: 'xxxxx1', //Batch unique identifier
signedTx: '70736274ff01007d....', // Signature string or PSBT to be linked
type: 52, // BRC-20 52 or NFT 20
dependItemId: ['xxxxx0 '], //The dependent transaction itemId. If there is no dependency, this field may not be passed
extJson: {
// NFTID
inscription:
'885441055c7bb5d1c54863e33f5c3a06e5a14cc4749cb61a9b3ff1dbe52a5bbbi0',
},
},
],
from
)[
// response
({ xxxxx0: 'txId1' }, { xxxxx1: 'txId2' }) //Failure txId returns null
];
accountChanged#
Description
OKX Wallet allows you to seamlessly manage multiple accounts from a single extension or mobile application. Whenever you switch accounts, OKX Wallet will send an accountChanged
event.
If you switch accounts while still connected to the application, and if the new account has placed the application on the allowlist, you will remain connected and OKX Wallet will pass the public key of the new account:
Usage
window.okxwallet.bitcoin.on('accountChanged', (addressInfo) => {
console.log(addressInfo);
// example
{
"address": "bc1pwqye6x35g2n6xpwalywhpsvsu39k3l6086cvdgqazlw9mz2meansz9knaq",
"publicKey": "4a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497",
"compressedPublicKey": "034a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497"
}
});
accountsChanged#
Description
The accountsChanged will be emitted whenever the user's exposed account address changes.
Usage
window.okxwallet.bitcoin.on('accountsChanged', (accounts) => {
console.log(accounts)[
// example
'tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'
];
});