Ejemplo n.º 1
0
 public bool Verify(NormalTransaction tx)
 {
     /*
      * bool flag = false;
      * for(int i = 0;i<leavesNumber;i++)
      * {
      *  if (tx.id == tree[i])
      *  {
      *      flag = true;
      *      break;
      *  }
      * }
      * if (flag == false) return false;
      */
     return(true);
 }
Ejemplo n.º 2
0
        public bool Transfer(Blockchain bc, byte[] sender, byte[] receiver, int amount)
        {
            //calculate inputs
            List <TxIn> tx_ins             = new List <TxIn>();
            int         tx_in_amount_count = 0;

            foreach (KeyValuePair <TxIn, int> unspend_tx_out in bc.FindUnspendTxOut(sender))
            {
                if (tx_in_amount_count < amount)
                {
                    tx_in_amount_count += unspend_tx_out.Value;
                    tx_ins.Add(unspend_tx_out.Key);
                }
                else
                {
                    break;
                }
            }
            if (tx_in_amount_count < amount)
            {
                Console.WriteLine("Not enough money");
                return(false);
                // not enough money in the account
            }

            TxOut[] outputs;
            //calculate ouputs
            if (tx_in_amount_count > amount)
            {
                outputs            = new TxOut[2];
                outputs[1]         = new TxOut();
                outputs[1].address = sender;
                outputs[1].amount  = tx_in_amount_count - amount;
            }
            else
            {
                outputs = new TxOut[1];
            }
            outputs[0]         = new TxOut();
            outputs[0].address = receiver;
            outputs[0].amount  = amount;
            NormalTransaction tx = new NormalTransaction(tx_ins.ToArray(), outputs, key);

            return(bc.CommitTransaction(tx));
        }
Ejemplo n.º 3
0
        public bool CommitTransaction(NormalTransaction tx)
        {
            //make sure the id is exactly the hash of the transaction
            if (!Program.CompareBytes(tx.id, Crypto.CalculateSHA256(tx)))
            {
                Console.WriteLine("Transaction ID verification failed");
                return(false);
            }
            bool found_flag;

            //make sure the TxIn is avaliable
            foreach (TxIn txIn in tx.inputs)
            {
                found_flag = false;
                for (int i = 1; i < blocks.Count; i++)
                {
                    //find out if the txIn point to an awardTransaction
                    if (txIn.outIndex == 0 && Crypto.VerifyHash(tx.id, tx.signature, blocks[i].awardTransaction.output.address))
                    {
                        found_flag = true;
                        break;
                    }
                    foreach (NormalTransaction _tx in blocks[i].transactions)
                    {
                        if (Program.CompareBytes(_tx.id, txIn.outId))
                        {
                            if (_tx.outputs.Length > txIn.outIndex && Crypto.VerifyHash(tx.id, tx.signature, _tx.outputs[txIn.outIndex].address))
                            {
                                found_flag = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        if (found_flag)
                        {
                            break;
                        }
                    }
                    if (found_flag)
                    {
                        break;
                    }
                }
                if (!found_flag)
                {
                    return(false);             //no found corresponding txOut of the txIn in blockchain
                }
            }
            //provent double spend attack
            foreach (NormalTransaction _tx in transactions)
            {
                foreach (TxIn _txIn in _tx.inputs)
                {
                    foreach (TxIn txIn in tx.inputs)
                    {
                        if (_txIn == txIn)
                        {
                            return(false);
                        }
                    }
                }
            }
            transactions.Add(tx);
            return(true);
        }