electrum-bitcoinprivate/lib/account.py

239 lines
7.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2013 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-08-01 11:08:56 -07:00
from bitcoin import *
2014-04-01 02:25:12 -07:00
from i18n import _
2013-09-04 10:58:34 -07:00
from transaction import Transaction
2013-08-01 11:08:56 -07:00
class Account(object):
def __init__(self, v):
self.addresses = v.get('0', [])
self.change = v.get('1', [])
def dump(self):
2013-08-17 02:46:19 -07:00
return {'0':self.addresses, '1':self.change}
2013-08-01 11:08:56 -07:00
def get_addresses(self, for_change):
return self.change[:] if for_change else self.addresses[:]
def create_new_address(self, for_change):
addresses = self.change if for_change else self.addresses
n = len(addresses)
2013-08-04 07:46:47 -07:00
address = self.get_address( for_change, n)
2013-08-01 11:08:56 -07:00
addresses.append(address)
print address
return address
2013-08-04 07:46:47 -07:00
def get_address(self, for_change, n):
2013-08-01 11:08:56 -07:00
pass
2013-10-03 04:31:59 -07:00
def get_pubkeys(self, sequence):
return [ self.get_pubkey( *sequence )]
2013-08-01 11:08:56 -07:00
def has_change(self):
return True
class PendingAccount(Account):
def __init__(self, v):
self.addresses = [ v['pending'] ]
self.change = []
def has_change(self):
return False
class ImportedAccount(Account):
def __init__(self, d):
self.addresses = d.keys()
def has_change(self):
return False
2013-08-01 11:08:56 -07:00
class OldAccount(Account):
""" Privatekey(type,n) = Master_private_key + H(n|S|type) """
2013-08-31 06:02:20 -07:00
def __init__(self, v):
self.addresses = v.get(0, [])
self.change = v.get(1, [])
self.mpk = v['mpk'].decode('hex')
def dump(self):
return {0:self.addresses, 1:self.change}
2013-08-01 11:08:56 -07:00
@classmethod
def mpk_from_seed(klass, seed):
curve = SECP256k1
secexp = klass.stretch_key(seed)
master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
master_public_key = master_private_key.get_verifying_key().to_string().encode('hex')
return master_public_key
@classmethod
def stretch_key(self,seed):
oldseed = seed
for i in range(100000):
seed = hashlib.sha256(seed + oldseed).digest()
return string_to_number( seed )
2013-08-31 06:02:20 -07:00
def get_sequence(self, for_change, n):
return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.mpk ) )
def get_address(self, for_change, n):
pubkey = self.get_pubkey(for_change, n)
address = public_key_to_bc_address( pubkey.decode('hex') )
2013-08-01 11:08:56 -07:00
return address
2013-08-31 06:02:20 -07:00
def get_pubkey(self, for_change, n):
2013-08-01 11:08:56 -07:00
curve = SECP256k1
2013-08-31 06:02:20 -07:00
mpk = self.mpk
z = self.get_sequence(for_change, n)
master_public_key = ecdsa.VerifyingKey.from_string( mpk, curve = SECP256k1 )
2013-08-01 11:08:56 -07:00
pubkey_point = master_public_key.pubkey.point + z*curve.generator
public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
return '04' + public_key2.to_string().encode('hex')
2013-08-31 06:02:20 -07:00
def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
2013-08-01 11:08:56 -07:00
order = generator_secp256k1.order()
2013-08-31 06:02:20 -07:00
secexp = ( secexp + self.get_sequence(for_change, n) ) % order
2013-08-01 11:08:56 -07:00
pk = number_to_string( secexp, generator_secp256k1.order() )
compressed = False
return SecretToASecret( pk, compressed )
2013-09-02 01:41:50 -07:00
def get_private_key(self, seed, sequence):
for_change, n = sequence
2013-08-01 11:08:56 -07:00
secexp = self.stretch_key(seed)
2013-08-31 06:02:20 -07:00
return self.get_private_key_from_stretched_exponent(for_change, n, secexp)
2013-08-01 11:08:56 -07:00
def check_seed(self, seed):
curve = SECP256k1
secexp = self.stretch_key(seed)
master_private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
2013-11-08 23:15:46 -08:00
master_public_key = master_private_key.get_verifying_key().to_string()
2013-08-01 11:08:56 -07:00
if master_public_key != self.mpk:
print_error('invalid password (mpk)', self.mpk.encode('hex'), master_public_key.encode('hex'))
2013-11-09 20:21:02 -08:00
raise Exception('Invalid password')
2013-08-01 11:08:56 -07:00
return True
2013-08-31 06:02:20 -07:00
def redeem_script(self, sequence):
return None
2013-08-01 11:08:56 -07:00
2014-04-01 02:25:12 -07:00
def get_master_pubkeys(self):
return [self.mpk]
def get_type(self):
return _('Old Electrum format')
2014-04-01 14:53:07 -07:00
def get_keyID(self, sequence):
a, b = sequence
return 'old(%s,%d,%d)'%(self.mpk,a,b)
2014-04-01 02:25:12 -07:00
2013-08-01 11:08:56 -07:00
class BIP32_Account(Account):
def __init__(self, v):
Account.__init__(self, v)
2014-04-01 02:25:12 -07:00
self.xpub = v['xpub']
2013-08-01 11:08:56 -07:00
def dump(self):
d = Account.dump(self)
2014-04-01 02:25:12 -07:00
d['xpub'] = self.xpub
2013-08-01 11:08:56 -07:00
return d
2013-08-04 07:46:47 -07:00
def get_address(self, for_change, n):
2014-04-03 07:11:28 -07:00
pubkey = self.get_pubkey(for_change, n)
address = public_key_to_bc_address( pubkey.decode('hex') )
2013-08-01 11:08:56 -07:00
return address
def first_address(self):
return self.get_address(0,0)
2014-04-03 07:11:28 -07:00
def get_master_pubkeys(self):
return [self.xpub]
def get_pubkey_from_x(self, xpub, for_change, n):
2014-04-03 05:15:02 -07:00
_, _, _, c, cK = deserialize_xkey(xpub)
2013-08-01 11:08:56 -07:00
for i in [for_change, n]:
2014-04-01 02:25:12 -07:00
cK, c = CKD_pub(cK, c, i)
return cK.encode('hex')
2013-08-01 11:08:56 -07:00
2014-04-01 02:25:12 -07:00
def get_pubkeys(self, sequence):
2014-04-03 07:11:28 -07:00
return sorted(map(lambda x: self.get_pubkey_from_x(x, *sequence), self.get_master_pubkeys()))
2014-04-01 02:25:12 -07:00
2014-04-03 07:11:28 -07:00
def get_pubkey(self, for_change, n):
return self.get_pubkeys((for_change, n))[0]
def redeem_script(self, sequence):
return None
2013-08-01 11:08:56 -07:00
2014-04-01 02:25:12 -07:00
def get_type(self):
return _('Standard 1 of 1')
2014-04-01 14:53:07 -07:00
def get_keyID(self, sequence):
s = '/' + '/'.join( map(lambda x:str(x), sequence) )
return '&'.join( map(lambda x: 'bip32(%s,%s)'%(x, s), self.get_master_pubkeys() ) )
2013-08-01 11:08:56 -07:00
class BIP32_Account_2of2(BIP32_Account):
def __init__(self, v):
BIP32_Account.__init__(self, v)
2014-04-01 02:25:12 -07:00
self.xpub2 = v['xpub2']
2013-08-01 11:08:56 -07:00
def dump(self):
d = BIP32_Account.dump(self)
2014-04-01 02:25:12 -07:00
d['xpub2'] = self.xpub2
2013-08-01 11:08:56 -07:00
return d
2013-08-15 06:27:03 -07:00
def redeem_script(self, sequence):
2014-04-03 05:15:02 -07:00
pubkeys = self.get_pubkeys(sequence)
2014-04-01 14:53:07 -07:00
return Transaction.multisig_script(pubkeys, 2)
2013-08-15 06:27:03 -07:00
2013-08-04 07:46:47 -07:00
def get_address(self, for_change, n):
2013-08-15 06:27:03 -07:00
address = hash_160_to_bc_address(hash_160(self.redeem_script((for_change, n)).decode('hex')), 5)
2013-08-01 11:08:56 -07:00
return address
2014-04-01 02:25:12 -07:00
def get_master_pubkeys(self):
return [self.xpub, self.xpub2]
def get_type(self):
return _('Multisig 2 of 2')
2013-08-15 06:27:03 -07:00
class BIP32_Account_2of3(BIP32_Account_2of2):
def __init__(self, v):
BIP32_Account_2of2.__init__(self, v)
2014-04-01 02:25:12 -07:00
self.xpub3 = v['xpub3']
2013-08-15 06:27:03 -07:00
def dump(self):
d = BIP32_Account_2of2.dump(self)
2014-04-01 02:25:12 -07:00
d['xpub3'] = self.xpub3
2013-08-15 06:27:03 -07:00
return d
2014-04-01 02:25:12 -07:00
def get_master_pubkeys(self):
return [self.xpub, self.xpub2, self.xpub3]
def get_type(self):
return _('Multisig 2 of 3')
2013-10-03 04:31:59 -07:00
2013-08-01 11:08:56 -07:00
2014-04-01 14:53:07 -07:00