get_pubkey_from_x
[electrum-nvc.git] / lib / account.py
index 9a7c22a..37ddc60 100644 (file)
@@ -122,6 +122,10 @@ class OldAccount(Account):
     def get_type(self):
         return _('Old Electrum format')
 
+    def get_keyID(self, sequence):
+        a, b = sequence
+        return 'old(%s,%d,%d)'%(self.mpk,a,b)
+
 
 
 class BIP32_Account(Account):
@@ -143,24 +147,30 @@ class BIP32_Account(Account):
     def first_address(self):
         return self.get_address(0,0)
 
-    def get_pubkey(self, for_change, n):
-        _, _, _, c, cK = deserialize_xkey(self.xpub)
+    def get_master_pubkeys(self):
+        return [self.xpub]
+
+    def get_pubkey_from_x(self, xpub, for_change, n):
+        _, _, _, c, cK = deserialize_xkey(xpub)
         for i in [for_change, n]:
             cK, c = CKD_pub(cK, c, i)
         return cK.encode('hex')
 
-    def redeem_script(self, sequence):
-        return None
-
     def get_pubkeys(self, sequence):
-        return [self.get_pubkey(*sequence)]
+        return sorted(map(lambda x: self.get_pubkey_from_x(x, *sequence), self.get_master_pubkeys()))
 
-    def get_master_pubkeys(self):
-        return [self.xpub]
+    def get_pubkey(self, for_change, n):
+        return self.get_pubkeys((for_change, n))[0]
+
+    def redeem_script(self, sequence):
+        return None
 
     def get_type(self):
         return _('Standard 1 of 1')
-        #acctype = 'multisig 2 of 2' if len(roots) == 2 else 'multisig 2 of 3' if len(roots) == 3 else 'standard 1 of 1'
+
+    def get_keyID(self, sequence):
+        s = '/' + '/'.join( map(lambda x:str(x), sequence) )
+        return '&'.join( map(lambda x: 'bip32(%s,%s)'%(x, s), self.get_master_pubkeys() ) )
 
 
 class BIP32_Account_2of2(BIP32_Account):
@@ -174,25 +184,14 @@ class BIP32_Account_2of2(BIP32_Account):
         d['xpub2'] = self.xpub2
         return d
 
-    def get_pubkey2(self, for_change, n):
-        _, _, _, c, cK = deserialize_xkey(self.xpub2)
-        for i in [for_change, n]:
-            cK, c = CKD_prime(cK, c, i)
-        return cK.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)
+        pubkeys = self.get_pubkeys(sequence)
+        return Transaction.multisig_script(pubkeys, 2)
 
     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_pubkeys(self, sequence):
-        return [ self.get_pubkey( *sequence ), self.get_pubkey2( *sequence )]
-
     def get_master_pubkeys(self):
         return [self.xpub, self.xpub2]
 
@@ -211,22 +210,6 @@ class BIP32_Account_2of3(BIP32_Account_2of2):
         d['xpub3'] = self.xpub3
         return d
 
-    def get_pubkey3(self, for_change, n):
-        _, _, _, c, cK = deserialize_xkey(self.xpub3)
-        for i in [for_change, n]:
-            cK, c = CKD_prime(cK, c, i)
-        return cK.encode('hex')
-
-    def get_redeem_script(self, sequence):
-        chain, i = sequence
-        pubkey1 = self.get_pubkey(chain, i)
-        pubkey2 = self.get_pubkey2(chain, i)
-        pubkey3 = self.get_pubkey3(chain, i)
-        return Transaction.multisig_script([pubkey1, pubkey2, pubkey3], 3)
-
-    def get_pubkeys(self, sequence):
-        return [ self.get_pubkey( *sequence ), self.get_pubkey2( *sequence ), self.get_pubkey3( *sequence )]
-
     def get_master_pubkeys(self):
         return [self.xpub, self.xpub2, self.xpub3]
 
@@ -235,3 +218,4 @@ class BIP32_Account_2of3(BIP32_Account_2of2):
 
 
 
+