3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2013 thomasv@gitorious
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.
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.
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/>.
22 from transaction import Transaction, is_extended_pubkey
23 from util import print_msg
26 class Account(object):
27 def __init__(self, v):
28 self.receiving_pubkeys = v.get('receiving', [])
29 self.change_pubkeys = v.get('change', [])
30 # addresses will not be stored on disk
31 self.receiving_addresses = map(self.pubkeys_to_address, self.receiving_pubkeys)
32 self.change_addresses = map(self.pubkeys_to_address, self.change_pubkeys)
35 return {'receiving':self.receiving_pubkeys, 'change':self.change_pubkeys}
37 def get_pubkey(self, for_change, n):
38 pubkeys_list = self.change_pubkeys if for_change else self.receiving_pubkeys
39 return pubkeys_list[n]
41 def get_address(self, for_change, n):
42 addr_list = self.change_addresses if for_change else self.receiving_addresses
45 def get_pubkeys(self, for_change, n):
46 return [ self.get_pubkey(for_change, n)]
48 def get_addresses(self, for_change):
49 addr_list = self.change_addresses if for_change else self.receiving_addresses
52 def derive_pubkeys(self, for_change, n):
55 def create_new_address(self, for_change):
56 pubkeys_list = self.change_pubkeys if for_change else self.receiving_pubkeys
57 addr_list = self.change_addresses if for_change else self.receiving_addresses
59 pubkeys = self.derive_pubkeys(for_change, n)
60 address = self.pubkeys_to_address(pubkeys)
61 pubkeys_list.append(pubkeys)
62 addr_list.append(address)
66 def pubkeys_to_address(self, pubkey):
67 return public_key_to_bc_address(pubkey.decode('hex'))
72 def get_name(self, k):
73 return _('Main account')
75 def get_keyID(self, *sequence):
78 def redeem_script(self, *sequence):
82 class PendingAccount(Account):
83 def __init__(self, v):
84 self.pending_address = v['pending']
86 def get_addresses(self, is_change):
87 return [self.pending_address]
93 return {'pending':self.pending_address }
95 def get_name(self, k):
96 return _('Pending account')
98 def get_master_pubkeys(self):
101 class ImportedAccount(Account):
102 def __init__(self, d):
103 self.keypairs = d['imported']
105 def get_addresses(self, for_change):
106 return [] if for_change else sorted(self.keypairs.keys())
108 def get_pubkey(self, *sequence):
109 for_change, i = sequence
110 assert for_change == 0
111 addr = self.get_addresses(0)[i]
112 return self.keypairs[addr][0]
114 def get_xpubkeys(self, for_change, n):
115 return self.get_pubkeys(for_change, n)
117 def get_private_key(self, sequence, wallet, password):
118 from wallet import pw_decode
119 for_change, i = sequence
120 assert for_change == 0
121 address = self.get_addresses(0)[i]
122 pk = pw_decode(self.keypairs[address][1], password)
123 # this checks the password
124 assert address == address_from_private_key(pk)
127 def has_change(self):
130 def add(self, address, pubkey, privkey, password):
131 from wallet import pw_encode
132 self.keypairs[address] = (pubkey, pw_encode(privkey, password ))
134 def remove(self, address):
135 self.keypairs.pop(address)
138 return {'imported':self.keypairs}
140 def get_name(self, k):
141 return _('Imported keys')
144 def update_password(self, old_password, new_password):
145 for k, v in self.keypairs.items():
147 b = pw_decode(a, old_password)
148 c = pw_encode(b, new_password)
149 self.keypairs[k] = (pubkey, c)
152 class OldAccount(Account):
153 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
155 def __init__(self, v):
156 Account.__init__(self, v)
157 self.mpk = v['mpk'].decode('hex')
161 def mpk_from_seed(klass, seed):
163 secexp = klass.stretch_key(seed)
164 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
165 master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
166 return master_public_key
169 def stretch_key(self,seed):
171 for i in range(100000):
172 seed = hashlib.sha256(seed + oldseed).digest()
173 return string_to_number( seed )
176 def get_sequence(self, mpk, for_change, n):
177 return string_to_number( Hash( "%d:%d:"%(n,for_change) + mpk ) )
179 def get_address(self, for_change, n):
180 pubkey = self.get_pubkey(for_change, n)
181 address = public_key_to_bc_address( pubkey.decode('hex') )
185 def get_pubkey_from_mpk(self, mpk, for_change, n):
187 z = self.get_sequence(mpk, for_change, n)
188 master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
189 pubkey_point = master_public_key.pubkey.point + z*curve.generator
190 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
191 return '04' + public_key2.to_string().encode('hex')
193 def derive_pubkeys(self, for_change, n):
194 return self.get_pubkey_from_mpk(self.mpk, for_change, n)
196 def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
197 order = generator_secp256k1.order()
198 secexp = ( secexp + self.get_sequence(self.mpk, for_change, n) ) % order
199 pk = number_to_string( secexp, generator_secp256k1.order() )
201 return SecretToASecret( pk, compressed )
204 def get_private_key(self, sequence, wallet, password):
205 seed = wallet.get_seed(password)
206 self.check_seed(seed)
207 for_change, n = sequence
208 secexp = self.stretch_key(seed)
209 pk = self.get_private_key_from_stretched_exponent(for_change, n, secexp)
213 def check_seed(self, seed):
215 secexp = self.stretch_key(seed)
216 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
217 master_public_key = master_private_key.get_verifying_key().to_string()
218 if master_public_key != self.mpk:
219 print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
220 raise Exception('Invalid password')
223 def redeem_script(self, sequence):
226 def get_master_pubkeys(self):
227 return [self.mpk.encode('hex')]
230 return _('Old Electrum format')
232 def get_xpubkeys(self, sequence):
233 s = ''.join(map(lambda x: bitcoin.int_to_hex(x,2), sequence))
234 mpk = self.mpk.encode('hex')
235 x_pubkey = 'fe' + mpk + s
239 def parse_xpubkey(self, x_pubkey):
240 assert is_extended_pubkey(x_pubkey)
246 n = int(bitcoin.rev_hex(dd[0:4]), 16)
253 class BIP32_Account(Account):
255 def __init__(self, v):
256 Account.__init__(self, v)
257 self.xpub = v['xpub']
260 d = Account.dump(self)
261 d['xpub'] = self.xpub
264 def first_address(self):
265 pubkeys = self.derive_pubkeys(0, 0)
266 address = self.pubkeys_to_address(pubkeys)
269 def get_master_pubkeys(self):
273 def derive_pubkey_from_xpub(self, xpub, for_change, n):
274 _, _, _, c, cK = deserialize_xkey(xpub)
275 for i in [for_change, n]:
276 cK, c = CKD_pub(cK, c, i)
277 return cK.encode('hex')
279 def get_pubkey_from_xpub(self, xpub, for_change, n):
280 xpubs = self.get_master_pubkeys()
281 i = xpubs.index(xpub)
282 pubkeys = self.get_pubkeys(sequence, n)
285 def derive_pubkeys(self, for_change, n):
286 return self.derive_pubkey_from_xpub(self.xpub, for_change, n)
289 def get_private_key(self, sequence, wallet, password):
291 xpubs = self.get_master_pubkeys()
292 roots = [k for k, v in wallet.master_public_keys.iteritems() if v in xpubs]
294 xpriv = wallet.get_master_private_key(root, password)
297 _, _, _, c, k = deserialize_xkey(xpriv)
298 pk = bip32_private_key( sequence, k, c )
302 def redeem_script(self, sequence):
306 return _('Standard 1 of 1')
308 def get_xpubkeys(self, for_change, n):
310 s = ''.join(map(lambda x: bitcoin.int_to_hex(x,2), (for_change,n)))
311 xpubs = self.get_master_pubkeys()
312 return map(lambda xpub: 'ff' + bitcoin.DecodeBase58Check(xpub).encode('hex') + s, xpubs)
315 def parse_xpubkey(self, pubkey):
316 assert is_extended_pubkey(pubkey)
317 pk = pubkey.decode('hex')
319 xkey = bitcoin.EncodeBase58Check(pk[0:78])
323 n = int( bitcoin.rev_hex(dd[0:2].encode('hex')), 16)
330 def get_name(self, k):
331 name = "Unnamed account"
332 m = re.match("m/(\d+)'", k)
336 name = "Main account"
338 name = "Account %s"%num
344 class BIP32_Account_2of2(BIP32_Account):
346 def __init__(self, v):
347 BIP32_Account.__init__(self, v)
348 self.xpub2 = v['xpub2']
351 d = BIP32_Account.dump(self)
352 d['xpub2'] = self.xpub2
355 def get_pubkeys(self, for_change, n):
356 return self.get_pubkey(for_change, n)
358 def derive_pubkeys(self, for_change, n):
359 return map(lambda x: self.derive_pubkey_from_xpub(x, for_change, n), self.get_master_pubkeys())
361 def redeem_script(self, for_change, n):
362 pubkeys = self.get_pubkeys(for_change, n)
363 return Transaction.multisig_script(sorted(pubkeys), 2)
365 def pubkeys_to_address(self, pubkeys):
366 redeem_script = Transaction.multisig_script(sorted(pubkeys), 2)
367 address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5)
370 def get_address(self, for_change, n):
371 return self.pubkeys_to_address(self.get_pubkeys(for_change, n))
373 def get_master_pubkeys(self):
374 return [self.xpub, self.xpub2]
377 return _('Multisig 2 of 2')
380 class BIP32_Account_2of3(BIP32_Account_2of2):
382 def __init__(self, v):
383 BIP32_Account_2of2.__init__(self, v)
384 self.xpub3 = v['xpub3']
387 d = BIP32_Account_2of2.dump(self)
388 d['xpub3'] = self.xpub3
391 def get_master_pubkeys(self):
392 return [self.xpub, self.xpub2, self.xpub3]
395 return _('Multisig 2 of 3')