cannot handle old versions

This commit is contained in:
thomasv 2011-12-16 15:40:05 +01:00
parent c0bf69e3b3
commit 6f9d7fee94
3 changed files with 27 additions and 58 deletions

View file

@ -211,8 +211,6 @@ def raw_tx( inputs, outputs, for_sig = None ):
if for_sig is not None: s += int_to_hex(1, 4) # hash type if for_sig is not None: s += int_to_hex(1, 4) # hash type
return s return s
class InvalidPassword(Exception):
pass
@ -330,6 +328,9 @@ class Wallet:
def create_new_address2(self, for_change): def create_new_address2(self, for_change):
""" Publickey(type,n) = Master_public_key + H(n|S|type)*point """ """ Publickey(type,n) = Master_public_key + H(n|S|type)*point """
if self.master_public_key is None:
raise BaseException("Cannot create new addresses with this wallet.\nIf this is an old wallet, please move your complete balance to a new wallet.")
curve = SECP256k1 curve = SECP256k1
n = len(self.change_addresses) if for_change else len(self.addresses) n = len(self.change_addresses) if for_change else len(self.addresses)
z = self.get_sequence(n,for_change) z = self.get_sequence(n,for_change)
@ -351,7 +352,8 @@ class Wallet:
def synchronize(self): def synchronize(self):
if self.master_public_key is None: return False # will be None if we read an older format
while True: while True:
if self.change_addresses == []: if self.change_addresses == []:
self.create_new_address2(True) self.create_new_address2(True)
@ -408,6 +410,7 @@ class Wallet:
f.close() f.close()
def read(self): def read(self):
upgrade_msg = """This wallet seed is deprecated. Please run upgrade.py for a diagnostic."""
try: try:
f = open(self.path,"r") f = open(self.path,"r")
data = f.read() data = f.read()
@ -431,15 +434,12 @@ class Wallet:
self.labels = d.get('labels') self.labels = d.get('labels')
self.addressbook = d.get('contacts') self.addressbook = d.get('contacts')
except: except:
raise BaseException("Error; could not parse wallet. If this is an old wallet format, please use upgrade.py.",0) raise BaseException(upgrade_msg)
self.update_tx_history() self.update_tx_history()
if self.seed_version != SEED_VERSION: if self.seed_version != SEED_VERSION:
raise BaseException("""Seed version mismatch: your wallet seed is deprecated. raise BaseException(upgrade_msg)
Please create a new wallet, and send your coins to the new wallet.
We apologize for the inconvenience. We try to keep this kind of upgrades as rare as possible.
See the release notes for more information.""",1)
return True return True
@ -612,7 +612,7 @@ See the release notes for more information.""",1)
try: try:
d.decode('hex') d.decode('hex')
except: except:
raise InvalidPassword() raise BaseException("Invalid password")
return d return d
else: else:
return s return s
@ -662,14 +662,13 @@ See the release notes for more information.""",1)
def mktx(self, to_address, amount, label, password, fee=None): def mktx(self, to_address, amount, label, password, fee=None):
if not self.is_valid(to_address): if not self.is_valid(to_address):
return False, "Invalid address" raise BaseException("Invalid address")
inputs, total, fee = wallet.choose_tx_inputs( amount, fee ) inputs, total, fee = wallet.choose_tx_inputs( amount, fee )
if not inputs: return False, "Not enough funds %d %d"%(total, fee) if not inputs:
try: raise BaseException("Not enough funds")
outputs = wallet.choose_tx_outputs( to_address, amount, fee, total ) outputs = wallet.choose_tx_outputs( to_address, amount, fee, total )
s_inputs = wallet.sign_inputs( inputs, outputs, password ) s_inputs = wallet.sign_inputs( inputs, outputs, password )
except InvalidPassword:
return False, "Wrong password"
tx = filter( raw_tx( s_inputs, outputs ) ) tx = filter( raw_tx( s_inputs, outputs ) )
if to_address not in self.addressbook: if to_address not in self.addressbook:
self.addressbook.append(to_address) self.addressbook.append(to_address)
@ -677,7 +676,7 @@ See the release notes for more information.""",1)
tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex') tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
wallet.labels[tx_hash] = label wallet.labels[tx_hash] = label
wallet.save() wallet.save()
return True, tx return tx
def sendtx(self, tx): def sendtx(self, tx):
tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex') tx_hash = Hash(tx.decode('hex') )[::-1].encode('hex')
@ -848,7 +847,6 @@ if __name__ == '__main__':
print addr, " ", wallet.labels.get(addr) print addr, " ", wallet.labels.get(addr)
elif cmd in [ 'addresses']: elif cmd in [ 'addresses']:
if options.show_keys: private_keys = ast.literal_eval( wallet.pw_decode( wallet.private_keys, password ) )
for addr in wallet.addresses: for addr in wallet.addresses:
if options.show_all or not wallet.is_change(addr): if options.show_all or not wallet.is_change(addr):
label = wallet.labels.get(addr) if not wallet.is_change(addr) else "[change]" label = wallet.labels.get(addr) if not wallet.is_change(addr) else "[change]"
@ -861,8 +859,7 @@ if __name__ == '__main__':
else: no += 1 else: no += 1
b = "%d %d %f"%(no, ni, wallet.get_addr_balance(addr)[0]*1e-8) b = "%d %d %f"%(no, ni, wallet.get_addr_balance(addr)[0]*1e-8)
else: b='' else: b=''
pk = private_keys[wallet.addresses.index(addr)] if options.show_keys else '' print addr, b, label
print addr, pk, b, label
if cmd == 'history': if cmd == 'history':
lines = wallet.get_tx_history() lines = wallet.get_tx_history()

View file

@ -84,13 +84,8 @@ def init_wallet(wallet):
try: try:
found = wallet.read() found = wallet.read()
except BaseException, e: except BaseException, e:
show_message(e.args[0]) show_message(e.message)
if e.args[1] == 0: exit(1) exit(1)
found = 1
except:
exit()
if not found: if not found:
# ask if the user wants to create a new wallet, or recover from a seed. # ask if the user wants to create a new wallet, or recover from a seed.
@ -662,11 +657,13 @@ class BitcoinGUI:
password = password_dialog() if self.wallet.use_encryption else None password = password_dialog() if self.wallet.use_encryption else None
status, tx = self.wallet.mktx( to_address, amount, label, password, fee ) try:
self.update_session = True # we created a new change address tx = self.wallet.mktx( to_address, amount, label, password, fee )
if not status: except BaseException, e:
self.show_message(tx) self.show_message(e.message)
return return
self.update_session = True # we created a new change address
status, msg = self.wallet.sendtx( tx ) status, msg = self.wallet.sendtx( tx )
if status: if status:

View file

@ -67,33 +67,8 @@ if __name__ == "__main__":
if type(x) == tuple: if type(x) == tuple:
seed_version, use_encryption, fee, host, port, blocks, seed, all_addresses, private_keys, change_indexes, status, history, labels, addressbook = x seed_version, use_encryption, fee, host, port, blocks, seed, all_addresses, private_keys, change_indexes, status, history, labels, addressbook = x
addresses = [] print """This wallet is deprecated.
change_addresses = [] Please create a new wallet, open the old wallet with Electrum 0.33, and send your coins to your new wallet"""
for i in range(len(all_addresses)):
if i in change_indexes:
change_addresses.append(all_addresses[i])
else:
addresses.append(all_addresses[i])
s = {
'seed_version':seed_version,
'use_encryption':use_encryption,
'master_public_key':None,
'fee':fee,
'host':host,
'port':port,
'blocks':blocks,
'seed':seed,
'addresses':addresses,
'change_addresses':change_addresses,
'status':status,
'history':history,
'labels':labels,
'contacts':addressbook
}
f = open(path,"w")
f.write( repr(s) )
f.close()
print "wallet format was upgraded."
exit(1) exit(1)
wallet = electrum.Wallet(path) wallet = electrum.Wallet(path)