jimmysong / programmingbitcoin

Repository for the book
Other
1.74k stars 655 forks source link

Chapter 4 - checksum should be double hashed? #277

Open ArmanTheParman opened 11 months ago

ArmanTheParman commented 11 months ago

On page 83 (chapter 4 under "Address Format", there is this code:

def encode_base58_checksum(b): return encode_base58(b + hash256(b)[:4])

I believe the data should be hashed twice before extracting the checksum. Also, hash256 isn't part of hashlib library, I used sha256. I suppose if hash256 is a custom double sha256 function, then nothing to see here, move along folks.

I'm getting the right answer for the test with this code:

def base58check_encode(b): checksum = hashlib.sha256(hashlib.sha256(b).digest()).digest()[:4] return base58.b58encode(b + checksum)

Gaming32 commented 9 months ago

hash256 is defined like this in helper.py:

def hash256(s):
    '''two rounds of sha256'''
    return hashlib.sha256(hashlib.sha256(s).digest()).digest()

The book never shows the implementation, but it's in the helper.py of each chapter folder in this repository.