fix modules
[electrum-nvc.git] / lib / account.py
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2013 thomasv@gitorious
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 from bitcoin import *
21 from transaction import Transaction
22
23 class Account(object):
24     def __init__(self, v):
25         self.addresses = v.get('0', [])
26         self.change = v.get('1', [])
27
28     def dump(self):
29         return {'0':self.addresses, '1':self.change}
30
31     def get_addresses(self, for_change):
32         return self.change[:] if for_change else self.addresses[:]
33
34     def create_new_address(self, for_change):
35         addresses = self.change if for_change else self.addresses
36         n = len(addresses)
37         address = self.get_address( for_change, n)
38         addresses.append(address)
39         print address
40         return address
41
42     def get_address(self, for_change, n):
43         pass
44         
45
46
47
48 class OldAccount(Account):
49     """  Privatekey(type,n) = Master_private_key + H(n|S|type)  """
50
51     def __init__(self, v):
52         self.addresses = v.get(0, [])
53         self.change = v.get(1, [])
54         self.mpk = v['mpk'].decode('hex')
55
56     def dump(self):
57         return {0:self.addresses, 1:self.change}
58
59     @classmethod
60     def mpk_from_seed(klass, seed):
61         curve = SECP256k1
62         secexp = klass.stretch_key(seed)
63         master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
64         master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
65         return master_public_key
66
67     @classmethod
68     def stretch_key(self,seed):
69         oldseed = seed
70         for i in range(100000):
71             seed = hashlib.sha256(seed + oldseed).digest()
72         return string_to_number( seed )
73
74     def get_sequence(self, for_change, n):
75         return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.mpk ) )
76
77     def get_address(self, for_change, n):
78         pubkey = self.get_pubkey(for_change, n)
79         address = public_key_to_bc_address( pubkey.decode('hex') )
80         return address
81
82     def get_pubkey(self, for_change, n):
83         curve = SECP256k1
84         mpk = self.mpk
85         z = self.get_sequence(for_change, n)
86         master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
87         pubkey_point = master_public_key.pubkey.point + z*curve.generator
88         public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
89         return '04' + public_key2.to_string().encode('hex')
90
91     def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
92         order = generator_secp256k1.order()
93         secexp = ( secexp + self.get_sequence(for_change, n) ) % order
94         pk = number_to_string( secexp, generator_secp256k1.order() )
95         compressed = False
96         return SecretToASecret( pk, compressed )
97         
98     def get_private_key(self, seed, sequence):
99         for_change, n = sequence
100         secexp = self.stretch_key(seed)
101         return self.get_private_key_from_stretched_exponent(for_change, n, secexp)
102
103     def check_seed(self, seed):
104         curve = SECP256k1
105         secexp = self.stretch_key(seed)
106         master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
107         master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
108         if master_public_key != self.mpk:
109             print_error('invalid password (mpk)')
110             raise BaseException('Invalid password')
111         return True
112
113     def redeem_script(self, sequence):
114         return None
115
116
117 class BIP32_Account(Account):
118
119     def __init__(self, v):
120         Account.__init__(self, v)
121         self.c = v['c'].decode('hex')
122         self.K = v['K'].decode('hex')
123         self.cK = v['cK'].decode('hex')
124
125     def dump(self):
126         d = Account.dump(self)
127         d['c'] = self.c.encode('hex')
128         d['K'] = self.K.encode('hex')
129         d['cK'] = self.cK.encode('hex')
130         return d
131
132     def get_address(self, for_change, n):
133         pubkey = self.get_pubkey(for_change, n)
134         address = public_key_to_bc_address( pubkey.decode('hex') )
135         return address
136
137     def first_address(self):
138         return self.get_address(0,0)
139
140     def get_pubkey(self, for_change, n):
141         K = self.K
142         chain = self.c
143         for i in [for_change, n]:
144             K, K_compressed, chain = CKD_prime(K, chain, i)
145         return K_compressed.encode('hex')
146
147     def redeem_script(self, sequence):
148         return None
149
150
151
152
153 class BIP32_Account_2of2(BIP32_Account):
154
155     def __init__(self, v):
156         BIP32_Account.__init__(self, v)
157         self.c2 = v['c2'].decode('hex')
158         self.K2 = v['K2'].decode('hex')
159         self.cK2 = v['cK2'].decode('hex')
160
161     def dump(self):
162         d = BIP32_Account.dump(self)
163         d['c2'] = self.c2.encode('hex')
164         d['K2'] = self.K2.encode('hex')
165         d['cK2'] = self.cK2.encode('hex')
166         return d
167
168     def get_pubkey2(self, for_change, n):
169         K = self.K2
170         chain = self.c2
171         for i in [for_change, n]:
172             K, K_compressed, chain = CKD_prime(K, chain, i)
173         return K_compressed.encode('hex')
174
175     def redeem_script(self, sequence):
176         chain, i = sequence
177         pubkey1 = self.get_pubkey(chain, i)
178         pubkey2 = self.get_pubkey2(chain, i)
179         return Transaction.multisig_script([pubkey1, pubkey2], 2)
180
181     def get_address(self, for_change, n):
182         address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
183         return address
184
185
186 class BIP32_Account_2of3(BIP32_Account_2of2):
187
188     def __init__(self, v):
189         BIP32_Account_2of2.__init__(self, v)
190         self.c3 = v['c3'].decode('hex')
191         self.K3 = v['K3'].decode('hex')
192         self.cK3 = v['cK3'].decode('hex')
193
194     def dump(self):
195         d = BIP32_Account_2of2.dump(self)
196         d['c3'] = self.c3.encode('hex')
197         d['K3'] = self.K3.encode('hex')
198         d['cK3'] = self.cK3.encode('hex')
199         return d
200
201     def get_pubkey3(self, for_change, n):
202         K = self.K3
203         chain = self.c3
204         for i in [for_change, n]:
205             K, K_compressed, chain = CKD_prime(K, chain, i)
206         return K_compressed.encode('hex')
207
208     def get_redeem_script(self, sequence):
209         chain, i = sequence
210         pubkey1 = self.get_pubkey(chain, i)
211         pubkey2 = self.get_pubkey2(chain, i)
212         pubkey3 = self.get_pubkey3(chain, i)
213         return Transaction.multisig_script([pubkey1, pubkey2, pubkey3], 3)
214
215