save account name as label
[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 get_pubkey(self, for_change, n):
164         K = self.K
165         chain = self.c
166         for i in [for_change, n]:
167             K, K_compressed, chain = CKD_prime(K, chain, i)
168         return K_compressed.encode('hex')
169
170     def redeem_script(self, sequence):
171         return None
172
173
174
175
176 class BIP32_Account_2of2(BIP32_Account):
177
178     def __init__(self, v):
179         BIP32_Account.__init__(self, v)
180         self.c2 = v['c2'].decode('hex')
181         self.K2 = v['K2'].decode('hex')
182         self.cK2 = v['cK2'].decode('hex')
183
184     def dump(self):
185         d = BIP32_Account.dump(self)
186         d['c2'] = self.c2.encode('hex')
187         d['K2'] = self.K2.encode('hex')
188         d['cK2'] = self.cK2.encode('hex')
189         return d
190
191     def get_pubkey2(self, for_change, n):
192         K = self.K2
193         chain = self.c2
194         for i in [for_change, n]:
195             K, K_compressed, chain = CKD_prime(K, chain, i)
196         return K_compressed.encode('hex')
197
198     def redeem_script(self, sequence):
199         chain, i = sequence
200         pubkey1 = self.get_pubkey(chain, i)
201         pubkey2 = self.get_pubkey2(chain, i)
202         return Transaction.multisig_script([pubkey1, pubkey2], 2)
203
204     def get_address(self, for_change, n):
205         address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
206         return address
207
208
209 class BIP32_Account_2of3(BIP32_Account_2of2):
210
211     def __init__(self, v):
212         BIP32_Account_2of2.__init__(self, v)
213         self.c3 = v['c3'].decode('hex')
214         self.K3 = v['K3'].decode('hex')
215         self.cK3 = v['cK3'].decode('hex')
216
217     def dump(self):
218         d = BIP32_Account_2of2.dump(self)
219         d['c3'] = self.c3.encode('hex')
220         d['K3'] = self.K3.encode('hex')
221         d['cK3'] = self.cK3.encode('hex')
222         return d
223
224     def get_pubkey3(self, for_change, n):
225         K = self.K3
226         chain = self.c3
227         for i in [for_change, n]:
228             K, K_compressed, chain = CKD_prime(K, chain, i)
229         return K_compressed.encode('hex')
230
231     def get_redeem_script(self, sequence):
232         chain, i = sequence
233         pubkey1 = self.get_pubkey(chain, i)
234         pubkey2 = self.get_pubkey2(chain, i)
235         pubkey3 = self.get_pubkey3(chain, i)
236         return Transaction.multisig_script([pubkey1, pubkey2, pubkey3], 3)
237
238