Ejemplo n.º 1
0
        public string GetMerkleRoot()
        {
            List <string> bottom       = new List <string>();
            List <string> middle       = new List <string>();
            List <string> top          = new List <string>();
            string        merkleRoot   = "";
            string        hashOfBottom = "";
            string        hashOfMiddle = "";
            string        hashOfTop    = "";

            // Hash all transactions first
            for (int i = 0; i < transactionList.Length; i++)
            {
                bottom.Add(BlockchainFunctions.SHA256(BlockchainFunctions.Stringify(transactionList[i]))); //add the has to the bottom list
                //Console.WriteLine("-" + BlockchainFunctions.Stringify(bottom));
            }

            for (int i = 0; i < bottom.ToArray().Length; i++)
            {
                //Check if i == to the last element of the array and if the array is has odd number of elements
                if (i == bottom.ToArray().Length - 1 && (i + 1) % 2 != 0)
                {
                    hashOfBottom += bottom.ToArray()[i] + bottom.ToArray()[i];
                    middle.Add(BlockchainFunctions.SHA256(hashOfBottom));

                    Console.WriteLine("#1");

                    if (middle.ToArray().Length == 1)
                    {
                        merkleRoot = middle.ToArray()[i];
                        Console.WriteLine("#2");
                        return(merkleRoot);
                    }
                }
                // Hash the hashes of the bottom list and put it in an array
                if (i > 0 && (i + 1) % 2 == 0)
                {
                    hashOfBottom += bottom.ToArray()[i - 1];
                    hashOfBottom += bottom.ToArray()[i];
                    middle.Add(BlockchainFunctions.SHA256(hashOfBottom));
                    hashOfBottom = "";
                }
            }


            for (int i = 0; i < middle.ToArray().Length; i++)
            {
                //Check if i == to the last element of the array and if the array is has odd number of elements
                if (i == middle.ToArray().Length - 1 && (i + 1) % 2 != 0)
                {
                    hashOfMiddle += middle.ToArray()[i] + middle.ToArray()[i];
                    top.Add(BlockchainFunctions.SHA256(hashOfMiddle));
                    Console.WriteLine("#3");

                    if (top.ToArray().Length == 1)
                    {
                        merkleRoot = top.ToArray()[i];
                        Console.WriteLine("#4");
                        return(merkleRoot);
                    }
                }

                // Hash the hashes of the bottom list and put it in an array
                if (i > 0 && (i + 1) % 2 == 0)
                {
                    hashOfMiddle += middle.ToArray()[i - 1];
                    hashOfMiddle += middle.ToArray()[i];
                    top.Add(BlockchainFunctions.SHA256(hashOfBottom));
                    hashOfMiddle = "";
                }
            }


            for (int i = 0; i < top.ToArray().Length; i++)
            {
                //Check if i == to the last element of the array and if the array is has odd number of elements
                if (i == top.ToArray().Length - 1 && (i + 1) % 2 != 0)
                {
                    hashOfTop += top.ToArray()[i] + top.ToArray()[i];
                    merkleRoot = BlockchainFunctions.SHA256(hashOfTop);
                    Console.WriteLine("#5");
                    if (top.ToArray().Length == 1)
                    {
                        Console.WriteLine("#6");
                        return(merkleRoot);
                    }
                }

                // Hash the hashes of the bottom list and put it in an array
                if (i > 0 && (i + 1) % 2 == 0)
                {
                    hashOfTop += middle.ToArray()[i - 1];
                    hashOfTop += middle.ToArray()[i];
                    merkleRoot = BlockchainFunctions.SHA256(hashOfTop);
                    Console.WriteLine("#7");
                    return(merkleRoot);
                }
            }
            return(merkleRoot);
        }
Ejemplo n.º 2
0
 //-----Functions
 public string CalculateHash()
 {
     return(BlockchainFunctions.SHA256(header.blockNumber + header.previousHash + header.merkleRoot + header.timestamp + header.target + header.nonce));
 }