python 比特币地址转换为RIPEMD160

vngu2lb8  于 2022-12-10  发布在  Python
关注(0)|答案(2)|浏览(135)

为什么结果没有保存RIPEMD160.txt给出一个错误我可以在处理器上看到代码正在工作,但文件是空的我总是得到相同的IndentationError:取消缩进不匹配任何外部缩进级别

import base58

with open('addresses.txt', 'r') as f, \
     open('RIPEMD160.txt', 'a') as i:
   for addr in f:
       addr = base58.b58decode_check(addr).encode('hex')[2:]
      
def decode(addr):

     b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

     def base58_to_dec(addr):
        dec = 0
        for i in range(len(addr)):
            dec = int(dec * 58 + b58.index(addr[i]))
        return(dec)

     def dec_to_byte(dec):
        out = ''
        while dec != 0:
            remn = mpf(dec % 256)
            dec = mpf((dec - remn) / 256)
            temp = hex(int(remn))
            if len(temp) == 3:
                temp = '0' + temp[-1]
            else:
                temp = temp[2:]
            out = temp + out

            return (out)

        dec = base58_to_dec(addr)
        out = dec_to_byte(dec)
            return (out)
tjvv9vkg

tjvv9vkg1#

因为您没有向该文件写入任何内容,并且您的代码格式不正确。

import base58

def base58_to_dec(addr):
    dec = 0
    for i in range(len(addr)):
        dec = int(dec * 58 + b58.index(addr[i]))
        return(dec)

def dec_to_byte(dec):
    out = ''
    while dec != 0:
        remn = mpf(dec % 256)
        dec = mpf((dec - remn) / 256)
        temp = hex(int(remn))
        if len(temp) == 3:
            temp = '0' + temp[-1]
        else:
            temp = temp[2:]
        out = temp + out

        return (out)

def decode(addr):

    b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
    dec = base58_to_dec(addr)
    out = dec_to_byte(dec)
    return (out)

with open('addresses.txt', 'r') as f, \
     open('RIPEMD160.txt', 'a') as i:
    for addr in f:
        addr = base58.b58decode_check(addr).encode('hex')[2:]
        ads = decode(addr)
        i.write(ads)
    i.close()
rekjcdws

rekjcdws2#

生成比特币地址的过程是这样的

public_key=>sha256(sha256(public_key))=>RIPEMD160_address=>base58_address

因此不需要其它过程,而只需如下所示将base58反转为rmd160

import base58

i =open('RIPEMD160.txt', 'a') #open file with append mode on

with open('addresses.txt', 'r') as f:#open files with addresses
    for addr in f:
        addr = addr.strip()#remove trailing space and newline character
        rmd160 = base58.b58decode_check(str(addr)).encode('hex')[2:]#notice str forcing addr to be a string
        i.write(rmd160+"\n")
    i.close()
f.close()

相关问题