Connect to Extension Wallet
Provider API (Fractal Bitcoin)

Provider API (Fractal Bitcoin)#

What is Injected Provider API (Fractal Bitcoin)?#

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.

Note: Fractal Bitcoin is only available for wallet plugin versions 3.12.1 or higher.

connect#

Description

Connects the wallet

okxwallet.fractalBitcoin.connect()

Parameters

None

Return Value

  • Promise - object
    • address - string: The current account's address
    • publicKey - string: The public key of the current account

Example

const result = await okxwallet.fractalBitcoin.connect()
// example
{
  address: 'bc1pwqye6x35g2n6xpwalywhpsvsu39k3l6086cvdgqazlw9mz2meansz9knaq',
  publicKey: '4a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
  compressedPublicKey:'034a627f388196639041ce226c0229560127ef9a5a39d4885123cd82dc82d8b497',
}

requestAccounts#

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'];

getAccounts#

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'];

getPublicKey#

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

getBalance#

okxwallet.fractalBitcoin.getBalance()

Description

Retrieves the BTC balance

Parameters

None

Return Value

  • Promise - object:
  • confirmed - number: Amount of confirmed satoshis
  • unconfirmed - number: Amount of unconfirmed satoshis
  • total - number: Total amount of satoshis

Example

try {
  let res = await okxwallet.fractalBitcoin.getBalance();
  console.log(res)
} catch (e) {
  console.log(e);
}
// example
{
  "confirmed":0,
  "unconfirmed":100000,
  "total":100000
}

signMessage#

okxwallet.fractalBitcoin.signMessage(signStr[, type])

Description

Signs a message

Parameters

  • signStr - string: The data to be signed
  • type - string: (Optional) "ecdsa" | "bip322-simple", default is "ecdsa". (Note: Versions below 6.51.0 only support "ecdsa" signing algorithm, while versions 6.51.0 or higher support all signature types.)

Return Value

  • Promise - string: Signed result

Example

const signStr = 'need sign string';
const result = await window.okxwallet.fractalBitcoin.signMessage(signStr, 'ecdsa')
// example
INg2ZeG8b6GsiYLiWeQQpvmfFHqCt3zC6ocdlN9ZRQLhSFZdGhgYWF8ipar1wqJtYufxzSYiZm5kdlAcnxgZWQU=

signPsbt#

okxwallet.fractalBitcoin.signPsbt(psbtHex[, options])

Description

Signs a psbt, this method will sign all inputs matching the current address

Parameters

  • psbtHex - string: Hexadecimal string of the psbt to be signed
Note: When building a transaction to generate a psbt (string type), if the input address is of Taproot type, a public key is required.

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

  • Promise - string: Hexadecimal string of the signed psbt

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,});

signPsbts#

okxwallet.fractalBitcoin.signPsbts(psbtHexs[, options])

Description

Signs multiple PSBTs. This method will iterate through all inputs that match the current address for signing.

Parameters

  • psbtHexs - string[]: The hexadecimal strings of the PSBTs to be signed.
Note: When generating the PSBT (string type), if the input address is Taproot type, a public key is required.

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

  • Promise - string[]: The hexadecimal strings of the signed PSBTs.

Example

try {
  let res = await okxwallet.fractalBitcoin.signPsbts([
    '70736274ff01007d...',
    '70736274ff01007d...',
  ]);
  console.log(res);
} catch (e) {
  console.log(e);
}

pushPsbt#

okxwallet.fractalBitcoin.pushPsbt(psbtHex)

Description

Broadcasts a PSBT transaction.#

Parameters

  • psbtHex - string: The hexadecimal string of the PSBT to be pushed.

Return Value

  • Promise - string: The transaction hash.

Example

try {
  let res = await okxwallet.fractalBitcoin.pushPsbt('70736274ff01007d....');
  console.log(res);
} catch (e) {
  console.log(e);
}

pushTx#

okxwallet.fractalBitcoin.pushTx(rawTx)

Description

Pushes a transaction.

Parameters

  • rawTx - string: The raw transaction to be pushed on-chain.

Return Value

  • Promise - string: The transaction hash.

Example

try {
  let txid = await okxwallet.fractalBitcoin.pushTx('0200000000010135bd7d...');
  console.log(txid);
} catch (e) {
  console.log(e);
}