from util import print_msg, print_error, user_dir, format_satoshis
from bitcoin import *
-
+from account import *
# AES encryption
EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s))
self.seed_version = config.get('seed_version', SEED_VERSION)
self.gap_limit = config.get('gap_limit', 5)
self.use_change = config.get('use_change',True)
- self.fee = int(config.get('fee_per_kb',20000))
+ self.fee = int(config.get('fee_per_kb',50000))
self.num_zeros = int(config.get('num_zeros',0))
self.use_encryption = config.get('use_encryption', False)
self.seed = config.get('seed', '') # encrypted
self.frozen_addresses = config.get('frozen_addresses',[])
self.prioritized_addresses = config.get('prioritized_addresses',[])
self.addressbook = config.get('contacts', [])
+
self.imported_keys = config.get('imported_keys',{})
self.history = config.get('addr_history',{}) # address -> list(txid, height)
- self.accounts = config.get('accounts', {}) # this should not include public keys
- self.SequenceClass = ElectrumSequence
- self.sequences = {}
- self.sequences[0] = self.SequenceClass(self.config.get('master_public_key'))
- if self.accounts.get(0) is None:
- self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
+ self.master_public_keys = config.get('master_public_keys',{})
+ self.master_private_keys = config.get('master_private_keys', {})
+
+ self.load_accounts(config)
self.transactions = {}
tx = config.get('transactions',{})
if self.seed_version != SEED_VERSION:
raise ValueError("This wallet seed is deprecated. Please run upgrade.py for a diagnostic.")
- for tx_hash in self.transactions.keys():
- self.update_tx_outputs(tx_hash)
+ for tx_hash, tx in self.transactions.items():
+ if self.check_new_tx(tx_hash, tx):
+ self.update_tx_outputs(tx_hash)
+ else:
+ print_error("unreferenced tx", tx_hash)
+ self.transactions.pop(tx_hash)
def set_up_to_date(self,b):
def import_key(self, sec, password):
# check password
seed = self.decode_seed(password)
- address = address_from_private_key(sec)
+ try:
+ address = address_from_private_key(sec)
+ except:
+ raise BaseException('Invalid private key')
if self.is_mine(address):
raise BaseException('Address already in wallet')
seed = random_seed(128)
self.seed = seed
+
def save_seed(self):
self.config.set_key('seed', self.seed, True)
self.config.set_key('seed_version', self.seed_version, True)
- mpk = self.SequenceClass.mpk_from_seed(self.seed)
- self.init_sequence(mpk)
+
+ master_k, master_c, master_K, master_cK = bip32_init(self.seed)
+
+ k0, c0, K0, cK0 = bip32_private_derivation(master_k, master_c, "m/", "m/0'/")
+ k1, c1, K1, cK1 = bip32_private_derivation(master_k, master_c, "m/", "m/1'/")
+ k2, c2, K2, cK2 = bip32_private_derivation(master_k, master_c, "m/", "m/2'/")
+
+ self.master_public_keys = {
+ "m/0'/": (c0, K0, cK0),
+ "m/1'/": (c1, K1, cK1),
+ "m/2'/": (c2, K2, cK2)
+ }
+
+ self.master_private_keys = {
+ "m/0'/": k0,
+ "m/1'/": k1
+ }
+ # send k2 to service
+
+ self.config.set_key('master_public_keys', self.master_public_keys, True)
+ self.config.set_key('master_private_keys', self.master_private_keys, True)
+
+ # create default account
+ self.create_new_account('Main account')
+
+
+ def create_new_account(self, name):
+ keys = self.accounts.keys()
+ i = 0
+
+ while True:
+ derivation = "m/0'/%d'"%i
+ if derivation not in keys: break
+ i += 1
+
+ start = "m/0'/"
+ master_c, master_K, master_cK = self.master_public_keys[start]
+ master_k = self.master_private_keys[start] # needs decryption
+ k, c, K, cK = bip32_private_derivation(master_k, master_c, start, derivation) # this is a type 1 derivation
+
+ self.accounts[derivation] = BIP32_Account({ 'name':name, 'c':c, 'K':K, 'cK':cK })
+ self.save_accounts()
+
+ def create_p2sh_account(self, name):
+ keys = self.accounts.keys()
+ i = 0
+ while True:
+ account_id = "m/1'/%d & m/2'/%d"%(i,i)
+ if account_id not in keys: break
+ i += 1
+
+ master_c1, master_K1, _ = self.master_public_keys["m/1'/"]
+ c1, K1, cK1 = bip32_public_derivation(master_c1.decode('hex'), master_K1.decode('hex'), "m/1'/", "m/1'/%d"%i)
+
+ master_c2, master_K2, _ = self.master_public_keys["m/2'/"]
+ c2, K2, cK2 = bip32_public_derivation(master_c2.decode('hex'), master_K2.decode('hex'), "m/2'/", "m/2'/%d"%i)
+
+ self.accounts[account_id] = BIP32_Account_2of2({ 'name':name, 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
+ self.save_accounts()
+
+
+ def save_accounts(self):
+ d = {}
+ for k, v in self.accounts.items():
+ d[k] = v.dump()
+ self.config.set_key('accounts', d, True)
+
+
+ def load_accounts(self, config):
+ d = config.get('accounts', {})
+ self.accounts = {}
+ for k, v in d.items():
+ if '&' in k:
+ self.accounts[k] = BIP32_Account_2of2(v)
+ else:
+ self.accounts[k] = BIP32_Account(v)
- def init_sequence(self, mpk):
- self.config.set_key('master_public_key', mpk, True)
- self.sequences[0] = self.SequenceClass(mpk)
- self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
- self.config.set_key('accounts', self.accounts, True)
def addresses(self, include_change = True):
return s[0] == 1
def get_master_public_key(self):
+ raise
return self.config.get("master_public_key")
def get_address_index(self, address):
return -1, None
for account in self.accounts.keys():
for for_change in [0,1]:
- addresses = self.accounts[account][for_change]
+ addresses = self.accounts[account].get_addresses(for_change)
for addr in addresses:
if address == addr:
return account, (for_change, addresses.index(addr))
def get_public_key(self, address):
account, sequence = self.get_address_index(address)
- return self.sequences[account].get_pubkey( sequence )
+ return self.accounts[account].get_pubkey( sequence )
def decode_seed(self, password):
seed = pw_decode(self.seed, password)
- self.sequences[0].check_seed(seed)
+ #todo: #self.sequences[0].check_seed(seed)
return seed
def get_private_key(self, address, password):
# decode seed in any case, in order to test the password
seed = self.decode_seed(password)
out = {}
- l_sequences = []
- l_addresses = []
for address in addresses:
if address in self.imported_keys.keys():
out[address] = pw_decode( self.imported_keys[address], password )
else:
account, sequence = self.get_address_index(address)
- if account == 0:
- l_sequences.append(sequence)
- l_addresses.append(address)
+ print "found index", address, account, sequence
+ if account == "m/0'/0'":
+ # FIXME: this is ugly
+ master_k = self.master_private_keys["m/0'/"]
+ master_c, _, _ = self.master_public_keys["m/0'/"]
+ master_k, master_c = CKD(master_k, master_c, 0 + BIP32_PRIME)
+ pk = self.accounts["m/0'/0'"].get_private_key(sequence, master_k)
+ out[address] = pk
+
+ elif account == "m/1'/0 & m/2'/0":
+ master_k = self.master_private_keys["m/1'/"]
+ master_c, master_K, _ = self.master_public_keys["m/1'/"]
+ master_k, master_c = CKD(master_k.decode('hex'), master_c.decode('hex'), 0)
+ pk = self.accounts[account].get_private_key(sequence, master_k)
+ out[address] = pk
- pk = self.sequences[0].get_private_keys(l_sequences, seed)
- for i, address in enumerate(l_addresses): out[address] = pk[i]
return out
if txin.get('KeyID'):
account, name, sequence = txin.get('KeyID')
if name != 'Electrum': continue
- sec = self.sequences[account].get_private_key(sequence, seed)
- addr = self.sequences[account].get_address(sequence)
+ sec = self.accounts[account].get_private_key(sequence, seed)
+ addr = self.accounts[account].get_address(sequence)
txin['address'] = addr
private_keys[addr] = sec
print_error("Verification error: {0}".format(e))
return False
- def create_new_address(self, account, for_change):
- addresses = self.accounts[account][for_change]
- n = len(addresses)
- address = self.get_new_address( account, for_change, n)
- self.accounts[account][for_change].append(address)
- self.history[address] = []
- print_msg(address)
- return address
-
-
- def get_new_address(self, account, for_change, n):
- return self.sequences[account].get_address((for_change, n))
- print address
- return address
def change_gap_limit(self, value):
if value >= self.gap_limit:
self.accounts[key][0] = addresses
self.gap_limit = value
- self.save()
+ self.config.set_key('gap_limit', self.gap_limit, True)
+ self.save_accounts()
return True
else:
return False
nmax = 0
for account in self.accounts.values():
- addresses = account[0]
+ addresses = account.get_addresses(0)
k = self.num_unused_trailing_addresses(addresses)
for a in addresses[0:-k]:
if self.history.get(a):
def synchronize_sequence(self, account, for_change):
limit = self.gap_limit_for_change if for_change else self.gap_limit
- addresses = self.accounts[account][for_change]
new_addresses = []
while True:
+ addresses = account.get_addresses(for_change)
if len(addresses) < limit:
- new_addresses.append( self.create_new_address(account, for_change) )
+ address = account.create_new_address(for_change)
+ self.history[address] = []
+ new_addresses.append( address )
continue
+
if map( lambda a: self.address_is_old(a), addresses[-limit:] ) == limit*[False]:
break
else:
- new_addresses.append( self.create_new_address(account, for_change) )
+ address = account.create_new_address(for_change)
+ self.history[address] = []
+ new_addresses.append( address )
+
return new_addresses
def synchronize(self):
new = []
- for account in self.accounts.keys():
+ for account in self.accounts.values():
new += self.synchronize_account(account)
+ if new:
+ self.save_accounts()
+ self.config.set_key('addr_history', self.history, True)
return new
def add_contact(self, address, label=None):
self.addressbook.append(address)
- self.config.set_key('addressbook', self.addressbook, True)
+ self.config.set_key('contacts', self.addressbook, True)
if label:
self.labels[address] = label
- self.config.set_key('labels',self.labels)
+ self.config.set_key('labels', self.labels)
+
+ def delete_contact(self, addr):
+ if addr in self.addressbook:
+ self.addressbook.remove(addr)
+ self.config.set_key('addressbook', self.addressbook, True)
def fill_addressbook(self):
def get_accounts(self):
accounts = {}
for k, account in self.accounts.items():
- accounts[k] = account.get('name')
+ accounts[k] = account.name
if self.imported_keys:
accounts[-1] = 'Imported keys'
return accounts
o = self.imported_keys.keys()
else:
ac = self.accounts[a]
- o = ac[0][:]
- if include_change: o += ac[1]
+ o = ac.get_addresses(0)
+ if include_change: o += ac.get_addresses(1)
return o
def get_imported_balance(self):
def receive_tx_callback(self, tx_hash, tx, tx_height):
-
if not self.check_new_tx(tx_hash, tx):
# may happen due to pruning
print_error("received transaction that is no longer referenced in history", tx_hash)
with self.transaction_lock:
self.transactions[tx_hash] = tx
+
+ self.interface.pending_transactions_for_notifications.append(tx)
+
+ self.save_transactions()
if self.verifier and tx_height>0:
self.verifier.add(tx_hash, tx_height)
self.update_tx_outputs(tx_hash)
- self.save()
+ def save_transactions(self):
+ tx = {}
+ for k,v in self.transactions.items():
+ tx[k] = str(v)
+ self.config.set_key('transactions', tx, True)
def receive_history_callback(self, addr, hist):
with self.lock:
self.history[addr] = hist
- self.save()
+ self.config.set_key('addr_history', self.history, True)
if hist != ['*']:
for tx_hash, tx_height in hist:
pk_addresses.append(address)
continue
account, sequence = self.get_address_index(address)
- txin['KeyID'] = (account, 'Electrum', sequence) # used by the server to find the key
- pk_addr, redeemScript = self.sequences[account].get_input_info(sequence)
+
+ txin['KeyID'] = (account, 'BIP32', sequence) # used by the server to find the key
+
+ _, redeemScript = self.accounts[account].get_input_info(sequence)
+
if redeemScript: txin['redeemScript'] = redeemScript
- pk_addresses.append(pk_addr)
+ pk_addresses.append(address)
+
+ print "pk_addresses", pk_addresses
# get all private keys at once.
if self.seed:
private_keys = self.get_private_keys(pk_addresses, password)
+ print "private keys", private_keys
tx.sign(private_keys)
for address, x in outputs:
def save(self):
+ print_error("Warning: wallet.save() is deprecated")
tx = {}
for k,v in self.transactions.items():
tx[k] = str(v)
s = {
'use_change': self.use_change,
'fee_per_kb': self.fee,
- 'accounts': self.accounts,
'addr_history': self.history,
'labels': self.labels,
'contacts': self.addressbook,
threading.Thread.__init__(self)
self.daemon = True
self.wallet = wallet
+ wallet.synchronizer = self
self.interface = self.wallet.interface
self.interface.register_channel('synchronizer')
self.wallet.interface.register_callback('connected', lambda: self.wallet.set_up_to_date(False))
if self.was_updated and not requested_tx:
self.interface.trigger_callback('updated')
- self.was_updated = False
-
+ self.interface.trigger_callback("new_transaction") # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
+
+ self.was_updated = False