OKX Injected Providers API (Fractal Bitcoin) is based on a JavaScript model and is embedded by OKX into websites visited by users.
DApp projects can call this API to request user account information, read data from the blockchain the user is connected to, and assist the user in signing messages and transactions.
Description
Connects the wallet
okxwallet.fractalBitcoin.connect()
Parameters
None
Return Value
Example
const result = await okxwallet.fractalBitcoin.connect()
// example
{
address: 'bc1pwqye6x35g2n6xpwalywhpsvsu39k3l6086cvdgqazlw9mz2meansz9knaq',
publicKey: '4a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
compressedPublicKey:'034a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
}
okxwallet.fractalBitcoin.requestAccounts()
Description
Requests to connect the current account
Parameters
None
Return Value
Promise - string[]: The current account's address
Example
try {
let accounts = await okxwallet.fractalBitcoin.requestAccounts();
console.log('connect success', accounts);
} catch (e) {
console.log('connect failed');
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
okxwallet.fractalBitcoin.getAccounts()
Description
Retrieves the current account address
Parameters
None
Return Value
Promise - string[]: The current account address
Example
try {
let res = await okxwallet.fractalBitcoin.getAccounts();
console.log(res);
} catch (e) {
console.log(e);
}
// example
['tb1qrn7tvhdf6wnh790384ahj56u0xaa0kqgautnnz'];
okxwallet.fractalBitcoin.getPublicKey()
Description
Retrieves the public key of the current account
Parameters
None
Return Value
Promise - string: Public key
Example
try {
let res = await okxwallet.fractalBitcoin.getPublicKey();
console.log(res)
} catch (e) {
console.log(e);
}
// example
03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f
okxwallet.fractalBitcoin.getBalance()
Description
Retrieves the BTC balance
Parameters
None
Return Value
Example
try {
let res = await okxwallet.fractalBitcoin.getBalance();
console.log(res)
} catch (e) {
console.log(e);
}
// example
{
"confirmed":0,
"unconfirmed":100000,
"total":100000
}
okxwallet.fractalBitcoin.signMessage(signStr[, type])
Description
Signs a message
Parameters
Return Value
Example
const signStr = 'need sign string';
const result = await window.okxwallet.fractalBitcoin.signMessage(signStr, 'ecdsa')
// example
INg2ZeG8b6GsiYLiWeQQpvmfFHqCt3zC6ocdlN9ZRQLhSFZdGhgYWF8ipar1wqJtYufxzSYiZm5kdlAcnxgZWQU=
okxwallet.fractalBitcoin.signPsbt(psbtHex[, options])
Description
Signs a psbt, this method will sign all inputs matching the current address
Parameters
Example: Refer to the 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 the psbt is finalized after signing, default is true
- toSignInputs - array:
- index - number: Input to be signed
- address - string: Address corresponding to the private key used for signing
- publicKey - string: Public key corresponding to the private key used for signing
- sighashTypes - number[]: (Optional) sighashTypes
- disableTweakSigner - boolean: (Optional) When signing and unlocking Taproot addresses, tweakSigner is used by default to generate signatures. Enabling this option allows signing with the raw private key.
Return Value
Example
try {let res = await okxwallet.fractalBitcoin.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.fractalBitcoin.signPsbt('xxxxxxxx', {
toSignInputs: [{ index: 0, publicKey: 'xxxxxx', disableTweakSigner: true }],
autoFinalized: false,});
okxwallet.fractalBitcoin.signPsbts(psbtHexs[, options])
Description
Signs multiple PSBTs. This method will iterate through all inputs that match the current address for signing.
Parameters
Example: You can refer to the following txInput and publicKey
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[]: Options for signing the PSBT.
- autoFinalized - boolean: Whether to finalize the PSBT after signing, default is true.
- toSignInputs - array:
- index - number: The input to be signed.
- address - string: The address corresponding to the private key used for signing.
- publicKey - string: The public key corresponding to the private key used for signing.
- sighashTypes - number[]: (Optional) sighashTypes.
Return Value
Example
try {
let res = await okxwallet.fractalBitcoin.signPsbts([
'70736274ff01007d...',
'70736274ff01007d...',
]);
console.log(res);
} catch (e) {
console.log(e);
}
okxwallet.fractalBitcoin.pushPsbt(psbtHex)
Description
Parameters
Return Value
Example
try {
let res = await okxwallet.fractalBitcoin.pushPsbt('70736274ff01007d....');
console.log(res);
} catch (e) {
console.log(e);
}
okxwallet.fractalBitcoin.pushTx(rawTx)
Description
Pushes a transaction.
Parameters
Return Value
Example
try {
let txid = await okxwallet.fractalBitcoin.pushTx('0200000000010135bd7d...');
console.log(txid);
} catch (e) {
console.log(e);
}