将 C 或 UC 地址转换为哈希值在 python 中返回错误结果

问题描述 投票:0回答:1

我创建了一个 Python 程序,可以将 C 或 UC 地址转换为哈希值。但是,当我输入输入地址时,程序返回了错误的哈希值。 我想将

C ADDRESS: 14EWK38NoTu8MDeCf5zWWjXyE7F4xU9S9S Or UC ADDRESS: 1AfaeyabCd51T45Sg7TdFsYJ2feBxBuJ7X
转换为:
c6eae793d27105b3ed165ca7fa76360e9d05e2f3

但是我得到了这个值

bee6402dd3500304c9b1ec2b9ecc9f1044bf4980
这是代码:

import hashlib, base58

def convert_bitcoin_address_to_hash(bitcoin_address):
    # Try to decode the address from base58 encoding, and catch any exceptions
    try:
        if bitcoin_address.startswith('C ADDRESS:'):
            decoded = base58.b58decode_check(bitcoin_address[len('C ADDRESS:'):].strip())
        elif bitcoin_address.startswith('UC ADDRESS:'):
            decoded = base58.b58decode_check(bitcoin_address[len('UC ADDRESS:'):].strip())
        else:
            raise ValueError('Invalid Bitcoin address format')
    except Exception as e:
        # If an exception is caught, print an error message and return None
        print('Invalid input: ' + str(e))
        return None

    # Compute the hash (double SHA-256) and take the first 20 bytes as the hash160
    hash160 = hashlib.new('ripemd160', hashlib.sha256(decoded).digest()).digest()

    return hash160

address = str(input('Please enter the C or UC Address: '))
hash = convert_bitcoin_address_to_hash(address)
if hash is not None:
    print(hash.hex())
else:
    if address.startswith('C ADDRESS:'):
        print('Invalid C ADDRESS format')
    elif address.startswith('UC ADDRESS:'):
        print('Invalid UC ADDRESS format')
    else:
        print('Invalid input format')

我想准确地得到这个结果 c6eae793d27105b3ed165ca7fa76360e9d05e2f3,这是有效的哈希值。请有人告诉我我做错了什么?

python hash checksum hashlib base58
1个回答
-2
投票

有人吗?我需要紧急修复它

© www.soinside.com 2019 - 2024. All rights reserved.