From fcaa318f0c1019135a922158b2a4057505f3d0c3 Mon Sep 17 00:00:00 2001 From: Kefkius Date: Fri, 12 Jun 2015 22:25:44 -0400 Subject: [PATCH 1/2] Enable message signing,verifying,encrypting,and decrypting for all chains --- gui/qt/main_window.py | 6 +++--- lib/eckey.py | 5 +++-- lib/wallet.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index 66c17e0c..b7cb3229 100644 --- a/gui/qt/main_window.py +++ b/gui/qt/main_window.py @@ -38,7 +38,7 @@ from chainkey.util import format_satoshis from chainkey import Transaction from chainkey import mnemonic -from chainkey import util, bitcoin, commands, Interface, Wallet +from chainkey import util, eckey, bitcoin, commands, Interface, Wallet from chainkey import SimpleConfig, Wallet, WalletStorage from chainkey import Imported_Wallet import chainkey.chainparams @@ -2066,7 +2066,7 @@ def do_sign(self, address, message, signature, password): def do_verify(self, address, message, signature): message = unicode(message.toPlainText()) message = message.encode('utf-8') - if bitcoin.verify_message(address.text(), str(signature.toPlainText()), message): + if eckey.verify_message(address.text(), str(signature.toPlainText()), message): self.show_message(_("Signature verified")) else: self.show_message(_("Error: wrong signature")) @@ -2126,7 +2126,7 @@ def do_encrypt(self, message_e, pubkey_e, encrypted_e): message = unicode(message_e.toPlainText()) message = message.encode('utf-8') try: - encrypted = bitcoin.encrypt_message(message, str(pubkey_e.text())) + encrypted = eckey.encrypt_message(message, str(pubkey_e.text())) encrypted_e.setText(encrypted) except BaseException as e: traceback.print_exc(file=sys.stdout) diff --git a/lib/eckey.py b/lib/eckey.py index ddde80d4..96b10b46 100644 --- a/lib/eckey.py +++ b/lib/eckey.py @@ -14,7 +14,7 @@ sys.exit("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'") from util_coin import var_int, Hash -from base58 import public_key_to_bc_address +from base58 import bc_address_to_hash_160, public_key_to_bc_address # AES encryption EncodeAES = lambda secret, s: base64.b64encode(aes.encryptData(secret,s)) @@ -299,7 +299,8 @@ def verify_message(self, address, signature, message): public_key.verify_digest( sig[1:], h, sigdecode = ecdsa.util.sigdecode_string) # check that we get the original signing address - addr = public_key_to_bc_address( point_to_ser(public_key.pubkey.point, compressed) ) + addrtype = bc_address_to_hash_160(address)[0] + addr = public_key_to_bc_address( point_to_ser(public_key.pubkey.point, compressed), addrtype ) if address != addr: raise Exception("Bad signature") diff --git a/lib/wallet.py b/lib/wallet.py index 2bbd143e..be2e1a5d 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -424,7 +424,7 @@ def sign_message(self, address, message, password): assert len(keys) == 1 sec = keys[0] key = regenerate_key(sec, self.active_chain.wif_version) - compressed = is_compressed(sec) + compressed = is_compressed(sec, addrtype=self.active_chain.wif_version) return key.sign_message(message, compressed, address) def decrypt_message(self, pubkey, message, password): From 89fb24b0b79a7af5d8b80fbb53e7d33234605070 Mon Sep 17 00:00:00 2001 From: Kefkius Date: Fri, 12 Jun 2015 22:31:36 -0400 Subject: [PATCH 2/2] Move some key-related functions to eckey.py --- lib/bitcoin.py | 22 ++++++---------------- lib/eckey.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index d8fc9529..a2b84cbd 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -156,30 +156,20 @@ def ASecretToSecret(key, addrtype=128): return base58.ASecretToSecret(key, addrtype) def regenerate_key(sec, addrtype=128): - b = ASecretToSecret(sec, addrtype) - if not b: - return False - b = b[0:32] - return EC_KEY(b) + """Deprecated.""" + return eckey.regenerate_key(sec, addrtype) def is_compressed(sec, addrtype=128): """Deprecated.""" return base58.is_compressed(sec, addrtype) def public_key_from_private_key(sec, addrtype=128): - """Gets the public key of a WIF private key.""" - # rebuild public key from private key, compressed or uncompressed - pkey = regenerate_key(sec, addrtype) - assert pkey - compressed = is_compressed(sec, addrtype) - public_key = GetPubKey(pkey.pubkey, compressed) - return public_key.encode('hex') + """Deprecated.""" + return eckey.public_key_from_private_key(sec, addrtype) def address_from_private_key(sec, addrtype=0, wif_version=128): - """Gets the address for a WIF private key.""" - public_key = public_key_from_private_key(sec, wif_version) - address = public_key_to_bc_address(public_key.decode('hex'), addrtype) - return address + """Deprecated.""" + return eckey.address_from_private_key(sec, addrtype, wif_version) def is_valid(addr, active_chain=None): """Deprecated.""" diff --git a/lib/eckey.py b/lib/eckey.py index 96b10b46..423d920d 100644 --- a/lib/eckey.py +++ b/lib/eckey.py @@ -14,6 +14,7 @@ sys.exit("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'") from util_coin import var_int, Hash +import base58 from base58 import bc_address_to_hash_160, public_key_to_bc_address # AES encryption @@ -76,6 +77,29 @@ def pw_decode(s, password): ################# code from pywallet ###################### +def regenerate_key(sec, addrtype=128): + """Gets the EC Key represented by a WIF key.""" + b = base58.ASecretToSecret(sec, addrtype) + if not b: + return False + b = b[0:32] + return EC_KEY(b) + +def public_key_from_private_key(sec, addrtype=128): + """Gets the public key of a WIF private key.""" + # rebuild public key from private key, compressed or uncompressed + pkey = regenerate_key(sec, addrtype) + assert pkey + compressed = base58.is_compressed(sec, addrtype) + public_key = GetPubKey(pkey.pubkey, compressed) + return public_key.encode('hex') + +def address_from_private_key(sec, addrtype=0, wif_version=128): + """Gets the address for a WIF private key.""" + public_key = public_key_from_private_key(sec, wif_version) + address = public_key_to_bc_address(public_key.decode('hex'), addrtype) + return address + # pywallet openssl private key implementation def i2d_ECPrivateKey(pkey, compressed=False):