Пример #1
0
 /// <summary>
 /// Create HigherLevelBlock 
 /// </summary>
 /// <param name="period"></param>
 /// <param name="hashPrev"></param>
 /// <param name="txset"></param>
 /// <param name="privateKey">PrivateKey of the bank.</param>
 /// <returns></returns>
 public static HigherLevelBlock CreateHigherLevelBlock(long period, string hashPrev, List<PaymentTransaction> txset, string privateKey)
 {
     string hash = CryptoHelper.Hash(hashPrev + JsonHelper.Serialize(txset));
     List<LowerLevelBlock> lowerLevelBlockSet = new List<LowerLevelBlock>();
     LowerLevelBlock lowerLevelBlock = new LowerLevelBlock(txset);
     lowerLevelBlockSet.Add(lowerLevelBlock);
     return new HigherLevelBlock
     {
         Period = period,
         Hash = hash,
         LowerLevelBlockSet = lowerLevelBlockSet,
         Signature = CryptoHelper.Sign(privateKey, hash)
     };
 }
Пример #2
0
 public LowerLevelBlock AddLowerLevelBlock(LowerLevelBlock newLowerLevelBlock)
 {
     var result = QueryStoreProcedure("AddLowerLevelBlock", new Dictionary<string, object>
                                                 {
                                                     {"@blockSize", newLowerLevelBlock.blockSize },
                                                     {"@blockHeader", newLowerLevelBlock.blockHeader },
                                                     {"@transactionCounter", newLowerLevelBlock.TransactionCounter },
                                                     {"@transactions", newLowerLevelBlock.TransactionSet },
                                                     {"@period", newLowerLevelBlock.Period },
                                                     {"@hash", newLowerLevelBlock.Hash },
                                                     {"@signature", newLowerLevelBlock.Signature },
                                                     {"@signatureToCertifyIssuer", newLowerLevelBlock.SignatureToCertifyIssuer },
                                                 });
     if (result.Tables[0].Rows.Count > 0)
     {
         var llb = new LowerLevelBlock().FromRow(result.Tables[0].Rows[0]);
         return llb;
     }
     return null;
 }
Пример #3
0
        public void CreateLowerLevelBlockForIssuer1()
        {
            if (issuer1.s_PaymentPool.Count == 0)
                return;
            //Construct low level block
            LowerLevelBlock block = new LowerLevelBlock();
            block.TransactionCounter = 0;

            //Construct the block header
            block.blockHeader.hashPrevBlock = GetPreviousTransactionHash();
            block.blockHeader.Issuer = issuer1.Id.ToString();
            block.blockHeader.Time = GetTime(DateTime.Now);
            block.blockHeader.Version = issuer1.version;

            while(issuer1.s_PaymentPool.Count != 0 && block.TransactionCounter < MAX_TRANSACTION)
            {
                PaymentTransaction transaction = issuer1.s_PaymentPool.Dequeue();
                if(VerifyTransaction(transaction) == true)
                {
                    block.TransactionSet.Add(transaction);
                    block.TransactionCounter++;
                }
            }
            //Add the Merkle root
            block.blockHeader.hashMerkleRoot = ConstructMerkleTree(block);

            //Add the hash & size of this block
            block.Hash = CryptoHelper.Hash(JsonHelper.Serialize(block));
            block.blockSize = Marshal.SizeOf(block);

            //Call api to post the lowerlevelblock to Bank
            string requestUri = string.Format("certifier/api/postLowerLevelBlock");
            HttpContent content = new StringContent(JsonHelper.Serialize(block));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage response = RestApiHelper.HttpClient.PostAsync(requestUri, content).Result;
            response.EnsureSuccessStatusCode();
        }
Пример #4
0
        /// <summary>
        /// Verify LowerLevelBlock
        /// </summary>
        /// <param name="block"></param>
        /// <param name="hashPrev"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static bool VerifyLowerLevelBlock(LowerLevelBlock block, string hashPrev, string publicKey, string issuerPublicKey)
        {
            string hash = CryptoHelper.Hash(JsonHelper.Serialize(block.TransactionSet));

            if (block.Hash == hash)
            {
                if (CryptoHelper.Verify(issuerPublicKey, hash, block.Signature))
                {
                    if (CryptoHelper.Verify(publicKey, issuerPublicKey, block.SignatureToCertifyIssuer))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Пример #5
0
        public void ReceiveLowerLevelBlock(LowerLevelBlock block)
        {
            bankService.bank.s_LowerLevelBlockPool.Enqueue(block);
            bankService.bank.prevBlockHash = block.Hash;

            //TODO: write the received block into database
        }
Пример #6
0
 private bool CheckingLedger(LowerLevelBlock llb)
 {
     //TODO:check ledger
     throw new NotImplementedException();
 }
Пример #7
0
 private string ConstructMerkleTree(LowerLevelBlock block)
 {
     int numberofPayment = block.TransactionCounter;
     int counter = 0;
     Queue<string> MerkleTree = new Queue<string>();
     foreach (var transaction in block.TransactionSet)
     {
         MerkleTree.Enqueue(CryptoHelper.Hash(JsonHelper.Serialize(transaction)));
     }
     if (numberofPayment % 2 == 1)
     {
         MerkleTree.Enqueue(CryptoHelper.Hash(JsonHelper.Serialize(block.TransactionSet[numberofPayment - 1])));
         numberofPayment++;
     }
     while (MerkleTree.Count != 1)
     {
         string hash = MerkleTree.Dequeue();
         hash += MerkleTree.Dequeue();
         MerkleTree.Enqueue(CryptoHelper.Hash(JsonHelper.Serialize(hash)));
         counter += 2;
         if(counter + 1 == numberofPayment)
         {
             hash = MerkleTree.Dequeue();
             hash += hash;
             MerkleTree.Enqueue(CryptoHelper.Hash(JsonHelper.Serialize(hash)));
             counter++;
         }
         if(counter == numberofPayment)
         {
             counter = 0;
             numberofPayment /= 2;
         }
     }
     string result = MerkleTree.Dequeue();
     MerkleTree = null;
     return result;
 }