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
24 class Account(object):
25 def __init__(self, v):
26 self.addresses = v.get('0', [])
27 self.change = v.get('1', [])
30 return {'0':self.addresses, '1':self.change}
32 def get_addresses(self, for_change):
33 return self.change[:] if for_change else self.addresses[:]
35 def create_new_address(self, for_change):
36 addresses = self.change if for_change else self.addresses
38 address = self.get_address( for_change, n)
39 addresses.append(address)
43 def get_address(self, for_change, n):
46 def get_pubkeys(self, sequence):
47 return [ self.get_pubkey( *sequence )]
51 class OldAccount(Account):
52 """ Privatekey(type,n) = Master_private_key + H(n|S|type) """
54 def __init__(self, v):
55 self.addresses = v.get(0, [])
56 self.change = v.get(1, [])
57 self.mpk = v['mpk'].decode('hex')
60 return {0:self.addresses, 1:self.change}
63 def mpk_from_seed(klass, seed):
65 secexp = klass.stretch_key(seed)
66 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
67 master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
68 return master_public_key
71 def stretch_key(self,seed):
73 for i in range(100000):
74 seed = hashlib.sha256(seed + oldseed).digest()
75 return string_to_number( seed )
77 def get_sequence(self, for_change, n):
78 return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.mpk ) )
80 def get_address(self, for_change, n):
81 pubkey = self.get_pubkey(for_change, n)
82 address = public_key_to_bc_address( pubkey.decode('hex') )
85 def get_pubkey(self, for_change, n):
88 z = self.get_sequence(for_change, n)
89 master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
90 pubkey_point = master_public_key.pubkey.point + z*curve.generator
91 public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
92 return '04' + public_key2.to_string().encode('hex')
94 def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
95 order = generator_secp256k1.order()
96 secexp = ( secexp + self.get_sequence(for_change, n) ) % order
97 pk = number_to_string( secexp, generator_secp256k1.order() )
99 return SecretToASecret( pk, compressed )
101 def get_private_key(self, seed, sequence):
102 for_change, n = sequence
103 secexp = self.stretch_key(seed)
104 return self.get_private_key_from_stretched_exponent(for_change, n, secexp)
106 def check_seed(self, seed):
108 secexp = self.stretch_key(seed)
109 master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
110 master_public_key = master_private_key.get_verifying_key().to_string()
111 if master_public_key != self.mpk:
112 print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
113 raise Exception('Invalid password')
116 def redeem_script(self, sequence):
119 def get_master_pubkeys(self):
123 return _('Old Electrum format')
125 def get_keyID(self, sequence):
127 return 'old(%s,%d,%d)'%(self.mpk,a,b)
131 class BIP32_Account(Account):
133 def __init__(self, v):
134 Account.__init__(self, v)
135 self.xpub = v['xpub']
138 d = Account.dump(self)
139 d['xpub'] = self.xpub
142 def get_address(self, for_change, n):
143 pubkey = self.get_pubkey(self.xpub, for_change, n)
144 address = public_key_to_bc_address( pubkey.decode('hex') )
147 def first_address(self):
148 return self.get_address(0,0)
150 def get_pubkey(self, xpub, for_change, n):
151 _, _, _, c, cK = deserialize_xkey(xpub)
152 for i in [for_change, n]:
153 cK, c = CKD_pub(cK, c, i)
154 return cK.encode('hex')
156 def redeem_script(self, sequence):
159 def get_pubkeys(self, sequence):
160 return [self.get_pubkey(self.xpub, *sequence)]
162 def get_master_pubkeys(self):
166 return _('Standard 1 of 1')
168 def get_keyID(self, sequence):
169 s = '/' + '/'.join( map(lambda x:str(x), sequence) )
170 return '&'.join( map(lambda x: 'bip32(%s,%s)'%(x, s), self.get_master_pubkeys() ) )
173 class BIP32_Account_2of2(BIP32_Account):
175 def __init__(self, v):
176 BIP32_Account.__init__(self, v)
177 self.xpub2 = v['xpub2']
180 d = BIP32_Account.dump(self)
181 d['xpub2'] = self.xpub2
184 def redeem_script(self, sequence):
185 pubkeys = self.get_pubkeys(sequence)
186 return Transaction.multisig_script(pubkeys, 2)
188 def get_address(self, for_change, n):
189 address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
192 def get_pubkeys(self, sequence):
193 return sorted(map(lambda x: self.get_pubkey(x, *sequence ), self.get_master_pubkeys()))
195 def get_master_pubkeys(self):
196 return [self.xpub, self.xpub2]
199 return _('Multisig 2 of 2')
202 class BIP32_Account_2of3(BIP32_Account_2of2):
204 def __init__(self, v):
205 BIP32_Account_2of2.__init__(self, v)
206 self.xpub3 = v['xpub3']
209 d = BIP32_Account_2of2.dump(self)
210 d['xpub3'] = self.xpub3
213 def get_master_pubkeys(self):
214 return [self.xpub, self.xpub2, self.xpub3]
217 return _('Multisig 2 of 3')