decoration back

Python + Bitcoin Cash = ๐Ÿ˜

merc1er
merc1er
December 10 2019

This article shows off how easy it is for a developer to integrate Bitcoin Cash payments in their app with the Python programming language using a tool called BitCash.

Developing with Bitcoin is hard... ๐Ÿ˜ฐ

I love Python for its simplicity and intuitiveness.

By contrast, interacting with Bitcoin through software can often be complex and intimidating. You are usually required to know how Bitcoin works to even start using a library.

For example, in the "getting started" doc page of one of the most popular BCH libraries you are confronted with terms like ECPair, HDNode, Mnemonic or Schnorr.

I believe that, in the future, most people should not have to be familiar with these technical concepts to interact with Bitcoin.

...unless you have the right tools! ๐Ÿ”จ

I came across an amazing Python library[2] that removes all this complexity and allows users to integrate Bitcoin inside their Python app easily. The subtitle reads:

Bit is Pythonโ€™s fastest Bitcoin library and was designed from the beginning to feel intuitive, be effortless to use, and have readable source code. It is heavily inspired by Requests and Keras.

As a requests (Python HTTP library) user myself, I think the comparison is excellent: most requests users have no idea how an HTTP request works under the hood. But the library is so well designed that it is not required to know this to be able to use it effectively.

Let's get our hands dirty

Now, this "Bit" library got forked into BitCash to be compatible with Bitcoin Cash. Let me show you how easy it is to use it:

First, you want to install it using PyPi. To do so, open a terminal and run the following command:

Once complete, open up you favorite text editor or type python3 in your terminal, and create a wallet like so:

If everything worked correctly, it should have printed a Bitcoin Cash address. Congratulations! You just created a wallet ๐Ÿ˜€

Check your balance โš–๏ธ

Now that you have a wallet, send a few cents to the address printed. Note: you can use testnet coins instead by simply replacing Key with PrivateKeyTestnet.

You will want to check if the money has arrived:

By default, it will return the amount in satoshis. If you are like me and do not know how much 1 satoshi is in your local currency, use:

Send money ๐Ÿ’ธ

When you have a few satoshis in your wallet, do the following:

BOOM! That was easy, wasn't it?

There's more! You can send in US Dollars, Euros, and many other currencies:

โš ๏ธ Use key.get_balance() or key.get_unspents() before calling send to refresh the balance (it refreshes something call the UTXO set necessary to create a transaction) โš ๏ธ

You can also send to multiple recipients at once (do not forget the coma at the end of a line as output is a list):

Note that key.send() returns the transaction ID. You can view the detailed transaction in a block explorer like explorer.bitcoin.com.

Conclusion

There are so many more thing you can do with BitCash, like for example import private keys or send memos (memo = small piece of text or data) to the blockchain.

If you find BitCash interesting, learn more by reading the docs here.