Exemple #1
0
        //TODO
        public bool ProposeTransaction(byte[] to, int value)
        {
            if (ServerList.Count == 0)
            {
                return(false);
            }
            int oldBalance = Task.Run(async() => await BalanceFromAccountTable()).Result;
            int newBalance = oldBalance - value;

            List <TxIn> ins  = new List <TxIn>();
            String      addr = Base64.ToBase64String(Account.address);

            Console.WriteLine(addr);
            foreach (UtxoReturn utxoReturn in userTxService.userUtxo.UtxoReturns)
            {
                ins.Add(new TxIn(HexHelper.StringToByteArray(utxoReturn.hash), utxoReturn.index, Account.address));
            }

            TxIn[] insArray = ins.ToArray();

            foreach (TxIn txIn in insArray)
            {
                Console.WriteLine("In: " + HexHelper.ByteArrayToString(txIn.hash) + " " + txIn.index + " " +
                                  Convert.ToBase64String(txIn.script));
            }

            signedTransaction = userTxService.MakeSignedTransaction(insArray, to, this.Account, value);
            if (signedTransaction == null)
            {
                return(false);
            }

            payment = false;
            int           bizantine = ServerList.Count / 3;
            List <Thread> threads   = new List <Thread>();

            for (int i = 0; i < bizantine + 1; i++)
            {
                Thread t = new Thread(paymentThread);
                t.Start(i);
                threads.Add(t);
            }
            foreach (Thread t in threads)
            {
                t.Join();
            }

            return(payment);
        }
        //Generate signed transaction
        public byte[] MakeSignedTransaction(TxIn[] ins, byte[] to, Account from, int value)
        {
            List <TxOut> outs  = new List <TxOut>();
            int          total = 0;

            //Get aggregate balance of user
            foreach (TxIn txIn in ins)
            {
                UtxoOutput utxoOut = UtxoTable.LookUpEntry(HexHelper.ByteArrayToString(txIn.hash), txIn.index, from.address);
                Console.WriteLine("UtxoOut: " + utxoOut);
                if (utxoOut != null && !utxoOut.spent)
                {
                    total += utxoOut.value;
                }
            }

            //If the value of proposed transaction is bigger than total balance
            //Return null
            if (total < value)
            {
                Console.WriteLine("Insufficient balance");
                return(null);
            }

            int change = total - value;

            //Add TxOuts to Transaction
            outs.Add(new TxOut(value, from.publicKey, Convert.ToBase64String(to)));
            outs.Add(new TxOut(change, from.publicKey, Convert.ToBase64String(from.address)));

            Tx tx = new Tx();

            tx.TxIns.AddRange(ins);
            tx.TxOuts.AddRange(outs);

            //Get the hash of Transaction
            tx.getHash();

            return(tx.SignTx(from.key));
        }