[PYTHON] Check the increase / decrease of Bitcoin for each address from the blockchain

This article uses Mac OS Sierra 10.12.4. This is the third article related to bitcoin.

Part 1: Build an environment for bitcoind with docker Part 2: Access bitcoind from python

Check the increase / decrease of Bitcoin from the transaction

Last time got a block of Bitcoin from python and went to the point of looking inside. Bitcoin data is a stack of blocks, in which transactions are recorded. Since the transaction records which address the Bitcoin moved from, it is theoretically possible to calculate the balance of the address from the accumulation of them.

Take a look at the contents of the block

First, let's take a look at the contents of the block

Create connection


from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
rpc_user="bitcoinrpc"
rpc_password="passwordxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%(rpc_user, rpc_password))

Get a block with a block height of 110,000


blhash = rpc_connection.getblockhash(110000) #blhash is a block hash string
rpc_connection.getblock(blhash) #Get block information

result


{'bits': '1b01cc26',
 'chainwork': '000000000000000000000000000000000000000000000000135fae65c9d5f919',
 'confirmations': 219401,
 'difficulty': Decimal('36459.88692507513'),
 'hash': '000000000001bbda3f22ef8e476b470a2d3ae16821c23a6d22db77318d0799a9',
 'height': 110000,
 'mediantime': 1298490035,
 'merkleroot': 'bb988af992654871e8cefe8bbe05e9f9679611eadcfa53980ee515978eebcd52',
 'nextblockhash': '0000000000018d067c617c2e262e544bae4807e413122b1f4814077a15ac57c6',
 'nonce': 3052574605,
 'previousblockhash': '0000000000017947e1ea1c4f011b5bf712a9edd5e35d2c4db331f2d1764c593a',
 'size': 3202,
 'strippedsize': 3202,
 'time': 1298491791,
 'tx': ['4e10436ca8206a2dd760dd351210a5120a3824d4eb53011be0a7b9a33b368208',
  '76dc5788be4a8cf6925aff15fd8c8fbf6417b4ad6c30a1ac12cd117e95c5820b',
  '7452bfa629b104985f7c937e0f7836206935d83872882c88ae183234fe9bcf97',
  'e325a8a968368aeb6d89bcdb362d311833f5a9fe5a80f3f0730b684922439a68',
  '8510c531b585e77a66a986cb0dfdd0ca280ff0747d2dca0d6fa87b0f8af4810a',
  'f9a5d31e7894c3983d38215060c55665db0024ad7fb373fa58db7316dd223ea9',
  '83e74406b0876fed2db187444dd0a4f3eedad42e9adb32ce82b3ff729fe77b58',
  '0f3e5f5b833dfad8ff19115ecd29fa40566aace67f2880da0ab4fa1acac00bcc',
  'a1743f0803926ef4343e217c78324ec3e3ebc4cfc7c96739f3696b3c510cc7d1',
  '5015ed6455e7d3fb50ce5ef1f63888fe7d4c37042f17f5f89bea176dce4ca0bd',
  '7b14b05552f053a862df22824f92e94f155722f9e5d91341b934a6de010b6560',
  '2564b40ef226c73eb63409f9dbdcc64aeacca6e3a7136e86156a64573ba3f6d1'],
 'version': 1,
 'versionHex': '00000001',
 'weight': 12808}

Although it is written in various ways, it is a transaction in which tx is written in a block. There are 12 stored in this block.

Take a look at the contents of the transaction

Let's take a look at the transaction in the block above. Use getrawtransaction to check the contents of the transaction.

Get the contents of a transaction


rpc_connection.getrawtransaction("76dc5788be4a8cf6925aff15fd8c8fbf6417b4ad6c30a1ac12cd117e95c5820b")

result


'0100000001601f2f0143aa441690c7a2e1c90ae3696ceabf977afad8e08905a1efa20adced010000008b483045022100c3a2945b4cf7b6346a7e5e479bd3a7a2ca828ae6db2be5207f2377c737f64887022059116e34fded0bf5df39e2862cff59489b7701d60fe52367b62ea650b6360faf0141042d4b24c0c1a54e86399cc32e2f4b8524bae9442e6890960b4b11fd511794ccc16bd085102efdea3bb0f1809d10103f54db9597908eaf4f47bee694f73c742992ffffffff02404b4c00000000001976a914ec0994cb53016845edb1e207323faca42884629288ac40162094040000001976a914cb186543ad2d7c1195351678f1e94488c2813a0588ac00000000'

You don't know what this is, I will call decoderawtransaction to decode it.

Transaction decoding


rpc_connection.decoderawtransaction(rpc_connection.getrawtransaction(txid))

Transaction decoding result


{'hash': '76dc5788be4a8cf6925aff15fd8c8fbf6417b4ad6c30a1ac12cd117e95c5820b',
 'locktime': 0,
 'size': 258,
 'txid': '76dc5788be4a8cf6925aff15fd8c8fbf6417b4ad6c30a1ac12cd117e95c5820b',
 'version': 1,
 'vin': [{'scriptSig': {'asm': '3045022100c3a2945b4cf7b6346a7e5e479bd3a7a2ca828ae6db2be5207f2377c737f64887022059116e34fded0bf5df39e2862cff59489b7701d60fe52367b62ea650b6360faf[ALL] 042d4b24c0c1a54e86399cc32e2f4b8524bae9442e6890960b4b11fd511794ccc16bd085102efdea3bb0f1809d10103f54db9597908eaf4f47bee694f73c742992',
    'hex': '483045022100c3a2945b4cf7b6346a7e5e479bd3a7a2ca828ae6db2be5207f2377c737f64887022059116e34fded0bf5df39e2862cff59489b7701d60fe52367b62ea650b6360faf0141042d4b24c0c1a54e86399cc32e2f4b8524bae9442e6890960b4b11fd511794ccc16bd085102efdea3bb0f1809d10103f54db9597908eaf4f47bee694f73c742992'},
   'sequence': 4294967295,
   'txid': 'eddc0aa2efa10589e0d8fa7a97bfea6c69e30ac9e1a2c7901644aa43012f1f60', #Refer to this transaction for destination and remittance amount
   'vout': 1 #Number of transaction to refer to}],
 'vout': [{'n': 0,
   'scriptPubKey': {'addresses': ['1NX3wzGPxFaMJotm2KcDjrqAYGwu3N7rXb'] #destination address,
    'asm': 'OP_DUP OP_HASH160 ec0994cb53016845edb1e207323faca428846292 OP_EQUALVERIFY OP_CHECKSIG',
    'hex': '76a914ec0994cb53016845edb1e207323faca42884629288ac',
    'reqSigs': 1,
    'type': 'pubkeyhash'},
   'value': Decimal('0.05000000') #Remittance amount},
  {'n': 1,
   'scriptPubKey': {'addresses': ['1KWsPyZ9BYFq9izzRcQXeGffLunCAKx32d'] #destination address,
    'asm': 'OP_DUP OP_HASH160 cb186543ad2d7c1195351678f1e94488c2813a05 OP_EQUALVERIFY OP_CHECKSIG',
    'hex': '76a914cb186543ad2d7c1195351678f1e94488c2813a0588ac',
    'reqSigs': 1,
    'type': 'pubkeyhash'},
   'value': Decimal('196.65000000') #Remittance amount}],
 'vsize': 258}

It has become a form that you can understand somehow. There are various items, but we will focus only on vin and vout. The vout has the destination address and amount written on it. Focusing on the value, it says 0.05 and 196.65. 0.05 Bitcoin at the address "1NX3wzGPxFaMJotm2KcDjrqAYGwu3N7rXb" You sent a total of 196.70 bitcoins to 196.65 bitcoins at the address "1KWsPyZ9BYFq9izzRcQXeGffLunCAKx32d".

vin has another txid (transaction ID) and "vout" number written on it. This indicates the number of the destination address and amount written in the vout of this transaction ID. It's a bit confusing, which means you have to go to another txid written in vin to get the destination and the amount of the transfer. The code looks like this.

vin transaction acquisition


txid = "76dc5788be4a8cf6925aff15fd8c8fbf6417b4ad6c30a1ac12cd117e95c5820b"
in_txid = rpc_connection.decoderawtransaction(rpc_connection.getrawtransaction(txid)).get("vin")[0].get("txid")
in_no = rpc_connection.decoderawtransaction(rpc_connection.getrawtransaction(txid)).get("vin")[0].get("vout")
rpc_connection.decoderawtransaction(rpc_connection.getrawtransaction(in_txid)).get("vout")[in_no]

result


{'n': 1,
 'scriptPubKey': {'addresses': ['1LfAi3G7xYmZKjr823RHWTANnCYdZvmDhK'],
  'asm': 'OP_DUP OP_HASH160 d7a24cc6e59ca8a1e553bc5c42c5f6e4c1277bbf OP_EQUALVERIFY OP_CHECKSIG',
  'hex': '76a914d7a24cc6e59ca8a1e553bc5c42c5f6e4c1277bbf88ac',
  'reqSigs': 1,
  'type': 'pubkeyhash'},
 'value': Decimal('196.70000000')}

You can see that vin is sending 196.70 Bitcoins. This is the same as the sum of vouts. In other words, this is from the address "1LfAi3G7xYmZKjr823RHWTANnCYdZvmDhK" 0.05 Bitcoin for "1NX3wzGPxFaMJotm2KcDjrqAYGwu3N7rXb" This is a transaction in which 196.65 bit coins were sent to "1KWsPyZ9BYFq9izzRcQXeGffLunCAKx32d".

It looks like this in the figure. スクリーンショット 2017-07-16 11.51.46.png

Get Bitcoin increase / decrease for each address

At this point, you have successfully captured the transaction. If you follow this from the past history, you can check the increase / decrease of Bitcoin in the address.

Part of the acquisition result

Address Subtract Addition Block High Transaction
0	12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX	0	50.0    1	0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68be...
1	coinbase	                        0	 0.0    1   0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68be...
2	1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1	0	50.0	2	9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a...
3	coinbase                            0	 0.0	2	9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a...
4	1FvzCLoTPGANNjWoUo6jUGuAG3wg1w4YjR	0	50.0	3	999e1c837c76a1b7fbb7e57baf87b309960f5ffefbf2a9...

It's out. Since this time we targeted the early blocks, it is a record when Satoshi Nakamoto was mining. Coinbase is a mined record, formally just created. Subtraction is a remittance record and addition is a receipt record. This block is only mining, so you just received 50 bitcoins from coinbase.

In this way, it was confirmed that the blockchain is ready for anyone to check the transaction record as a third-party ledger.

Recommended Posts

Check the increase / decrease of Bitcoin for each address from the blockchain
Check the operation of Python for .NET in each environment
Check for the existence of BigQuery tables in Java
Date the name of the decomposed PDF for each page
Check the processing time and the number of calls for each process in python (cProfile)
Get the number of occurrences for each element in the list
[tensorflow, keras, mnist] Take out n sheets for each label from the mnist data and create 10 * n sheets of data.
Studying web scraping for the purpose of extracting data from Filmarks # 2
Check the memory protection of linux kerne with the code for ARM
python note: map -do the same for each element of the list
(For lawyers) Extract the behavior of Office software from .evtx files
Existence from the viewpoint of Python
Survey for practical use of BlockChain
Increase the UI size of MyPaint
Get the address from the zip code
I analyzed the voting results of the Osaka Metropolis Plan for each ward
[Ota Ward] Check the status of application processing for special flat-rate benefits
Get the list of packages for the specified user from the packages registered on PyPI