[PYTHON] Connect to Bitcoin Testnet with Pycoin

What is Pycoin

A Python library that can interact with Bitcoin and Altcoin networks. I think it's common to install Bitcoin Core to connect to a Bitcoin network, but it's quite a hassle. Pycoin makes it easy to connect to your Bitcoin network. There are also useful command line tools called KU and TX, and this article will show you how to use this command line tool and how to set environment variables to connect to Bitcoin Testnet.

Pycoin can handle not only Bitcoin, but also relatively famous Altcoin such as Litecoin, Dogecoin, Dash and other coins you have never heard of. It supports Python 2.7 and 3.3 or later. https://github.com/richardkiss/pycoin

Environment

You can install it with pip. Let's build and install a virtual environment with venv etc.

$ pip install pycoin

What is Testnet

Testnet is usually used for Bitcoin development. There is no reason to use real BTC for development, the production is called Mainnet, but Testnet is built for developers.

This article is detailed. Application test environment using Bitcoin http://techmedia-think.hatenablog.com/entry/2015/05/10/123243

Coins on Testnet have no value and can be obtained at Faucet. https://tpfaucet.appspot.com/

You can check Testnet here. https://testnet.blockexplorer.com/

KU and TX

Pycoin has useful command line tools called KU and TX. https://github.com/richardkiss/pycoin/blob/master/COMMAND-LINE-TOOLS.md

KU is a "key utility" and, according to developer Richard Kiss, it's like a "Swiss Army knife" for manipulating the Key. Is that so convenient?

TX is a tool for checking and manipulating transactions for human readability.

Let's type ku create, which is also in the sample code. BIP32 keys, which can be said to be the standard for HD wallets, are randomly issued.

$ ku create
warning: can't open gpg, can't use as entropy source

input                        : create
network                      : Bitcoin mainnet
netcode                      : BTC
wallet key                   : xprv9s21ZrQH143K2K5pPUoMrx8g8B3xSeJuAAXtt7XP6Gs1w9WWCRibNaMiEUjN9j\
                                 YpXaejADY9GGE3KyKX4oztPCt7bCZwVTw8fuyr7rPcVp3
public version               : xpub661MyMwAqRbcEoAHVWLNE65QgCtSr72kXPTVgVvzecPzowqejy2qvNgC5mofRo\
                                 PR2QT7xC4XY9pxtP2H3EoLbB2urCPgvD6FBMm1LXEBope
tree depth                   : 0
fingerprint                  : 9a087148
parent f'print               : 00000000
child index                  : 0
chain code                   : 1980c24d0593ce4a42f3dae1991cb7617f8cbac03df219a796be5392775ff361
private key                  : yes
secret exponent              : 62147175208710988790827765505072286205595207768729086914666605171876233295603
 hex                         : 89660dfd51eb4720adb856375cb0273222b6aaccf34d48ec3abbcc3e865936f3
wif                          : L1po7g8b8xtn7w7ukg8Gv6j3sEQykCS2NcWwLRZD1tDAUMMVYJ5Y
 uncompressed                : 5JroFCatgsGovgT2z2AMNYf5GutCVpT4V1LvYb7PfGEBJdEbts1
public pair x                : 68090829527568288081052782941849647349285350892240776672153772254112346326898
public pair y                : 51301029354091858472661926463670541630631183134816042193916222104982852838049
 x as hex                    : 968a0b34e401a041c684d7a675857ad7b9b1ed1936d056e43b01ac6a0180c772
 y as hex                    : 716b5a882afdc55f901d07dfe5c769106dead6710a70858e51fe51a15039a2a1
y parity                     : odd
key pair as sec              : 03968a0b34e401a041c684d7a675857ad7b9b1ed1936d056e43b01ac6a0180c772
 uncompressed                : 04968a0b34e401a041c684d7a675857ad7b9b1ed1936d056e43b01ac6a0180c772\
                                 716b5a882afdc55f901d07dfe5c769106dead6710a70858e51fe51a15039a2a1
hash160                      : 9a087148075e0ae4cd68fd4019e63f2df521572c
 uncompressed                : 017773ac09011e42b64ede8701f67a15056455ed
Bitcoin address              : 1F3TB8yLpkDxRZ8jsX9o7FPCF99VDcZsg5
Bitcoin address uncompressed : 18kmrPFEDGxXiUjr5GYsw2TxCzqqUiZ5F
Bitcoin segwit address       : p2y8WmmTAGv9XjWT8bjjyuZemZFrUz5xA7oY

Connect to Testnet from the command line

You can specify the network name by taking -n as an argument. -NXTN to specify Bitcoin's Testnet.

ku -nXTN 1

input                        : 1
network                      : Bitcoin testnet3
netcode                      : XTN
secret exponent              : 1
 hex                         : 1
wif                          : cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA
 uncompressed                : 91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx
public pair x                : 55066263022277343669578718895168534326250603453777594175500187360389116729240
public pair y                : 32670510020758816978083085130507043184471273380659243275938904335757337482424
 x as hex                    : 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
 y as hex                    : 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
y parity                     : even
key pair as sec              : 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
 uncompressed                : 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\
                                 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
hash160                      : 751e76e8199196d454941c45d1b3a323f1433bd6
 uncompressed                : 91b24bf9f5288532960ac687abb035127b1d28a5
Bitcoin address              : mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r
Bitcoin address uncompressed : mtoKs9V381UAhUia3d7Vb9GNak8Qvmcsme
Bitcoin segwit address       : QWz8QvrJz5QmWegmBv9tXP447AdU7JQtyakg

Set environment variables and connect to Testnet

You can specify the network not only from the command line, but of course from a file.

Basically, if you do not specify anything, you will be connected to Bitcoin Mainnet.

I inadvertently did it on Mainnet! I don't like this, so I'll show you how to set environment variables on your device and specify the network by default.

This time we will default to Bitcoin Testnet.

Set environment variable settings. The net code that can be specified is written in all.pylegacy_network.py of networks.

$ export PYCOIN_DEFAULT_NETCODE="XTN"

Let's check.

sample.py


from pycoin.networks import default

default_netcode = default._netcode_for_env()
print(default_netcode)

It's okay if you do this and the specified netcode is displayed.

Since netcode may be taken as an argument such as creating a Key, it is OK if you specify it at that time. Try it on Testnet and reflect it on Mainnet! I thought it would be convenient because I only had to change the environment variable settings.

It's easy, but I showed you how to connect to Bitcoin Testnet with Pycoin. Next time, I would like to create a Key with Testnet, create a transaction, and exchange bitcoins.

Recommended Posts

Connect to Bitcoin Testnet with Pycoin
Connect to BigQuery with Python
Connect to Postgresql with GO
Connect to the Bitcoin network using pycoin (Python Cryptocoin Utili)
Connect to Elastic MQ with boto
Connect to MySQL with Python within Docker
Connect to GNU / Linux with Remote Desktop
Connect to s3 with AWS Lambda Python
Connect to pepper with PEPPER Mac's python interpreter
How to use SQLAlchemy / Connect with aiomysql
Connect to mysql
I made a simple Bitcoin wallet with pycoin
Easily connect Xillybus to user logic with cReComp
Connect to the Bitcoin network using pybitcoin tools
Connect to MySQL with Python on Raspberry Pi
Convert 202003 to 2020-03 with pandas
Connect python to mysql
Connect to centos6 on virtualbox with ssh connection from Mac
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Try to factorial with recursion
Post to slack with Python 3
Output to syslog with Loguru
Introduction to RDB with sqlalchemy Ⅰ
Easy to make with syntax
Connect Vagrant's MySQL with MySQL Workbench
To run gym_torcs with ubutnu16
How to cast with Theano
How to Alter with SQLAlchemy?
Connect to sqlite from python
Switch python to 2.7 with alternatives
Write to csv with Python
How to separate strings with','
Connect to s3 tokyo region
How to RDP with Fedora31
2 ways to deal with SessionNotCreatedException
How to Delete with SQLAlchemy?
How to connect to Cloud Firestore from Google Cloud Functions with python code