save account name as label
[electrum-nvc.git] / lib / wallet.py
index 685a929..4c52f7a 100644 (file)
@@ -209,7 +209,7 @@ class Wallet:
 
     def account_id(self, account_type, i):
         if account_type is None:
-            return "m/0'/%d'"%i
+            return "m/0'/%d"%i
         elif account_type == '2of2':
             return "m/1'/%d & m/2'/%d"%(i,i)
         elif account_type == '2of3':
@@ -230,19 +230,19 @@ class Wallet:
 
     def create_account(self, name, account_type = None):
         i = self.num_accounts(account_type)
-        acount_id = self.account_id(account_type,i)
+        account_id = self.account_id(account_type,i)
 
         if account_type is None:
             master_c0, master_K0, _ = self.master_public_keys["m/0'/"]
             c0, K0, cK0 = bip32_public_derivation(master_c0.decode('hex'), master_K0.decode('hex'), "m/0'/", "m/0'/%d"%i)
-            account = BIP32_Account({ 'name':name, 'c':c0, 'K':K0, 'cK':cK0 })
+            account = BIP32_Account({ 'c':c0, 'K':K0, 'cK':cK0 })
 
         elif account_type == '2of2':
             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)
-            account = BIP32_Account_2of2({ 'name':name, 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
+            account = BIP32_Account_2of2({ 'c':c1, 'K':K1, 'cK':cK1, 'c2':c2, 'K2':K2, 'cK2':cK2 })
 
         elif account_type == '2of3':
             master_c3, master_K3, _ = self.master_public_keys["m/3'/"]
@@ -251,10 +251,12 @@ class Wallet:
             c4, K4, cK4 = bip32_public_derivation(master_c4.decode('hex'), master_K4.decode('hex'), "m/4'/", "m/4'/%d"%i)
             master_c5, master_K5, _ = self.master_public_keys["m/5'/"]
             c5, K5, cK5 = bip32_public_derivation(master_c5.decode('hex'), master_K5.decode('hex'), "m/5'/", "m/5'/%d"%i)
-            account = BIP32_Account_2of3({ 'name':name, 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
+            account = BIP32_Account_2of3({ 'c':c3, 'K':K3, 'cK':cK3, 'c2':c4, 'K2':K4, 'cK2':cK4, 'c3':c5, 'K3':K5, 'cK3':cK5 })
 
         self.accounts[account_id] = account
         self.save_accounts()
+        self.labels[account_id] = name
+        self.config.set_key('labels', self.labels, True)
 
 
     def save_accounts(self):
@@ -353,28 +355,19 @@ class Wallet:
         return out
 
 
-    def get_private_keys(self, addresses, password):
-        if not self.seed: return {}
-        # decode seed in any case, in order to test the password
-        seed = self.decode_seed(password)
-        out = {}
-        for address in addresses:
-            pk = self.get_private_key(address, password)
-            if pk: out[address] = pk
-
-        return out
 
 
     def signrawtransaction(self, tx, input_info, private_keys, password):
+        import deserialize
         unspent_coins = self.get_unspent_coins()
         seed = self.decode_seed(password)
 
-        # convert private_keys to dict 
-        pk = {}
+        # build a list of public/private keys
+        keypairs = {}
         for sec in private_keys:
-            address = address_from_private_key(sec)
-            pk[address] = sec
-        private_keys = pk
+            pubkey = public_key_from_private_key(sec)
+            keypairs[ pubkey ] = sec
+
 
         for txin in tx.inputs:
             # convert to own format
@@ -396,27 +389,30 @@ class Wallet:
                     # if neither, we might want to get it from the server..
                     raise
 
-            # find the address:
+            # find the address and fill private_keys
             if txin.get('KeyID'):
                 account, name, sequence = txin.get('KeyID')
-                if name != 'Electrum': continue
+                if name != 'BIP32': continue
                 sec = self.accounts[account].get_private_key(sequence, seed)
-                addr = self.accounts[account].get_address(sequence)
+                pubkey = self.accounts[account].get_pubkey(sequence)
                 txin['address'] = addr
-                private_keys[addr] = sec
+                keypairs[pubkey] = [sec]
 
-            elif txin.get("redeemScript"):
-                txin['address'] = hash_160_to_bc_address(hash_160(txin.get("redeemScript").decode('hex')), 5)
+            redeem_script = txin.get("redeemScript")
+            if redeem_script:
+                num, redeem_pubkeys = deserialize.parse_redeemScript(redeem_script)
+                addr = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5)
+                txin['address'] = addr
 
             elif txin.get("raw_output_script"):
-                import deserialize
                 addr = deserialize.get_address_from_output_script(txin.get("raw_output_script").decode('hex'))
                 sec = self.get_private_key(addr, password)
+                pubkey = public_key_from_private_key(sec)
                 if sec: 
-                    private_keys[addr] = sec
+                    keypairs[pubkey] = [sec]
                     txin['address'] = addr
 
-        tx.sign( private_keys )
+        tx.sign( keypairs )
 
     def sign_message(self, address, message, password):
         sec = self.get_private_key(address, password)
@@ -540,7 +536,7 @@ class Wallet:
         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, True)
 
     def delete_contact(self, addr):
         if addr in self.addressbook:
@@ -633,7 +629,7 @@ class Wallet:
     def get_accounts(self):
         accounts = {}
         for k, account in self.accounts.items():
-            accounts[k] = account.name
+            accounts[k] = self.labels.get(k, 'unnamed')
         if self.imported_keys:
             accounts[-1] = 'Imported keys'
         return accounts
@@ -921,9 +917,9 @@ class Wallet:
 
         tx = Transaction.from_io(inputs, outputs)
 
-        private_keys = {}
-        for i in range(len(tx.inputs)):
-            txin = tx.inputs[i]
+
+        keypairs = {}
+        for i, txin in enumerate(tx.inputs):
             address = txin['address']
             if address in self.imported_keys.keys():
                 pk_addresses.append(address)
@@ -935,11 +931,15 @@ class Wallet:
             if redeemScript: 
                 txin['redeemScript'] = redeemScript
                 assert address == self.accounts[account].get_address(*sequence)
+            else:
+                txin['redeemPubkey'] = self.accounts[account].get_pubkey(*sequence)
 
-            private_keys[address] = self.get_private_key(address, password)
+            private_keys = self.get_private_key(address, password)
+            for sec in private_keys:
+                pubkey = public_key_from_private_key(sec)
+                keypairs[ pubkey ] = sec
 
-        print_error( "private keys", private_keys )
-        tx.sign(private_keys)
+        tx.sign(keypairs)
 
         for address, x in outputs:
             if address not in self.addressbook and not self.is_mine(address):