commit 7ab5e14369b1a3b971f9ca0281a2dec20e2e6161
parent df85516bd054fa25b9ed77bb387a74c640ebb99b
Author: ThomasV <thomasv@gitorious>
Date: Sun, 28 Oct 2012 08:19:39 +0100
update validate_tx script
Diffstat:
1 file changed, 8 insertions(+), 11 deletions(-)
diff --git a/scripts/validate_tx b/scripts/validate_tx
@@ -2,23 +2,20 @@
import sys, hashlib
from electrum import Interface
-from electrum.bitcoin import Hash, rev_hex, int_to_hex
+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()
-encode = lambda x: x[::-1].encode('hex')
-decode = lambda x: x.decode('hex')[::-1]
-
-def do_merkle_root(merkle_s, target_hash):
- h = decode(target_hash)
- for item in merkle_s:
- is_left = item[0] == 'L'
- h = Hash( h + decode(item[1:]) ) if is_left else Hash( decode(item[1:]) + h )
- return encode(h)
+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):
@@ -33,7 +30,7 @@ def hash_header(res):
def verify_tx(tx_hash):
res = i.synchronous_get([ ('blockchain.transaction.get_merkle',[tx_hash]) ])[0]
- merkle_root = do_merkle_root(res['merkle'], 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):