Esempio n. 1
0
        public static Transaction CoinBaseGenesisTx(string to, string data)
        {
            if (data == "")
            {
                data = "Coins to " + to;
            }

            var txIn = new TxInput()
            {
                Id        = new byte[] { }, // no referencing any output
                Out       = -1,             // no referencing any output
                Signature = null,
                PubKey    = ByteHelper.GetBytesFromString(data)
            };

            var txOut = TxOutput.NewTxOutput(100, to);

            var tx = new Transaction()
            {
                Id      = null,
                Outputs = new List <TxOutput>()
                {
                    txOut
                },
                Inputs = new List <TxInput>()
                {
                    txIn
                }
            };

            tx.Id = tx.CalculateHash();

            return(tx);
        }
Esempio n. 2
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);
        }