You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

60 lines
1.9 KiB

#!/usr/bin/env python
import sys, hashlib
from electrum import Interface
from electrum.bitcoin import Hash, rev_hex, int_to_hex, hash_encode, hash_decode
"""validate a transaction (SPV)"""
i = Interface({'server':'ecdsa.org:50002:s'})
i.start()
def hash_merkle_root(merkle_s, target_hash, pos):
h = hash_decode(target_hash)
for i in range(len(merkle_s)):
item = merkle_s[i]
h = Hash( hash_decode(item) + h ) if ((pos >> i) & 1) else Hash( h + hash_decode(item) )
return hash_encode(h)
def hash_header(res):
header = int_to_hex(res.get('version'),4) \
+ rev_hex(res.get('prev_block_hash')) \
+ rev_hex(res.get('merkle_root')) \
+ int_to_hex(int(res.get('timestamp')),4) \
+ int_to_hex(int(res.get('bits')),4) \
+ int_to_hex(int(res.get('nonce')),4)
return rev_hex(Hash(header.decode('hex')).encode('hex'))
def verify_tx(tx_hash):
rawtx = i.synchronous_get([ ('blockchain.transaction.get',[tx_hash]) ])[0]
print rawtx
return
res = i.synchronous_get([ ('blockchain.transaction.get_merkle',[tx_hash]) ])[0]
merkle_root = hash_merkle_root(res['merkle'], tx_hash, res['pos'])
tx_height = res.get('block_height')
headers_requests = []
for height in range(tx_height-10,tx_height+10):
headers_requests.append( ('blockchain.block.get_header',[height]) )
headers = i.synchronous_get(headers_requests)
_hash = None
for header in headers:
if _hash: assert _hash == header.get('prev_block_hash')
_hash = hash_header(header)
height = header.get('block_height')
if height==tx_height:
assert header.get('merkle_root') == merkle_root
print height, _hash, '*'
else:
print height, _hash
try:
tx = sys.argv[1]
except:
tx = '587430e52af2cec98b3fd543083469ffa7a5f5dd2bd569898a7897a64e2eb031'
verify_tx(tx)