-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetNode.py
More file actions
39 lines (31 loc) · 1.41 KB
/
getNode.py
File metadata and controls
39 lines (31 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
import time
from bitcoinrpc.authproxy import AuthServiceProxy
rpc_user = 'bitcoin'
rpc_password = 'bitcoin'
rpc_port = '8332'
rpc_host = 'localhost'
def print_last_block_transactions():
rpc_connection = AuthServiceProxy(f'http://{rpc_user}:{rpc_password}@{rpc_host}:{rpc_port}')
latest_block_hash = rpc_connection.getbestblockhash()
latest_block_info = rpc_connection.getblock(latest_block_hash)
for txid in latest_block_info['tx']:
tx_info = rpc_connection.getrawtransaction(txid, True, latest_block_hash)
print("Transaction ID: ", txid)
print("Transaction hex: ", tx_info['hex'])
dec = bytes.fromhex(tx_info['hex']).decode('utf-8', 'ignore')
print("Transacrion decripted hex ", dec)
for vout in tx_info['vout']:
if 'scriptPubKey' in vout and 'hex' in vout['scriptPubKey']:
print("ScriptPubKey hex: ", vout['scriptPubKey']['hex'])
return latest_block_hash
if __name__ == '__main__':
# endless print the latest block transactions, every minute check for new block
last_hash = print_last_block_transactions()
time.sleep(60)
while True:
rpc_connection = AuthServiceProxy(f'http://{rpc_user}:{rpc_password}@{rpc_host}:{rpc_port}')
test = latest_block_hash = rpc_connection.getbestblockhash()
if last_hash != test:
last_hash = print_last_block_transactions()
time.sleep(60)