3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2011 thomasv@gitorious
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
33 from ecdsa.util import string_to_number, number_to_string
34 from util import print_error, user_dir, format_satoshis
38 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
39 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
42 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
43 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
46 from version import ELECTRUM_VERSION, SEED_VERSION
50 def __init__(self, config={}):
53 self.electrum_version = ELECTRUM_VERSION
56 self.seed_version = config.get('seed_version', SEED_VERSION)
57 self.gap_limit = config.get('gap_limit', 5)
58 self.use_change = config.get('use_change',True)
59 self.fee = int(config.get('fee',100000))
60 self.num_zeros = int(config.get('num_zeros',0))
61 self.master_public_key = config.get('master_public_key','')
62 self.use_encryption = config.get('use_encryption', False)
63 self.addresses = config.get('addresses', []) # receiving addresses visible for user
64 self.change_addresses = config.get('change_addresses', []) # addresses used as change
65 self.seed = config.get('seed', '') # encrypted
66 self.history = config.get('history',{})
67 self.labels = config.get('labels',{}) # labels for addresses and transactions
68 self.aliases = config.get('aliases', {}) # aliases for addresses
69 self.authorities = config.get('authorities', {}) # trusted addresses
70 self.frozen_addresses = config.get('frozen_addresses',[])
71 self.prioritized_addresses = config.get('prioritized_addresses',[])
72 self.receipts = config.get('receipts',{}) # signed URIs
73 self.addressbook = config.get('contacts', []) # outgoing addresses, for payments
74 self.imported_keys = config.get('imported_keys',{})
77 self.receipt = None # next receipt
79 self.was_updated = True
83 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
84 # interface.is_up_to_date() returns true when all requests have been answered and processed
85 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
86 self.up_to_date_event = threading.Event()
87 self.up_to_date_event.clear()
88 self.up_to_date = False
89 self.lock = threading.Lock()
90 self.tx_event = threading.Event()
92 self.update_tx_history()
93 if self.seed_version != SEED_VERSION:
94 raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
96 def init_up_to_date(self):
97 self.up_to_date_event.clear()
98 self.up_to_date = False
100 def import_key(self, keypair, password):
101 address, key = keypair.split(':')
102 if not self.is_valid(address):
103 raise BaseException('Invalid Bitcoin address')
104 if address in self.all_addresses():
105 raise BaseException('Address already in wallet')
106 b = ASecretToSecret( key )
108 raise BaseException('Unsupported key format')
109 secexp = int( b.encode('hex'), 16)
110 private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve=SECP256k1 )
112 public_key = private_key.get_verifying_key()
113 if not address == public_key_to_bc_address( '04'.decode('hex') + public_key.to_string() ):
114 raise BaseException('Address does not match private key')
115 self.imported_keys[address] = self.pw_encode( key, password )
118 def new_seed(self, password):
119 seed = "%032x"%ecdsa.util.randrange( pow(2,128) )
122 self.seed = self.pw_encode( seed, password )
125 def init_mpk(self,seed):
128 secexp = self.stretch_key(seed)
129 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
130 self.master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
132 def all_addresses(self):
133 return self.addresses + self.change_addresses + self.imported_keys.keys()
135 def is_mine(self, address):
136 return address in self.all_addresses()
138 def is_change(self, address):
139 return address in self.change_addresses
141 def is_valid(self,addr):
142 ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
143 if not ADDRESS_RE.match(addr): return False
145 h = bc_address_to_hash_160(addr)
148 return addr == hash_160_to_bc_address(h)
150 def stretch_key(self,seed):
152 for i in range(100000):
153 seed = hashlib.sha256(seed + oldseed).digest()
154 return string_to_number( seed )
156 def get_sequence(self,n,for_change):
157 return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key.decode('hex') ) )
159 def get_private_key_base58(self, address, password):
160 pk = self.get_private_key(address, password)
161 if pk is None: return None
162 return SecretToASecret( pk )
164 def get_private_key(self, address, password):
165 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
166 order = generator_secp256k1.order()
168 if address in self.imported_keys.keys():
169 b = self.pw_decode( self.imported_keys[address], password )
170 if not b: return None
171 b = ASecretToSecret( b )
172 secexp = int( b.encode('hex'), 16)
174 if address in self.addresses:
175 n = self.addresses.index(address)
177 elif address in self.change_addresses:
178 n = self.change_addresses.index(address)
181 raise BaseException("unknown address")
183 seed = self.pw_decode( self.seed, password)
185 raise BaseException("Invalid password")
186 if not seed: return None
187 secexp = self.stretch_key(seed)
188 secexp = ( secexp + self.get_sequence(n,for_change) ) % order
190 pk = number_to_string(secexp,order)
193 def msg_magic(self, message):
194 return "\x18Bitcoin Signed Message:\n" + chr( len(message) ) + message
196 def sign_message(self, address, message, password):
197 private_key = ecdsa.SigningKey.from_string( self.get_private_key(address, password), curve = SECP256k1 )
198 public_key = private_key.get_verifying_key()
199 signature = private_key.sign_digest( Hash( self.msg_magic( message ) ), sigencode = ecdsa.util.sigencode_string )
200 assert public_key.verify_digest( signature, Hash( self.msg_magic( message ) ), sigdecode = ecdsa.util.sigdecode_string)
202 sig = base64.b64encode( chr(27+i) + signature )
204 self.verify_message( address, sig, message)
209 raise BaseException("error: cannot sign message")
212 def verify_message(self, address, signature, message):
213 """ See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """
214 from ecdsa import numbertheory, ellipticcurve, util
216 curve = curve_secp256k1
217 G = generator_secp256k1
219 # extract r,s from signature
220 sig = base64.b64decode(signature)
221 if len(sig) != 65: raise BaseException("Wrong encoding")
222 r,s = util.sigdecode_string(sig[1:], order)
224 if nV < 27 or nV >= 35:
225 raise BaseException("Bad encoding")
234 x = r + (recid/2) * order
236 alpha = ( x * x * x + curve.a() * x + curve.b() ) % curve.p()
237 beta = msqr.modular_sqrt(alpha, curve.p())
238 y = beta if (beta - recid) % 2 == 0 else curve.p() - beta
239 # 1.4 the constructor checks that nR is at infinity
240 R = ellipticcurve.Point(curve, x, y, order)
241 # 1.5 compute e from message:
242 h = Hash( self.msg_magic( message ) )
243 e = string_to_number(h)
245 # 1.6 compute Q = r^-1 (sR - eG)
246 inv_r = numbertheory.inverse_mod(r,order)
247 Q = inv_r * ( s * R + minus_e * G )
248 public_key = ecdsa.VerifyingKey.from_public_point( Q, curve = SECP256k1 )
249 # check that Q is the public key
250 public_key.verify_digest( sig[1:], h, sigdecode = ecdsa.util.sigdecode_string)
251 # check that we get the original signing address
252 addr = public_key_to_bc_address( encode_point(public_key, compressed) )
254 raise BaseException("Bad signature")
257 def create_new_address(self, for_change):
258 n = len(self.change_addresses) if for_change else len(self.addresses)
259 address = self.get_new_address(n, for_change)
261 self.change_addresses.append(address)
263 self.addresses.append(address)
264 self.history[address] = []
267 def get_new_address(self, n, for_change):
268 """ Publickey(type,n) = Master_public_key + H(n|S|type)*point """
270 z = self.get_sequence(n, for_change)
271 master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key.decode('hex'), curve = SECP256k1 )
272 pubkey_point = master_public_key.pubkey.point + z*curve.generator
273 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
274 address = public_key_to_bc_address( '04'.decode('hex') + public_key2.to_string() )
279 def change_gap_limit(self, value):
280 if value >= self.gap_limit:
281 self.gap_limit = value
283 self.interface.poke()
286 elif value >= self.min_acceptable_gap():
287 k = self.num_unused_trailing_addresses()
288 n = len(self.addresses) - k + value
289 self.addresses = self.addresses[0:n]
290 self.gap_limit = value
296 def num_unused_trailing_addresses(self):
298 for a in self.addresses[::-1]:
299 if self.history.get(a):break
303 def min_acceptable_gap(self):
304 # fixme: this assumes wallet is synchronized
307 k = self.num_unused_trailing_addresses()
308 for a in self.addresses[0:-k]:
309 if self.history.get(a):
313 if n > nmax: nmax = n
317 def synchronize(self):
318 if not self.master_public_key:
323 if self.change_addresses == []:
324 new_addresses.append( self.create_new_address(True) )
326 a = self.change_addresses[-1]
327 if self.history.get(a):
328 new_addresses.append( self.create_new_address(True) )
334 if len(self.addresses) < n:
335 new_addresses.append( self.create_new_address(False) )
337 if map( lambda a: self.history.get(a), self.addresses[-n:] ) == n*[[]]:
340 new_addresses.append( self.create_new_address(False) )
346 return (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
348 def fill_addressbook(self):
349 for tx in self.tx_history.values():
351 for i in tx['outputs']:
352 if not self.is_mine(i) and i not in self.addressbook:
353 self.addressbook.append(i)
355 self.update_tx_labels()
358 def get_address_flags(self, addr):
359 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
360 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
364 def get_addr_balance(self, addr):
365 assert self.is_mine(addr)
366 h = self.history.get(addr,[])
376 def get_balance(self):
378 for addr in self.all_addresses():
379 c, u = self.get_addr_balance(addr)
385 def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
386 """ todo: minimize tx size """
388 fee = self.fee if fixed_fee is None else fixed_fee
391 prioritized_coins = []
392 domain = [from_addr] if from_addr else self.all_addresses()
393 for i in self.frozen_addresses:
394 if i in domain: domain.remove(i)
396 for i in self.prioritized_addresses:
397 if i in domain: domain.remove(i)
400 h = self.history.get(addr)
401 if h is None: continue
403 if item.get('raw_output_script'):
404 coins.append( (addr,item))
406 coins = sorted( coins, key = lambda x: x[1]['timestamp'] )
408 for addr in self.prioritized_addresses:
409 h = self.history.get(addr)
410 if h is None: continue
412 if item.get('raw_output_script'):
413 prioritized_coins.append( (addr,item))
415 prioritized_coins = sorted( prioritized_coins, key = lambda x: x[1]['timestamp'] )
418 coins = prioritized_coins + coins
422 v = item.get('value')
424 inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], None, None) )
425 fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
426 if total >= amount + fee: break
428 #print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
430 return inputs, total, fee
432 def choose_tx_outputs( self, to_addr, amount, fee, total, change_addr=None ):
433 outputs = [ (to_addr, amount) ]
434 change_amount = total - ( amount + fee )
435 if change_amount != 0:
436 # normally, the update thread should ensure that the last change address is unused
438 change_addr = self.change_addresses[-1]
439 outputs.append( ( change_addr, change_amount) )
442 def sign_inputs( self, inputs, outputs, password ):
444 for i in range(len(inputs)):
445 addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
446 private_key = ecdsa.SigningKey.from_string( self.get_private_key(addr, password), curve = SECP256k1 )
447 public_key = private_key.get_verifying_key()
448 pubkey = public_key.to_string()
449 tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
450 sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
451 assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
452 s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
455 def pw_encode(self, s, password):
457 secret = Hash(password)
458 return EncodeAES(secret, s)
462 def pw_decode(self, s, password):
463 if password is not None:
464 secret = Hash(password)
465 d = DecodeAES(secret, s)
470 raise ValueError("Invalid password")
475 def get_status(self, address):
477 h = self.history.get(address)
482 status = lastpoint['block_hash']
483 if status == 'mempool':
484 status = status + ':%d'% len(h)
488 def receive_history_callback(self, addr, data):
489 #print "updating history for", addr
491 self.history[addr] = data
492 self.update_tx_history()
495 def get_tx_history(self):
497 lines = self.tx_history.values()
498 lines = sorted(lines, key=operator.itemgetter("timestamp"))
501 def get_tx_hashes(self):
503 hashes = self.tx_history.keys()
506 def get_transactions_at_height(self, height):
508 values = self.tx_history.values()[:]
512 if tx['height'] == height:
513 out.append(tx['tx_hash'])
516 def update_tx_history(self):
518 for addr in self.all_addresses():
519 h = self.history.get(addr)
520 if h is None: continue
522 tx_hash = tx['tx_hash']
523 line = self.tx_history.get(tx_hash)
525 self.tx_history[tx_hash] = copy.copy(tx)
526 line = self.tx_history.get(tx_hash)
528 line['value'] += tx['value']
529 if line['height'] == 0:
530 line['timestamp'] = 1e12
531 self.update_tx_labels()
533 def update_tx_labels(self):
534 for tx in self.tx_history.values():
537 for o_addr in tx['outputs']:
538 if not self.is_mine(o_addr):
540 default_label = self.labels[o_addr]
542 default_label = o_addr
544 for o_addr in tx['outputs']:
545 if self.is_mine(o_addr) and not self.is_change(o_addr):
548 for o_addr in tx['outputs']:
549 if self.is_mine(o_addr):
555 dest_label = self.labels.get(o_addr)
557 default_label = self.labels[o_addr]
559 default_label = o_addr
561 tx['default_label'] = default_label
563 def mktx(self, to_address, amount, label, password, fee=None, change_addr=None, from_addr= None):
564 if not self.is_valid(to_address):
565 raise ValueError("Invalid address")
566 inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
568 raise ValueError("Not enough funds")
570 if not self.use_change and not change_addr:
571 change_addr = inputs[0][0]
572 print "Sending change to", change_addr
574 outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr )
575 s_inputs = self.sign_inputs( inputs, outputs, password )
577 tx = filter( raw_tx( s_inputs, outputs ) )
578 if to_address not in self.addressbook:
579 self.addressbook.append(to_address)
581 tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
582 self.labels[tx_hash] = label
586 def sendtx(self, tx):
592 def send_tx(self, tx):
594 self.tx_event.clear()
595 tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
596 self.interface.send([('blockchain.transaction.broadcast', [tx])])
599 def receive_tx(self,tx_hash):
602 return False, "error: " + out
604 self.receipts[tx_hash] = self.receipt
609 def read_alias(self, alias):
610 # this might not be the right place for this function.
613 m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
614 m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
616 url = 'https://' + m1.group(2) + '/bitcoin.id/' + m1.group(1)
618 url = 'https://' + alias + '/bitcoin.id'
622 lines = urllib.urlopen(url).readlines()
627 line = lines[0].strip().split(':')
630 target = signing_addr = line[0]
632 target, auth_name, signing_addr, signature = line
633 msg = "alias:%s:%s:%s"%(alias,target,auth_name)
635 self.verify_message(signing_addr, signature, msg)
637 # other lines are signed updates
638 for line in lines[1:]:
640 if not line: continue
641 line = line.split(':')
644 target, signature = line
645 self.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
647 if not self.is_valid(target):
648 raise ValueError("Invalid bitcoin address")
650 return target, signing_addr, auth_name
652 def update_password(self, seed, old_password, new_password):
653 if new_password == '': new_password = None
654 self.use_encryption = (new_password != None)
655 self.seed = self.pw_encode( seed, new_password)
656 for k in self.imported_keys.keys():
657 a = self.imported_keys[k]
658 b = self.pw_decode(a, old_password)
659 c = self.pw_encode(b, new_password)
660 self.imported_keys[k] = c
663 def get_alias(self, alias, interactive = False, show_message=None, question = None):
665 target, signing_address, auth_name = self.read_alias(alias)
666 except BaseException, e:
667 # raise exception if verify fails (verify the chain)
669 show_message("Alias error: " + str(e))
672 print target, signing_address, auth_name
674 if auth_name is None:
675 a = self.aliases.get(alias)
677 msg = "Warning: the alias '%s' is self-signed.\nThe signing address is %s.\n\nDo you want to add this alias to your list of contacts?"%(alias,signing_address)
678 if interactive and question( msg ):
679 self.aliases[alias] = (signing_address, target)
683 if signing_address != a[0]:
684 msg = "Warning: the key of alias '%s' has changed since your last visit! It is possible that someone is trying to do something nasty!!!\nDo you accept to change your trusted key?"%alias
685 if interactive and question( msg ):
686 self.aliases[alias] = (signing_address, target)
690 if signing_address not in self.authorities.keys():
691 msg = "The alias: '%s' links to %s\n\nWarning: this alias was signed by an unknown key.\nSigning authority: %s\nSigning address: %s\n\nDo you want to add this key to your list of trusted keys?"%(alias,target,auth_name,signing_address)
692 if interactive and question( msg ):
693 self.authorities[signing_address] = auth_name
698 self.aliases[alias] = (signing_address, target)
703 def parse_url(self, url, show_message, question):
704 o = url[8:].split('?')
707 params = o[1].split('&')
711 amount = label = message = signature = identity = ''
715 if k == 'amount': amount = uv
716 elif k == 'message': message = uv
717 elif k == 'label': label = uv
718 elif k == 'signature':
719 identity, signature = uv.split(':')
720 url = url.replace('&%s=%s'%(k,v),'')
724 if label and self.labels.get(address) != label:
725 if question('Give label "%s" to address %s ?'%(label,address)):
726 if address not in self.addressbook and address not in self.all_addresses():
727 self.addressbook.append(address)
728 self.labels[address] = label
731 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
732 signing_address = self.get_alias(identity, True, show_message, question)
733 elif self.is_valid(identity):
734 signing_address = identity
736 signing_address = None
737 if not signing_address:
740 self.verify_message(signing_address, signature, url )
741 self.receipt = (signing_address, signature, url)
743 show_message('Warning: the URI contains a bad signature.\nThe identity of the recipient cannot be verified.')
744 address = amount = label = identity = message = ''
746 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', address):
747 payto_address = self.get_alias(address, True, show_message, question)
749 address = address + ' <' + payto_address + '>'
751 return address, amount, label, message, signature, identity, url
755 self.interface.poke('synchronizer')
756 self.up_to_date_event.wait(10000000000)
759 def freeze(self,addr):
760 if addr in self.all_addresses() and addr not in self.frozen_addresses:
761 self.unprioritize(addr)
762 self.frozen_addresses.append(addr)
763 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
768 def unfreeze(self,addr):
769 if addr in self.all_addresses() and addr in self.frozen_addresses:
770 self.frozen_addresses.remove(addr)
771 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
776 def prioritize(self,addr):
777 if addr in self.all_addresses() and addr not in self.prioritized_addresses:
779 self.prioritized_addresses.append(addr)
780 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
785 def unprioritize(self,addr):
786 if addr in self.all_addresses() and addr in self.prioritized_addresses:
787 self.prioritized_addresses.remove(addr)
788 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
795 'seed_version': self.seed_version,
796 'use_encryption': self.use_encryption,
797 'use_change': self.use_change,
798 'master_public_key': self.master_public_key,
801 'addresses': self.addresses,
802 'change_addresses': self.change_addresses,
803 'history': self.history,
804 'labels': self.labels,
805 'contacts': self.addressbook,
806 'imported_keys': self.imported_keys,
807 'aliases': self.aliases,
808 'authorities': self.authorities,
809 'receipts': self.receipts,
810 'num_zeros': self.num_zeros,
811 'frozen_addresses': self.frozen_addresses,
812 'prioritized_addresses': self.prioritized_addresses,
813 'gap_limit': self.gap_limit,
815 for k, v in s.items():
816 self.config.set_key(k,v)
824 class WalletSynchronizer(threading.Thread):
827 def __init__(self, wallet, config):
828 threading.Thread.__init__(self)
831 self.interface = self.wallet.interface
832 self.interface.register_channel('synchronizer')
833 self.wallet.interface.register_callback('connected', self.wallet.init_up_to_date)
834 self.wallet.interface.register_callback('connected', lambda: self.interface.send([('server.banner',[])],'synchronizer') )
836 def synchronize_wallet(self):
837 new_addresses = self.wallet.synchronize()
839 self.subscribe_to_addresses(new_addresses)
841 if self.interface.is_up_to_date('synchronizer'):
842 if not self.wallet.up_to_date:
843 self.wallet.up_to_date = True
844 self.wallet.was_updated = True
845 self.wallet.up_to_date_event.set()
847 if self.wallet.up_to_date:
848 self.wallet.up_to_date = False
849 self.wallet.was_updated = True
853 def subscribe_to_addresses(self, addresses):
855 for addr in addresses:
856 messages.append(('blockchain.address.subscribe', [addr]))
857 self.interface.send( messages, 'synchronizer')
861 # request banner, because 'connected' event happends before this thread is started
862 self.interface.send([('server.banner',[])],'synchronizer')
865 self.interface.send([('blockchain.numblocks.subscribe',[])], 'synchronizer')
866 self.interface.send([('server.peers.subscribe',[])],'synchronizer')
867 self.subscribe_to_addresses(self.wallet.all_addresses())
870 # 1. send new requests
871 self.synchronize_wallet()
873 if self.wallet.was_updated:
874 self.interface.trigger_callback('updated')
875 self.wallet.was_updated = False
878 r = self.interface.get_response('synchronizer')
886 if method == 'blockchain.address.subscribe':
888 if self.wallet.get_status(addr) != result:
889 self.interface.send([('blockchain.address.get_history', [address] )])
891 elif method == 'blockchain.address.get_history':
893 self.wallet.receive_history_callback(addr, result)
894 self.wallet.was_updated = True
896 elif method == 'blockchain.transaction.broadcast':
897 self.wallet.tx_result = result
898 self.wallet.tx_event.set()
900 elif method == 'blockchain.numblocks.subscribe':
901 self.wallet.blocks = result
902 self.wallet.was_updated = True
904 elif method == 'server.version':
907 elif method == 'server.peers.subscribe':
916 if re.match("[stgh]\d+", v):
917 ports.append((v[0], v[1:]))
918 if re.match("v(.?)+", v):
920 if ports and version:
921 servers.append((host, ports))
922 self.interface.servers = servers
923 self.interface.trigger_callback('peers')
925 elif method == 'server.banner':
926 self.wallet.banner = result
927 self.wallet.was_updated = True
930 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
932 if self.wallet.was_updated:
933 self.interface.trigger_callback('updated')
934 self.wallet.was_updated = False
937 encode = lambda x: x[::-1].encode('hex')
938 decode = lambda x: x.decode('hex')[::-1]
939 from bitcoin import Hash, rev_hex, int_to_hex
941 class WalletVerifier(threading.Thread):
943 def __init__(self, wallet, config):
944 threading.Thread.__init__(self)
948 self.interface = self.wallet.interface
949 self.interface.register_channel('verifier')
950 self.validated = config.get('verified_tx',[])
951 self.merkle_roots = config.get('merkle_roots',{})
952 self.headers = config.get('block_headers',{})
953 self.lock = threading.Lock()
960 txlist = self.wallet.get_tx_hashes()
963 if tx not in self.validated:
964 if tx not in requested:
966 self.request_merkle(tx)
971 r = self.interface.get_response('verifier',timeout=1)
973 if len(self.validated) == len(txlist) and not self.saved:
974 print "saving verified transactions"
975 self.config.set_key('verified_tx', self.validated, True)
984 if method == 'blockchain.transaction.get_merkle':
986 tx_height = result.get('block_height')
987 self.merkle_roots[tx_hash] = self.hash_merkle_root(result['merkle'], tx_hash)
988 # if we already have the header, check merkle root directly
989 header = self.headers.get(tx_height)
991 self.validated.append(tx_hash)
992 assert header.get('merkle_root') == self.merkle_roots[tx_hash]
993 self.request_headers(tx_height)
995 elif method == 'blockchain.block.get_header':
996 self.validate_header(result)
999 def request_merkle(self, tx_hash):
1000 self.interface.send([ ('blockchain.transaction.get_merkle',[tx_hash]) ], 'verifier')
1003 def request_headers(self, tx_height, delta=10):
1004 headers_requests = []
1005 for height in range(tx_height-delta,tx_height+delta): # we might can request blocks that do not exist yet
1006 if height not in self.headers:
1007 headers_requests.append( ('blockchain.block.get_header',[height]) )
1008 self.interface.send(headers_requests,'verifier')
1011 def validate_header(self, header):
1012 """ if there is a previous or a next block in the list, check the hash"""
1013 height = header.get('block_height')
1015 self.headers[height] = header # detect conflicts
1016 prev_header = next_header = None
1017 if height-1 in self.headers:
1018 prev_header = self.headers[height-1]
1019 if height+1 in self.headers:
1020 next_header = self.headers[height+1]
1023 prev_hash = self.hash_header(prev_header)
1024 assert prev_hash == header.get('prev_block_hash')
1026 _hash = self.hash_header(header)
1027 assert _hash == next_header.get('prev_block_hash')
1029 # check if there are transactions at that height
1030 for tx_hash in self.wallet.get_transactions_at_height(height):
1031 if tx_hash in self.validated: continue
1032 # check if we already have the merkle root
1033 merkle_root = self.merkle_roots.get(tx_hash)
1035 self.validated.append(tx_hash)
1036 assert header.get('merkle_root') == merkle_root
1038 def hash_header(self, res):
1039 header = int_to_hex(res.get('version'),4) \
1040 + rev_hex(res.get('prev_block_hash')) \
1041 + rev_hex(res.get('merkle_root')) \
1042 + int_to_hex(int(res.get('timestamp')),4) \
1043 + int_to_hex(int(res.get('bits')),4) \
1044 + int_to_hex(int(res.get('nonce')),4)
1045 return rev_hex(Hash(header.decode('hex')).encode('hex'))
1047 def hash_merkle_root(self, merkle_s, target_hash):
1048 h = decode(target_hash)
1049 for item in merkle_s:
1050 is_left = item[0] == 'L'
1051 h = Hash( h + decode(item[1:]) ) if is_left else Hash( decode(item[1:]) + h )