2

This is how we can find the WIF from a private key:

private_key = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
decoded_private_key = bitcoin.decode_privkey(private_key, 'hex')
wif = bitcoin.encode_privkey(decoded_private_key, 'wif')
# 5HpHgWkLaovGWySEFpng1XQ6pdG1TzNWR7SrETvfTRVdKHNXZh8

But how to get the WIF compressed?

Should I do:

private_key = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
compressed_private_key = private_key + '01'
wifcomp1 = bitcoin.encode_privkey(bitcoin.decode_privkey(private_key, 'hex'), \
             'wif_compressed')
# KwDidQJHSE67VJ6MWRvbBKAxhD3F48DvqRT6JRqrjd7MHLBjGF7V

or

wifcomp2 = bitcoin.encode_privkey(bitcoin.decode_privkey(compressed_private_key, 'hex'), \ 
            'wif_compressed')
# KwFfpDsaF7yxCELuyrH9gP5XL7TAt5b9HPWC1xCQbmrxfFzfMakg

It seems that #1 is correct and not #2.


Also it seems that:

  • the non-compressed WIF, imported in Electrum, produces finally a non-compressed Base58 address,

  • the compressed WIF, imported in Electrum, produces finally a compressed Base58 address

which seems obvious. But I'd like to be sure that compression of WIF and compression of addresses at the end is really linked.

Can we conclude that the use of a compressed or non-compressed WIF (both encoding the same private key) will determine, when importing them in Electrum, if the public-key (and thus the address eventually) will finally be compressed or non-compressed?

1
  • 1
    This answers the second part of the question: "[WIF compressed key] are not compressed private keys. They are private keys with a marker that indicates that their corresponding public should be compressed. That extra marker takes an extra byte."
    – Basj
    Jan 10, 2018 at 2:48

1 Answer 1

3

It seems that #1 is correct and not #2.

Yes, #1 is correct. The encoder will add the 01 flag byte to the end of the private key for you. By doing that manually in #2, you will result in the private key having an additional byte which is incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.