2019-05-30-继续研究NBitcoin

最近的目的是学习《programmingblockchain》https://programmingblockchain.gitbook.io/programmingblockchain/bitcoin_transfer/private_key,掌握调用比特币接口,创建比特币交易的方法。

之前 已经完成了三小节的内容:
-Bitcoin address
-ScriptPubKey
-Private key
复习了比特币系统中,公钥、私钥、公钥哈希、bitcoinsecret、scriptPubkey的知识,并基于Nbitcoin项目,对这些知识进行了编码、测试。

今天的目的是,学习“transaction”章节。
transaction是比特币系统中的最基础的数据结构。每一个transaction记录了一次通信内容(也可以成为交易数据)。一个时间段的transaction打包构成1个区块,所有交易构成的区块按照时间顺序有序组织,形成了区块链账本。区块链中所有的共识机制、密码学机制、验证机制都是为了确保transaction数据能够准确、可靠、合理的存储,并能够有效的共享给区块链网络中的所有节点。

从transaction往底层看,能够看到哈希、非对称加密算法、数据结构、时间戳等基础技术。
从transaction往上层看,能够看到P2P协议、共识机制、博弈论等上层算法。

学习本章,一方面复习相关知识,另一方面,希望在程序中实际构造一个区块链交易。

(Mastering Bitcoin) Transactions are the most important part of the bitcoin system. Everything else in bitcoin is designed to ensure that transactions can be created, propagated on the network, validated, and finally added to the global ledger of transactions (the blockchain). Transactions are data structures that encode the transfer of value between participants in the bitcoin system. Each transaction is a public entry in bitcoin’s blockchain, the global double-entry bookkeeping ledger.

Everything else in bitcoin is designed to ensure that transactions can be created, propagated on the network, validated, and finally added to the global ledger of transactions (the blockchain).

A transaction may have no recipient, or it may have several. The same can be said for senders! On the Blockchain, the sender and recipient are always abstracted with a ScriptPubKey, as we demonstrated in previous chapters.

需要注意的是,在比特币交易中,不是直接存储发送方和接收方的比特币地址Bitcoin address,而是存储对应的ScriptPubKey

Bitcoin address --相互转化-- public key hash --相互转化-- ScriptPubKey

更具体的,一个比特币交易中没有存储发送方的ScriptPubKey,而是存储了这笔资金的来源交易哈希值及索引值。根据这两个参数,能够找到发送方真实的ScriptPubKey

扫描二维码关注公众号,回复: 6488921 查看本文章

疑问:每一个节点收到比特币交易之后,需要检测交易是否合法,其中重要的一个检测内容就是检测交易中的输入资金的签名是否正确(即发送方是否有资格使用这笔资金)。检测方法:

当前交易的签名信息=签名验证算法(发送方的公钥,当前交易的待签名数据)
从此处可知,验证签名时需要发送方的公钥,但是,在交易中只能找到发送方的ScriptPubKey,即公钥的哈希值。由于哈希算法无法逆向执行,所以验证时如何在没有公钥,只有公钥哈希的条件下完成签名验证了?
猜测:签名验证算法中,本来就需要对公钥进行哈希。因此,只需要从ScriptPubKey中提取公钥的哈希即可。

注意:publicKeyHash=RIPEMD160 (SHA256(public key))
签名算法中应该不包含 RIPEMD160吧?
此处很疑惑,需要进一步研究。

Note: The Transaction ID is defined by SHA256(SHA256(txbytes))
Note: Do NOT use the Transaction ID to handle unconfirmed transactions. The Transaction ID can be manipulated before it is confirmed. This is known as “Transaction Malleability.”

Transaction ID==SHA256(SHA256(txbytes))
txbytes具体包括交易中的哪些内容了?包含签名数据么?

编程练习
利用QBitNinja.Client 提供的API获取指定哈希值的交易原始数据。

var transactionId = uint256.Parse("f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94");
// Query the transaction
GetTransactionResponse transactionResponse = client.GetTransaction(transactionId).Result;

NBitcoin.Transaction transaction = transactionResponse.Transaction;
Console.WriteLine(transactionResponse.TransactionId); // f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94
Console.WriteLine(transaction.GetHash()); // f13dc48fb035bbf0a6e989a26b3ecb57b84f85e0836e777d6edf60d87a4a2d94

QBitNinja.Client.Models和NBitcoin是两个库。
NBitcoin.Transaction transaction = transactionResponse.Transaction;
这句代码是在两个库之间进行数据转换。

这两个库都可以完成查看交易具体内容的功能。选用一种即可。准备采用NBitcoin的代码模式。

The terms TxOut, Output and out are synonymous.
Not to be confused with OutPoint, but more on this later.
In summary, the TxOut represents an amount of bitcoin and a ScriptPubKey. (Recipient)

TxOut 包括金额和接收方脚本。

Every TxOut is uniquely addressed at the blockchain level by the ID of the transaction which include it and its index inside it. We call such reference an Outpoint.

TxOut被区块链交易ID和索引值唯一确定,这样的引用关系被成为Outpoint

TxOut类似于未盖章的收据;
Outpoint类似于盖章和收据;

Outpoint包含交易ID和索引值,一个Outpoint唯一确定一个TxOut

The TxIn is composed of the Outpoint of the TxOut being spent and of the ScriptSig (we can see the ScriptSig as the “Proof of Ownership”). In our transaction there are actually 9 inputs.

TxIn由两部分组成:Outpoint 和ScriptSig
Outpoint 指定了资金来源
ScriptSig 指定资金的所有权

You should note that a coinbase transaction is the only transaction where the total output value is larger than the total input value. This effectively correspond to coin creation. So by definition there is no fee in a coinbase transaction. The coinbase transaction is the first transaction of every block.
The consensus rules enforce that the sum of output's value in the coinbase transaction does not exceed the sum of transaction fees in the block plus the mining reward.

coinbase交易中,输入金额小于输出金额。(输入金额是0)
其他交易中,共识机制强迫,coinbase的所有输出金额不能超过(当前区块的手续费之和与挖矿奖励)。
coinbse的输出金额<=区块手续费+挖矿奖励。
疑问:应该不会出现小于的情况吧?如果小于,意味着一部分资金消失了。
猜测:比如旷工只挖矿,但是不写入输出地址。此种情况,就满足小于的情况。

学习完本章内容了,主要是介绍了tansaction的组成元素,包括txOut,txIn,Outpoint 等。
本章没有练习生成交易的方法。

猜你喜欢

转载自blog.csdn.net/weixin_34061555/article/details/90775507