Python + Bitcoin Cash = 😍
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.
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
I believe that, in the future, most people should not have to be familiar with these technical concepts to interact with Bitcoin.
I came across an amazing Python library that removes all this complexity and allows users to integrate Bitcoin inside their Python app easily. The subtitle reads:
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.
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:
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 😀
Now that you have a wallet, send a few cents to the address printed. Note: you can use testnet coins instead by simply replacing
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
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'])
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)
key.send() returns the transaction ID. You can view the detailed transaction in a block explorer like explorer.bitcoin.com.
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/.
...and you will also help the author collect more tips.