read.cash Log in

@merc1er

Joined 8 December 2019 · 2 posts

Web. Bitcoin.

120 KT

0 KT · $8.57 received · 0 KT · $3.89 given

Posts

@merc1er

Python + Bitcoin Cash | Part 2 This article is **part 2** of my introduction to **BitCash**, a Bitcoin Cash software library for the Python programming language. https://ofek.github.io/bit/guide/intro.html#why-bit You can find part 1 here 👍🏻. https://read.cash/@merc1er/python-bitcoin-cash-a61e2bfe#developing-with-bitcoin-is-hard A quick recap Let's refresh our memory with the basic features we saw so far: Import the `Key` class and create a wallet: from bitcash import Key key = Key() Display the address and balance of the wallet (in USD): print(key.address) print(key.get_balance('usd')) Send 10 cents (notice the list of tuples - that is because you can send to multiple addresses at once if you want): key.send([('bitcoincash:qr02vc2t5yr9fe4ujdpkg99d5d0dgxstfqtgxg7umu', 0.1, 'usd')]) Good. Now let's do some more advanced stuff 😎 Now, one major issue is if you close your terminal, **your funds are gone**! We don't want that... Let's see how to backup your private key. Note: the private key is what allows you to spend the coins. If you lose it, the funds are lost. print(key.to_wif()) The `to_wif()` method returns the WIF - the Wallet Import Format. It is a human readable format of the private key. Save this somewhere, and if you accidentally close your terminal or your computer turns off, you can easily recover your wallet like so: https://en.bitcoin.it/wiki/Wallet_import_format recovered_key = Key('YOUR_WIF_HERE') Some examples 👨🏻‍💻 Show pair Let me show you what a basic script with BitCash could be: from bitcash import PrivateKeyTestnet def show_pair(): key = PrivateKeyTestnet() wif = key.to_wif() address = key.address return wif + ' is the private key associated with ' + address print(show_pair()) # returns: # cPQ6D6ad4ejj1xNuH5ApqM3QijtjL3pZwwkEM3Mxdmtts1fmHZtb is the private key associated with bchtest:qq25uvnlvacamvmuqulp2c3k7pgal0x3xucenm9cdv Satoshi's Shotgun ⚠️ We will need to send transactions from now on. I am using the `PrivateKeyTestnet` class instead of `Key` so that I can send testnet coins, which have no value. It is a good practice for learning in order to avoid losing funds ⚠️ Now, a **"Satoshi's Shotgun"**- a script that aims to 'flood' the block chain with a lot of transactions: import time from bitcash import PrivateKeyTestnet KEY = PrivateKeyTestnet('cRxqxzA7HPDzPxWvvE5HGDMZRQYwfwKyoWRzEdHxKREEo93yS4dd') ADDRESS = KEY.address def shoot(): output = [ (ADDRESS, 1000, 'satoshi'), ] KEY.get_unspents() # this refreshes the UTXO set (see part 1) print(KEY.send(output)) def main(tx_count=10): for i in range(tx_count): shoot() time.sleep(2) # sends a transaction every 2 seconds if __name__ == '__main__': main() In this script, we are sending a transaction every 2 seconds (we could do a lot better). Feel free to try it, I left some coins in the wallet. Add data to the block chain ⛓ Every transaction is recorded forever on the block chain. You can also add arbitrary data to a transaction. Let's do that now. In a Python shell, type: from bitcash import PrivateKeyTestnet testnet_key = PrivateKeyTestnet() testnet_key.to_wif() # 'cRxqxzA7HPDzPxWvvE5HGDMZRQYwfwKyoWRzEdHxKREEo93yS4dd' testnet_key.address # 'bchtest:qqt4x47tpvxpxgms3mhck94wujh9p29rmshrh9lqll' testnet_key.get_balance() # 1000000 output = [ ('bchtest:qq0sea6rkxzw8eakxy65rwfftfh3qf97zgnagypgvx', 1000, 'satoshi'), ] testnet_key.send(output, message='I am in the block chain!') # '65e38d77950f2da958aa2132a93d13d78409612a86b6b701dfbff724d473bc82' Boom! `65e38d77950f2da958aa2132a93d13d78409612a86b6b701dfbff724d473bc82` is the transaction ID. Let's see this in a block explorer (link here) https://explorer.bitcoin.com/tbch/tx/65e38d77950f2da958aa2132a93d13d78409612a86b6b701dfbff724d473bc82 Conclusion There is much more you can do with Bitcash (offline transactions, Set your own fee, server integration...) but we have the basics covered. In a next article, would you be interested in learning how to build a Telegram Bitcoin Cash tipping bot with BitCash? Please let me know in the comments 😃

@merc1er

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. 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. https://ofek.github.io/bit/guide/intro.html#why-bit https://github.com/kennethreitz/requests https://github.com/fchollet/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]