10 changed files with 66 additions and 119 deletions
@ -0,0 +1,20 @@ |
|||||||
|
#!/usr/bin/env python |
||||||
|
|
||||||
|
# A simple script that connects to a server and displays block headers |
||||||
|
|
||||||
|
import time, electrum |
||||||
|
|
||||||
|
# 1. start the interface and wait for connection |
||||||
|
interface = electrum.Interface('electrum.no-ip.org:50002:s') |
||||||
|
interface.start(wait = True) |
||||||
|
if not interface.is_connected: |
||||||
|
print "not connected" |
||||||
|
exit() |
||||||
|
|
||||||
|
# 2. send the subscription |
||||||
|
callback = lambda _,result: electrum.print_json(result.get('result')) |
||||||
|
interface.send([('blockchain.headers.subscribe',[])], callback) |
||||||
|
|
||||||
|
# 3. wait for results |
||||||
|
while True: |
||||||
|
time.sleep(1) |
||||||
@ -1,16 +0,0 @@ |
|||||||
#!/usr/bin/env python |
|
||||||
|
|
||||||
import sys, electrum |
|
||||||
|
|
||||||
i = electrum.Interface() |
|
||||||
i.register_callback('connected', lambda: sys.stderr.write("Connected to %s\n" % i.connection_msg)) |
|
||||||
i.start() |
|
||||||
i.send([('blockchain.numblocks.subscribe',[])]) |
|
||||||
|
|
||||||
while True: |
|
||||||
try: |
|
||||||
r = i.get_response() |
|
||||||
except KeyboardInterrupt: |
|
||||||
break |
|
||||||
if r.get('method') == 'blockchain.numblocks.subscribe': |
|
||||||
print r.get('result') |
|
||||||
@ -1,60 +0,0 @@ |
|||||||
#!/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): |
|
||||||
|
|
||||||
res = i.synchronous_get([ ('blockchain.transaction.get_merkle',[tx_hash]) ])[0] |
|
||||||
raw_tx = i.synchronous_get([ ('blockchain.transaction.get',[tx_hash, res['block_height']]) ])[0] |
|
||||||
assert hash_encode(Hash(raw_tx.decode('hex'))) == tx_hash |
|
||||||
|
|
||||||
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) |
|
||||||
|
|
||||||
Loading…
Reference in new issue