Browse Source

mangle (for_change, index) everywhere

master
thomasv 13 years ago
parent
commit
27b8b21f9e
  1. 34
      lib/bitcoin.py
  2. 25
      lib/wallet.py

34
lib/bitcoin.py

@ -438,23 +438,25 @@ class DeterministicSequence:
seed = hashlib.sha256(seed + oldseed).digest() seed = hashlib.sha256(seed + oldseed).digest()
return string_to_number( seed ) return string_to_number( seed )
def get_sequence(self, n, for_change): def get_sequence(self, sequence):
for_change, n = sequence
return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key.decode('hex') ) ) return string_to_number( Hash( "%d:%d:"%(n,for_change) + self.master_public_key.decode('hex') ) )
def get_address(self, for_change, n): def get_address(self, sequence):
if not self.is_p2sh: if not self.is_p2sh:
pubkey = self.get_pubkey(n, for_change) pubkey = self.get_pubkey(sequence)
address = public_key_to_bc_address( pubkey.decode('hex') ) address = public_key_to_bc_address( pubkey.decode('hex') )
else: else:
pubkey1 = self.get_pubkey(n, for_change) pubkey1 = self.get_pubkey(sequence)
pubkey2 = self.get_pubkey2(n, for_change) pubkey2 = self.get_pubkey2(sequence)
address = Transaction.multisig_script([pubkey1, pubkey2], 2)["address"] address = Transaction.multisig_script([pubkey1, pubkey2], 2)["address"]
return address return address
#sec = self.p2sh_sequence.get_private_key(n, for_change, seed) #sec = self.p2sh_sequence.get_private_key(n, for_change, seed)
#addr = hash_160_to_bc_address(hash_160(txin["redeemScript"].decode('hex')), 5) #addr = hash_160_to_bc_address(hash_160(txin["redeemScript"].decode('hex')), 5)
def get_pubkey2(self, n, for_change): def get_pubkey2(self, sequence):
for_change, n = sequence
curve = SECP256k1 curve = SECP256k1
z = string_to_number( Hash( "%d:%d:"%(n, for_change) + self.mpk2.decode('hex') ) ) z = string_to_number( Hash( "%d:%d:"%(n, for_change) + self.mpk2.decode('hex') ) )
master_public_key = ecdsa.VerifyingKey.from_string( self.mpk2.decode('hex'), curve = SECP256k1 ) master_public_key = ecdsa.VerifyingKey.from_string( self.mpk2.decode('hex'), curve = SECP256k1 )
@ -462,24 +464,24 @@ class DeterministicSequence:
public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 ) public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
return '04' + public_key2.to_string().encode('hex') return '04' + public_key2.to_string().encode('hex')
def get_pubkey(self, n, for_change): def get_pubkey(self, sequence):
curve = SECP256k1 curve = SECP256k1
z = self.get_sequence(n, for_change) z = self.get_sequence(sequence)
master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key.decode('hex'), curve = SECP256k1 ) master_public_key = ecdsa.VerifyingKey.from_string( self.master_public_key.decode('hex'), curve = SECP256k1 )
pubkey_point = master_public_key.pubkey.point + z*curve.generator pubkey_point = master_public_key.pubkey.point + z*curve.generator
public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 ) public_key2 = ecdsa.VerifyingKey.from_public_point( pubkey_point, curve = SECP256k1 )
return '04' + public_key2.to_string().encode('hex') return '04' + public_key2.to_string().encode('hex')
def get_private_key_from_stretched_exponent(self, n, for_change, secexp): def get_private_key_from_stretched_exponent(self, sequence, secexp):
order = generator_secp256k1.order() order = generator_secp256k1.order()
secexp = ( secexp + self.get_sequence(n,for_change) ) % order secexp = ( secexp + self.get_sequence(sequence) ) % order
pk = number_to_string( secexp, generator_secp256k1.order() ) pk = number_to_string( secexp, generator_secp256k1.order() )
compressed = False compressed = False
return SecretToASecret( pk, compressed ) return SecretToASecret( pk, compressed )
def get_private_key(self, n, for_change, seed): def get_private_key(self, sequence, seed):
secexp = self.stretch_key(seed) secexp = self.stretch_key(seed)
return self.get_private_key_from_stretched_exponent(n, for_change, secexp) return self.get_private_key_from_stretched_exponent(sequence, secexp)
def check_seed(self, seed): def check_seed(self, seed):
curve = SECP256k1 curve = SECP256k1
@ -493,14 +495,14 @@ class DeterministicSequence:
return True return True
def get_input_info(self, is_change, n): def get_input_info(self, sequence):
if not self.is_p2sh: if not self.is_p2sh:
pk_addr = self.get_address(is_change, n) pk_addr = self.get_address(sequence)
redeemScript = None redeemScript = None
else: else:
pubkey1 = self.get_pubkey(n, is_change) pubkey1 = self.get_pubkey(sequence)
pubkey2 = self.get_pubkey2(n, is_change) pubkey2 = self.get_pubkey2(sequence)
pk_addr = public_key_to_bc_address( pubkey1.decode('hex') ) # we need to return that address to get the right private key pk_addr = public_key_to_bc_address( pubkey1.decode('hex') ) # we need to return that address to get the right private key
redeemScript = Transaction.multisig_script([pubkey1, pubkey2], 2)['redeemScript'] redeemScript = Transaction.multisig_script([pubkey1, pubkey2], 2)['redeemScript']

25
lib/wallet.py

@ -224,13 +224,13 @@ class Wallet:
addresses = self.accounts[account][for_change] addresses = self.accounts[account][for_change]
for addr in addresses: for addr in addresses:
if address == addr: if address == addr:
return account, for_change, addresses.index(addr) return account, (for_change, addresses.index(addr))
raise BaseException("not found") raise BaseException("not found")
def get_public_key(self, address): def get_public_key(self, address):
account, n, for_change = self.get_address_index(address) account, sequence = self.get_address_index(address)
return self.sequences[account].get_pubkey(n, for_change) return self.sequences[account].get_pubkey( sequence )
def decode_seed(self, password): def decode_seed(self, password):
@ -250,9 +250,9 @@ class Wallet:
if address in self.imported_keys.keys(): if address in self.imported_keys.keys():
out[address] = pw_decode( self.imported_keys[address], password ) out[address] = pw_decode( self.imported_keys[address], password )
else: else:
account, for_change, n = self.get_address_index(address) account, sequence = self.get_address_index(address)
if account == 0: if account == 0:
out[address] = self.sequences[0].get_private_key_from_stretched_exponent(n, for_change, secexp) out[address] = self.sequences[0].get_private_key_from_stretched_exponent( sequence, secexp)
return out return out
@ -289,9 +289,9 @@ class Wallet:
# find the address: # find the address:
if txin.get('electrumKeyID'): if txin.get('electrumKeyID'):
account, for_change, n = txin.get('electrumKeyID') account, sequence = txin.get('electrumKeyID')
sec = self.sequences[account].get_private_key(n, for_change, seed) sec = self.sequences[account].get_private_key(sequence, seed)
addr = self.sequences[account].get_address(n, for_change) addr = self.sequences[account].get_address(sequence)
txin['address'] = addr txin['address'] = addr
private_keys[addr] = sec private_keys[addr] = sec
@ -325,7 +325,7 @@ class Wallet:
def get_new_address(self, account, for_change, n): def get_new_address(self, account, for_change, n):
return self.sequences[account].get_address(for_change, n) return self.sequences[account].get_address((for_change, n))
print address print address
return address return address
@ -626,6 +626,7 @@ class Wallet:
else: else:
#print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee)) #print "not enough funds: %s %s"%(format_satoshis(total), format_satoshis(fee))
inputs = [] inputs = []
return inputs, total, fee return inputs, total, fee
def add_tx_change( self, outputs, amount, fee, total, change_addr=None ): def add_tx_change( self, outputs, amount, fee, total, change_addr=None ):
@ -789,9 +790,9 @@ class Wallet:
if address in self.imported_keys.keys(): if address in self.imported_keys.keys():
pk_addresses.append(address) pk_addresses.append(address)
continue continue
account, is_change, n = self.get_address_index(address) account, sequence = self.get_address_index(address)
txin['electrumKeyID'] = (account, is_change, n) # used by the server to find the key txin['electrumKeyID'] = (account, sequence) # used by the server to find the key
pk_addr, redeemScript = self.sequences[account].get_input_info(is_change, n) pk_addr, redeemScript = self.sequences[account].get_input_info(sequence)
if redeemScript: txin['redeemScript'] = redeemScript if redeemScript: txin['redeemScript'] = redeemScript
pk_addresses.append(pk_addr) pk_addresses.append(pk_addr)

Loading…
Cancel
Save