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:http://blockdozer.com/insight-api
$ export PYCOIN_CACHE_DIR=./tx-cache

This example will query blockdozer.com 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
62263e20a70ec98cd52452ffca6f5c54fa02a45cc0a70aca14b99f2445407678/0/76a9143af25c9925ad4d20e1df648a82268d44445430ec88ac/1990000/0/0/0
b0011baf07830ec04d5adb66b1503ad6c9498dfe6c5590f05c8226c9f83fa0df/0/76a9143af25c9925ad4d20e1df648a82268d44445430ec88ac/1185613/0/0/0

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
signing...
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
0100000002dfa03ff8c926825cf090556cfe8d49c9d63a50b166db5a4dc00e8307af1b01b0000000008b483045022100879c1b9cb199865bb77892b3ccdddc1f52102aa3da3024556f864e34d380108e02205b3f17c412df05ebb7984f472dcbe574fb0472bee3fa9ac3032e90ab05e33691414104712ace3646c43b8f03662cd00d9b0cc24c17b530484bd4c377ea61dd044eca8227fb4415762fc4af5c3bc979a0d64e8d7348e6bab376efab527df65feebc634dffffffff78764045249fb914ca0aa7c05ca402fa545c6fcaff5224d58cc90ea7203e2662000000008b483045022100fb13941b3d32aa33139a1d73da7affdcb805a5e435ec307daaf3a9497c8b549e02205df9d8c729af25429b8c09940eab858324c0e91f7992961d8d1586f5b3694a48414104712ace3646c43b8f03662cd00d9b0cc24c17b530484bd4c377ea61dd044eca8227fb4415762fc4af5c3bc979a0d64e8d7348e6bab376efab527df65feebc634dffffffff01ad4d3000000000001976a914b68fb5c98ece06e3f462fbaa7fc8cd559860f2ec88ac00000000

Copy the hex transaction to the clipboard. Head to http://bcc.blockdozer.com/insight/tx/send 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.