繁体   English   中英

如何将 scriptPubKey 字节转换为比特币地址

[英]How to convert scriptPubKey bytes to Bitcoin address

我目前正在尝试使用pyblockchain解析区块链。 我的问题是我无法正确编码scriptPubKey - 尽管我不知道我可能做错了什么。

下面你可以看到我如何遍历区块链:

from blockchain.reader import BlockchainFileReader

import hashlib
import base58

block_reader = BlockchainFileReader('/media/Data/btc/blocks/blk00325.dat')

count = 0

for block in block_reader:    
    count +=1        
    for t in block.transactions:
        for outp in t.outputs:
            addr = base58.b58encode(outp.script_pub_key)
            if addr.startswith('1'):
                print(addr)        
    if count >= 5:
        break

如果我在我的 Jupyter 笔记本中查看outp ,我会找到

outp.script_pub_key
>> b'v\xa9\x14\x1e\xbev\x83\xceJd\xad\xc9\x17\xe9\xb1\x93\x7f\x12&Q\xcb\xab\xa1\x88\xac'

这个:

base58.b58encode(outp.script_pub_key)
>> 'pkJBVCg6k54E7ZiP7cvxbCvtN9aY9zEcgK'

这不是有效的比特币地址

显然,比特币地址应该被编码为Base58Check - 但是,这也不起作用:

base58.b58encode_check(outp.script_pub_key)
>> '6PSJQapdQn8VeG9SBuZdH8q2ysyP4ND6dmspzLZb'

那么我在这里做错了什么?

对于 C++ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)这里的代码中直接有bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet) https://github.com/bitcoin/bitcoin/blob/0cda5573405d75d695aba417e8f22f1301ded001/src/script/standard.cpp#L

因此,解决方案看起来很简单

const CScript & scriptPublicKey = block.vtx[ i ]->vout[ j ].scriptPubKey ;
CTxDestination destination ;
std::string address = "unknown" ;
if ( ExtractDestination( scriptPublicKey, destination ) )
    address = CBitcoinAddress( destination ).ToString() ;

至于Python..问题是首先将ExtractDestination逻辑转换为Python的语言,我敢打赌

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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