public static void NewKey(string privateKey, string publicKey, string address, string type) { float amount = 0; if (type == "first") { amount = 100; } XDocument doc = XDocument.Load(filePath); XElement element = doc.Element("UserInfo").Element("Keys"); XElement key = new XElement("Key"); key.Add(new XElement("DateCreated", DateTime.Now.ToLongDateString())); key.Add(new XElement("PrivateKey", privateKey)); key.Add(new XElement("PublicKey", publicKey)); key.Add(new XElement("Address", address)); key.Add(new XElement("TransactionId", CryptoCurrency.ByteArrayToString(CryptoCurrency.StringToSha256(publicKey)))); key.Add(new XElement("Index", element.Elements("Key").Count().ToString())); List <XAttribute> attribute = new List <XAttribute>() { new XAttribute("type", type), new XAttribute("amount", amount.ToString()), new XAttribute("isConfirm", "false"), new XAttribute("unSpend", "true") }; key.Element("Address").Add(attribute); element.Add(key); doc.Save(filePath); }
public static List <Input> GetTotal(float amount, Span <float> change) { XDocument doc = XDocument.Load(filePath); List <XElement> elements = new List <XElement>(doc.Element("UserInfo").Element("Keys").Elements("Key")); List <Input> inputs = new List <Input>(); int index = 0; float total = 0; while (index < elements.Count && total < amount) { XElement temp = elements[index].Element("Address"); XElement privateKey = elements[index].Element("PrivateKey"); if (temp.Attribute("unSpend").Value == "true") { if (temp.Attribute("isConfirm").Value == "true") { total += float.Parse(temp.Attribute("amount").Value); inputs.Add(new Input(temp.Value, CryptoCurrency.ByteArrayToIntArray(CryptoCurrency.Sign(temp.Value, CryptoCurrency.StringToByteArray(privateKey.Value))))); } } index++; } change[0] = total - amount; return(inputs); }
/// <summary> /// Normal transaction. If return invalid then the transaction is not possible /// </summary> public Transaction(List <Output> outputs) { float total = 0; foreach (Output output in outputs) { total += output.Amount; } float[] change = new float[] { 0 }; Span <float> changeSpan = change.AsSpan(); List <Input> inputs = XmlManager.GetTotal(total, change); if (change[0] == 0) { Inputs = inputs; Outputs = outputs; Type = "normal"; } else { byte[] privateKey = CryptoCurrency.Hash32Byte(); byte[] publicKey = CryptoCurrency.GetPublicKey(privateKey); string address = CryptoCurrency.Sha1(publicKey); Output changeOutput = new Output(address, CryptoCurrency.ByteArrayToIntArray(publicKey), change[0]); Type = "change"; XmlManager.NewKey(CryptoCurrency.ByteArrayToString(privateKey), CryptoCurrency.ByteArrayToString(publicKey), address, "change"); outputs.Add(changeOutput); Inputs = inputs; Outputs = outputs; Type = "normal"; } }
private void NewAddress(object sender, RoutedEventArgs e) { byte[] privateKey = CryptoCurrency.Hash32Byte(); byte[] publicKey = CryptoCurrency.GetPublicKey(privateKey); string address = CryptoCurrency.Sha1(publicKey); XmlManager.NewKey(CryptoCurrency.ByteArrayToString(privateKey), CryptoCurrency.ByteArrayToString(publicKey), address, "normal"); NewKeyWindow newKeyWindow = new NewKeyWindow(address, CryptoCurrency.ByteArrayToString(publicKey)); newKeyWindow.Show(); }
/// <summary> /// First transaction amount given is 100 coin /// </summary> public Transaction() { byte[] privateKey = CryptoCurrency.Hash32Byte(); byte[] publicKey = CryptoCurrency.GetPublicKey(privateKey); string address = CryptoCurrency.Sha1(publicKey); Inputs = null; Outputs = new List <Output>() { new Output(address, CryptoCurrency.ByteArrayToIntArray(publicKey), 100) }; Type = "first"; XmlManager.NewKey(CryptoCurrency.ByteArrayToString(privateKey), CryptoCurrency.ByteArrayToString(publicKey), address, "first"); }
private void AddAddress(object sender, RoutedEventArgs e) { string currentAddress = txtAddr.Text; int[] currentPublicKey = CryptoCurrency.ByteArrayToIntArray(CryptoCurrency.StringToByteArray(txtPubKey.Text)); float currentAmount; if (!float.TryParse(txtAmount.Text, out currentAmount)) { MessageBox.Show("Amount must be a float", "Error"); return; } Outputs.Add(new Output(currentAddress, currentPublicKey, currentAmount)); lvOutput.ItemsSource = null; lvOutput.ItemsSource = Outputs; txtAddr.Text = ""; txtAmount.Text = ""; txtPubKey.Text = ""; }