During wallet creation, do not write seed on disk before it is encrypted
[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     def get_pubkeys(self, sequence):
46         return [ self.get_pubkey( *sequence )]
47
48
49
50 class OldAccount(Account):
51     """  Privatekey(type,n) = Master_private_key + H(n|S|type)  """
52
53     def __init__(self, v):
54         self.addresses = v.get(0, [])
55         self.change = v.get(1, [])
56         self.mpk = v['mpk'].decode('hex')
57
58     def dump(self):
59         return {0:self.addresses, 1:self.change}
60
61     @classmethod
62     def mpk_from_seed(klass, seed):
63         curve = SECP256k1
64         secexp = klass.stretch_key(seed)
65         master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
66         master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
67         return master_public_key
68
69     @classmethod
70     def stretch_key(self,seed):
71         oldseed = seed
72         for i in range(100000):
73             seed = hashlib.sha256(seed + oldseed).digest()
74         return string_to_number( seed )
75
76     def get_sequence(self, for_change, n):
77         return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.mpk ) )
78
79     def get_address(self, for_change, n):
80         pubkey = self.get_pubkey(for_change, n)
81         address = public_key_to_bc_address( pubkey.decode('hex') )
82         return address
83
84     def get_pubkey(self, for_change, n):
85         curve = SECP256k1
86         mpk = self.mpk
87         z = self.get_sequence(for_change, n)
88         master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
89         pubkey_point = master_public_key.pubkey.point + z*curve.generator
90         public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
91         return '04' + public_key2.to_string().encode('hex')
92
93     def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
94         order = generator_secp256k1.order()
95         secexp = ( secexp + self.get_sequence(for_change, n) ) % order
96         pk = number_to_string( secexp, generator_secp256k1.order() )
97         compressed = False
98         return SecretToASecret( pk, compressed )
99         
100     def get_private_key(self, seed, sequence):
101         for_change, n = sequence
102         secexp = self.stretch_key(seed)
103         return self.get_private_key_from_stretched_exponent(for_change, n, secexp)
104
105     def check_seed(self, seed):
106         curve = SECP256k1
107         secexp = self.stretch_key(seed)
108         master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
109         master_public_key = master_private_key.get_verifying_key().to_string()
110         if master_public_key != self.mpk:
111             print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
112             raise Exception('Invalid password')
113         return True
114
115     def redeem_script(self, sequence):
116         return None
117
118
119 class BIP32_Account(Account):
120
121     def __init__(self, v):
122         Account.__init__(self, v)
123         self.c = v['c'].decode('hex')
124         self.K = v['K'].decode('hex')
125         self.cK = v['cK'].decode('hex')
126
127     def dump(self):
128         d = Account.dump(self)
129         d['c'] = self.c.encode('hex')
130         d['K'] = self.K.encode('hex')
131         d['cK'] = self.cK.encode('hex')
132         return d
133
134     def get_address(self, for_change, n):
135         pubkey = self.get_pubkey(for_change, n)
136         address = public_key_to_bc_address( pubkey.decode('hex') )
137         return address
138
139     def first_address(self):
140         return self.get_address(0,0)
141
142     def get_pubkey(self, for_change, n):
143         K = self.K
144         chain = self.c
145         for i in [for_change, n]:
146             K, K_compressed, chain = CKD_prime(K, chain, i)
147         return K_compressed.encode('hex')
148
149     def redeem_script(self, sequence):
150         return None
151
152
153
154
155 class BIP32_Account_2of2(BIP32_Account):
156
157     def __init__(self, v):
158         BIP32_Account.__init__(self, v)
159         self.c2 = v['c2'].decode('hex')
160         self.K2 = v['K2'].decode('hex')
161         self.cK2 = v['cK2'].decode('hex')
162
163     def dump(self):
164         d = BIP32_Account.dump(self)
165         d['c2'] = self.c2.encode('hex')
166         d['K2'] = self.K2.encode('hex')
167         d['cK2'] = self.cK2.encode('hex')
168         return d
169
170     def get_pubkey2(self, for_change, n):
171         K = self.K2
172         chain = self.c2
173         for i in [for_change, n]:
174             K, K_compressed, chain = CKD_prime(K, chain, i)
175         return K_compressed.encode('hex')
176
177     def redeem_script(self, sequence):
178         chain, i = sequence
179         pubkey1 = self.get_pubkey(chain, i)
180         pubkey2 = self.get_pubkey2(chain, i)
181         return Transaction.multisig_script([pubkey1, pubkey2], 2)
182
183     def get_address(self, for_change, n):
184         address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
185         return address
186
187     def get_pubkeys(self, sequence):
188         return [ self.get_pubkey( *sequence ), self.get_pubkey2( *sequence )]
189
190 class BIP32_Account_2of3(BIP32_Account_2of2):
191
192     def __init__(self, v):
193         BIP32_Account_2of2.__init__(self, v)
194         self.c3 = v['c3'].decode('hex')
195         self.K3 = v['K3'].decode('hex')
196         self.cK3 = v['cK3'].decode('hex')
197
198     def dump(self):
199         d = BIP32_Account_2of2.dump(self)
200         d['c3'] = self.c3.encode('hex')
201         d['K3'] = self.K3.encode('hex')
202         d['cK3'] = self.cK3.encode('hex')
203         return d
204
205     def get_pubkey3(self, for_change, n):
206         K = self.K3
207         chain = self.c3
208         for i in [for_change, n]:
209             K, K_compressed, chain = CKD_prime(K, chain, i)
210         return K_compressed.encode('hex')
211
212     def get_redeem_script(self, sequence):
213         chain, i = sequence
214         pubkey1 = self.get_pubkey(chain, i)
215         pubkey2 = self.get_pubkey2(chain, i)
216         pubkey3 = self.get_pubkey3(chain, i)
217         return Transaction.multisig_script([pubkey1, pubkey2, pubkey3], 3)
218
219     def get_pubkeys(self, sequence):
220         return [ self.get_pubkey( *sequence ), self.get_pubkey2( *sequence ), self.get_pubkey3( *sequence )]
221
222
223