move master_private_keys and master_public_keys to NewWallet
[electrum-nvc.git] / lib / wallet.py
index f8eba63..261f9f1 100644 (file)
@@ -43,11 +43,10 @@ DUST_THRESHOLD = 5430
 IMPORTED_ACCOUNT = '/x'
 
 
-
-class WalletStorage:
+class WalletStorage(object):
 
     def __init__(self, config):
-        self.lock = threading.Lock()
+        self.lock = threading.RLock()
         self.config = config
         self.data = {}
         self.file_exists = False
@@ -56,7 +55,6 @@ class WalletStorage:
         if self.path:
             self.read(self.path)
 
-
     def init_path(self, config):
         """Set the path of the wallet."""
 
@@ -84,7 +82,6 @@ class WalletStorage:
 
         return new_path
 
-
     def read(self, path):
         """Read the contents of the wallet file."""
         try:
@@ -100,12 +97,13 @@ class WalletStorage:
         self.data = d
         self.file_exists = True
 
-
     def get(self, key, default=None):
-        v = self.data.get(key)
-        if v is None:
-            v = default
-        return v
+
+        with self.lock:
+            v = self.data.get(key)
+            if v is None:
+                v = default
+            return v
 
     def put(self, key, value, save = True):
 
@@ -127,12 +125,11 @@ class WalletStorage:
             os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
 
 
-class Abstract_Wallet:
+class Abstract_Wallet(object):
     """
     Wallet classes are created to handle various address generation methods.
     Completion states (watching-only, single account, no seed, etc) are handled inside classes.
     """
-
     def __init__(self, storage):
         self.storage = storage
         self.electrum_version = ELECTRUM_VERSION
@@ -151,12 +148,8 @@ class Abstract_Wallet:
 
         self.fee                   = int(storage.get('fee_per_kb', 10000))
 
-        self.master_public_keys = storage.get('master_public_keys',{})
-        self.master_private_keys = storage.get('master_private_keys', {})
-
         self.next_addresses = storage.get('next_addresses',{})
 
-
         # This attribute is set when wallet.start_threads is called.
         self.synchronizer = None
 
@@ -179,8 +172,6 @@ class Abstract_Wallet:
                 print_error("removing unreferenced tx", h)
                 self.transactions.pop(h)
 
-
-
         # not saved
         self.prevout_values = {}     # my own transaction outputs
         self.spent_outputs = []
@@ -340,7 +331,6 @@ class Abstract_Wallet:
         return s[0] == 1
 
     def get_address_index(self, address):
-
         for account in self.accounts.keys():
             for for_change in [0,1]:
                 addresses = self.accounts[account].get_addresses(for_change)
@@ -364,26 +354,6 @@ class Abstract_Wallet:
         account_id, sequence = self.get_address_index(address)
         return self.accounts[account_id].get_pubkeys(sequence)
 
-    def can_sign(self, tx):
-
-        if self.is_watching_only():
-            return False
-
-        if tx.is_complete():
-            return False
-
-        addr_list, xpub_list = tx.inputs_to_sign()
-        for addr in addr_list:
-            if self.is_mine(addr):
-                return True
-
-        mpk = [ self.master_public_keys[k] for k in self.master_private_keys.keys() ]
-        for xpub, sequence in xpub_list:
-            if xpub in mpk:
-                return True
-
-        return False
-
     def add_keypairs(self, tx, keypairs, password):
         # first check the provided password. This will raise if invalid.
         self.check_password(password)
@@ -648,7 +618,7 @@ class Abstract_Wallet:
 
             # Insert the change output at a random position in the outputs
             posn = random.randint(0, len(outputs))
-            outputs[posn:posn] = [( change_addr,  change_amount)]
+            outputs[posn:posn] = [( 'address', change_addr,  change_amount)]
         return outputs
 
     def get_history(self, address):
@@ -1059,6 +1029,7 @@ class Imported_Wallet(Abstract_Wallet):
     def is_beyond_limit(self, address, account, is_change):
         return False
 
+
 class Deterministic_Wallet(Abstract_Wallet):
 
     def __init__(self, storage):
@@ -1252,7 +1223,7 @@ class Deterministic_Wallet(Abstract_Wallet):
             return False
         prev_addresses = prev_addresses[max(0, i - limit):]
         for addr in prev_addresses:
-            if self.address_is_old(addr):
+            if self.history.get(addr):
                 return False
         return True
 
@@ -1267,6 +1238,8 @@ class NewWallet(Deterministic_Wallet):
 
     def __init__(self, storage):
         Deterministic_Wallet.__init__(self, storage)
+        self.master_public_keys  = storage.get('master_public_keys', {})
+        self.master_private_keys = storage.get('master_private_keys', {})
 
     def default_account(self):
         return self.accounts["m/0'"]
@@ -1348,6 +1321,21 @@ class NewWallet(Deterministic_Wallet):
         self.add_master_public_key("m/", xpub)
         self.add_master_private_key("m/", xpriv, password)
 
+    def can_sign(self, tx):
+        if self.is_watching_only():
+            return False
+        if tx.is_complete():
+            return False
+        addr_list, xpub_list = tx.inputs_to_sign()
+        for addr in addr_list:
+            if self.is_mine(addr):
+                return True
+        mpk = [ self.master_public_keys[k] for k in self.master_private_keys.keys() ]
+        for xpub, sequence in xpub_list:
+            if xpub in mpk:
+                return True
+        return False
+
     def find_root_by_master_key(self, xpub):
         for key, xpub2 in self.master_public_keys.items():
             if key == "m/":continue
@@ -1549,6 +1537,19 @@ class OldWallet(Deterministic_Wallet):
     def check_pending_accounts(self):
         pass
 
+    def can_sign(self, tx):
+        if self.is_watching_only():
+            return False
+        if tx.is_complete():
+            return False
+        addr_list, xpub_list = tx.inputs_to_sign()
+        for addr in addr_list:
+            if self.is_mine(addr):
+                return True
+        for xpub, sequence in xpub_list:
+            if xpub == self.master_public_key:
+                return True
+        return False
 
 # former WalletFactory
 class Wallet(object):