optimized, cleaned up, commented
This commit is contained in:
@@ -329,10 +329,7 @@ class Abstract_Wallet(object):
|
||||
return changed
|
||||
|
||||
def addresses(self, include_change = True):
|
||||
o = []
|
||||
for a in self.accounts.keys():
|
||||
o += self.get_account_addresses(a, include_change)
|
||||
return o
|
||||
return list(addr for acc in self.accounts for addr in self.get_account_addresses(acc, include_change))
|
||||
|
||||
def is_mine(self, address):
|
||||
return address in self.addresses(True)
|
||||
@@ -344,12 +341,11 @@ class Abstract_Wallet(object):
|
||||
return s[0] == 1
|
||||
|
||||
def get_address_index(self, address):
|
||||
for account in self.accounts.keys():
|
||||
for acc_id in self.accounts:
|
||||
for for_change in [0,1]:
|
||||
addresses = self.accounts[account].get_addresses(for_change)
|
||||
for addr in addresses:
|
||||
if address == addr:
|
||||
return account, (for_change, addresses.index(addr))
|
||||
addresses = self.accounts[acc_id].get_addresses(for_change)
|
||||
if address in addresses:
|
||||
return acc_id, (for_change, addresses.index(address))
|
||||
raise Exception("Address not found", address)
|
||||
|
||||
def get_private_key(self, address, password):
|
||||
@@ -394,7 +390,8 @@ class Abstract_Wallet(object):
|
||||
self.storage.put('addressbook', self.addressbook, True)
|
||||
|
||||
def fill_addressbook(self):
|
||||
for tx_hash, tx in self.transactions.items():
|
||||
# todo: optimize this
|
||||
for tx_hash, tx in self.transactions.viewitems():
|
||||
is_relevant, is_send, _, _ = self.get_tx_value(tx)
|
||||
if is_send:
|
||||
for addr in tx.get_output_addresses():
|
||||
@@ -426,12 +423,13 @@ class Abstract_Wallet(object):
|
||||
self.spent_outputs.append(key)
|
||||
|
||||
def get_addr_balance(self, address):
|
||||
'returns the confirmed balance and pending (unconfirmed) balance change of this bitcoin address'
|
||||
#assert self.is_mine(address)
|
||||
h = self.history.get(address,[])
|
||||
if h == ['*']: return 0,0
|
||||
c = u = 0
|
||||
received_coins = [] # list of coins received at address
|
||||
|
||||
# go through all tx in history of this address and collect the coins arriving on this address
|
||||
for tx_hash, tx_height in h:
|
||||
tx = self.transactions.get(tx_hash)
|
||||
if not tx: continue
|
||||
@@ -440,12 +438,12 @@ class Abstract_Wallet(object):
|
||||
if addr == address:
|
||||
key = tx_hash + ':%d'%i
|
||||
received_coins.append(key)
|
||||
|
||||
# go through all tx in history of this address again
|
||||
for tx_hash, tx_height in h:
|
||||
tx = self.transactions.get(tx_hash)
|
||||
if not tx: continue
|
||||
v = 0
|
||||
|
||||
# substract the value of coins leaving from this address
|
||||
for item in tx.inputs:
|
||||
addr = item.get('address')
|
||||
if addr == address:
|
||||
@@ -453,16 +451,16 @@ class Abstract_Wallet(object):
|
||||
value = self.prevout_values.get( key )
|
||||
if key in received_coins:
|
||||
v -= value
|
||||
|
||||
# add the value of the coins arriving in this address
|
||||
for i, (addr, value) in enumerate(tx.get_outputs()):
|
||||
key = tx_hash + ':%d'%i
|
||||
if addr == address:
|
||||
v += value
|
||||
|
||||
if tx_height:
|
||||
c += v
|
||||
c += v # confirmed coins value
|
||||
else:
|
||||
u += v
|
||||
u += v # unconfirmed coins value
|
||||
return c, u
|
||||
|
||||
def get_account_name(self, k):
|
||||
@@ -474,14 +472,22 @@ class Abstract_Wallet(object):
|
||||
account_names[k] = self.get_account_name(k)
|
||||
return account_names
|
||||
|
||||
def get_account_addresses(self, a, include_change=True):
|
||||
if a is None:
|
||||
o = self.addresses(include_change)
|
||||
elif a in self.accounts:
|
||||
ac = self.accounts[a]
|
||||
o = ac.get_addresses(0)
|
||||
if include_change: o += ac.get_addresses(1)
|
||||
return o
|
||||
def get_account_addresses(self, acc_id, include_change=True):
|
||||
if acc_id is None:
|
||||
addr_list = self.addresses(include_change)
|
||||
elif acc_id in self.accounts:
|
||||
acc = self.accounts[acc_id]
|
||||
addr_list = acc.get_addresses(0)
|
||||
if include_change:
|
||||
addr_list += acc.get_addresses(1)
|
||||
return addr_list
|
||||
|
||||
def get_account_from_address(self, addr):
|
||||
"Returns the account that contains this address, or None"
|
||||
for acc_id in self.accounts: # similar to get_address_index but simpler
|
||||
if addr in self.get_account_addresses(acc_id):
|
||||
return self.accounts[acc_id]
|
||||
return None
|
||||
|
||||
def get_account_balance(self, account):
|
||||
return self.get_balance(self.get_account_addresses(account))
|
||||
@@ -524,7 +530,7 @@ class Abstract_Wallet(object):
|
||||
if coins[-1][0] != 0:
|
||||
while coins[0][0] == 0:
|
||||
coins = coins[1:] + [ coins[0] ]
|
||||
return [x[1] for x in coins]
|
||||
return [value for height, value in coins]
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user