Get Your BCash in Under Ten Minutes with pycoin

BCash, or Bitcoin Cash is a hard fork of Bitcoin as of August 1, 2017, so it’s pretty new. Recently, I’ve been working on a big refactor of pycoin to make it more flexible in handling altcoins. Creating BCash transactions seems to work now.

If you had bitcoin on August 1, you now have bcash. Here’s how you can create transactions with BCash with nothing but the private keys and a few calls to web services.

First, switch to a throwaway environment. I like to use a RAM disk.

On Mac OS X:

$ diskutil erasevolume HFS+ RAMDISK `hdiutil attach -nomount ram://81920`
$ cd /Volumes/RAMDISK/

Set up your service providers and a place to cache the source transactions. The service providers will be used to get spendables for the addresses you’re querying, as well as the source transactions.

$ export PYCOIN_BCH_PROVIDERS=insight:
$ export PYCOIN_CACHE_DIR=./tx-cache

This example will query to get spendables and transactions.

Copy all your relevant WIFs to a file here. It’s okay for the file to have WIFs that aren’t used, but you will obviously need all the WIFs you wish to spend from.

$ cat > wifs.txt
(paste to terminal, one WIF per line)

Alternatively, you can use a GPG-encrypted file.

$ gpg -e > wifs.gpg
(specify recipients, then paste to terminal, one WIF per line)

We’ll be using python 3. Install pycoin into a virtual environment.

$ python3 -m venv env
$ . env/bin/activate
$ pip install -e [email protected]:richardkiss/pycoin.git@57b508d5208f98a304b9bd3a491c3f0049aed96b#egg=pycoin

Verify that an address has coins to spend from.

$ tx -n BCH -i 16NgXiMwRimMcSXLTf4KFwoW968btQ1GmZ

Create an unsigned transaction that contains just the inputs.

$ tx -n BCH -a -i 16NgXiMwRimMcSXLTf4KFwoW968btQ1GmZ -o inputs.bin
warning: transaction has no outputs

Replace the address above with the address you want to send coins from.

Add an output. I’m sending all coins to 1HeJ94rPmgSWbPTQqAyB2Vp7ZDqyPvyXsz. You should use your own address.

$ tx -n BCH -a inputs.bin 1HeJ94rPmgSWbPTQqAyB2Vp7ZDqyPvyXsz -o utx.bin
all incoming transaction values validated

Sign the transaction, using BCH rules.

$ tx -n BCH utx.bin -f wifs.gpg -o tx.hex
all incoming transaction values validated

You could do all this in one line (fetch the inputs, add the output, sign), as follows:

$ tx -n BCH `tx -n BCH -i 16NgXiMwRimMcSXLTf4KFwoW968btQ1GmZ | xargs` 1HeJ94rPmgSWbPTQqAyB2Vp7ZDqyPvyXsz -f wifs.gpg

but it’s long and a bit confusing, and causes word wrap in the blog.

View the transaction. Note that the transaction does NOT validate on the bitcoin (BTC) network. This is due to the replay protection in BCH (the hash type of the signature is 0x41, which is invalid in BTC).

$ tx -a -n BTC tx.hex
(lots of output)

It does validate on the BCH network.

$ tx -a -n BCH tx.hex
(lots of output)

Add -d to disassemble it (just for fun).

$ tx -ad -n BCH tx.hex
(lots of output)

View the hex.

$ cat tx.hex

Copy the hex transaction to the clipboard. Head to to paste it, and send it off!

Ditch the RAM Disk.

$ cd # can't unmount while we're in the directory
$ hdiutil unmount /Volumes/RAMDISK/

4 thoughts on “Get Your BCash in Under Ten Minutes with pycoin

  1. In this code PYCOIN_BTC_PROVIDERS still must exist (not BCH!), so one has to set this variable to the BCH provider given in the blog.

    “cat > wifs.txt”: what are the input(s) here?

  2. Thank you.

    I know about WIF, but I was wondering about the file format and how do you generate it because I can’t see where you’re “cat-ing” from or how you generate them.

    For example if I have 20 addresses, do I just run “bitcoin-cli dumpprivkey $eachAdd >> wifs.txt” 20 times, do I run dumpwallet to get them in that dumpwallet multi-column format, or do I need to use dumprivkey to create 20 different files with just the key itself (single field, CSV format).

Comments are closed.