Beispiel #1
0
        //ver1
        public static Transaction NewTransaction(string from, string to, int amount, UTXOSet set)
        {
            var inputs  = new List <TxInput>();
            var outputs = new List <TxOutput>();

            var walletBank = new WalletBank();
            var wallet     = walletBank.FindWallet(from);
            var pubKeyHash = wallet.PublicKeyHash;

            var spendableOutputs = set.FindSpendableOutputs(pubKeyHash, amount);
            var account          = spendableOutputs.Item2;
            var validOutputs     = spendableOutputs.Item1;

            if (account < amount)
            {
                Console.WriteLine("Insufficient funds");
                return(null);
            }
            else
            {
                foreach (var output in validOutputs.Keys)
                {
                    var txId = (output);

                    if (validOutputs.ContainsKey(txId))
                    {
                        foreach (var @out in validOutputs[txId])
                        {
                            var input = new TxInput()
                            {
                                Id = HexadecimalEncoding.FromHexStringToByte(txId), Out = @out, Signature = null, PubKey = wallet.PublicKey
                            };
                            inputs.Add(input);
                        }
                    }
                }

                outputs.Add(TxOutput.NewTxOutput(amount, to));

                if (account > amount)
                {
                    outputs.Add(TxOutput.NewTxOutput(account - amount, from));
                }
            }

            var tx = new Transaction()
            {
                Id        = null,
                Inputs    = inputs,
                Outputs   = outputs,
                TimeStamp = DateTime.Now
            };

            tx.Id = tx.CalculateHash();
            set.Chain.SignTransaction(tx, wallet.PrivateKey);
            return(tx);
        }
Beispiel #2
0
        /// <summary>
        /// 1. Clear Database,
        /// 2. Finds UTXO
        /// 3. Add to DB
        /// </summary>
        public void ReIndex()
        {
            DeleteKeys();
            var UTXO = Chain.FindUtxo();

            var index = 0;

            foreach (var outputs in UTXO)
            {
                var key = HexadecimalEncoding.FromHexStringToByte(outputs.Key);
                Chain.TransactionDB.Put(key, outputs.Value.Serialize());

                index++;
            }
        }
Beispiel #3
0
        public bool Parse(Byte[] data)
        {
            //parse the message from "incoming data packet"
            var messageContent = Encoding.UTF8.GetString(data);
            //////////////////////////////////////////////////////////////////////////
            //Data validation
            XmlDocument xmlDoc = new XmlDocument();

            try
            {
                xmlDoc.LoadXml(messageContent);
            }
            catch (XmlException ex)
            {
                System.Diagnostics.Debug.WriteLine("There was an xml parsing error in the received message : " +
                                                   ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("There was an error in the received message : " + ex.Message);
                return(false);
            }

            MessageType type = MessageType.EmptyMessage;

            XmlElement messageElement = xmlDoc.DocumentElement;

            if (messageElement.Name == "message")
            {
                foreach (XmlNode node in messageElement.ChildNodes)
                {
                    if (node.Name == "type")
                    {
                        type = (MessageType)Enum.Parse(typeof(MessageType), node.InnerText);
                        break;
                    }
                }
            }

            if (type != MessageType.CommandMessage)
            {
                Console.WriteLine("The supplied data was the wrong message type!");
                return(false);
            }


            foreach (XmlNode node in messageElement.ChildNodes)
            {
                if (node.Name == "data")
                {
                    this.Data = HexadecimalEncoding.FromHexStringToByte(node.InnerText);
                }
                else if (node.Name == "command")
                {
                    Enum.TryParse(node.InnerText, out CommandType command);

                    Command = command;
                }
                else if (node.Name == "clientdetails")
                {
                    mClient = new ClientDetails();

                    foreach (XmlNode detailsNode in node.ChildNodes)
                    {
                        if (detailsNode.Name == "name")
                        {
                            mClient.ClientName = detailsNode.InnerText;
                        }
                        else if (detailsNode.Name == "ipaddress")
                        {
                            mClient.ClientIPAddress = detailsNode.InnerText;
                        }
                        else if (detailsNode.Name == "listenport")
                        {
                            mClient.ClientListenPort = int.Parse(detailsNode.InnerText);
                        }
                    }
                }
            }

            //////////////////////////////////////////////////////////////////////////
            return(true);
        }