programmingbitcoin icon indicating copy to clipboard operation
programmingbitcoin copied to clipboard

Chapter 4 - checksum should be double hashed?

Open ArmanTheParman opened this issue 2 years ago • 1 comments

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)

ArmanTheParman avatar Dec 12 '23 14:12 ArmanTheParman

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.

Gaming32 avatar Feb 01 '24 21:02 Gaming32