public byte[] MakeSignedTransaction(TxIn[] ins, byte[] to, Account from, int value)
        {
            List <TxOut> outs  = new List <TxOut>();
            int          total = 0;

            foreach (UtxoOutput utxoOut in userUtxo.UtxoOutputs)
            {
                Console.WriteLine("UtxoOut: " + utxoOut);
                if (utxoOut != null && !utxoOut.spent)
                {
                    total += utxoOut.value;
                }
            }

            if (total < value)
            {
                Console.WriteLine("Insufficient balance");
                return(null);
            }

            int change = total - value;

            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);
            tx.getHash();

            return(tx.SignTx(from.key));
        }
示例#2
0
        public Task <List <Tuple <string, int> > > GetTransactionToAccount()
        {
            return(Task.Run(() =>
            {
                if (ServerList.Count == 0)
                {
                    return null;
                }
                TxInresults = new Dictionary <byte[], int>(new ByteArrayComparer());
                int bizantine = ServerList.Count / 3;
                List <Thread> threads = new List <Thread>();
                for (int i = 0; i < 2 * bizantine + 1; i++)
                {
                    Thread t = new Thread(GetTransactionToAccountThread);
                    t.Start(i);
                    threads.Add(t);
                }
                foreach (Thread t in threads)
                {
                    t.Join();
                }
                if (TxInresults.Keys.Count == 0)
                {
                    return null;
                }
                byte[] value = TxInresults.Keys.First();
                int occurence = TxInresults[value];
                foreach (byte[] b in TxInresults.Keys)
                {
                    if (TxInresults[b] > occurence)
                    {
                        value = b;
                        occurence = TxInresults[b];
                    }
                }
                Console.WriteLine("Occurence:" + TxInresults[value]);

                string Txs = Encoding.UTF8.GetString(value);
                Txs = Txs.Substring(1, Txs.Length - 2);
                List <string> list = Txs.Split(',').ToList();
                List <Tuple <string, int> > txOuts = new List <Tuple <string, int> >();
                foreach (string str in list)
                {
                    string tx = str.Substring(1, str.Length - 2);
                    Tx transaction = Tx.DeserializeSignedTx(Convert.FromBase64String(tx));
                    if (transaction != null)
                    {
                        foreach (TxOut txOut in transaction.TxOuts)
                        {
                            if (txOut.address.Equals(Convert.ToBase64String(this.Account.address)))
                            {
                                txOuts.Add(new Tuple <string, int>(Convert.ToBase64String(transaction.FromAddress), txOut.value));
                            }
                        }
                    }
                }
                return txOuts;
            }));
        }
示例#3
0
        //129.78.10.53,7822
        //129.78.10.53,7722
        //129.78.10.53,7622
        //129.78.10.53,7522
        //"{'method':'RpcNode.BootstrapTable','params':[null],'id':0}";
        //"{'method':'RpcNode.GetTableAccount','params':[null],'id':0}"
        public static void Call(string method, Object[] parameters, TcpClient client)
        {
            JsonObj json = new JsonObj(method, parameters, 0);
            dynamic recvJson;

            using (NetworkStream networkStream = client.GetStream())
            {
                String jsonData = JsonConvert.SerializeObject(json);
                Byte[] jsonByte = Encoding.UTF8.GetBytes(jsonData);
                networkStream.Write(jsonByte, 0, jsonByte.Length);
                String recv         = "";
                int    bufferLength = -1;
                Byte[] buffer       = new Byte[40000];
                do
                {
                    bufferLength = networkStream.Read(buffer, 0, buffer.Length);
                    recv        += Encoding.UTF8.GetString(buffer);
                    if (recv.IndexOf("\n") != -1)
                    {
                        break;
                    }
                } while (bufferLength != 0);
                recv     = recv.Substring(0, recv.IndexOf("\n"));
                recvJson = JObject.Parse(recv);
                Console.WriteLine(recvJson);
            }
            string result = recvJson.GetValue("result");

            //Console.WriteLine(result);
            byte[] b = Convert.FromBase64String(result);


            string Txs = Encoding.UTF8.GetString(b);

            Console.WriteLine(Txs);
            Txs = Txs.Substring(1, Txs.Length - 2);
            List <string> list = Txs.Split(',').ToList();

            foreach (string str in list)
            {
                Console.WriteLine();

                string tx = str.Substring(1, str.Length - 2);
                //Console.WriteLine("Signed Transaction Hash: "+tx);

                Tx transaction = Tx.DeserializeSignedTx(Convert.FromBase64String(tx));
                Console.WriteLine("From address: " + Convert.ToBase64String(transaction.FromAddress));
                Console.WriteLine("TxOuts:");

                foreach (TxOut txOut in transaction.TxOuts)
                {
                    Console.WriteLine("To address: " + txOut.address + "\nValue: " + txOut.value);
                }
            }
        }
示例#4
0
        //Deserialize TxIn
        public static TxIn Deserialize(byte[] serializedTxIn)
        {
            int length  = serializedTxIn.Length;
            int current = 0;

            byte[] bytes;

            //Check if the system uses little endian;
            //Reverse the array of bytes if it's little endian;
            bool reverse = BitConverter.IsLittleEndian;

            if (length < 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTxIn, 0, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int totalLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            if (length != totalLen + 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTxIn, current, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int hashLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            byte[] hash = Tx.SubArray(serializedTxIn, current, hashLen);
            current += hashLen;

            bytes = Tx.SubArray(serializedTxIn, current, 8);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int index = (int)BitConverter.ToInt64(bytes, 0);

            current += 8;

            byte[] script = Tx.SubArray(serializedTxIn, current, length - current);

            return(new TxIn(hash, index, script));
        }
示例#5
0
        //Deserialize the byte array of a list of TxIns;
        public static List <TxIn> DeserializeTxIns(byte[] serializedTxIns)
        {
            List <TxIn> TxIns  = new List <TxIn>();
            int         length = 0;
            int         inLen  = 0;

            byte[] bytes;
            bool   reverse = BitConverter.IsLittleEndian;

            while ((length = serializedTxIns.Length) > 0)
            {
                if (length < 4)
                {
                    return(null);
                }

                bytes = Tx.SubArray(serializedTxIns, 0, 4);
                if (reverse)
                {
                    Array.Reverse(bytes);
                }
                inLen = BitConverter.ToInt32(bytes, 0);

                if (length < 4 + inLen)
                {
                    return(null);
                }

                TxIn txIn = Deserialize(Tx.SubArray(serializedTxIns, 0, 4 + inLen));

                if (txIn == null)
                {
                    return(null);
                }
                serializedTxIns = Tx.SubArray(serializedTxIns, 4 + inLen, length - 4 - inLen);

                TxIns.Add(txIn);
            }

            return(TxIns);
        }
        //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));
        }
示例#7
0
        //Deserialize the byte array of a list of TxOuts;
        public static List <TxOut> DeserializeTxOuts(byte[] serializedTxOuts)
        {
            List <TxOut> txOuts = new List <TxOut>();
            int          length;
            int          outLen;

            byte[] bytes;
            bool   reverse = BitConverter.IsLittleEndian;

            while ((length = serializedTxOuts.Length) > 0)
            {
                if (length < 4)
                {
                    return(null);
                }

                bytes = Tx.SubArray(serializedTxOuts, 0, 4);
                if (reverse)
                {
                    Array.Reverse(bytes);
                }
                outLen = BitConverter.ToInt32(bytes, 0);

                if (length < outLen + 4)
                {
                    return(null);
                }

                TxOut txOut = Deserialize(Tx.SubArray(serializedTxOuts, 0, 4 + outLen));

                if (txOut == null)
                {
                    return(null);
                }
                serializedTxOuts = Tx.SubArray(serializedTxOuts, 4 + outLen, length - outLen - 4);

                txOuts.Add(txOut);
            }
            return(txOuts);
        }
示例#8
0
        //Deserialzie serialized Tx
        public static Tx Deserialize(byte[] serializedTx)
        {
            int current = 0;
            int length  = serializedTx.Length;

            byte[] bytes;
            bool   reverse = BitConverter.IsLittleEndian;

            if (length < 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTx, 0, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int hashLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            if (length < current + hashLen)
            {
                return(null);
            }

            byte[] hash = SubArray(serializedTx, current, hashLen);
            current += hashLen;

            if (length < current + 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTx, current, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int insLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            if (length < current + insLen)
            {
                return(null);
            }

            byte[] insBytes = SubArray(serializedTx, current, insLen);
            current += insLen;
            List <TxIn> txIns = TxIn.DeserializeTxIns(insBytes);

            if (txIns == null)
            {
                return(null);
            }

            if (length < current + 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTx, current, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int outsLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            if (length < current + outsLen)
            {
                return(null);
            }

            byte[]       outsBytes = SubArray(serializedTx, current, outsLen);
            List <TxOut> txOuts    = TxOut.DeserializeTxOuts(outsBytes);

            if (txOuts == null)
            {
                return(null);
            }

            return(new Tx(txIns, txOuts));
        }
示例#9
0
        //Deserialize SignedTX
        public static Tx DeserializeSignedTx(byte[] signedTx)
        {
            int length       = signedTx.Length;
            int currentIndex = 0;

            byte[] bytes;
            bool   reverse = BitConverter.IsLittleEndian;

            if (length < 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(signedTx, 0, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int sigLen = BitConverter.ToInt32(bytes, 0);

            currentIndex += 4;

            if (length < currentIndex + sigLen)
            {
                return(null);
            }

            byte[] sigser = SubArray(signedTx, currentIndex, sigLen);
            currentIndex += sigLen;

            if (length < currentIndex + 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(signedTx, currentIndex, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int pubLen = BitConverter.ToInt32(bytes, 0);

            currentIndex += 4;

            if (length < currentIndex + pubLen)
            {
                return(null);
            }

            byte[] pubKey = SubArray(signedTx, currentIndex, pubLen);
            currentIndex += pubLen;
            byte[] addr = Account.ToAddr(pubKey);

            byte[] txBytes = SubArray(signedTx, currentIndex, length - currentIndex);
            Tx     tx      = Deserialize(txBytes);

            tx.FromAddress = addr;
            tx.Hash        = pubKey;

            return(tx);
        }
示例#10
0
        //Deserialize TxOut;
        public static TxOut Deserialize(byte[] serializedTxOut)
        {
            int length  = serializedTxOut.Length;
            int current = 0;

            byte[] bytes;


            //Check if the system uses little endian;
            //Reverse the array of bytes if it's little endian;
            bool reverse = BitConverter.IsLittleEndian;

            if (length < 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTxOut, 0, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int totalLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            if (length != totalLen + 4)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTxOut, current, 8);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int value = (int)BitConverter.ToInt64(bytes, 0);

            current += 8;

            if (value < 0)
            {
                return(null);
            }

            bytes = Tx.SubArray(serializedTxOut, current, 4);
            if (reverse)
            {
                Array.Reverse(bytes);
            }
            int scriptLen = BitConverter.ToInt32(bytes, 0);

            current += 4;

            byte[] script = Tx.SubArray(serializedTxOut, current, scriptLen);
            current += scriptLen;

            byte[] addr = Tx.SubArray(serializedTxOut, current, length - current);

            return(new TxOut(value, script, Convert.ToBase64String(addr)));
        }