From 6b041e5341fe1c05392d0809ba0784d9213f3906 Mon Sep 17 00:00:00 2001 From: drakos <4613678+Jolly-Pirate@users.noreply.github.com> Date: Sat, 27 Feb 2021 23:37:29 -0500 Subject: [PATCH] Fix for Private Key (WIF-Compressed) When testing with a `private_key = '0000000000000000000000000000000000000000000000000000000000000001'` instead of `private_key = bitcoin.random_key()`, the generated Private Key (WIF-Compressed) is wrong `5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsrefhvB5QZ`, it should be `KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn`. As it turns out, the proper way to get the correct compressed WIF is to pass `'wif_compressed'` and not `'wif'` to the `bitcoin.encode_privkey` function, as pointed out in the Main.py from the bitcoin python package. ``` def encode_privkey(priv, formt, vbyte=0): if not isinstance(priv, int_types): return encode_privkey(decode_privkey(priv), formt, vbyte) if formt == 'decimal': return priv elif formt == 'bin': return encode(priv, 256, 32) elif formt == 'bin_compressed': return encode(priv, 256, 32)+b'\x01' elif formt == 'hex': return encode(priv, 16, 64) elif formt == 'hex_compressed': return encode(priv, 16, 64)+'01' elif formt == 'wif': return bin_to_b58check(encode(priv, 256, 32), 128+int(vbyte)) elif formt == 'wif_compressed': return bin_to_b58check(encode(priv, 256, 32)+b'\x01', 128+int(vbyte)) else: raise Exception("Invalid format!") ``` The fix can be done in a couple ways: `bitcoin.encode_privkey(bitcoin.decode_privkey(compressed_private_key, 'hex'), 'wif')` -> 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsrefhvB5QZ (WRONG) `bitcoin.encode_privkey(bitcoin.decode_privkey(compressed_private_key, 'hex'), 'wif_compressed')` -> KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU9FMLM39zT (WRONG) `bitcoin.encode_privkey(bitcoin.decode_privkey(compressed_private_key, 'hex_compressed'), 'wif_compressed')` -> KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn (CORRECT) `bitcoin.encode_privkey(bitcoin.decode_privkey(private_key, 'hex'), 'wif_compressed')` -> KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn (CORRECT) --- code/key-to-address-ecc-example.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/key-to-address-ecc-example.py b/code/key-to-address-ecc-example.py index 72f958a1..a5aff236 100644 --- a/code/key-to-address-ecc-example.py +++ b/code/key-to-address-ecc-example.py @@ -21,7 +21,10 @@ print("Private Key Compressed (hex) is: ", compressed_private_key) # Generate a WIF format from the compressed private key (WIF-compressed) wif_compressed_private_key = bitcoin.encode_privkey( - bitcoin.decode_privkey(compressed_private_key, 'hex'), 'wif') + bitcoin.decode_privkey(compressed_private_key, 'hex_compressed'), 'wif_compressed') +# also can be done correctly with +#wif_compressed_private_key = bitcoin.encode_privkey( +# bitcoin.decode_privkey(private_key, 'hex'), 'wif_compressed') print("Private Key (WIF-Compressed) is: ", wif_compressed_private_key) # Multiply the EC generator point G with the private key to get a public key point