The Missing Local Devnet for Developing Bitcoin DApps
How to Set Up Regtest in Leather to Speed Up Your Bitcoin DApp Development
If you have been using Bitcoin’s testnet recently, you would know that it has been quite congested. It’s hard to get tBTC, and signet is fast, but the faucet for signet is always out of service. Additionally, testnet3 is about to be deprecated, but testnet4 is not ready yet. This creates a problem for the rapid development and debugging of DApps on Bitcoin.
This is a problem that has been troubling us too, so we thought we’d share some of our attempts to solve it, and together, we can push the development of the Bitcoin ecosystem forward.
Quick Answer
For those who want a quick solution, you can just download a docker-compose file from this link. After you start it up, run a few commands, and then you can simply switch the Leather wallet to Devnet for testing.
Command 1: Set up some block data, so the api can work correctly
docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet createwallet default
docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet generatetoaddress 101 $(docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet getnewaddress)
Command 2: Generate a block docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet generatetoaddress 1 $(docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet getnewaddress)
Command 3: Send testing bitcoins to an account docker exec --user bitcoin bitcoind bitcoin-cli --regtest --rpcuser=devnet --rpcpassword=devnet -named sendtoaddress amount=100 fee_rate=1 address=<your_address>
Command 4: If it has been restarted, you need to reload the wallet and generate at least one block
docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet loadwallet default
docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet generatetoaddress 1 $(docker exec --user bitcoin bitcoind bitcoin-cli -regtest --rpcuser=devnet --rpcpassword=devnet getnewaddress)
A mempool explorer is also included at http://localhost:8083 . You will happy to see a lot of testing bitcoins in your leather, just like the image shown at the start of this post.
Technical Details
Now let’s dive into the details. These steps will give you a better understanding of how to setup this developing environment.
As we all known, bitcoin’s regtest is a handy local development testnet, but it cannot be directly used with a DApp. After trying almost all the browser wallets on the market, we were happy to find Leather, which directly supports switching to sBtcDevenv or Devnet. The next step was to figure out how to connect this wallet to the local regtest network.
The development environment for sBTC can be found in this repository and this repository. Regrettably, the latter repository did not compile successfully. Although the former repository compiled successfully, the exposed port 8999 is not an API that Leather can use. However, upon reviewing both repositories, it was discovered that neither provided the API needed by Leather. Additionally, we were unable to find any instructions on how to set up a devnet for Leather.
So we decided to start from the basics.
First, by opening the developer console for Leather, we found that Leather calls two types of APIs — one is Blockstream’s API, used for Bitcoin-related queries, and the other is Stacks’ API.
When we switch to Devnet, we can see that Blockstream’s API is switched to port 18443, and Stacks’s API is switched to 3999.
So, the next step is to run services on these two ports.
Blockstream’s API service can be found in this repository. There are many versions of electrs, and mempool also forked one, but their APIs are different, so we can only use Blockstream’s version.
Stacks’ API service can be found in this repository, but after testing, if you only need Bitcoin development, you don’t need to start this service.
So the final containers need to be started are:
- A bitcoin-core that runs regtest
- A blockstream electrs that connects to regtest and exposes api at 18443 for Leather
- An esplora api and a db for mempool, then the mempool API and frontend
We hope this guide will help you in developing your Bitcoin DApp!