简体   繁体   中英

Generating Blockchain header is failing with error

I have been trying process a block chain header when the code is stopping with the below error -

Traceback (most recent call last): File "C:\Users\SHAHRIAR\AppData\Local\Programs\Python\Python37-32\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) TypeError: a bytes-like object is required, not 'str'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "f:/PYTHON/blockchain/index.py", line 19, in struct.pack('<L', blockChainObject['bits']) + TypeError: decoding with 'utf-8' codec failed (TypeError: a bytes-like object is required, not 'str')

This is my full code -

import hashlib
import codecs
import struct

blockChainObject = {
    'version':536871426,
    'previousHash':'aa11661d07d7e13b94403bc00a9786b07fe711140743f0f9d7e35a478d80e840',
    'merkleRoot':'a41494afe694e450a7163103fd08ea3d4b5fcb30556165f6e567893989c39222',
    'bits': 0x19030d6c,
    'time':1610982871,
    'nonce':3341292488
}

blockChainHeader = (
    struct.pack('<L', blockChainObject['version']) + 
    codecs.decode(blockChainObject['previousHash'])[::-1] + 
    codecs.decode(blockChainObject['merkleRoot'])[::-1] + 
    struct.pack('<L', blockChainObject['time']) + 
    struct.pack('<L', blockChainObject['bits']) + 
    struct.pack('<L', blockChainObject['nonce'])
)

blockChainHashObject = hashlib.sha256(blockChainHeader).digest()
blockChainHashDigest = hashlib.sha256(blockChainHashObject).hexdigest()

print(blockChainHashDigest)

I am running Python 3.7.2 on Win 7 (64-Bit)

I am asking for expert's advice, what am I doing wrong?

You have to convert argument of decode functions to bytes . Also struct.pack . Also struct.pack returns bytes and codecs.decode returns string so have to convert results of decode to bytes also.

import hashlib
import codecs
import struct
import sys

blockChainObject = {
    'version':536871426,
    'previousHash': b'aa11661d07d7e13b94403bc00a9786b07fe711140743f0f9d7e35a478d80e840',
    'merkleRoot': b'a41494afe694e450a7163103fd08ea3d4b5fcb30556165f6e567893989c39222',
    'bits': 0x19030d6c,
    'time':1610982871,
    'nonce':3341292488
}


blockChainHeader = (
    struct.pack('<L', blockChainObject['version']) +
    bytes(codecs.decode(blockChainObject['previousHash'])[::-1], 'utf-8') +
    bytes(codecs.decode(blockChainObject['merkleRoot'])[::-1], 'utf-8') +
    struct.pack('<L', blockChainObject['time']) +
    struct.pack('<L', blockChainObject['bits']) +
    struct.pack('<L', blockChainObject['nonce'])
)

blockChainHashObject = hashlib.sha256(blockChainHeader).digest()
blockChainHashDigest = hashlib.sha256(blockChainHashObject).hexdigest()

print(blockChainHashDigest)

Result: 2d2a436603cca71be4c27b51e5e1aa7911cc6cf4ff5ad67a13c17ab7127d7f85

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM