1
0

update a few scripts

This commit is contained in:
ThomasV
2013-10-06 12:28:45 +02:00
parent 33b41f22fe
commit 66f224eab4
10 changed files with 66 additions and 119 deletions

20
scripts/block_headers Executable file
View File

@@ -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)

View File

@@ -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')

View File

@@ -2,8 +2,8 @@
import sys
from electrum import Interface
from electrum import bitcoin, Transaction
from electrum import Network, SimpleConfig
from electrum import bitcoin, Transaction, Network
def get_transaction(network, tx_hash, tx_height):
@@ -75,7 +75,7 @@ def update_tx_outputs(tx, prevout_values):
def main(address):
network = Network(SimpleConfig({'server':'btc.it-zone.org:110:s','verbose':False}))
network = Network()
network.start(wait=True)
c, u = get_addr_balance(network, address)

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python
import sys
from electrum import Interface
from electrum import Network
try:
addr = sys.argv[1]
@@ -9,9 +9,9 @@ except:
print "usage: get_history <bitcoin_address>"
sys.exit(1)
i = Interface({'server':'electrum.be:50001:t'})
i.start()
h = i.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0]
n = Network()
n.start(wait=True)
h = n.synchronous_get([ ('blockchain.address.get_history',[addr]) ])[0]
for item in h:
print item['tx_hash'], item['height']

View File

@@ -1,9 +1,12 @@
#!/usr/bin/env python
from electrum import interface, Interface
import time, electrum
electrum.set_verbosity(False) # default is True
network = electrum.Network({'verbose':False})
network.start(wait=True)
time.sleep(1)
electrum.print_json( network.heights )
i = Interface()
i.start()
servers = i.synchronous_get([('server.peers.subscribe',[])])[0]
servers = i.parse_servers( servers )
print servers

View File

@@ -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)

View File

@@ -1,7 +1,6 @@
#!/usr/bin/env python
import sys, time
from electrum import Interface
import sys, time, electrum
try:
addr = sys.argv[1]
@@ -9,18 +8,19 @@ except:
print "usage: watch_address <bitcoin_address>"
sys.exit(1)
i = Interface()
i.start()
i.send([('blockchain.address.subscribe',[addr])] )
time.sleep(1)
# 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.address.subscribe',[addr])], callback)
# 3. wait for results
while True:
r = i.get_response()
method = r.get('method')
if method == 'blockchain.address.subscribe':
i.send([('blockchain.address.get_history',[addr])])
elif method == 'blockchain.address.get_history':
for line in r.get('result'):
print line
print "---"
time.sleep(1)