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

Installation and Setup

To get started with the useBitcoin hook from the Zky Toolkit, follow these steps:

  1. Install the Zky Toolkit package: Ensure you have the toolkit installed in your project by running:

    npm install @kondor-finance/zky-toolkit
    2
  • 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. See a full example creating PSBT

        • broadcast (boolean): The allowed sign hash type. Optional, default = false

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

          • the keys are the addresses to use for signing

          • the values are the indexes of the inputs to sign with each address.

        • Output: Returns a signed PSBT as a promise.

          • psbt (string): The PSBT signed.

          • txId(string): The transaction id if the transaction was broadcasted.

      • Example:

        import { useZky, useBitcoin } from "@kondor-finance/zky-toolkit";
        import { useCallback, useState } from "react";
        
        export default function SignPsbt() {
          const { publicKeys, addresses } = useZky();
          const [ psbtSigned, setPsbtSigned ] = useState<string | null>(null);
          const { signPsbt } = useBitcoin();
        
        
          const handleSignPsbt = async () => {
              const recipient = "tb1..52"
              const response = await signPsbt({
                psbt: psbtB64,
                signInputs: { [addresses.ordinalsAddress!]: [0] },
              })
              console.log("PSBT signed:", response);
          };
        
          return (
            <div>
              <p>Psbt Signed:  {psbtSigned}</p>
                <button onClick={handleSignPsbt} >
                  Sign BTC Psbt
                </button>
              </div>
          );
        }
        
  • 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