// loop through all unspent transaction outputs
        // and calculate this wallet's balance
        public float GetBalance()
        {
            float balance = 0;

            foreach (KeyValuePair <string, TransactionOutput> item in Blockchain.Instance.UTXOs)
            {
                TransactionOutput UTXO = item.Value;

                if (UTXO.Recipient == this.publicKey)
                {
                    balance += UTXO.Value;
                }
            }

            return(balance);
        }
 public TransactionInput(TransactionOutput UTXO)
 {
     this.transactionOutputId = UTXO.Id;
     this.UTXO = UTXO;
 }
        public Transaction CreateTransaction(string recipient, float value)
        {
            if (this.privateKey == null)
            {
                throw new OperationCanceledException("Wallet is missing privateKey.");
            }

            // check the balance
            if (this.GetBalance() < value)
            {
                throw new OperationCanceledException("Not enough funds. Transaction canceled.");
            }

            // get my unspent transaction outputs
            Dictionary <string, TransactionOutput> myUTXOs = new Dictionary <string, TransactionOutput>();

            foreach (KeyValuePair <string, TransactionOutput> item in Blockchain.Instance.UTXOs)
            {
                if (item.Value.Recipient == this.publicKey)
                {
                    myUTXOs.Add(item.Key, item.Value);
                }
            }

            List <TransactionInput> inputs = new List <TransactionInput>();

            float inputsValue = 0;

            // fill transaction inputs from unspent transaction outputs
            foreach (KeyValuePair <string, TransactionOutput> item in myUTXOs)
            {
                TransactionOutput UTXO = item.Value;
                inputsValue += UTXO.Value;
                inputs.Add(new TransactionInput(UTXO));
                Blockchain.Instance.UTXOs.Remove(item.Key);

                if (inputsValue > value)
                {
                    break;
                }
            }

            // generate transaction outputs
            float leftOver = inputsValue - value;
            List <TransactionOutput> outputs = new List <TransactionOutput>();

            outputs.Add(new TransactionOutput(recipient, value));
            if (leftOver > 0)
            {
                outputs.Add(new TransactionOutput(this.publicKey, leftOver));
            }
            // add new outputs to global unspent transaction outputs
            foreach (TransactionOutput output in outputs)
            {
                Blockchain.Instance.AddUTXO(output);
            }

            // create new transaction
            Transaction transaction = new Transaction(this.publicKey, recipient, value, inputs, outputs);

            transaction.GenerateSignature(this.privateKey);

            Blockchain.Instance.AddCurrentTransaction(transaction);

            return(transaction);
        }
Beispiel #4
0
 public void AddUTXO(TransactionOutput UTXO)
 {
     this.UTXOs.Add(UTXO.Id, UTXO);
 }
Beispiel #5
0
        // generate new unspent transaction output as a reward for miner
        public void GenerateMiningReward(string minerPublicKey)
        {
            TransactionOutput transactionOutput = new TransactionOutput(minerPublicKey, minerReward);

            Instance.AddUTXO(transactionOutput);
        }