fix for non-p2sh addresses
[electrum-nvc.git] / lib / account.py
index b734d14..6d8defc 100644 (file)
@@ -1,18 +1,21 @@
-"""
-todolist:
- * passwords, private keys storage
- * multisig service
- * compatibility with old addresses for restore
- * gui
-        an account may use one or several MPKs.
-        due to the type 1 derivations, we need to pass the mpk to this function
-        None : all accounts
-        -1 : imported
-        0,1... : seeded sequences
-
-        each account has a public and private master key
-"""
+#!/usr/bin/env python
+#
+# Electrum - lightweight Bitcoin client
+# Copyright (C) 2013 thomasv@gitorious
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
 
 from bitcoin import *
 
@@ -35,12 +38,12 @@ class Account(object):
     def create_new_address(self, for_change):
         addresses = self.change if for_change else self.addresses
         n = len(addresses)
-        address = self.get_new_address( for_change, n)
+        address = self.get_address( for_change, n)
         addresses.append(address)
         print address
         return address
 
-    def get_new_address(self, for_change, n):
+    def get_address(self, for_change, n):
         pass
         
 
@@ -156,9 +159,9 @@ class BIP32_Account(Account):
         d['cK'] = self.cK.encode('hex')
         return d
 
-    def get_new_address(self, for_change, n):
+    def get_address(self, for_change, n):
         pubkey = self.get_pubkey(for_change, n)
-        address = public_key_to_bc_address( pubkey )
+        address = public_key_to_bc_address( pubkey.decode('hex') )
         return address
 
     def get_pubkey(self, for_change, n):
@@ -166,32 +169,11 @@ class BIP32_Account(Account):
         chain = self.c
         for i in [for_change, n]:
             K, K_compressed, chain = CKD_prime(K, chain, i)
-        return K_compressed
+        return K_compressed.encode('hex')
 
-    def get_address(self, sequence):
-        for_change, n = sequence
-        pubkey = self.get_pubkey(for_change, n)
-        address = public_key_to_bc_address( pubkey )
-        return address
+    def redeem_script(self, sequence):
+        return None
 
-    def get_private_key(self, sequence, master_k):
-        chain = self.c
-        k = master_k
-        for i in sequence:
-            k, chain = CKD(k, chain, i)
-        return SecretToASecret(k, True)
-
-    def get_private_keys(self, sequence_list, seed):
-        return [ self.get_private_key( sequence, seed) for sequence in sequence_list]
-
-    def check_seed(self, seed):
-        master_secret, master_chain, master_public_key, master_public_key_compressed = bip32_init(seed)
-        assert self.mpk == (master_public_key.encode('hex'), master_chain.encode('hex'))
-
-    def get_input_info(self, sequence):
-        pk_addr = self.get_address(sequence)
-        redeemScript = None
-        return pk_addr, redeemScript
 
 
 
@@ -215,20 +197,46 @@ class BIP32_Account_2of2(BIP32_Account):
         chain = self.c2
         for i in [for_change, n]:
             K, K_compressed, chain = CKD_prime(K, chain, i)
-        return K_compressed
+        return K_compressed.encode('hex')
+
+    def redeem_script(self, sequence):
+        chain, i = sequence
+        pubkey1 = self.get_pubkey(chain, i)
+        pubkey2 = self.get_pubkey2(chain, i)
+        return Transaction.multisig_script([pubkey1, pubkey2], 2)
 
-    def get_new_address(self, for_change, n):
-        pubkey1 = self.get_pubkey(for_change, n)
-        pubkey2 = self.get_pubkey2(for_change, n)
-        address = Transaction.multisig_script([pubkey1.encode('hex'), pubkey2.encode('hex')], 2)["address"]
+    def get_address(self, for_change, n):
+        address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
         return address
 
-    def get_input_info(self, sequence):
+
+class BIP32_Account_2of3(BIP32_Account_2of2):
+
+    def __init__(self, v):
+        BIP32_Account_2of2.__init__(self, v)
+        self.c3 = v['c3'].decode('hex')
+        self.K3 = v['K3'].decode('hex')
+        self.cK3 = v['cK3'].decode('hex')
+
+    def dump(self):
+        d = BIP32_Account_2of2.dump(self)
+        d['c3'] = self.c3.encode('hex')
+        d['K3'] = self.K3.encode('hex')
+        d['cK3'] = self.cK3.encode('hex')
+        return d
+
+    def get_pubkey3(self, for_change, n):
+        K = self.K3
+        chain = self.c3
+        for i in [for_change, n]:
+            K, K_compressed, chain = CKD_prime(K, chain, i)
+        return K_compressed.encode('hex')
+
+    def get_redeem_script(self, sequence):
         chain, i = sequence
         pubkey1 = self.get_pubkey(chain, i)
         pubkey2 = self.get_pubkey2(chain, i)
-        # fixme
-        pk_addr = None # public_key_to_bc_address( pubkey1 ) # we need to return that address to get the right private key
-        redeemScript = Transaction.multisig_script([pubkey1.encode('hex'), pubkey2.encode('hex')], 2)['redeemScript']
-        return pk_addr, redeemScript
+        pubkey3 = self.get_pubkey3(chain, i)
+        return Transaction.multisig_script([pubkey1, pubkey2, pubkey3], 3)
+