Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable message signing/verifying/encrypting/decrypting for all chains. #113

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 6 additions & 16 deletions lib/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
29 changes: 27 additions & 2 deletions lib/eckey.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
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
import base58
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))
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -299,7 +323,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")

Expand Down
2 changes: 1 addition & 1 deletion lib/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down