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/>.
34 from ecdsa.util import string_to_number, number_to_string
35 from util import print_error, user_dir, format_satoshis
39 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
40 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
43 EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
44 DecodeAES = lambda secret, e: aes.decryptData(secret, base64.b64decode(e))
47 from version import ELECTRUM_VERSION, SEED_VERSION
51 def __init__(self, config={}):
54 self.electrum_version = ELECTRUM_VERSION
57 self.seed_version = config.get('seed_version', SEED_VERSION)
58 self.gap_limit = config.get('gap_limit', 5)
59 self.use_change = config.get('use_change',True)
60 self.fee = int(config.get('fee',100000))
61 self.num_zeros = int(config.get('num_zeros',0))
62 self.master_public_key = config.get('master_public_key','')
63 self.use_encryption = config.get('use_encryption', False)
64 self.addresses = config.get('addresses', []) # receiving addresses visible for user
65 self.change_addresses = config.get('change_addresses', []) # addresses used as change
66 self.seed = config.get('seed', '') # encrypted
67 self.labels = config.get('labels',{'1NmduGNyC5XejoysbuioodCN3jR3yf64xM':'Electrum donation address'})
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', ['1NmduGNyC5XejoysbuioodCN3jR3yf64xM'])
74 self.imported_keys = config.get('imported_keys',{})
75 self.history = config.get('addr_history',{}) # address -> list(txid, height)
76 self.transactions = config.get('transactions',{}) # txid -> deserialised
78 self.requested_amounts = config.get('requested_amounts',{}) # txid -> deserialised
81 self.prevout_values = {} # my own transaction outputs
82 self.spent_outputs = []
83 self.receipt = None # next receipt
89 # there is a difference between wallet.up_to_date and interface.is_up_to_date()
90 # interface.is_up_to_date() returns true when all requests have been answered and processed
91 # wallet.up_to_date is true when the wallet is synchronized (stronger requirement)
93 self.up_to_date = False
94 self.lock = threading.Lock()
95 self.tx_event = threading.Event()
97 if self.seed_version != SEED_VERSION:
98 raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
100 for tx_hash in self.transactions.keys():
101 self.update_tx_outputs(tx_hash)
104 def set_up_to_date(self,b):
105 with self.lock: self.up_to_date = b
107 def is_up_to_date(self):
108 with self.lock: return self.up_to_date
111 self.up_to_date = False
112 self.interface.poke('synchronizer')
113 while not self.is_up_to_date(): time.sleep(0.1)
115 def import_key(self, keypair, password):
117 address, sec = keypair.split(':')
118 if not self.is_valid(address):
119 raise BaseException('Invalid Bitcoin address')
120 if address in self.all_addresses():
121 raise BaseException('Address already in wallet')
123 # rebuild public key from private key, compressed or uncompressed
124 pkey = regenerate_key(sec)
128 # figure out if private key is compressed
129 compressed = is_compressed(sec)
131 # rebuild private and public key from regenerated secret
132 private_key = GetPrivKey(pkey, compressed)
133 public_key = GetPubKey(pkey, compressed)
134 addr = public_key_to_bc_address(public_key)
137 if not address == addr :
138 raise BaseException('Address does not match private key')
140 # store the originally requested keypair into the imported keys table
141 self.imported_keys[address] = self.pw_encode(sec, password )
144 def new_seed(self, password):
145 seed = "%032x"%ecdsa.util.randrange( pow(2,128) )
148 self.seed = self.pw_encode( seed, password )
151 def init_mpk(self,seed):
154 secexp = self.stretch_key(seed)
155 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
156 self.master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
158 def all_addresses(self):
159 return self.addresses + self.change_addresses + self.imported_keys.keys()
161 def is_mine(self, address):
162 return address in self.all_addresses()
164 def is_change(self, address):
165 return address in self.change_addresses
167 def is_valid(self,addr):
168 ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
169 if not ADDRESS_RE.match(addr): return False
171 h = bc_address_to_hash_160(addr)
174 return addr == hash_160_to_bc_address(h)
176 def stretch_key(self,seed):
178 for i in range(100000):
179 seed = hashlib.sha256(seed + oldseed).digest()
180 return string_to_number( seed )
182 def get_sequence(self,n,for_change):
183 return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key.decode('hex') ) )
185 def get_private_key_base58(self, address, password):
186 secexp, compressed = self.get_private_key(address, password)
187 if secexp is None: return None
188 pk = number_to_string( secexp, generator_secp256k1.order() )
189 return SecretToASecret( pk, compressed )
191 def get_private_key(self, address, password):
192 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
193 order = generator_secp256k1.order()
195 if address in self.imported_keys.keys():
196 sec = self.pw_decode( self.imported_keys[address], password )
197 if not sec: return None, None
199 pkey = regenerate_key(sec)
200 compressed = is_compressed(sec)
204 if address in self.addresses:
205 n = self.addresses.index(address)
207 elif address in self.change_addresses:
208 n = self.change_addresses.index(address)
211 raise BaseException("unknown address")
213 seed = self.pw_decode( self.seed, password)
215 raise BaseException("Invalid password")
216 if not seed: return None
217 secexp = self.stretch_key(seed)
218 secexp = ( secexp + self.get_sequence(n,for_change) ) % order
221 return secexp, compressed
223 def msg_magic(self, message):
224 return "\x18Bitcoin Signed Message:\n" + chr( len(message) ) + message
226 def sign_message(self, address, message, password):
227 secexp, compressed = self.get_private_key(address, password)
228 private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
229 public_key = private_key.get_verifying_key()
230 signature = private_key.sign_digest( Hash( self.msg_magic( message ) ), sigencode = ecdsa.util.sigencode_string )
231 assert public_key.verify_digest( signature, Hash( self.msg_magic( message ) ), sigdecode = ecdsa.util.sigdecode_string)
233 sig = base64.b64encode( chr(27 + i + (4 if compressed else 0)) + signature )
235 self.verify_message( address, sig, message)
240 raise BaseException("error: cannot sign message")
243 def verify_message(self, address, signature, message):
244 """ See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """
245 from ecdsa import numbertheory, ellipticcurve, util
247 curve = curve_secp256k1
248 G = generator_secp256k1
250 # extract r,s from signature
251 sig = base64.b64decode(signature)
252 if len(sig) != 65: raise BaseException("Wrong encoding")
253 r,s = util.sigdecode_string(sig[1:], order)
255 if nV < 27 or nV >= 35:
256 raise BaseException("Bad encoding")
265 x = r + (recid/2) * order
267 alpha = ( x * x * x + curve.a() * x + curve.b() ) % curve.p()
268 beta = msqr.modular_sqrt(alpha, curve.p())
269 y = beta if (beta - recid) % 2 == 0 else curve.p() - beta
270 # 1.4 the constructor checks that nR is at infinity
271 R = ellipticcurve.Point(curve, x, y, order)
272 # 1.5 compute e from message:
273 h = Hash( self.msg_magic( message ) )
274 e = string_to_number(h)
276 # 1.6 compute Q = r^-1 (sR - eG)
277 inv_r = numbertheory.inverse_mod(r,order)
278 Q = inv_r * ( s * R + minus_e * G )
279 public_key = ecdsa.VerifyingKey.from_public_point( Q, curve = SECP256k1 )
280 # check that Q is the public key
281 public_key.verify_digest( sig[1:], h, sigdecode = ecdsa.util.sigdecode_string)
282 # check that we get the original signing address
283 addr = public_key_to_bc_address( encode_point(public_key, compressed) )
285 raise BaseException("Bad signature")
288 def create_new_address(self, for_change):
289 n = len(self.change_addresses) if for_change else len(self.addresses)
290 address = self.get_new_address(n, for_change)
292 self.change_addresses.append(address)
294 self.addresses.append(address)
295 self.history[address] = []
298 def get_new_address(self, n, for_change):
299 """ Publickey(type,n) = Master_public_key + H(n|S|type)*point """
301 z = self.get_sequence(n, for_change)
302 master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key.decode('hex'), curve = SECP256k1 )
303 pubkey_point = master_public_key.pubkey.point + z*curve.generator
304 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
305 address = public_key_to_bc_address( '04'.decode('hex') + public_key2.to_string() )
310 def change_gap_limit(self, value):
311 if value >= self.gap_limit:
312 self.gap_limit = value
314 self.interface.poke('synchronizer')
317 elif value >= self.min_acceptable_gap():
318 k = self.num_unused_trailing_addresses()
319 n = len(self.addresses) - k + value
320 self.addresses = self.addresses[0:n]
321 self.gap_limit = value
327 def num_unused_trailing_addresses(self):
329 for a in self.addresses[::-1]:
330 if self.history.get(a):break
334 def min_acceptable_gap(self):
335 # fixme: this assumes wallet is synchronized
338 k = self.num_unused_trailing_addresses()
339 for a in self.addresses[0:-k]:
340 if self.history.get(a):
344 if n > nmax: nmax = n
348 def synchronize(self):
349 if not self.master_public_key:
354 if self.change_addresses == []:
355 new_addresses.append( self.create_new_address(True) )
357 a = self.change_addresses[-1]
358 if self.history.get(a):
359 new_addresses.append( self.create_new_address(True) )
365 if len(self.addresses) < n:
366 new_addresses.append( self.create_new_address(False) )
368 if map( lambda a: self.history.get(a, []), self.addresses[-n:] ) == n*[[]]:
371 new_addresses.append( self.create_new_address(False) )
377 return (len(self.change_addresses) > 1 ) or ( len(self.addresses) > self.gap_limit )
379 def fill_addressbook(self):
380 for tx_hash, tx in self.transactions.items():
381 is_send, _, _ = self.get_tx_value(tx_hash)
383 for o in tx['outputs']:
384 addr = o.get('address')
385 if not self.is_mine(addr) and addr not in self.addressbook:
386 self.addressbook.append(addr)
388 # self.update_tx_labels()
391 def get_address_flags(self, addr):
392 flags = "C" if self.is_change(addr) else "I" if addr in self.imported_keys.keys() else "-"
393 flags += "F" if addr in self.frozen_addresses else "P" if addr in self.prioritized_addresses else "-"
397 def get_tx_value(self, tx_hash, addresses = None):
398 # return the balance for that tx
399 if addresses is None: addresses = self.all_addresses()
403 v_in = v_out = v_out_mine = 0
404 d = self.transactions.get(tx_hash)
408 for item in d.get('inputs'):
409 addr = item.get('address')
410 if addr in addresses:
412 key = item['prevout_hash'] + ':%d'%item['prevout_n']
413 value = self.prevout_values.get( key )
421 for item in d.get('outputs'):
422 addr = item.get('address')
423 value = item.get('value')
425 if addr in addresses:
429 # all inputs are mine:
431 v = v_out_mine - v_in
433 # some inputs are mine:
436 v = v_out_mine - v_out
441 return is_send, v, fee
444 def get_tx_details(self, tx_hash):
446 if not tx_hash: return ''
447 tx = self.transactions.get(tx_hash)
448 is_mine, v, fee = self.get_tx_value(tx_hash)
449 conf, timestamp = self.verifier.get_confirmations(tx_hash)
452 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
456 inputs = map(lambda x: x.get('address'), tx['inputs'])
457 outputs = map(lambda x: x.get('address'), tx['outputs'])
458 tx_details = "Transaction Details" +"\n\n" \
459 + "Transaction ID:\n" + tx_hash + "\n\n" \
460 + "Status: %d confirmations\n"%conf
463 tx_details += "Amount sent: %s\n"% format_satoshis(v-fee, False) \
464 + "Transaction fee: %s\n"% format_satoshis(fee, False)
466 tx_details += "Amount sent: %s\n"% format_satoshis(v, False) \
467 + "Transaction fee: unknown\n"
469 tx_details += "Amount received: %s\n"% format_satoshis(v, False) \
471 tx_details += "Date: %s\n\n"%time_str \
472 + "Inputs:\n-"+ '\n-'.join(inputs) + "\n\n" \
473 + "Outputs:\n-"+ '\n-'.join(outputs)
475 r = self.receipts.get(tx_hash)
477 tx_details += "\n_______________________________________" \
478 + '\n\nSigned URI: ' + r[2] \
479 + "\n\nSigned by: " + r[0] \
480 + '\n\nSignature: ' + r[1]
485 def update_tx_outputs(self, tx_hash):
486 tx = self.transactions.get(tx_hash)
487 for item in tx.get('outputs'):
488 value = item.get('value')
489 key = tx_hash+ ':%d'%item.get('index')
491 self.prevout_values[key] = value
493 for item in tx.get('inputs'):
494 if self.is_mine(item.get('address')):
495 key = item['prevout_hash'] + ':%d'%item['prevout_n']
496 self.spent_outputs.append(key)
499 def get_addr_balance(self, address):
500 assert self.is_mine(address)
501 h = self.history.get(address,[])
502 if h == ['*']: return 0,0
504 received_coins = [] # list of coins received at address
506 for tx_hash, tx_height in h:
507 d = self.transactions.get(tx_hash)
509 for item in d.get('outputs'):
510 addr = item.get('address')
512 key = tx_hash + ':%d'%item['index']
513 received_coins.append(key)
515 for tx_hash, tx_height in h:
516 d = self.transactions.get(tx_hash)
520 for item in d.get('inputs'):
521 addr = item.get('address')
523 key = item['prevout_hash'] + ':%d'%item['prevout_n']
524 value = self.prevout_values.get( key )
525 if key in received_coins:
528 for item in d.get('outputs'):
529 addr = item.get('address')
530 key = tx_hash + ':%d'%item['index']
532 v += item.get('value')
540 def get_balance(self):
542 for addr in self.all_addresses():
543 c, u = self.get_addr_balance(addr)
549 def choose_tx_inputs( self, amount, fixed_fee, from_addr = None ):
550 """ todo: minimize tx size """
552 fee = self.fee if fixed_fee is None else fixed_fee
555 prioritized_coins = []
556 domain = [from_addr] if from_addr else self.all_addresses()
557 for i in self.frozen_addresses:
558 if i in domain: domain.remove(i)
560 for i in self.prioritized_addresses:
561 if i in domain: domain.remove(i)
564 h = self.history.get(addr, [])
565 if h == ['*']: continue
566 for tx_hash, tx_height in h:
567 tx = self.transactions.get(tx_hash)
568 for output in tx.get('outputs'):
569 if output.get('address') != addr: continue
570 key = tx_hash + ":%d" % output.get('index')
571 if key in self.spent_outputs: continue
572 output['tx_hash'] = tx_hash
576 for addr in self.prioritized_addresses:
577 h = self.history.get(addr, [])
578 if h == ['*']: continue
579 for tx_hash, tx_height in h:
580 tx = self.transactions.get(tx_hash)
581 for output in tx.get('outputs'):
582 if output.get('address') != addr: continue
583 key = tx_hash + ":%d" % output.get('index')
584 if key in self.spent_outputs: continue
585 output['tx_hash'] = tx_hash
586 prioritized_coins.append(output)
590 coins = prioritized_coins + coins
593 addr = item.get('address')
594 v = item.get('value')
596 inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], None, None) )
597 fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
598 if total >= amount + fee: break
600 #print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
602 return inputs, total, fee
604 def add_tx_change( self, outputs, amount, fee, total, change_addr=None ):
605 change_amount = total - ( amount + fee )
606 if change_amount != 0:
607 # normally, the update thread should ensure that the last change address is unused
609 change_addr = self.change_addresses[-1]
610 outputs.append( ( change_addr, change_amount) )
613 def sign_inputs( self, inputs, outputs, password ):
615 for i in range(len(inputs)):
616 addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
617 secexp, compressed = self.get_private_key(addr, password)
618 private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
619 public_key = private_key.get_verifying_key()
621 pkey = EC_KEY(secexp)
622 pubkey = GetPubKey(pkey, compressed)
624 tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
625 sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
626 assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
627 s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
630 def pw_encode(self, s, password):
632 secret = Hash(password)
633 return EncodeAES(secret, s)
637 def pw_decode(self, s, password):
638 if password is not None:
639 secret = Hash(password)
640 d = DecodeAES(secret, s)
645 raise ValueError("Invalid password")
651 def get_history(self, address):
653 return self.history.get(address)
655 def get_status(self, h):
656 if not h: return None
657 if h == ['*']: return '*'
659 for tx_hash, height in h:
660 status += tx_hash + ':%d:' % height
661 return hashlib.sha256( status ).digest().encode('hex')
665 def receive_tx_callback(self, tx_hash, tx):
667 if not self.check_new_tx(tx_hash, tx):
668 raise BaseException("error: received transaction is not consistent with history"%tx_hash)
671 self.transactions[tx_hash] = tx
673 tx_height = tx.get('height')
674 if self.verifier and tx_height>0:
675 self.verifier.add(tx_hash, tx_height)
677 self.update_tx_outputs(tx_hash)
682 def receive_history_callback(self, addr, hist):
684 if not self.check_new_history(addr, hist):
685 raise BaseException("error: received history for %s is not consistent with known transactions"%addr)
688 self.history[addr] = hist
692 for tx_hash, tx_height in hist:
694 # add it in case it was previously unconfirmed
695 if self.verifier: self.verifier.add(tx_hash, tx_height)
696 # set the height in case it changed
697 tx = self.transactions.get(tx_hash)
699 if tx.get('height') != tx_height:
700 print_error( "changing height for tx", tx_hash )
701 tx['height'] = tx_height
704 def get_tx_history(self):
706 history = self.transactions.values()
707 history.sort(key = lambda x: x.get('height') if x.get('height') else 1e12)
712 is_mine, v, fee = self.get_tx_value(tx['tx_hash'])
713 if v is not None: balance += v
714 c, u = self.get_balance()
717 v_str = format_satoshis( c+u - balance, True, self.num_zeros)
718 result.append( ('', 1000, 0, c+u-balance, None, c+u-balance, None ) )
720 balance = c + u - balance
722 tx_hash = tx['tx_hash']
723 conf, timestamp = self.verifier.get_confirmations(tx_hash) if self.verifier else (None, None)
724 is_mine, value, fee = self.get_tx_value(tx_hash)
725 if value is not None:
728 result.append( (tx_hash, conf, is_mine, value, fee, balance, timestamp) )
732 def get_transactions_at_height(self, height):
734 values = self.transactions.values()[:]
738 if tx['height'] == height:
739 out.append(tx['tx_hash'])
743 def get_label(self, tx_hash):
744 label = self.labels.get(tx_hash)
745 is_default = (label == '') or (label is None)
746 if is_default: label = self.get_default_label(tx_hash)
747 return label, is_default
749 def get_default_label(self, tx_hash):
750 tx = self.transactions.get(tx_hash)
753 is_mine, _, _ = self.get_tx_value(tx_hash)
755 for o in tx['outputs']:
756 o_addr = o.get('address')
757 if not self.is_mine(o_addr):
759 default_label = self.labels[o_addr]
761 default_label = o_addr
763 for o in tx['outputs']:
764 o_addr = o.get('address')
765 if self.is_mine(o_addr) and not self.is_change(o_addr):
768 for o in tx['outputs']:
769 o_addr = o.get('address')
770 if self.is_mine(o_addr):
776 dest_label = self.labels.get(o_addr)
778 default_label = self.labels[o_addr]
780 default_label = o_addr
785 def mktx(self, outputs, label, password, fee=None, change_addr=None, from_addr= None):
787 for address, x in outputs:
788 assert self.is_valid(address)
790 amount = sum( map(lambda x:x[1], outputs) )
791 inputs, total, fee = self.choose_tx_inputs( amount, fee, from_addr )
793 raise ValueError("Not enough funds")
795 if not self.use_change and not change_addr:
796 change_addr = inputs[-1][0]
797 print_error( "Sending change to", change_addr )
798 outputs = self.add_tx_change(outputs, amount, fee, total, change_addr)
801 return repr({'inputs':inputs, 'outputs':outputs})
803 tx = self.signed_tx(inputs, outputs, password)
805 for address, x in outputs:
806 if address not in self.addressbook and not self.is_mine(address):
807 self.addressbook.append(address)
810 tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
811 self.labels[tx_hash] = label
815 def signed_tx(self, inputs, outputs, password):
816 s_inputs = self.sign_inputs( inputs, outputs, password )
817 tx = filter( raw_tx( s_inputs, outputs ) )
820 def sendtx(self, tx):
824 return self.receive_tx(h)
826 def send_tx(self, tx):
828 self.tx_event.clear()
829 tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
830 self.interface.send([('blockchain.transaction.broadcast', [tx])], 'synchronizer')
833 def receive_tx(self,tx_hash):
836 return False, "error: " + out
838 self.receipts[tx_hash] = self.receipt
843 def read_alias(self, alias):
844 # this might not be the right place for this function.
847 m1 = re.match('([\w\-\.]+)@((\w[\w\-]+\.)+[\w\-]+)', alias)
848 m2 = re.match('((\w[\w\-]+\.)+[\w\-]+)', alias)
850 url = 'https://' + m1.group(2) + '/bitcoin.id/' + m1.group(1)
852 url = 'https://' + alias + '/bitcoin.id'
856 lines = urllib.urlopen(url).readlines()
861 line = lines[0].strip().split(':')
864 target = signing_addr = line[0]
866 target, auth_name, signing_addr, signature = line
867 msg = "alias:%s:%s:%s"%(alias,target,auth_name)
869 self.verify_message(signing_addr, signature, msg)
871 # other lines are signed updates
872 for line in lines[1:]:
874 if not line: continue
875 line = line.split(':')
878 target, signature = line
879 self.verify_message(previous, signature, "alias:%s:%s"%(alias,target))
881 if not self.is_valid(target):
882 raise ValueError("Invalid bitcoin address")
884 return target, signing_addr, auth_name
886 def update_password(self, seed, old_password, new_password):
887 if new_password == '': new_password = None
888 self.use_encryption = (new_password != None)
889 self.seed = self.pw_encode( seed, new_password)
890 for k in self.imported_keys.keys():
891 a = self.imported_keys[k]
892 b = self.pw_decode(a, old_password)
893 c = self.pw_encode(b, new_password)
894 self.imported_keys[k] = c
897 def get_alias(self, alias, interactive = False, show_message=None, question = None):
899 target, signing_address, auth_name = self.read_alias(alias)
900 except BaseException, e:
901 # raise exception if verify fails (verify the chain)
903 show_message("Alias error: " + str(e))
906 print target, signing_address, auth_name
908 if auth_name is None:
909 a = self.aliases.get(alias)
911 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)
912 if interactive and question( msg ):
913 self.aliases[alias] = (signing_address, target)
917 if signing_address != a[0]:
918 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
919 if interactive and question( msg ):
920 self.aliases[alias] = (signing_address, target)
924 if signing_address not in self.authorities.keys():
925 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)
926 if interactive and question( msg ):
927 self.authorities[signing_address] = auth_name
932 self.aliases[alias] = (signing_address, target)
937 def parse_url(self, url, show_message, question):
938 o = url[8:].split('?')
941 params = o[1].split('&')
945 amount = label = message = signature = identity = ''
949 if k == 'amount': amount = uv
950 elif k == 'message': message = uv
951 elif k == 'label': label = uv
952 elif k == 'signature':
953 identity, signature = uv.split(':')
954 url = url.replace('&%s=%s'%(k,v),'')
958 if label and self.labels.get(address) != label:
959 if question('Give label "%s" to address %s ?'%(label,address)):
960 if address not in self.addressbook and address not in self.all_addresses():
961 self.addressbook.append(address)
962 self.labels[address] = label
965 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', identity):
966 signing_address = self.get_alias(identity, True, show_message, question)
967 elif self.is_valid(identity):
968 signing_address = identity
970 signing_address = None
971 if not signing_address:
974 self.verify_message(signing_address, signature, url )
975 self.receipt = (signing_address, signature, url)
977 show_message('Warning: the URI contains a bad signature.\nThe identity of the recipient cannot be verified.')
978 address = amount = label = identity = message = ''
980 if re.match('^(|([\w\-\.]+)@)((\w[\w\-]+\.)+[\w\-]+)$', address):
981 payto_address = self.get_alias(address, True, show_message, question)
983 address = address + ' <' + payto_address + '>'
985 return address, amount, label, message, signature, identity, url
989 def freeze(self,addr):
990 if addr in self.all_addresses() and addr not in self.frozen_addresses:
991 self.unprioritize(addr)
992 self.frozen_addresses.append(addr)
993 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
998 def unfreeze(self,addr):
999 if addr in self.all_addresses() and addr in self.frozen_addresses:
1000 self.frozen_addresses.remove(addr)
1001 self.config.set_key('frozen_addresses', self.frozen_addresses, True)
1006 def prioritize(self,addr):
1007 if addr in self.all_addresses() and addr not in self.prioritized_addresses:
1009 self.prioritized_addresses.append(addr)
1010 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
1015 def unprioritize(self,addr):
1016 if addr in self.all_addresses() and addr in self.prioritized_addresses:
1017 self.prioritized_addresses.remove(addr)
1018 self.config.set_key('prioritized_addresses', self.prioritized_addresses, True)
1025 'seed_version': self.seed_version,
1026 'use_encryption': self.use_encryption,
1027 'use_change': self.use_change,
1028 'master_public_key': self.master_public_key,
1031 'addresses': self.addresses,
1032 'change_addresses': self.change_addresses,
1033 'addr_history': self.history,
1034 'labels': self.labels,
1035 'contacts': self.addressbook,
1036 'imported_keys': self.imported_keys,
1037 'aliases': self.aliases,
1038 'authorities': self.authorities,
1039 'receipts': self.receipts,
1040 'num_zeros': self.num_zeros,
1041 'frozen_addresses': self.frozen_addresses,
1042 'prioritized_addresses': self.prioritized_addresses,
1043 'gap_limit': self.gap_limit,
1044 'transactions': self.transactions,
1045 'requested_amounts': self.requested_amounts,
1047 for k, v in s.items():
1048 self.config.set_key(k,v)
1051 def set_verifier(self, verifier):
1052 self.verifier = verifier
1054 # review stored transactions and send them to the verifier
1055 # (they are not necessarily in the history, because history items might have have been pruned)
1056 for tx_hash, tx in self.transactions.items():
1057 tx_height = tx.get('height')
1059 print_error( "skipping", tx_hash, tx_height )
1063 self.verifier.add(tx_hash, tx_height)
1065 # review transactions that are in the history
1066 for addr, hist in self.history.items():
1067 if hist == ['*']: continue
1068 for tx_hash, tx_height in hist:
1070 # add it in case it was previously unconfirmed
1071 self.verifier.add(tx_hash, tx_height)
1072 # set the height in case it changed
1073 tx = self.transactions.get(tx_hash)
1075 if tx.get('height') != tx_height:
1076 print_error( "changing height for tx", tx_hash )
1077 tx['height'] = tx_height
1080 def is_addr_in_tx(self, addr, tx):
1082 for txin in tx.get('inputs'):
1083 if addr == txin.get('address'):
1086 for txout in tx.get('outputs'):
1087 if addr == txout.get('address'):
1093 def check_new_history(self, addr, hist):
1095 # check that all tx in hist are relevant
1097 for tx_hash, height in hist:
1098 tx = self.transactions.get(tx_hash)
1100 if not self.is_addr_in_tx(addr,tx):
1103 # check that we are not "orphaning" a transaction
1104 old_hist = self.history.get(addr,[])
1105 if old_hist == ['*']: return True
1107 for tx_hash, height in old_hist:
1108 if tx_hash in map(lambda x:x[0], hist): continue
1110 for _addr, _hist in self.history.items():
1111 if _addr == addr: continue
1112 if _hist == ['*']: continue
1113 _tx_hist = map(lambda x:x[0], _hist)
1114 if tx_hash in _tx_hist:
1119 tx = self.transactions.get(tx_hash)
1121 if tx.get('height'):
1124 print_error("new history is orphaning transaction:", tx_hash)
1125 # check that all outputs are not mine, request histories
1127 for o in tx.get('outputs'):
1128 _addr = o.get('address')
1129 # assert not self.is_mine(_addr)
1130 ext_requests.append( ('blockchain.address.get_history', [_addr]) )
1132 ext_h = self.interface.synchronous_get(ext_requests)
1135 if h == ['*']: continue
1137 if item.get('tx_hash') == tx_hash:
1138 height = item.get('height')
1139 tx['height'] = height
1141 print_error("found height for", tx_hash, height)
1142 self.verifier.add(tx_hash, height)
1144 print_error("removing orphaned tx from history", tx_hash)
1145 self.transactions.pop(tx_hash)
1151 def check_new_tx(self, tx_hash, tx):
1152 # 1 check that tx is referenced in addr_history.
1154 for addr, hist in self.history.items():
1155 if hist == ['*']:continue
1156 for txh, height in hist:
1158 addresses.append(addr)
1163 # 2 check that referencing addresses are in the tx
1164 for addr in addresses:
1165 if not self.is_addr_in_tx(addr, tx):
1173 class WalletSynchronizer(threading.Thread):
1176 def __init__(self, wallet, config):
1177 threading.Thread.__init__(self)
1179 self.wallet = wallet
1180 self.interface = self.wallet.interface
1181 self.interface.register_channel('synchronizer')
1182 self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
1183 self.wallet.interface.register_callback('connected', lambda: self.interface.send([('server.banner',[])],'synchronizer') )
1184 self.was_updated = True
1185 self.running = False
1186 self.lock = threading.Lock()
1189 with self.lock: self.running = False
1190 self.interface.poke('synchronizer')
1192 def is_running(self):
1193 with self.lock: return self.running
1195 def synchronize_wallet(self):
1196 new_addresses = self.wallet.synchronize()
1198 self.subscribe_to_addresses(new_addresses)
1199 self.wallet.up_to_date = False
1202 if not self.interface.is_up_to_date('synchronizer'):
1203 if self.wallet.is_up_to_date():
1204 self.wallet.set_up_to_date(False)
1205 self.was_updated = True
1208 self.wallet.set_up_to_date(True)
1209 self.was_updated = True
1212 def subscribe_to_addresses(self, addresses):
1214 for addr in addresses:
1215 messages.append(('blockchain.address.subscribe', [addr]))
1216 self.interface.send( messages, 'synchronizer')
1220 with self.lock: self.running = True
1224 requested_histories = {}
1226 # request any missing transactions
1227 for history in self.wallet.history.values():
1228 if history == ['*']: continue
1229 for tx_hash, tx_height in history:
1230 if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
1231 missing_tx.append( (tx_hash, tx_height) )
1232 print_error("missing tx", missing_tx)
1234 # wait until we are connected, in case the user is not connected
1235 while not self.interface.is_connected:
1238 # request banner, because 'connected' event happens before this thread is started
1239 self.interface.send([('server.banner',[])],'synchronizer')
1242 self.subscribe_to_addresses(self.wallet.all_addresses())
1244 while self.is_running():
1245 # 1. send new requests
1246 self.synchronize_wallet()
1248 for tx_hash, tx_height in missing_tx:
1249 if (tx_hash, tx_height) not in requested_tx:
1250 self.interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], 'synchronizer')
1251 requested_tx.append( (tx_hash, tx_height) )
1254 if self.was_updated:
1255 self.interface.trigger_callback('updated')
1256 self.was_updated = False
1259 r = self.interface.get_response('synchronizer')
1261 # poke sends None. (needed during stop)
1264 # 3. handle response
1265 method = r['method']
1266 params = r['params']
1267 result = r.get('result')
1268 error = r.get('error')
1273 if method == 'blockchain.address.subscribe':
1275 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
1276 if requested_histories.get(addr) is None:
1277 self.interface.send([('blockchain.address.get_history', [addr])], 'synchronizer')
1278 requested_histories[addr] = result
1280 elif method == 'blockchain.address.get_history':
1282 print_error("receiving history", addr, result)
1284 assert requested_histories.pop(addr) == '*'
1285 self.wallet.receive_history_callback(addr, result)
1288 # check that txids are unique
1291 tx_hash = item['tx_hash']
1292 if tx_hash not in txids:
1293 txids.append(tx_hash)
1294 hist.append( (tx_hash, item['height']) )
1296 if len(hist) != len(result):
1297 raise BaseException("error: server sent history with non-unique txid", result)
1299 # check that the status corresponds to what was announced
1300 rs = requested_histories.pop(addr)
1301 if self.wallet.get_status(hist) != rs:
1302 raise BaseException("error: status mismatch: %s"%addr)
1304 # store received history
1305 self.wallet.receive_history_callback(addr, hist)
1307 # request transactions that we don't have
1308 for tx_hash, tx_height in hist:
1309 if self.wallet.transactions.get(tx_hash) is None:
1310 if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
1311 missing_tx.append( (tx_hash, tx_height) )
1313 elif method == 'blockchain.transaction.get':
1315 tx_height = params[1]
1316 d = self.deserialize_tx(tx_hash, tx_height, result)
1317 self.wallet.receive_tx_callback(tx_hash, d)
1318 self.was_updated = True
1319 requested_tx.remove( (tx_hash, tx_height) )
1320 print_error("received tx:", d)
1322 elif method == 'blockchain.transaction.broadcast':
1323 self.wallet.tx_result = result
1324 self.wallet.tx_event.set()
1326 elif method == 'server.banner':
1327 self.wallet.banner = result
1328 self.was_updated = True
1331 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
1333 if self.was_updated and not requested_tx:
1334 self.interface.trigger_callback('updated')
1335 self.was_updated = False
1338 def deserialize_tx(self, tx_hash, tx_height, raw_tx):
1340 assert tx_hash == hash_encode(Hash(raw_tx.decode('hex')))
1342 vds = deserialize.BCDataStream()
1343 vds.write(raw_tx.decode('hex'))
1344 d = deserialize.parse_Transaction(vds)
1345 d['height'] = tx_height
1346 d['tx_hash'] = tx_hash