Skip to main content

How to Sign Messages, get the balance and send transactions on Bitcoin using AppKit

Learn how to use Reown AppKit for essential wallet functionalities such as signing messages, getting the balance and sending transactions.


In this recipe, you will learn how to:

  • Sign a message using a connected wallet.
  • Send a transaction to the Bitcoin blockchain.
  • Get the balance from an Address.

This guide takes approximately 20 minutes to complete.

Let's dive in!

Prerequisites

  • A fundamental understanding of JavaScript and React.
  • A minimal installation of AppKit in React.
  • Obtain a new project Id on Reown Cloud at https://cloud.reown.com

Final project

AppKit Minimal Installation

You can start a small project following the guidelines from our installation React docs using Bitcoin

Start building

In this guide we are going to use AppKit to make the calls to the Bitcoin blockchain and interact with the wallet.

  1. Start by importing the useAppKitProvider and useAppKitAccount hooks.
import { useAppKitProvider, useAppKitAccount } from '@reown/appkit/react';
import type { BitcoinConnector } from '@reown/appkit-adapter-bitcoin';
  1. Extract the walletProvider function from the useAppKitProvider hook. This function allows you to prompt the connected wallet to sign a specific message. Also, we are using useAppKitAccount to get the address and isConnected as explain before.
// Get the wallet provider with the AppKit hook
const { walletProvider } = useAppKitProvider<BitcoinConnector>('bip122');

// AppKit hook to get the address and check if the user is connected
const { address, isConnected } = useAppKitAccount()

Sign a message

In order to raise the modal to sign a message with your wallet continue with the following steps:

  1. Create a function to prompt the modal for signing the message.
// function to sing a message
const handleSignMsg = async () => {
// raise the modal to sign the message
const signature = await walletProvider.signMessage({
address,
message: "Hello Reown AppKit!"
});

// Print the signed message in console
console.log(signature);
}
  1. Finally, in order to call the function:
return (
isConnected && (
<div >
<button onClick={handleSignMsg}>Sign Message</button>
</div>
)
)

Send a transaction in Bitcoin

  1. Create the function to raise the modal to send the transaction
// function to send a TX
const handleSendTx = () => {
const signature = await walletProvider.sendTransfer({
recipient: recipientAddress,
amount: "1000" // amount in satoshis
})

// print the Transaction Signature in console
console.log(signature);
}
  1. Finally, in order to call the function:
return (
isConnected && (
<div >
<button onClick={handleSendTx}>Send Transaction</button>
</div>
)
)

Get Balance

  1. Create the function to get the balance

const handleGetBalance = () => {
const isTestnet = true; // change to false if you want to get the balance in mainnet

// get all the utxos from the address
const response = await fetch(
`https://mempool.space${isTestnet ? '/testnet' : ''}/api/address/${address}/utxo`
);
const data = await response.json();

// get the utxos - the list of unspent transactions that the sender has
const utxos = await getUTXOs(address, isTestnet(caipNetwork.caipNetworkId))
// return the sum of the utxos ... The balance of the sender
const balance = utxos.reduce((sum, utxo) => sum + utxo.value, 0)

// print the balance in console
console.log(balance);
}
  1. Finally, in order to call the function:
return (
isConnected && (
<div >
<button onClick={handleGetBalance}>Get Balance</button>
</div>
)
)

Conclusion

By following this guide, you've learned how to integrate Reown AppKit and Bitcoin into your React application to perform essential wallet operations. You can now sign messages, get the balance and send transactions in the Bitcoin blockchain.

Keep exploring AppKit to enhance your dApp's functionality and user experience!