我正在尝试找到与比特币区块的交易输入和输出相关的公钥。
谁能告诉我这些编码在哪里?
谢谢你。
我不能说它们都能正常工作,而且我没有进行多重签名,但这就是我使用 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;
}
简单来说,一般来说你不能。
根据签名模式,您可以获得的只是该公钥的 hash,或者更糟糕的是,兑换脚本的 hash。
更具体地说,在某些情况下您可以获得some公钥。 这是您可以获得的部分列表:
交易中,您应该能够获得源公钥或目标公钥。 您可以做的是对整个区块链进行索引,并填补交易中未包含 BTC 地址及其公钥的空白。但是,例如,如果您的 BTC 地址仅出现在 P2PKH 输出中,您就无法找到该公钥。