1200wd / bitcoinlib

Bitcoin and other Cryptocurrencies Library for Python. Includes a fully functional wallet, Mnemonic key generation and management and connection with various service providers to receive and send blockchain and transaction information.
http://bitcoinlib.readthedocs.io/
GNU General Public License v3.0
596 stars 199 forks source link

Base58 works incorrectly in case of 3 leading zero bytes #154

Closed dimaa6 closed 3 years ago

dimaa6 commented 3 years ago

from bitcoinlib import keys k = keys.Key("L1pQn89VyJ7hTCTRLeuFMEpW4ZDqR9s2JnQMzhukBjAHme8NtD2d") print(k.address(script_type="p2pkh", encoding="base58"))

Outputs:

1111GxfgFVyDW3zcFpUF1upSZoL7GCRiLk

This is incorrect, one leading 1 is extra. Public key hash is: 00003acd8f60b766e48e9db32093b419c21de7e9

Together with version prefix 00 this gives 3 leading zero bytes. Base58 encoding rules in the area of leading zeroes are: add one 1 for each leading zero byte. Thus, we must have 3, not 4 leading 1s. Base58 encoding in other libraries produce correct address: 111GxfgFVyDW3zcFpUF1upSZoL7GCRiLk

mccwdev commented 3 years ago

You are right, thanks. Fixed in https://github.com/1200wd/bitcoinlib/commit/1d315667020c0d26fdbcf34d645cdd5fdd95d7b5