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 redeem_script(self, for_change, n):
79 class PendingAccount(Account):
80 def __init__(self, v):
81 self.pending_address = v['pending']
83 def get_addresses(self, is_change):
84 return [self.pending_address]
90 return {'pending':self.pending_address }
92 def get_name(self, k):
93 return _('Pending account')
95 def get_master_pubkeys(self):
98 class ImportedAccount(Account):
99 def __init__(self, d):
100 self.keypairs = d['imported']
102 def get_addresses(self, for_change):
103 return [] if for_change else sorted(self.keypairs.keys())
105 def get_pubkey(self, *sequence):
106 for_change, i = sequence
107 assert for_change == 0
108 addr = self.get_addresses(0)[i]
109 return self.keypairs[addr][0]
111 def get_xpubkeys(self, for_change, n):
112 return self.get_pubkeys(for_change, n)
114 def get_private_key(self, sequence, wallet, password):
115 from wallet import pw_decode
116 for_change, i = sequence
117 assert for_change == 0
118 address = self.get_addresses(0)[i]
119 pk = pw_decode(self.keypairs[address][1], password)
120 # this checks the password
121 assert address == address_from_private_key(pk)
124 def has_change(self):
127 def add(self, address, pubkey, privkey, password):
128 from wallet import pw_encode
129 self.keypairs[address] = (pubkey, pw_encode(privkey, password ))
131 def remove(self, address):
132 self.keypairs.pop(address)
135 return {'imported':self.keypairs}
137 def get_name(self, k):
138 return _('Imported keys')
141 def update_password(self, old_password, new_password):
142 for k, v in self.keypairs.items():
144 b = pw_decode(a, old_password)
145 c = pw_encode(b, new_password)
146 self.keypairs[k] = (pubkey, c)
149 class OldAccount(Account):
150 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
152 def __init__(self, v):
153 Account.__init__(self, v)
154 self.mpk = v['mpk'].decode('hex')
158 def mpk_from_seed(klass, seed):
160 secexp = klass.stretch_key(seed)
161 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
162 master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
163 return master_public_key
166 def stretch_key(self,seed):
168 for i in range(100000):
169 seed = hashlib.sha256(seed + oldseed).digest()
170 return string_to_number( seed )
173 def get_sequence(self, mpk, for_change, n):
174 return string_to_number( Hash( "%d:%d:"%(n,for_change) + mpk ) )
176 def get_address(self, for_change, n):
177 pubkey = self.get_pubkey(for_change, n)
178 address = public_key_to_bc_address( pubkey.decode('hex') )
182 def get_pubkey_from_mpk(self, mpk, for_change, n):
184 z = self.get_sequence(mpk, for_change, n)
185 master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
186 pubkey_point = master_public_key.pubkey.point + z*curve.generator
187 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
188 return '04' + public_key2.to_string().encode('hex')
190 def derive_pubkeys(self, for_change, n):
191 return self.get_pubkey_from_mpk(self.mpk, for_change, n)
193 def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
194 order = generator_secp256k1.order()
195 secexp = ( secexp + self.get_sequence(self.mpk, for_change, n) ) % order
196 pk = number_to_string( secexp, generator_secp256k1.order() )
198 return SecretToASecret( pk, compressed )
201 def get_private_key(self, sequence, wallet, password):
202 seed = wallet.get_seed(password)
203 self.check_seed(seed)
204 for_change, n = sequence
205 secexp = self.stretch_key(seed)
206 pk = self.get_private_key_from_stretched_exponent(for_change, n, secexp)
210 def check_seed(self, seed):
212 secexp = self.stretch_key(seed)
213 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
214 master_public_key = master_private_key.get_verifying_key().to_string()
215 if master_public_key != self.mpk:
216 print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
217 raise Exception('Invalid password')
220 def get_master_pubkeys(self):
221 return [self.mpk.encode('hex')]
224 return _('Old Electrum format')
226 def get_xpubkeys(self, for_change, n):
227 s = ''.join(map(lambda x: bitcoin.int_to_hex(x,2), (for_change, n)))
228 mpk = self.mpk.encode('hex')
229 x_pubkey = 'fe' + mpk + s
233 def parse_xpubkey(self, x_pubkey):
234 assert is_extended_pubkey(x_pubkey)
240 n = int(bitcoin.rev_hex(dd[0:4]), 16)
247 class BIP32_Account(Account):
249 def __init__(self, v):
250 Account.__init__(self, v)
251 self.xpub = v['xpub']
254 d = Account.dump(self)
255 d['xpub'] = self.xpub
258 def first_address(self):
259 pubkeys = self.derive_pubkeys(0, 0)
260 address = self.pubkeys_to_address(pubkeys)
263 def get_master_pubkeys(self):
267 def derive_pubkey_from_xpub(self, xpub, for_change, n):
268 _, _, _, c, cK = deserialize_xkey(xpub)
269 for i in [for_change, n]:
270 cK, c = CKD_pub(cK, c, i)
271 return cK.encode('hex')
273 def get_pubkey_from_xpub(self, xpub, for_change, n):
274 xpubs = self.get_master_pubkeys()
275 i = xpubs.index(xpub)
276 pubkeys = self.get_pubkeys(sequence, n)
279 def derive_pubkeys(self, for_change, n):
280 return self.derive_pubkey_from_xpub(self.xpub, for_change, n)
283 def get_private_key(self, sequence, wallet, password):
285 xpubs = self.get_master_pubkeys()
286 roots = [k for k, v in wallet.master_public_keys.iteritems() if v in xpubs]
288 xpriv = wallet.get_master_private_key(root, password)
291 _, _, _, c, k = deserialize_xkey(xpriv)
292 pk = bip32_private_key( sequence, k, c )
297 return _('Standard 1 of 1')
299 def get_xpubkeys(self, for_change, n):
301 s = ''.join(map(lambda x: bitcoin.int_to_hex(x,2), (for_change,n)))
302 xpubs = self.get_master_pubkeys()
303 return map(lambda xpub: 'ff' + bitcoin.DecodeBase58Check(xpub).encode('hex') + s, xpubs)
306 def parse_xpubkey(self, pubkey):
307 assert is_extended_pubkey(pubkey)
308 pk = pubkey.decode('hex')
310 xkey = bitcoin.EncodeBase58Check(pk[0:78])
314 n = int( bitcoin.rev_hex(dd[0:2].encode('hex')), 16)
321 def get_name(self, k):
322 name = "Unnamed account"
323 m = re.match("m/(\d+)'", k)
327 name = "Main account"
329 name = "Account %s"%num
335 class BIP32_Account_2of2(BIP32_Account):
337 def __init__(self, v):
338 BIP32_Account.__init__(self, v)
339 self.xpub2 = v['xpub2']
342 d = BIP32_Account.dump(self)
343 d['xpub2'] = self.xpub2
346 def get_pubkeys(self, for_change, n):
347 return self.get_pubkey(for_change, n)
349 def derive_pubkeys(self, for_change, n):
350 return map(lambda x: self.derive_pubkey_from_xpub(x, for_change, n), self.get_master_pubkeys())
352 def redeem_script(self, for_change, n):
353 pubkeys = self.get_pubkeys(for_change, n)
354 return Transaction.multisig_script(sorted(pubkeys), 2)
356 def pubkeys_to_address(self, pubkeys):
357 redeem_script = Transaction.multisig_script(sorted(pubkeys), 2)
358 address = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 20)
361 def get_address(self, for_change, n):
362 return self.pubkeys_to_address(self.get_pubkeys(for_change, n))
364 def get_master_pubkeys(self):
365 return [self.xpub, self.xpub2]
368 return _('Multisig 2 of 2')
371 class BIP32_Account_2of3(BIP32_Account_2of2):
373 def __init__(self, v):
374 BIP32_Account_2of2.__init__(self, v)
375 self.xpub3 = v['xpub3']
378 d = BIP32_Account_2of2.dump(self)
379 d['xpub3'] = self.xpub3
382 def get_master_pubkeys(self):
383 return [self.xpub, self.xpub2, self.xpub3]
386 return _('Multisig 2 of 3')