commit 18d16ba82c8c0a543b71d88c654a3a53d8956281
parent 5ee4a87f29bfa1d5e9a24affd6fcacdb2dc95ce5
Author: ThomasV <thomasv@gitorious>
Date: Mon, 23 Feb 2015 19:33:31 +0100
bugfix: add pubkey to PendingAccount, to be able to spend from it
Diffstat:
2 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/lib/account.py b/lib/account.py
@@ -96,7 +96,9 @@ class Account(object):
class PendingAccount(Account):
def __init__(self, v):
- self.pending_address = v['pending']
+ self.pending_address = v['address']
+ self.change_pubkeys = []
+ self.receiving_pubkeys = [ v['pubkey'] ]
def synchronize(self, wallet):
return
@@ -108,7 +110,7 @@ class PendingAccount(Account):
return False
def dump(self):
- return {'pending':self.pending_address }
+ return {'pending':True, 'address':self.pending_address, 'pubkey':self.receiving_pubkeys[0] }
def get_name(self, k):
return _('Pending account')
@@ -119,6 +121,9 @@ class PendingAccount(Account):
def get_type(self):
return _('pending')
+ def get_xpubkeys(self, for_change, n):
+ return self.get_pubkeys(for_change, n)
+
class ImportedAccount(Account):
def __init__(self, d):
self.keypairs = d['imported']
@@ -285,8 +290,8 @@ class BIP32_Account(Account):
def first_address(self):
pubkeys = self.derive_pubkeys(0, 0)
- address = self.pubkeys_to_address(pubkeys)
- return address
+ addr = self.pubkeys_to_address(pubkeys)
+ return addr, pubkeys
def get_master_pubkeys(self):
return [self.xpub]
diff --git a/lib/wallet.py b/lib/wallet.py
@@ -253,7 +253,10 @@ class Abstract_Wallet(object):
elif v.get('xpub'):
self.accounts[k] = BIP32_Account(v)
elif v.get('pending'):
- self.accounts[k] = PendingAccount(v)
+ try:
+ self.accounts[k] = PendingAccount(v)
+ except:
+ pass
else:
print_error("cannot load account", v)
@@ -717,7 +720,7 @@ class Abstract_Wallet(object):
# send change to one of the accounts involved in the tx
address = inputs[0].get('address')
account, _ = self.get_address_index(address)
- if not self.use_change or account == IMPORTED_ACCOUNT:
+ if not self.use_change or not self.accounts[account].has_change():
change_addr = address
else:
change_addr = self.accounts[account].get_addresses(1)[-self.gap_limit_for_change]
@@ -1363,7 +1366,7 @@ class BIP32_Simple_Wallet(BIP32_Wallet):
class BIP32_HD_Wallet(BIP32_Wallet):
# wallet that can create accounts
def __init__(self, storage):
- self.next_account = storage.get('next_account', None)
+ self.next_account = storage.get('next_account2', None)
BIP32_Wallet.__init__(self, storage)
def can_create_accounts(self):
@@ -1372,14 +1375,14 @@ class BIP32_HD_Wallet(BIP32_Wallet):
def addresses(self, b=True):
l = BIP32_Wallet.addresses(self, b)
if self.next_account:
- next_address = self.next_account[2]
+ _, _, _, next_address = self.next_account
if next_address not in l:
l.append(next_address)
return l
def get_address_index(self, address):
if self.next_account:
- next_id, next_xpub, next_address = self.next_account
+ next_id, next_xpub, next_pubkey, next_address = self.next_account
if address == next_address:
return next_id, (0,0)
return BIP32_Wallet.get_address_index(self, address)
@@ -1406,9 +1409,9 @@ class BIP32_HD_Wallet(BIP32_Wallet):
if xprv:
self.add_master_private_key(derivation, xprv, password)
account = BIP32_Account({'xpub':xpub})
- addr = account.first_address()
+ addr, pubkey = account.first_address()
self.add_address(addr)
- return account_id, xpub, addr
+ return account_id, xpub, pubkey, addr
def create_main_account(self, password):
# First check the password is valid (this raises if it isn't).
@@ -1417,13 +1420,13 @@ class BIP32_HD_Wallet(BIP32_Wallet):
self.create_account('Main account', password)
def create_account(self, name, password):
- account_id, xpub, addr = self.get_next_account(password)
+ account_id, xpub, _, _ = self.get_next_account(password)
account = BIP32_Account({'xpub':xpub})
self.add_account(account_id, account)
self.set_label(account_id, name)
# add address of the next account
self.next_account = self.get_next_account(password)
- self.storage.put('next_account', self.next_account)
+ self.storage.put('next_account2', self.next_account)
def account_is_pending(self, k):
return type(self.accounts.get(k)) == PendingAccount
@@ -1436,11 +1439,11 @@ class BIP32_HD_Wallet(BIP32_Wallet):
def create_pending_account(self, name, password):
if self.next_account is None:
self.next_account = self.get_next_account(password)
- self.storage.put('next_account', self.next_account)
- next_id, next_xpub, next_address = self.next_account
+ self.storage.put('next_account2', self.next_account)
+ next_id, next_xpub, next_pubkey, next_address = self.next_account
if name:
self.set_label(next_id, name)
- self.accounts[next_id] = PendingAccount({'pending':next_address})
+ self.accounts[next_id] = PendingAccount({'pending':True, 'address':next_address, 'pubkey':next_pubkey})
self.save_accounts()
def synchronize(self):
@@ -1449,21 +1452,21 @@ class BIP32_HD_Wallet(BIP32_Wallet):
if self.next_account is None and not self.use_encryption:
self.next_account = self.get_next_account(None)
- self.storage.put('next_account', self.next_account)
+ self.storage.put('next_account2', self.next_account)
# check pending account
if self.next_account is not None:
- next_id, next_xpub, next_address = self.next_account
+ next_id, next_xpub, next_pubkey, next_address = self.next_account
if self.address_is_old(next_address):
print_error("creating account", next_id)
self.add_account(next_id, BIP32_Account({'xpub':next_xpub}))
# here the user should get a notification
self.next_account = None
- self.storage.put('next_account', self.next_account)
+ self.storage.put('next_account2', self.next_account)
elif self.history.get(next_address, []):
if next_id not in self.accounts:
print_error("create pending account", next_id)
- self.accounts[next_id] = PendingAccount({'pending':next_address})
+ self.accounts[next_id] = PendingAccount({'pending':True, 'address':next_address, 'pubkey':next_pubkey})
self.save_accounts()