Python + Bitcoin Cash = 😍

6 1049
Avatar for merc1er
4 years ago

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[1] 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"[3] 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:

pip install bitcash

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

from bitcash import Key

key = Key()
print(key.address)

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:

# your previous code.....
print(key.get_balance())

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:

print(key.get_balance('usd'))
# or
print(key.get_balance('eur'))
# you understood, simply pass your currency's ticker in argument

Send money 💸

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

output = [
    ('bitcoincash:qr02vc2t5yr9fe4ujdpkg99d5d0dgxstfqtgxg7umu', 2000, 'satoshi')
]

key.send(output)

BOOM! That was easy, wasn't it?

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

key.get_balance()
key.send([('bitcoincash:qr02vc2t5yr9fe4ujdpkg99d5d0dgxstfqtgxg7umu', 0.10, 'usd'])
key.get_balance()
key.send([('bitcoincash:qr02vc2t5yr9fe4ujdpkg99d5d0dgxstfqtgxg7umu', 0.10, 'eur'])

⚠️ 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):

key.get_balance()
output = [
    ('bitcoincash:qr02vc2t5yr9fe4ujdpkg99d5d0dgxstfqtgxg7umu', 2000, 'satoshi'),
    ('bitcoincash:qqgw6z9n7k8pvfy5wdxvh8ake23vl072qup0v6zwpv', 1000, 'jpy'),
]
key.send(output)

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 at https://sporestack.github.io/bitcash/.

[1](https://developer.bitcoin.com/bitbox/docs/getting-started)

[2](https://ofek.dev/bit/)

[3](https://sporestack.github.io/bitcash/)

Sponsors of merc1er
empty
empty
empty

4
$ 5.23
$ 1.01 from @Telesfor
$ 1.00 from @Read.Cash
$ 1.00 from @Morti
+ 11
Avatar for merc1er
4 years ago

Comments

Awesome work!

$ 0.00
4 years ago

A very good introduction for developers.

$ 0.00
4 years ago

Excellent article!

$ 0.00
4 years ago

I can use this library for doing my magic for sure :)))

$ 0.00
4 years ago

Very interesting. I learned from your article too that one can use Unicode emojis in posts. Thank you!

$ 0.00
4 years ago