Merged
Conversation
ribasushi
approved these changes
May 25, 2020
vmx
approved these changes
May 25, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This completes the set needed to interact with the full Bitcoin graph with IPLD. The Witness Commitment is the link to the second binary merkle that contains the transactions with full witness data, whereas the initial binary merkle pointed to from the block header (known as
merkleroot) points to the transactions without witness data. Since SegWit, this second binary merkle tree gets linked to from the coinbasescriptPubKeyproperty and the coinbase is excluded from the binary merkle (replaced with0x000...000) as a result. The reason we need this new element is that the coinbase points to an intermediate structure which concatenates a nonce and the newmerkleroottogether and the nonce, in the standard representation, becomes the "witness" property of the coinbase itself. So to reconstruct the original full block graph structure you have to navigate to the transactions in the witness merkle and on the way pick up this nonce to re-attach it to the coinbase. This Witness Commitment is 64-bytes long and otherwise looks like a node in the normal binary merkle, but the leftmost side is the nonce (which happens to be0x000...000on most blocks because Bitcoin Core does that, but other miners are allowed to insert random strings here and there are so far ~4000 of these in the blockchain).Decoding a
bitcoin-witness-commitmentyields a:Where a
MaybeTransactionis a union that could be an actual transaction or a node in a binary merkle tree, which are also 64-bytes long but look like this:Another notable problem that makes a dedicated codec for this helpful is that there are pre-SegWit blocks with a coinbase
scriptPubKeythat look like they point to a witness commitment and there's no firm way to determine whether it is one or not unless you try to load it (from wherever these blocks are coming from). A failure to load an0xb2can be a signal that the presumed witness commitment is actually not a witness commitment. A failure to load anything else can be firmly interpreted as a failure to locate something that should exist.This is implemented and in use in the code @ ipld/js-ipld-bitcoin#63 (will likely be made into a separate repo for
@ipld/bitcoinwith the js-ipld-bitcoin repo being the k