useBitcoin Hook

The useBitcoin hook is a part of the Zky Toolkit that allows you to interact with Bitcoin-related functionalities, including signing messages, signing PSBTs, and sending Bitcoin transfers.

Overview

The useBitcoin hook provides methods that require the wallet name to be set in order to perform various actions with a connected Bitcoin wallet. The hook internally manages the walletName retrieval and throws an error if no wallet is connected.

Available Methods

The useBitcoin hook returns an object with the following methods:

  • signMessage: Signs a message using the connected Bitcoin wallet.

  • signPsbt: Signs a Partially Signed Bitcoin Transaction (PSBT).

  • sendTransfer: Sends a Bitcoin transfer to a specified recipient.

Example Usage

Here is an example of how to use the useBitcoin hook in your components:

import { useBitcoin } from "@kondor-finance/zky-toolkit";

const ExampleComponent = () => {
  const { signMessage, signPsbt, sendTransfer } = useBitcoin();

  const handleSendTransfer = () => {
    sendTransfer(2N3FqSKBPmM6XUxSftdAmVASt4jmFGdPygF, "100000")
      .then((txId) => {
        console.log("Transaction successful with ID:", txId);
      })
      .catch((error) => {
        console.error("Failed to send transaction:", error);
      });
  };

  return (
    <button onClick={handleSendTransfer}>
      Send Bitcoin
    </button>
  );
};

Method Details

  • signMessage

    • Input: message (string) - The message you want to sign.

    • Output: Returns a signed message as a promise.

    • Example:

      signMessage("Hello, Bitcoin!")
        .then((signature) => {
          console.log("Signed message:", signature);
        })
        .catch((error) => {
          console.error("Error signing message:", error);
        });
  • signPsbt

    • Input:

      • psbtBase64 (string): The PSBT in base64 format.

      • allowedSignHash (number): The allowed sign hash type.

      • signInputs (Record<string, number[]>): A mapping of inputs to be signed.

    • Output: Returns a signed PSBT as a promise.

    • Example:

      signPsbt(psbtBase64, allowedSignHash, signInputs)
        .then((signedPsbt) => {
          console.log("Signed PSBT:", signedPsbt);
        })
        .catch((error) => {
          console.error("Error signing PSBT:", error);
        });

  • sendTransfer

    • Input:

      • recipient (string): The recipient's Bitcoin address.

      • amount (string): The amount to send in satoshis.

    • Output: Returns a transaction ID as a promise.

    • Example:

      sendTransfer("recipientBitcoinAddress", "100000")
        .then((txId) => {
          console.log("Transaction ID:", txId);
        })
        .catch((error) => {
          console.error("Error sending transfer:", error);
        });

Prerequisites

To use the useBitcoin hook, ensure that:

  • Zky Toolkit is set up as described in the Quickstart section.

  • A wallet is connected using the ZkyToolkitProvider

Last updated