-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from reown-com/feat/add-bitcoin-guide
feat: add bitcoin guide
- Loading branch information
Showing
3 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
--- | ||
title: Send a Bitcoin Transaction Using AppKit Web | ||
--- | ||
|
||
import GithubBox from '../../components/GithubBox' | ||
|
||
# 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 will take 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 | ||
<GithubBox name="Appkit Example for Interacting with the Bitcoin Blockchain" url="https://github.com/reown-com/appkit-web-examples/tree/main/react/react-bitcoin" description="Clone this Github repo to try it out locally."></GithubBox> | ||
|
||
## AppKit Minimal Installation | ||
|
||
You can start a small project following the guidelines from our [installation React docs using Bitcoin](https://docs.reown.com/appkit/react/core/installation?platform=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. | ||
|
||
```js | ||
import { useAppKitProvider, useAppKitAccount } from '@reown/appkit/react'; | ||
import type { BitcoinConnector } from '@reown/appkit-adapter-bitcoin'; | ||
``` | ||
|
||
2. 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 explained before. | ||
```js | ||
// 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: | ||
|
||
3. Create a function to prompt the modal for signing the message. | ||
|
||
```js | ||
// function to sign 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); | ||
} | ||
``` | ||
|
||
4. Finally, to call the function: | ||
|
||
```jsx | ||
return ( | ||
isConnected && ( | ||
<div > | ||
<button onClick={handleSignMsg}>Sign Message</button> | ||
</div> | ||
) | ||
) | ||
``` | ||
|
||
### Send a transaction in Bitcoin | ||
|
||
3. Create a function to raise the modal to send the transaction | ||
|
||
```js | ||
// 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); | ||
} | ||
``` | ||
|
||
4. Finally, to call the function: | ||
|
||
```jsx | ||
return ( | ||
isConnected && ( | ||
<div > | ||
<button onClick={handleSendTx}>Send Transaction</button> | ||
</div> | ||
) | ||
) | ||
``` | ||
|
||
### Get Balance | ||
|
||
3. Create the function to get the balance | ||
|
||
```js | ||
|
||
const handleGetBalance = () => { | ||
const isTestnet = true; // change to false if you want to get the balance on 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); | ||
} | ||
``` | ||
|
||
4. Finally, to call the function: | ||
|
||
```jsx | ||
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 functionality and user experience! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters