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):
85 self.pending_pubkey = v['pending_pubkey']
89 def get_addresses(self, is_change):
96 return {} #{'pending_pubkey':self.pending_pubkey }
98 def get_name(self, k):
99 return _('Pending account')
101 def get_master_pubkeys(self):
104 class ImportedAccount(Account):
105 def __init__(self, d):
106 self.keypairs = d['imported']
108 def get_addresses(self, for_change):
109 return [] if for_change else sorted(self.keypairs.keys())
111 def get_pubkey(self, *sequence):
112 for_change, i = sequence
113 assert for_change == 0
114 addr = self.get_addresses(0)[i]
115 return self.keypairs[addr][0]
117 def get_xpubkeys(self, for_change, n):
118 return self.get_pubkeys(for_change, n)
120 def get_private_key(self, sequence, wallet, password):
121 from wallet import pw_decode
122 for_change, i = sequence
123 assert for_change == 0
124 address = self.get_addresses(0)[i]
125 pk = pw_decode(self.keypairs[address][1], password)
126 # this checks the password
127 assert address == address_from_private_key(pk)
130 def has_change(self):
133 def add(self, address, pubkey, privkey, password):
134 from wallet import pw_encode
135 self.keypairs[address] = (pubkey, pw_encode(privkey, password ))
137 def remove(self, address):
138 self.keypairs.pop(address)
141 return {'imported':self.keypairs}
143 def get_name(self, k):
144 return _('Imported keys')
147 def update_password(self, old_password, new_password):
148 for k, v in self.keypairs.items():
150 b = pw_decode(a, old_password)
151 c = pw_encode(b, new_password)
152 self.keypairs[k] = (pubkey, c)
155 class OldAccount(Account):
156 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
158 def __init__(self, v):
159 Account.__init__(self, v)
160 self.mpk = v['mpk'].decode('hex')
164 def mpk_from_seed(klass, seed):
166 secexp = klass.stretch_key(seed)
167 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
168 master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
169 return master_public_key
172 def stretch_key(self,seed):
174 for i in range(100000):
175 seed = hashlib.sha256(seed + oldseed).digest()
176 return string_to_number( seed )
179 def get_sequence(self, mpk, for_change, n):
180 return string_to_number( Hash( "%d:%d:"%(n,for_change) + mpk ) )
182 def get_address(self, for_change, n):
183 pubkey = self.get_pubkey(for_change, n)
184 address = public_key_to_bc_address( pubkey.decode('hex') )
188 def get_pubkey_from_mpk(self, mpk, for_change, n):
190 z = self.get_sequence(mpk, for_change, n)
191 master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
192 pubkey_point = master_public_key.pubkey.point + z*curve.generator
193 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
194 return '04' + public_key2.to_string().encode('hex')
196 def derive_pubkeys(self, for_change, n):
197 return self.get_pubkey_from_mpk(self.mpk, for_change, n)
199 def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
200 order = generator_secp256k1.order()
201 secexp = ( secexp + self.get_sequence(self.mpk, for_change, n) ) % order
202 pk = number_to_string( secexp, generator_secp256k1.order() )
204 return SecretToASecret( pk, compressed )
207 def get_private_key(self, sequence, wallet, password):
208 seed = wallet.get_seed(password)
209 self.check_seed(seed)
210 for_change, n = sequence
211 secexp = self.stretch_key(seed)
212 pk = self.get_private_key_from_stretched_exponent(for_change, n, secexp)
216 def check_seed(self, seed):
218 secexp = self.stretch_key(seed)
219 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
220 master_public_key = master_private_key.get_verifying_key().to_string()
221 if master_public_key != self.mpk:
222 print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
223 raise Exception('Invalid password')
226 def redeem_script(self, sequence):
229 def get_master_pubkeys(self):
230 return [self.mpk.encode('hex')]
233 return _('Old Electrum format')
235 def get_xpubkeys(self, sequence):
236 s = ''.join(map(lambda x: bitcoin.int_to_hex(x,2), sequence))
237 mpk = self.mpk.encode('hex')
238 x_pubkey = 'fe' + mpk + s
242 def parse_xpubkey(self, x_pubkey):
243 assert is_extended_pubkey(x_pubkey)
249 n = int(bitcoin.rev_hex(dd[0:4]), 16)
256 class BIP32_Account(Account):
258 def __init__(self, v):
259 Account.__init__(self, v)
260 self.xpub = v['xpub']
263 d = Account.dump(self)
264 d['xpub'] = self.xpub
267 def first_address(self):
268 return self.get_address(0,0)
270 def get_master_pubkeys(self):
274 def derive_pubkey_from_xpub(self, xpub, for_change, n):
275 _, _, _, c, cK = deserialize_xkey(xpub)
276 for i in [for_change, n]:
277 cK, c = CKD_pub(cK, c, i)
278 return cK.encode('hex')
280 def get_pubkey_from_xpub(self, xpub, for_change, n):
281 xpubs = self.get_master_pubkeys()
282 i = xpubs.index(xpub)
283 pubkeys = self.get_pubkeys(sequence, n)
286 def derive_pubkeys(self, for_change, n):
287 return self.derive_pubkey_from_xpub(self.xpub, for_change, n)
290 def get_private_key(self, sequence, wallet, password):
292 xpubs = self.get_master_pubkeys()
293 roots = [k for k, v in wallet.master_public_keys.iteritems() if v in xpubs]
295 xpriv = wallet.get_master_private_key(root, password)
298 _, _, _, c, k = deserialize_xkey(xpriv)
299 pk = bip32_private_key( sequence, k, c )
303 def redeem_script(self, sequence):
307 return _('Standard 1 of 1')
309 def get_xpubkeys(self, for_change, n):
311 s = ''.join(map(lambda x: bitcoin.int_to_hex(x,2), (for_change,n)))
312 xpubs = self.get_master_pubkeys()
313 return map(lambda xpub: 'ff' + bitcoin.DecodeBase58Check(xpub).encode('hex') + s, xpubs)
316 def parse_xpubkey(self, pubkey):
317 assert is_extended_pubkey(pubkey)
318 pk = pubkey.decode('hex')
320 xkey = bitcoin.EncodeBase58Check(pk[0:78])
324 n = int( bitcoin.rev_hex(dd[0:2].encode('hex')), 16)
331 def get_name(self, k):
332 name = "Unnamed account"
333 m = re.match("m/(\d+)'", k)
337 name = "Main account"
339 name = "Account %s"%num
345 class BIP32_Account_2of2(BIP32_Account):
347 def __init__(self, v):
348 BIP32_Account.__init__(self, v)
349 self.xpub2 = v['xpub2']
352 d = BIP32_Account.dump(self)
353 d['xpub2'] = self.xpub2
356 def get_pubkeys(self, for_change, n):
357 return self.get_pubkey(for_change, n)
359 def derive_pubkeys(self, for_change, n):
360 return map(lambda x: self.derive_pubkey_from_xpub(x, for_change, n), self.get_master_pubkeys())
362 def redeem_script(self, for_change, n):
363 pubkeys = self.get_pubkeys(for_change, n)
364 return Transaction.multisig_script(sorted(pubkeys), 2)
366 def pubkeys_to_address(self, pubkeys):
367 redeem_script = Transaction.multisig_script(sorted(pubkeys), 2)
368 address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5)
371 def get_address(self, for_change, n):
372 return self.pubkeys_to_address(self.get_pubkeys(for_change, n))
374 def get_master_pubkeys(self):
375 return [self.xpub, self.xpub2]
378 return _('Multisig 2 of 2')
381 class BIP32_Account_2of3(BIP32_Account_2of2):
383 def __init__(self, v):
384 BIP32_Account_2of2.__init__(self, v)
385 self.xpub3 = v['xpub3']
388 d = BIP32_Account_2of2.dump(self)
389 d['xpub3'] = self.xpub3
392 def get_master_pubkeys(self):
393 return [self.xpub, self.xpub2, self.xpub3]
396 return _('Multisig 2 of 3')