Skip to content

Commit

Permalink
Add sending Bitcoin example
Browse files Browse the repository at this point in the history
  • Loading branch information
kenrogers committed Jul 4, 2023
1 parent d4fabc5 commit 056f939
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/cookbook/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Got an idea you think would make a good addition? We're always [accepting contri

## Bitcoin Integration

[Sending Bitcoin with Hiro wallet](./sending-bitcoin-with-hiro-wallet.md)

Getting a Stacks address from a public key

[Verifying a transaction on the BTC chain](./verifying-a-btc-tx-was-mined.md)
Expand Down
38 changes: 38 additions & 0 deletions docs/cookbook/sending-bitcoin-with-hiro-wallet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Sending Bitcoin with Hiro Wallet
description: Initiate a basic Bitcoin transaction with the Hiro wallet
---

Using Hiro's web wallet, we can easily initiate a simple Bitcoin transaction using only a few lines of code.

You'll need to be authenticated with the Hiro wallet for this to work, with you can see how to do in the [Authentication with Stacks.js](./stacks-js-auth) recipe.

Once you have the wallet hooked up, you can use the Hiro wallet API to initiate a simple Bitcoin transaction in your JS app like this.

```javascript
const sendBitcoin = async () => {
const resp = await window.btc?.request("sendTransfer", {
address: "tb1qya9wtp4dyq67ldxz2pyuz40esvgd0cgx9s3pjl", //replace this with whatever address you want to send to
amount: "10000", // the amount you want to send denoted in satoshis
});

// Storing txid in local storage
// We'll get back the transaction IF, which we can then use to do whatever we want
if (typeof window !== "undefined") {
localStorage.setItem("txid", JSON.stringify(resp.result.txid));
}

// We may want to do something once this transaction has confirmed, so we can set it to pending here and then use an API like mempool.space to query the Bitcoin chain for information about this transaction
localStorage.setItem("txStatus", "pending");
};
```
Then all we would do is hook up our button to call this `sendBitcoin` function.
```javascript
<button onClick={sendBitcoin}>Send Bitcoin</button>
```
You can take a look at the [Verifying a transaction on the BTC chain](./verifying-a-btc-tx-was-mined.md) recipe to see a more complex user flow of verifying a transaction was mined using this returned ID as a starting point.
You can take a look at a bit more info about this simple API on the [Hiro wallet developer docs](https://hirowallet.gitbook.io/developers/bitcoin/sign-transactions/sending-bitcoin).

1 comment on commit 056f939

@vercel
Copy link

@vercel vercel bot commented on 056f939 Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.