1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-21 23:58:09 +00:00
bitcoinbook/code
drakos 6b041e5341
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)
2021-02-27 23:37:29 -05:00
..
addr.cpp code fixes to update to libbitcoin v3 2017-07-18 12:07:31 +08:00
bip-table.py print() function for Python 3 2017-08-24 18:14:02 +02:00
ec-math.py Update ec-math.py 2019-01-01 16:58:31 +00:00
extract-from-pk-script.go Extract info from pk script example 2014-07-31 20:21:26 +03:00
get-utxo.py whitespace fixes to placate flake8 2018-02-03 01:33:46 +01:00
hash_example.py fixed hashlib library error for python3 2018-03-16 20:32:21 +05:30
key-to-address-ecc-example.py Fix for Private Key (WIF-Compressed) 2021-02-27 23:37:29 -05:00
max_money.py max_money.py: use variable start_block_reward (eliminate magic number) 2018-05-19 09:10:18 -04:00
merkle.cpp code fixes to update to libbitcoin v3 2017-07-18 12:07:31 +08:00
p2wpkh.js transaction fees 2016-12-12 15:00:27 +02:00
pay-to-addr-script.go Handle error when decoding address 2014-07-26 15:58:56 +03:00
proof-of-work-example.py fixed hashlib library error for python3 2018-03-16 20:32:21 +05:30
pycoin_example.py whitespace fixes to placate flake8 2018-02-03 01:33:46 +01:00
rpc_block.py whitespace fixes to placate flake8 2018-02-03 01:33:46 +01:00
rpc_example.py Rework ch03 for most recent version of Bitcoin Core 2017-10-19 22:35:15 +02:00
rpc_transaction.py ch03 rpc API, code examples, libraries 2016-02-04 20:52:15 -06:00
satoshi-words.cpp code fixes to update to libbitcoin v3 2017-07-18 12:07:31 +08:00
select-utxo.py long() and print() for Python 3 2017-08-24 18:44:48 +02:00
vanity-miner.cpp Stop using numeric_limits for the constant "255", and fix a bug. 2018-03-25 20:17:35 -07:00
websocket-example.go Add websocket example 2014-08-07 15:01:22 +03:00