detect if wallet can sign tx before showing sign button
[electrum-nvc.git] / lib / wallet.py
index e1adcd7..69226da 100644 (file)
@@ -392,49 +392,54 @@ class Abstract_Wallet:
         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
         seed = self.get_seed(password)
 
-        for txin in tx.inputs:
-            x_pubkeys = txin['x_pubkeys']
-            address = txin['address']
-
-            if self.is_mine(address):
+        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)
                 for sec in private_keys:
                     pubkey = public_key_from_private_key(sec)
                     keypairs[ pubkey ] = sec
 
+        for xpub, sequence in xpub_list:
+            # look for account that can sign
+            for k, account in self.accounts.items():
+                if xpub in account.get_master_pubkeys():
+                    break
             else:
+                continue
 
-                from account import BIP32_Account
-                print "scanning", x_pubkeys
-
-                for x_pubkey in x_pubkeys:
-                    if not is_extended_pubkey(x_pubkey):
-                        continue
-
-                    xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey)
-                    print "xpub", xpub
-
-                    # look for account that can sign
-                    for k, account in self.accounts.items():
-                        if xpub in account.get_master_pubkeys():
-                            break
-                    else:
-                        continue
-                    print "found xpub", xpub, sequence
-
-                    addr = account.get_address(*sequence)
-                    print addr, txin['address']
-                    assert txin['address'] == addr
-                    pk = self.get_private_key(addr, password)
-                    for sec in pk:
-                        pubkey = public_key_from_private_key(sec)
-                        keypairs[pubkey] = sec
-
+            addr = account.get_address(*sequence)
+            pk = self.get_private_key(addr, password)
+            for sec in pk:
+                pubkey = public_key_from_private_key(sec)
+                keypairs[pubkey] = sec
 
 
 
@@ -863,7 +868,9 @@ class Abstract_Wallet:
         account = self.accounts[account_id]
         redeemScript = account.redeem_script(sequence)
         txin['x_pubkeys'] = account.get_xpubkeys(sequence)
-        txin['pubkeys'] = account.get_pubkeys(sequence) 
+        txin['pubkeys'] = pubkeys = account.get_pubkeys(sequence)
+        txin['signatures'] = [None] * len(pubkeys)
+
         if redeemScript: 
             txin['redeemScript'] = redeemScript
             txin['num_sig'] = 2
@@ -1592,28 +1599,6 @@ class OldWallet(Deterministic_Wallet):
         s = self.get_seed(password)
         return ' '.join(mnemonic.mn_encode(s))
 
-
-    def add_keypairs_from_KeyID(self, tx, keypairs, password):
-        # first check the provided password
-        for txin in tx.inputs:
-            keyid = txin.get('KeyID')
-            if keyid:
-                m = re.match("old\(([0-9a-f]+),(\d+),(\d+)", keyid)
-                if not m: continue
-                mpk = m.group(1)
-                if mpk != self.storage.get('master_public_key'): continue 
-                for_change = int(m.group(2))
-                num = int(m.group(3))
-                account = self.accounts[0]
-                addr = account.get_address(for_change, num)
-                txin['address'] = addr # fixme: side effect
-                pk = account.get_private_key((for_change, num), self, password)
-                for sec in pk:
-                    pubkey = public_key_from_private_key(sec)
-                    keypairs[pubkey] = sec
-
-
-
     def check_pending_accounts(self):
         pass