/
Py3_BlockchainInfoAPI.py
235 lines (173 loc) · 4.79 KB
/
Py3_BlockchainInfoAPI.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 21 12:37:05 2018
@author: Gareth
Validate an imported block against Blockchain.info's api
https://blockchain.info/api
Respects API's request limting queries to 1 every 10s
Queries for blocks are done using block hash, and are returned in json format
"""
# %% Imports
import requests
import time
from pybit.py3.chain import Dat
# %% Get genesis block from Blockchain.info api
# api url
url = "https://blockchain.info/rawblock/"
# Query by blockhash
blockHash = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
# Query
resp = requests.get(url + blockHash)
# Check reponse 200
if resp.status_code == 200:
print("Good response")
else:
print("Bad response code {}".format(resp.status_code))
# %% Look at response
# Print block info
print(resp.json())
# Print individual fields, eg
print("\n")
print(resp.json()['bits'])
print(resp.json()['mrkl_root'])
# %% Function version
def get_resp(h,
lastTime=0,
url="https://blockchain.info/rawblock/",
pr=True):
# Wait if last query was less than 10s ago
dTime = (time.time() - lastTime)
if dTime < 11:
sleep_time = 11 - dTime
if pr:
print("Sleeping for {0}".format(sleep_time))
time.sleep(sleep_time)
# Query
print(url+h)
resp = requests.get(url + h)
# Record the last time
lastTime = time.time()
if resp.status_code == 200:
# Get the json
jr = resp.json()
else:
# Or return the response code on error
print(resp.status_code)
jr = None
return jr
def block_validate(block,
pr=True,
**kwargs):
"""
Query a block hash from Blockchain.info's api. Check it matches the block
on size, merkle root, number of transactions, previous block hash
Respects apis request limting queries to 1 every 10s.
"""
# Wait if last query was less than 10s ago
jr = get_resp(block.hash,
url="https://blockchain.info/rawblock/",
**kwargs)
if jr is not None:
# Check size
t1 = jr['size'] == block.blockSize
if pr:
print(t1)
# Check merkle root
t2 = jr['mrkl_root'] == block.merkleRootHash
if pr:
print(t2)
# Check number of transactions
t3 = jr['n_tx'] == block.nTransactions
if pr:
print(t3)
# Check previous block hash
t4 = jr['prev_block'] == block.prevHash
if pr:
print(t4)
else:
t1 = t2 = t3 = t4 = False
# The the overall result
result = t1 & t2 & t3 & t4
# Report
if pr:
if result:
print("Pass")
else:
print("Fail")
return lastTime, result
# Load a block
f = 'Blocks/blk00000.dat'
dat = Dat(f,
verb=5)
dat.read_next_block()
block = dat.blocks[0]
# Reset timer
lastTime = 0
# Query using this block hash
lastTime, result = block_validate(block)
# Run same query again to test wait timer
print("\n")
lastTime, result = block_validate(block, lastTime)
# %% Transaction validation
# Get a transaction hash
transHash = dat.blocks[0].trans[0].hash
print(transHash)
# api url
url = "https://blockchain.info/rawtx/"
# Query
resp = requests.get(url + transHash)
# Check reponse 200
if resp.status_code == 200:
print("Good response")
else:
print("Bad response code {}".format(resp.status_code))
# %% Look at response
# Print block info
print(resp.json())
# Print individual fields, eg
print("\n")
# Script from input 0
print(resp.json()['inputs'][0]['script'])
# Transaction hash
print(resp.json()['hash'])
# Some meta info for output 0 (not in block)
print(resp.json()['out'][0]['addr_tag'])
# Decoded output address
print(resp.json()['out'][0]['addr'])
# %% Function version
def trans_validate(trans,
pr=True):
"""
Query a transaction hash from Blockchain.info's api.
Check it matches on:
- Stuff
Respects apis request limting queries to 1 every 10s.
Handles single input
"""
jr = get_resp(trans.hash,
lastTime=0,
url="https://blockchain.info/rawtx/",
pr=True)
# Input 0 script
t1 = jr['inputs'][0]['script'] == trans.txIn[0].scriptSig
if pr:
print(t1)
# Output 0 script
t2 = jr['out'][0]['script'] == trans.txOut[0].pkScript
if pr:
print(t2)
# Decoded output address
t3 = jr['out'][0]['addr'] == trans.txOut[0].outputAddr
if pr:
print(t3)
# The the overall result
result = t1 & t2 & t3
# Report
if pr:
if result:
print("Pass")
else:
print("Fail")
return lastTime, result
trans = dat.blocks[0].trans[0]
trans_validate(trans)