diff --git a/src/mining/coinbase.go b/src/mining/coinbase.go index d375289..cf6c20d 100644 --- a/src/mining/coinbase.go +++ b/src/mining/coinbase.go @@ -47,17 +47,25 @@ var WitnessMagicBytes = []byte{ 0xed, } +/* + What parts are all involved in witness commitment? + 1. Calculation of Merkle root + 2. Witness Commitment = HASH256(witnessMerkleRoot | witnessNonce) + 3. Add (Witness Magic Bytes | Witness commitment) as the ScriptPubKey of the last output in the coinbase transaction + +*/ func AddWitnessCommitmentX(coinbaseTx *txn.Transaction, blockTxns []*txn.Transaction) []byte { var witnessNonce [32]byte coinbaseTx.Vin[0].Witness = []string{hex.EncodeToString(witnessNonce[:])} - fmt.Printf("\n\nSerialized coinbase tx: %x\n\n", blockTxns[0].RawHex()) + fmt.Printf("\n\nSerialized tx: %x\n\n", blockTxns[1].RawHex()) // Next, obtain the merkle root of a tree which consists of the // wtxid of all transactions in the block. The coinbase // transaction will have a special wtxid of all zeroes. witnessMerkleRoot := CalcMerkleRoot(blockTxns, true) + fmt.Printf("Witness Merkle Root: %x\n", witnessMerkleRoot) // The preimage to the witness commitment is: // witnessRoot || coinbaseWitness @@ -71,13 +79,13 @@ func AddWitnessCommitmentX(coinbaseTx *txn.Transaction, // OP_DATA_36 {0xaa21a9ed || witnessCommitment}. The leading // prefix is referred to as the "witness magic bytes". witnessCommitment := utils.DoubleHashRaw(witnessPreimage[:]) - witnessScript := append(WitnessMagicBytes, witnessCommitment[:]...) + // witnessScript := append(WitnessMagicBytes, witnessCommitment[:]...) // Finally, create the OP_RETURN carrying witness commitment // output as an additional output within the coinbase. commitmentOutput := txn.Vout{ Value: 0, - ScriptPubKey: hex.EncodeToString(witnessScript), + ScriptPubKey: hex.EncodeToString(witnessCommitment[:]), } coinbaseTx.Vout = append(coinbaseTx.Vout, commitmentOutput) @@ -85,40 +93,5 @@ func AddWitnessCommitmentX(coinbaseTx *txn.Transaction, return witnessCommitment[:] } -/* - What parts are all involved in witness commitment? - 1. Calculation of Merkle root - 2. Witness Commitment = HASH256(witnessMerkleRoot | witnessNonce) - 3. Add (Witness Magic Bytes | Witness commitment) as the ScriptPubKey of the last output in the coinbase transaction -*/ -func AddWitnessCommitment(coinbase *txn.Transaction, txns []*txn.Transaction) error { - var witnessNonce [32]byte - coinbase.Vin[0].Witness = append(coinbase.Vin[0].Witness, WitnessReserveHexString) - // assuming that the merkle root is right - witnessMerkleRoot := CalcMerkleRoot(txns, true) - - var witnessPreimage [64]byte - copy(witnessPreimage[:32], witnessMerkleRoot[:]) - copy(witnessPreimage[32:], witnessNonce[:]) - witnessCommitment := utils.DoubleHashRaw(witnessPreimage[:]) - witnessCommitment = [32]byte(witnessCommitment[:]) - witnessScript := []byte{ - // OP_RETURN - 0x6a, - // OP_DATA36 - 0x24, - 0xaa, - 0x21, - 0xa9, - 0xed, - } - witnessScript = append(witnessScript, witnessCommitment[:]...) - witnessOut := txn.Vout{} - witnessOut.Value = 0 - witnessOut.ScriptPubKey = hex.EncodeToString(witnessScript) - coinbase.Vout = append(coinbase.Vout, witnessOut) - - return nil -} diff --git a/src/mining/merkle_tree.go b/src/mining/merkle_tree.go index da1eb4e..21837bc 100644 --- a/src/mining/merkle_tree.go +++ b/src/mining/merkle_tree.go @@ -87,7 +87,7 @@ func (s *rollingMerkleTreeStore) calcMerkleRoot(adds []*transaction.Transaction, s.add(zeroHash) case witness: s.add([32]byte(adds[i].WitnessHash())) - fmt.Printf("Witness Hash: %x", adds[i].WitnessHash()) + fmt.Printf("Witness Hash: %x, I: %d \n", adds[i].WitnessHash(), i) default: s.add([32]byte(adds[i].TxHash())) } diff --git a/src/mining/mining.go b/src/mining/mining.go index c73b606..59285b6 100644 --- a/src/mining/mining.go +++ b/src/mining/mining.go @@ -90,7 +90,7 @@ func findNonce(candidateBlock *Block) uint32 { hash := utils.DoubleHashRaw(w.Bytes()) - // fmt.Printf("Hash: %x\n", hash) + // fmt.Printf("Hash: %x\n", hash) // fmt.Printf("Hash Value: %d\n", HashToBig(&hash)) // fmt.Printf("Target Value: %d\n", NbitsToTarget(candidateBlock.BlockHeader.Bits))