从比特币区块链数据结构中提取钱包ID(公钥)

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

我正在尝试找到与比特币区块的交易输入和输出相关的公钥。

谁能告诉我这些编码在哪里?

谢谢你。

blockchain bitcoin blockchain.info-api nbitcoin
2个回答
0
投票

我不能说它们都能正常工作,而且我没有进行多重签名,但这就是我使用 NBitcoin 所做的:

public static string? GetScriptPubKey(this TxOut txOut)
{
    if (txOut.ScriptPubKey.FindTemplate() is PayToWitTemplate p2w)
        return p2w.ExtractScriptPubKeyParameters(txOut.ScriptPubKey)?.GetAddress(Network.Main).ToString();

    if (txOut.ScriptPubKey.FindTemplate() is PayToScriptHashTemplate p2sh)
        return p2sh.ExtractScriptPubKeyParameters(txOut.ScriptPubKey)?.GetAddress(Network.Main).ToString();

    if (txOut.ScriptPubKey.FindTemplate() is PayToTaprootTemplate p2t)
        return p2t.ExtractScriptPubKeyParameters(txOut.ScriptPubKey)?.GetAddress(Network.Main).ToString();

    if (txOut.ScriptPubKey.FindTemplate() is PayToPubkeyHashTemplate p2pkh)
        return p2pkh.ExtractScriptPubKeyParameters(txOut.ScriptPubKey)?.GetAddress(Network.Main).ToString();

    if (txOut.ScriptPubKey.FindTemplate() is PayToPubkeyTemplate p2pk)
        return p2pk.ExtractScriptPubKeyParameters(txOut.ScriptPubKey).ToString();

    if (txOut.ScriptPubKey.FindTemplate() is PayToWitPubKeyHashTemplate p2wpkh)
        return p2wpkh.ExtractScriptPubKeyParameters(txOut.ScriptPubKey)?.GetAddress(Network.Main).ToString();

    if (txOut.ScriptPubKey.FindTemplate() is PayToWitScriptHashTemplate p2wsh)
        return p2wsh.ExtractScriptPubKeyParameters(txOut.ScriptPubKey)?.GetAddress(Network.Main).ToString();

    return null;
}

-1
投票

简单来说,一般来说你不能。

根据签名模式,您可以获得的只是该公钥的 hash,或者更糟糕的是,兑换脚本的 hash

更具体地说,在某些情况下您可以获得

some公钥。 这是您可以获得的部分列表:

    Pay-to-pubkey-hash 脚本(P2PKH):您从输入 BTC 地址的 scriptSig 中获取公钥
  1. Pay-to-pubkey (P2PK):您从 scriptPubKey 获取输出 BTC 地址的公钥
  2. Pay-to-script-hash 脚本(P2SH):您从输入 BTC 地址的 scriptSig 中获取公共
  3. keys
  4. 还有其他签名方案,在
标准

交易中,您应该能够获得源公钥目标公钥。 您可以做的是对整个区块链进行索引,并填补交易中未包含 BTC 地址及其公钥的空白。但是,例如,如果您的 BTC 地址仅出现在 P2PKH 输出中,您就无法找到该公钥。

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