Connect to the Bitcoin network using pybitcoin tools

What is pybitcoin tools

Reference: https://github.com/vbuterin/pybitcointools

Installation (bitcoin)

python


$ pip install bitcoin
Collecting bitcoin
  Downloading bitcoin-1.1.42.tar.gz
Building wheels for collected packages: bitcoin
  Running setup.py bdist_wheel for bitcoin ... done
  Stored in directory: 
 :
 :
Successfully built bitcoin
Installing collected packages: bitcoin
Successfully installed bitcoin-1.1.42

Operation check (bitcoin)

$ ipython
[1] from bitcoin import *
#[The string that is the source of the private key in the wallet]
[2] priv = sha256('some big long brainwallet password')
[3] priv
Out[3]: '57c617d9b4e1f7af6ec97ca2ff57e94a28279a7eedd4d12a99fa11170e94f5a4'
#Generate a public key from a private key
[4] pub = privtopub(priv)
[5] pub
Out[5]: '0420f34c2786b4bae593e22596631b025f3ff46e200fc1d4b52ef49bbdc2ed00b26c584b7e32523fb01be2294a1f8a5eb0cf71a203cc034ced46ea92a8df16c6e9'
#Generate transaction address from public key
[6] addr = pubtoaddr(pub)
[7] addr
Out[7]: '1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6'
[8] h = history(addr)
[9] h
Out[9]: 
[{'address': u'1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6',
  'block_height': 262537,
  'output': u'4cc806bb04f730c445c60b3e0f4f44b54769a1c196ca37d8d4002135e4abd171:1',
  'spend': u'772bebdf445d719d90d52a3029d27e90aa8f26eca8d431fc77cc487c377a89c4:1',
  'value': 50000},
 {'address': u'1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6',
  'block_height': 363957,
  'output': u'b0aad2e5184099b20d53100a678e9bec2eab1b0710fb06930f333387492a82b3:0',
  'spend': u'05551814576ba084a0736abd8df411061c5ea7e4ae72d334317f5504f6b331d6:0',
  'value': 500000},
 {'address': u'1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6',
  'block_height': 262537,
  'output': u'97f7c7d8ac85e40c255f8a763b6cd9a68f3a94d2e93e8bfa08f977b92e55465e:0',
  'spend': u'772bebdf445d719d90d52a3029d27e90aa8f26eca8d431fc77cc487c377a89c4:0',
  'value': 50000}]
[10] outs = [{'value': 100, 'address': '16iw1MQ1sy1DtRPYw3ao1bCamoyBJtRB4t'}]
#Create a deal
[11] outs
Out[11]: [{'address': '16iw1MQ1sy1DtRPYw3ao1bCamoyBJtRB4t', 'value': 90000}]
[12] tx = mktx(h,outs)
#Digitally sign
[13] tx
Out[13]: '010000000371d1abe4352100d4d837ca96c1a16947b5444f0f3e0bc645c430f704bb06c84c0100000000ffffffffb3822a498733330f9306fb10071bab2eec9b8e670a10530db2994018e5d2aab00000000000ffffffff5e46552eb977f908fa8b3ee9d2943a8fa6d96c3b768a5f250ce485acd8c7f7970000000000ffffffff01905f0100000000001976a9143ec6c3ed8dfc3ceabcc1cbdb0c5aef4e2d02873c88ac00000000'
[14] tx2 = sign(tx,0,priv)
[15] tx2
Out[15]: '010000000371d1abe4352100d4d837ca96c1a16947b5444f0f3e0bc645c430f704bb06c84c010000008b483045022100ec9817f8a9c88ab2bbb79900fb58a422a5ef37e6b4824a12b908c047134ba9d402201bdb6b2d303ee2a50be376388fcc73f5a67afb3be7c1eab312d51e6ee8c1b9ea01410420f34c2786b4bae593e22596631b025f3ff46e200fc1d4b52ef49bbdc2ed00b26c584b7e32523fb01be2294a1f8a5eb0cf71a203cc034ced46ea92a8df16c6e9ffffffffb3822a498733330f9306fb10071bab2eec9b8e670a10530db2994018e5d2aab00000000000ffffffff5e46552eb977f908fa8b3ee9d2943a8fa6d96c3b768a5f250ce485acd8c7f7970000000000ffffffff01905f0100000000001976a9143ec6c3ed8dfc3ceabcc1cbdb0c5aef4e2d02873c88ac00000000'
[16] tx3 = sign(tx2,1,priv)
[17] tx3
Out[17]: '010000000371d1abe4352100d4d837ca96c1a16947b5444f0f3e0bc645c430f704bb06c84c010000008b483045022100ec9817f8a9c88ab2bbb79900fb58a422a5ef37e6b4824a12b908c047134ba9d402201bdb6b2d303ee2a50be376388fcc73f5a67afb3be7c1eab312d51e6ee8c1b9ea01410420f34c2786b4bae593e22596631b025f3ff46e200fc1d4b52ef49bbdc2ed00b26c584b7e32523fb01be2294a1f8a5eb0cf71a203cc034ced46ea92a8df16c6e9ffffffffb3822a498733330f9306fb10071bab2eec9b8e670a10530db2994018e5d2aab0000000008a47304402200a7eda04709c50835fd4baa13dac3305bd32bbb2c8184529dcf30e92a931f4cd02205b9ee52d5a38665c132970842ce6df0a7d7f8b160ad77f2abcb23f22ab2b46af01410420f34c2786b4bae593e22596631b025f3ff46e200fc1d4b52ef49bbdc2ed00b26c584b7e32523fb01be2294a1f8a5eb0cf71a203cc034ced46ea92a8df16c6e9ffffffff5e46552eb977f908fa8b3ee9d2943a8fa6d96c3b768a5f250ce485acd8c7f7970000000000ffffffff01905f0100000000001976a9143ec6c3ed8dfc3ceabcc1cbdb0c5aef4e2d02873c88ac00000000'
#Broadcast a transaction
[18] pushtx(tx3)
Out[18]: 'Transaction Submitted'

Installation (pybitcointools)

python


$ pip install pybitcointools
Collecting pybitcointools
  Downloading pybitcointools-1.1.15.tar.gz
Building wheels for collected packages: pybitcointools
  Running setup.py bdist_wheel for pybitcointools ... done
  Stored in directory: /Users/abenben/Library/Caches/pip/wheels/22/5f/48/7410a0e03801249fcc708771586cd5970d1105fa0d9d67d3d6
 :
 :
Successfully built pybitcointools
Installing collected packages: pybitcointools
Successfully installed pybitcointools-1.1.15

Operation check (pybitcointools command)

I tried running the sample command on GitHub of pybitcointools

python


$ #Generate random numbers
$ pybtctool random_electrum_seed
e2221d71720a90f3167f5465d014feee

$ #Create a private key
$ pybtctool electrum_privkey e2221d71720a90f3167f5465d014feee 0 0
5c58bd2955c83b015f45cd62416f417e3821bb2a16745dae19d2aef0c97a2de9

$ #Create a public key from a private key
$ pybtctool electrum_mpk e2221d71720a90f3167f5465d014feee
05d54161b92b112718ac2de6af3ab6801fa8f268581c134f81432e2a37606ad55c9a8c7260b6bef22578874f9dcd93410ad0ea164d0e75c0b8b126a6ce76ed2b

$ pybtctool bip32_master_key 21456t243rhgtucyadh3wgyrcubw3grydfbng
xprv9s21ZrQH143K2napkeoHT48gWmoJa89KCQj4nqLfdGybyWHP9Z8jvCGzuEDv4ihCyoed7RFPNbc9NxoSF7cAvH9AaNSvepUaeqbSpJZ4rbT

$ pybtctool bip32_ckd xprv9s21ZrQH143K2napkeoHT48gWmoJa89KCQj4nqLfdGybyWHP9Z8jvCGzuEDv4ihCyoed7RFPNbc9NxoSF7cAvH9AaNSvepUaeqbSpJZ4rbT 0
xprv9vfzYrpwo7QHFdtrcvsSCTrBESFPUf1g7NRvayy1QkEfUekpDKLfqvHjgypF5w3nAvnwPjtQUNkyywWNkLbiUS95khfHCzJXFkLEdwRepbw

$ pybtctool bip32_privtopub xprv9s21ZrQH143K2napkeoHT48gWmoJa89KCQj4nqLfdGybyWHP9Z8jvCGzuEDv4ihCyoed7RFPNbc9NxoSF7cAvH9AaNSvepUaeqbSpJZ4rbT
xpub661MyMwAqRbcFGfHrgLHpC5R4odnyasAZdefbDkHBcWarJcXh6SzTzbUkWuhnP142ZFdKdAJSuTSaiGDYjvm7bCLmA8DZqksYjJbYmcgrYF

$ pybtctool sha256 'some big long brainwallet password' | \
   pybtctool -s privtoaddr | \
    pybtctool -s history | jq
[
  {
    "output": "4cc806bb04f730c445c60b3e0f4f44b54769a1c196ca37d8d4002135e4abd171:1",
    "block_height": 262537,
    "spend": "772bebdf445d719d90d52a3029d27e90aa8f26eca8d431fc77cc487c377a89c4:1",
    "value": 50000,
    "address": "1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6"
  },
  {
    "output": "b0aad2e5184099b20d53100a678e9bec2eab1b0710fb06930f333387492a82b3:0",
    "block_height": 363957,
    "spend": "05551814576ba084a0736abd8df411061c5ea7e4ae72d334317f5504f6b331d6:0",
    "value": 500000,
    "address": "1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6"
  },
  {
    "output": "97f7c7d8ac85e40c255f8a763b6cd9a68f3a94d2e93e8bfa08f977b92e55465e:0",
    "block_height": 262537,
    "spend": "772bebdf445d719d90d52a3029d27e90aa8f26eca8d431fc77cc487c377a89c4:0",
    "value": 50000,
    "address": "1CQLd3bhw4EzaURHbKCwM5YZbUQfA4ReY6"
  }
]

$ pybtctool sha256 123 | \
   pybtctool -s changebase 16 256 | \
    pybtctool -b changebase 256 16
a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3

$ pybtctool unspent 1FxkfJQLJTXpW6QmxGT6oF43ZH959ns8Cq | \
   pybtctool -j select 200000001 | \
    pybtctool -j mksend 1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P:20000 1FxkfJQLJTXpW6QmxGT6oF43ZH959ns8Cq 1000 | pybtctool -s signall 805cd74ca322633372b9bfb857f3be41db0b8de43a3c44353b238c0acff9d523
0100000001c42fa5643c40022890a01096fcc84f48b6c39ab2a2c3f1c1776bad41f33bae7e000000008a47304402206b7fdbd574910550370a6a2b3c118a81914bc0b4b282a75a87c10fef3f6ef46902201b9087184868f50310d5f6e5a241aafafa692d93fd8bc970149bb1a741d2ea6c0141048ef80f6bd6b073407a69299c2ba89de48adb59bb9689a5ab040befbbebcfbb15d01b006a6b825121a0d2c546c277acb60f0bd3203bd501b8d67c7dba91f27f47ffffffff02204e0000000000001976a914946cb2e08075bcbaf157e47bcb67eb2b2339d24288ace648410c000000001976a914a41d15ae657ad3bfd0846771a34d7584c37d54a288ac00000000

$ pybtctool history 1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P | \
   pybtctool -j multiaccess value | \
    pybtctool -j sum
Fetching more transactions... 50
Fetching more transactions... 100
Fetching more transactions... 150
 :
 :
625216206372

$ pybtctool history 1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P | \
   pybtctool -j count
Fetching more transactions... 50
Fetching more transactions... 100
Fetching more transactions... 150
 :
 :
6198

Recommended Posts

Connect to the Bitcoin network using pybitcoin tools
Connect to the Bitcoin network using pycoin (Python Cryptocoin Utili)
Connect to MySQL using Flask SQLAlchemy
Connect Sipeed Lichee Zero to the net
Connect your Raspberry Pi to your smartphone using Blynk
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
How to write a GUI using the maya command
I tried to approximate the sin function using chainer
Write data to KINTONE using the Python requests module
Post to your account using the API on Twitter
[Introduction to Python] How to stop the loop using break?
I want to automate ssh using the expect command!
The story of using circleci to build manylinux wheels
I tried to identify the language using CNN + Melspectogram
I tried to complement the knowledge graph using OpenKE
I tried to compress the image using machine learning
How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"