From 799c6571f50e4d2c8ed40bc6be8fa5216b188035 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Sat, 17 Aug 2013 11:09:19 +0200 Subject: [PATCH] simplify: public_key_from_private_key --- lib/bitcoin.py | 14 +++++++------- lib/wallet.py | 8 +++----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index 529143ad3..d1f427e24 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -244,18 +244,18 @@ def is_compressed(sec): return len(b) == 33 -def address_from_private_key(sec): +def public_key_from_private_key(sec): # rebuild public key from private key, compressed or uncompressed pkey = regenerate_key(sec) assert pkey - - # figure out if private key is compressed compressed = is_compressed(sec) - - # rebuild private and public key from regenerated secret - private_key = GetPrivKey(pkey, compressed) public_key = GetPubKey(pkey.pubkey, compressed) - address = public_key_to_bc_address(public_key) + return public_key.encode('hex') + + +def address_from_private_key(sec): + public_key = public_key_from_private_key(sec) + address = public_key_to_bc_address(public_key.decode('hex')) return address diff --git a/lib/wallet.py b/lib/wallet.py index 3163db671..b2ecbd5dc 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -363,10 +363,8 @@ class Wallet: # build a list of public/private keys keypairs = {} for sec in private_keys: - compressed = is_compressed(sec) - pkey = regenerate_key(sec) - pubkey = GetPubKey(pkey.pubkey, compressed) - keypairs[ pubkey.encode('hex') ] = sec + pubkey = public_key_from_private_key(sec) + keypairs[ pubkey ] = sec for txin in tx.inputs: @@ -404,10 +402,10 @@ class Wallet: addr = hash_160_to_bc_address(hash_160(redeem_script.decode('hex')), 5) txin['address'] = addr - elif txin.get("raw_output_script"): addr = deserialize.get_address_from_output_script(txin.get("raw_output_script").decode('hex')) sec = self.get_private_key(addr, password) + pubkey = public_key_from_private_key(sec) if sec: keypairs[pubkey] = [sec] txin['address'] = addr