matja / bitcoin-tool

Tool for converting Bitcoin keys and addresses
216 stars 118 forks source link

Bech32 possible for uncompressed key #40

Closed streamofstars closed 4 years ago

streamofstars commented 4 years ago

36 added bech32 support as input and output address format. It is now possible to generate Bech32 formatted Bitcoin address even for uncompressed public key. Example:

./bitcoin-tool \
    --input-type private-key \
    --input-format raw \
    --input-file <(echo -n sausage|openssl dgst -sha256 -binary) \
    --public-key-compression uncompressed \
    --network bitcoin \
    --output-type address \
    --output-format bech32

This will return: bc1qq5gsj64s0prnjy0qyghuhseh2v2w9w43dqsgr5 However, this is not a valid Bitcoin address! Native SegWit addresses are generated only based on compressed public key (see https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#Restrictions_on_public_key_type).

I recommend adding a warning message when trying to generate Bech32 address based on uncompressed pubkey, similar that is implemented when manually defining compression for --input-type private-key-wif --input-format base58check using --input-format base58check with --public-key-compression other than auto to override the WIF compression type is very unusual, please be sure what you are doing!

matja commented 4 years ago

Thanks for the report, I wasn't aware of this restriction. Considering the strictness of the requirement (explicitly 'must') then I think we should return an error and emit no output rather than just warn. If a valid use-case is found then an option could be introduced to override the check, named accordingly to discourage casual use.