public double GetBalance() { double total = 0; var UTXOs = BlockChain.GetUTXO(); foreach (var UTXO in UTXOs) { if (UTXO.IsMine(PublicKey)) { //if output belongs to me ( if coins belong to me ) _unspentTransactionOutputs[UTXO.Id] = UTXO; total += UTXO.Value; } } return(total); }
///<summary> ///Ritorna lista di UTXO intestati a un dato indirizzo ///</summary> public List <UTXO> GetUTXObyHash(string hash) { lock (HashTable) { List <string> paths = (List <string>) this.HashTable[hash]; if (paths == null) { paths = new List <string>(); } List <UTXO> utxos = new List <UTXO>(); foreach (string path in paths) { utxos.Add(UTXO.Deserialize(File.ReadAllText(path))); } return(utxos); } }
///<summary> ///Inserisce nell'hashtable un nuovo indirizzo a un dato UTXO, salvandolo poi a tale indirizzo ///</summary> public void SetTransactionPath(UTXO utxo) { string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string specificFolder = Path.Combine(appDataFolder, "Blockchain\\UTXODB"); string filename = specificFolder + "\\" + utxo.TxHash + ".json"; if (Directory.Exists(specificFolder)) { File.WriteAllText(filename, utxo.Serialize()); } else { Directory.CreateDirectory(specificFolder); File.WriteAllText(filename, utxo.Serialize()); } foreach (Output output in utxo.Output) { SetTransactionPath(output.PubKeyHash, filename); } }
public Transaction(double amount, string hashReceiver, RSACryptoServiceProvider csp, bool testing) //costruttore per testing { this.outputs = new Output[] { new Output(amount, hashReceiver) }; this.PubKey = RSA.ExportPubKey(csp); this.inputs = this.GetEnoughInputs(); //forse vanno anche controllate le firme ma non penso this.Hash = Utilities.SHA2Hash(JsonConvert.SerializeObject(this)); //Calcolo l'hash di questa transazione inizializzata fino a questo punto, esso farà da txId RSA.HashSignTransaction(this, csp); //firmo la transazione fino a questo punto string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string specificFolder = Path.Combine(appDataFolder, "Blockchain\\UTXODB"); UTXO utxo = new UTXO(this.Hash, this.outputs); if (Directory.Exists(specificFolder)) { File.WriteAllText(specificFolder + "\\" + this.Hash + ".json", utxo.Serialize()); } else { Directory.CreateDirectory(specificFolder); File.WriteAllText(specificFolder + "\\" + this.Hash + ".json", utxo.Serialize()); } //CPeers.Instance.DoRequest(ERequest.SendTransaction, this); TODO : implementa richiesta di invio transazione }
public CoinbaseTransaction(RSACryptoServiceProvider csp, bool testing)//Costruttore da usare SOLO per testing { this.inputs = null; this.outputs = new Output[] { new Output(50, Utilities.Base64SHA2Hash(RSA.ExportPubKey(csp))) }; this.PubKey = RSA.ExportPubKey(csp); this.Hash = Utilities.SHA2Hash(JsonConvert.SerializeObject(this)); RSA.HashSignTransaction(this, csp); //Salva transazione su disco string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string specificFolder = Path.Combine(appDataFolder, "Blockchain\\UTXODB"); UTXO utxo = new UTXO(this.Hash, this.outputs); if (Directory.Exists(specificFolder)) { File.WriteAllText(specificFolder + "\\" + this.Hash + ".json", utxo.Serialize()); } else { Directory.CreateDirectory(specificFolder); File.WriteAllText(specificFolder + "\\" + this.Hash + ".json", utxo.Serialize()); } }
public void AddUTXO(UTXO utxo) { this.SetTransactionPath(utxo); }