On the 31st of May, we released version 0.1.0 of Bitcoin-S. Bitcoin-S is a feature-rich toolkit for making Bitcoin and Lightning Applications, written in Scala. It can be used on all JVM platforms, such as Java, Scala, Kotlin, Groovy and Clojure.
This is the first proper release of the library on Maven Central, making it available to all users of JVM languages. With this release we also have brought big improvements to the documentation of the library. The main effort here has been the brand new Bitcoin-S.org website. This website contains guides to using the Bitcoin-S libraries, the complete Scaladocs and contact information on how to get in touch with the Bitcoin-S developers. Our goal with Bitcoin-S is to be a modern and safe alternative to libraries like BitcoinJ, and we hope that this latest release makes it easier for more developers to get started building Bitcoin applications on the JVM.
In the remainder of this blog post, we want to show some examples of how to implement common Bitcoin tasks with Bitcoin-S.
Generating addresses (SegWit and legacy)
Generating native SegWit addresses in the bech32 format is something that all Bitcoin applications should enable, as it makes the transaction fees less expensive, and also makes the addresses more readable by humans. However, it has seen slower than necessary adoption. With Bitcoin-S you can generate bech32 addresses in four(!) lines of code (not counting comments and imports), so now there’s no reason to keep using legacy transaction formats.
If you need to generate legacy addresses for backwards compatability reasons, that’s also a walk in the park. Take a look:
Making and signing a transaction you can submit to the Bitcoin network
Constructing and signing raw bitcoin transactions is a notoriously hard task. With Bitcoin-S, this becomes something anyone can do, without shooting themselves in the foot along the way. Here’s how:
Querying your Bitcoin Core full node
When working with Bitcoin applications, a common task to accomplish is connecting to a service like Bitcoin Core, and use that for tasks like generating addresses, verifying payments and monitoring the blockchain. This typically happens through tools like bitcoin-cli
, or the Bitcoin Core HTTP RPC server interface. One big drawback to this, is that you lose all type-safety in your application. Even if you have a custom type that represents a Bitcoin transaction, how do you get that to play nicely with the result that Bitcoin Core gives you after signing a transaction? A random hexadecimal string in a HTTP response could be anything from a public key, a transaction or a block header.
Here’s a small example showing how easy it is to get started with the Bitcoin-S RPC client for Bitcoin Core:
Using Hierarchical Deterministic key generation with mnemonic wallet seeds
In modern Bitcoin wallets, users only need to write down a sequence of words, and that sequence is a complete backup of their wallet. This is thanks to what’s called Hierarchical Deterministic key generation. In short, every wallet using HD key generation has a root seed for each wallet, and this seed can be used to generate an arbitrary amount of later private and public keys. This is done in a standardized manner, so different wallets can operate with the same standard. If you want to jump into the details of how this work, you should check out BIP 32.
Bitcoin-S supports generating keys in this fashion. Here’s a full example of how to obtain a wallet seed, and then use that to generate further private and public keys:
One the coolest features of HD wallets is that it’s possible to generate addresses offline, without having access to the private keys. This feature is commonly called watch-only wallets, where a wallet can import information about all your past and future transactions, without being able to spend or steal any of your money.
Let’s see an example of this:
We hope you are as excited as we are about Bitcoin-S, and that you have seen enough to get started with building your own applications. Take a look at Bitcoin-S.org for more documentation and guides, and feel free to reach out to us with any questions.
Contact us @Suredbits.