Example #1
0
        public static SlimBlockHeader ToSlimBlockHeader(this Block block)
        {
            var slimBlockHeader = new SlimBlockHeader
            {
                Version       = block.Header.Version,
                HashPrevBlock = block.Header.HashPrevBlock.ToBytes(),
                Bits          = block.Header.Bits,
                MerkleRoot    = MerkleRoot.Build(block.Transactions.Select(x => x.GetHash().ToBytes()).ToList()),
                Timestamp     = (uint)block.Header.BlockTime.ToUnixTimeSeconds(),
                Nonce         = 0,
            };

            return(slimBlockHeader);
        }
Example #2
0
        static byte[] BlockWitnessMerkleRoot(List <Transaction> payloadTransactions)
        {
            var leaves = new List <byte[]>();

            // The witness hash of the coinbase transaction is 0.
            leaves.Add(new byte[32]);

            foreach (Transaction tx in payloadTransactions)
            {
                leaves.Add(tx.GetWitHash().ToBytes());
            }

            // A witness root hash is calculated with all those wtxid as leaves, in a way similar to the hashMerkleRoot in the block header.
            return(MerkleRoot.Build(leaves));
        }
Example #3
0
        public static void UpdateHeaderHashMerkleRoot(this SlimBlock slimBlock)
        {
            var txHashes = new List <byte[]>();

            txHashes.Add(slimBlock.CoinbaseTransaction.GetHash().ToBytes());
            if (slimBlock.IsProofOfStake)
            {
                txHashes.Add(slimBlock.CoinstakeTransaction.GetHash().ToBytes());
            }
            foreach (var transaction in slimBlock.PayloadTransactions)
            {
                txHashes.Add(transaction.GetHash().ToBytes());
            }
            var hashMerkleRoot = MerkleRoot.Build(txHashes);

            slimBlock.SlimBlockHeader.MerkleRoot = hashMerkleRoot;
            slimBlock.SlimBlockHeader.Data       = slimBlock.SlimBlockHeader.SerializeTo80Bytes();
        }