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