0

I have checked related questions and did not find answer. I also tried to use "secp256k1", "bip-0032", "seedrecover" and did not succeed.

When my input is a private key in the format "'xprv'..." I get problems related to decoding issues, or length of the key (like in the module 'ecdsa' I get 'ecdsa.keys.MalformedPointError: Invalid length of private key, received 77, expected 32') and so on.

I am asking for simple python-3 code snippet to get the public key and address or a simple mathematical explanation so I can code it myself. I know that bip32 is related to deriving keys and addresses but I do not need the derived ones, but the ones which are corresponding to the given private key.

A code that work for other private keys and do not work here:

def get_public_from_private(private_key):

#   Checks if the input is in valid hexadecimal format, if it is not hexadecimal but it is base58,
#   it converts it to hexadecimal, otherwise it returns an error.

if not utilities.is_hexadecimal(private_key):
    if utilities.is_base58check(private_key):
        private_key = Bitcoin().__convert_private_wif_to_hex(private_key)
    else:
        raise Exception('From Bitcoin::__get_public_from_private::Exception caught: ' + "the key format is "
                                                                                        "not hexadecimal")
#   From that point, key is valid hexadecimal private key.
private_key_bytes = codecs.decode(private_key, 'hex')
# Get ECDSA public key
key = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1).verifying_key
key_bytes = key.to_string()
key_hex = codecs.encode(key_bytes, 'hex')
# Add bitcoin byte
bitcoin_byte = b'04'
public_key = bitcoin_byte + key_hex
return public_key.decode("utf-8")
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.