Moved bitcoin.py tests to their own file
[electrum-nvc.git] / lib / wallet.py
index 58258d7..eb9a039 100644 (file)
@@ -123,7 +123,7 @@ class WalletStorage:
         f.write( s )
         f.close()
         if 'ANDROID_DATA' not in os.environ:
-            import stat  #XXX: IS this really android only?
+            import stat
             os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
 
 
@@ -383,11 +383,12 @@ class Abstract_Wallet:
 
     def add_keypairs(self, tx, keypairs, password):
         # first check the provided password. This will raise if invalid.
-        self.get_seed(password)
+        self.check_password(password)
+
         addr_list, xpub_list = tx.inputs_to_sign()
         for addr in addr_list:
             if self.is_mine(addr):
-                private_keys = self.get_private_key(address, password)
+                private_keys = self.get_private_key(addr, password)
                 for sec in private_keys:
                     pubkey = public_key_from_private_key(sec)
                     keypairs[ pubkey ] = sec
@@ -1005,6 +1006,20 @@ class Abstract_Wallet:
         c, u = self.get_addr_balance(address)
         return len(h), len(h) > 0 and c == -u
 
+    def address_is_old(self, address, age_limit=2):
+        age = -1
+        h = self.history.get(address, [])
+        if h == ['*']:
+            return True
+        for tx_hash, tx_height in h:
+            if tx_height == 0:
+                tx_age = 0
+            else:
+                tx_age = self.network.get_local_height() - tx_height + 1
+            if tx_age > age:
+                age = tx_age
+        return age > age_limit
+
 
 class Imported_Wallet(Abstract_Wallet):
 
@@ -1033,6 +1048,11 @@ class Imported_Wallet(Abstract_Wallet):
         h = self.history.get(address,[])
         return len(h), False
 
+    def get_master_public_keys(self):
+        return {}
+
+    def is_beyond_limit(self, address, account, is_change):
+        return False
 
 class Deterministic_Wallet(Abstract_Wallet):
 
@@ -1115,20 +1135,6 @@ class Deterministic_Wallet(Abstract_Wallet):
                     if n > nmax: nmax = n
         return nmax + 1
 
-    def address_is_old(self, address):
-        age = -1
-        h = self.history.get(address, [])
-        if h == ['*']:
-            return True
-        for tx_hash, tx_height in h:
-            if tx_height == 0:
-                tx_age = 0
-            else:
-                tx_age = self.network.get_local_height() - tx_height + 1
-            if tx_age > age:
-                age = tx_age
-        return age > 2
-
     def synchronize_sequence(self, account, for_change):
         limit = self.gap_limit_for_change if for_change else self.gap_limit
         new_addresses = []
@@ -1235,6 +1241,22 @@ class Deterministic_Wallet(Abstract_Wallet):
         self.accounts[account_id] = PendingAccount({'pending':addr})
         self.save_accounts()
 
+    def is_beyond_limit(self, address, account, is_change):
+        if type(account) == ImportedAccount:
+            return False
+        addr_list = account.get_addresses(is_change)
+        i = addr_list.index(address)
+        prev_addresses = addr_list[:max(0, i)]
+        limit = self.gap_limit_for_change if is_change else self.gap_limit
+        if len(prev_addresses) < limit:
+            return False
+        prev_addresses = prev_addresses[max(0, i - limit):]
+        for addr in prev_addresses:
+            num, is_used = self.is_used(addr)
+            if num > 0:
+                return False
+        return True
+
 
 class NewWallet(Deterministic_Wallet):
 
@@ -1274,7 +1296,7 @@ class NewWallet(Deterministic_Wallet):
 
     def create_accounts(self, password):
         # First check the password is valid (this raises if it isn't).
-        pw_decode(self.seed, password)
+        self.check_password(password)
         self.create_account('Main account', password)
 
     def add_master_public_key(self, name, mpk):