static void CreateTransaction(List <string> from, string to, int amount, string change, BlockChainClassLib.CommandProcessor cmdProc) { WalletLib.WalletEntry we; //build the transaction: BlockChainClassLib.Transaction t = new BlockChainClassLib.Transaction(); t.id = Guid.NewGuid(); foreach (string address in from) { we = cmdProc.Wallet_FindEntry(address); string publicKey = we.publicKey; string signature = cmdProc.SignAddress(address, t.id.ToString(), we.publicKey, we.privateKey); t.Inputs.Add(new BlockChainClassLib.Input(address, signature, publicKey)); } //add the primary payment address t.Outputs.Add(new BlockChainClassLib.Output(to, amount)); //add the address to collect change t.Outputs.Add(new BlockChainClassLib.Output(change, 0)); //sign the transaction we = cmdProc.Wallet_FindEntry(from[0]); t.PublicKey = we.publicKey; cmdProc.SignTransaction(t, we.publicKey, we.privateKey); //Instruct the command processor to submit the transaction cmdProc.Transfer(t); }
//Provides an interactive mechanism for the user to create blockchain transactions static void InteractiveCreateTransaction(string[] cmdArgs, BlockChainClassLib.CommandProcessor cmdProc) { //1) ask the user for a list of input transactions: System.Console.WriteLine("Create Transaction:"); System.Console.WriteLine("Enter Addresses from your Wallet (one on each line, blank to cancel entry):"); WalletLib.WalletEntry we; //build a list of input addresses List <string> addressList = new List <string>(); int i = 1; string line = ""; do { System.Console.Write(string.Format("Enter Source Address ({0})=", i)); line = System.Console.ReadLine().Trim(); if (line == "") { break; } we = cmdProc.Wallet_FindEntry(line); if (we != null) { addressList.Add(line); i++; } else { System.Console.WriteLine("address not found"); } } while (line != ""); if (addressList.Count() == 0) { return; } //2) Enter primary payment address System.Console.Write("Enter payment address (blank line to cancel transaction)="); string paymentAddress = System.Console.ReadLine().Trim(); if (paymentAddress == "") { return; } //3) Enter payment amount string paymentAmount = ""; int amount = 0; do { System.Console.Write("Enter payment amount="); paymentAmount = System.Console.ReadLine().Trim(); if (paymentAmount == "") { return; } } while (!int.TryParse(paymentAmount, out amount)); //Enter address to post any change to (enter "new" to create a new one and add to your wallet) System.Console.Write("Enter change address (new to create one)="); string changeAddress = System.Console.ReadLine().Trim(); if (changeAddress == "") { return; } if (changeAddress.Trim() == "new") { changeAddress = cmdProc.Wallet_CreateAddress(); cmdProc.Wallet_Save(); System.Console.WriteLine("Address added to your wallet: " + changeAddress); } CreateTransaction(addressList, paymentAddress, amount, changeAddress, cmdProc); }