06.07 「区块链研究实验室」比特币地址协议ScriptPubKey

「区块链研究实验室」比特币地址协议ScriptPubKey

就区块链而言,最重要的是并不是比特币地址,而是比特币协议通过ScriptPubKey标识比特币的接收者。

「区块链研究实验室」比特币地址协议ScriptPubKey

ScriptPubKey如下所示:

OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG

它是一个简短的脚本,解释了要求拥有比特币的条件。我们可以从比特币地址生成ScriptPubKey。 这是所有比特币客户端友好的将比特币地址转换为区块链可读地址的一个步骤。

「区块链研究实验室」比特币地址协议ScriptPubKey

var publicKeyHash = new KeyId("14836dbe7f38c5ac3d49e8d790af808a4ee9edcf");

var testNetAddress = publicKeyHash.GetAddress(Network.TestNet);

var mainNetAddress = publicKeyHash.GetAddress(Network.Main);

Console.WriteLine(mainNetAddress.ScriptPubKey);

// OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG

Console.WriteLine(testNetAddress.ScriptPubKey);

// OP_DUP OP_HASH160 14836dbe7f38c5ac3d49e8d790af808a4ee9edcf OP_EQUALVERIFY OP_CHECKSIG

注意testnet和mainnet地址的ScriptPubKey是一样的吗?

注意ScriptPubKey包含公钥的哈希值吗?

我们不详细讨论细节,但请注意,ScriptPubKey是与Bitcoin地址无关,但它确实显示了公钥的散列。

比特币地址由版本字节组成,该版本字节标识网络在哪里使用该地址以及公钥的散列。 所以我们可以后退并从ScriptPubKey和网络标识符生成比特币地址。

var paymentScript = publicKeyHash.ScriptPubKey;

var sameMainNetAddress = paymentScript.GetDestinationAddress(Network.Main);

Console.WriteLine(mainNetAddress == sameMainNetAddress); // True

也可以从ScriptPubKey中检索哈希值并从中生成一个比特币地址:

var samePublicKeyHash = (KeyId) paymentScript.GetDestination();

Console.WriteLine(publicKeyHash == samePublicKeyHash); // True

var sameMainNetAddress2 = new BitcoinPubKeyAddress(samePublicKeyHash, Network.Main);

Console.WriteLine(mainNetAddress == sameMainNetAddress2); // True

因此,现在您了解了私钥,公钥,公钥哈希,比特币地址和ScriptPubKey之间的关系。

来源:区块链研究实验室(bc-tech-lab)


分享到:


相關文章: