Split application and library.

This commit is contained in:
2020-09-02 00:03:14 +02:00
parent cd7a71bfe0
commit f97321d929
11 changed files with 123 additions and 19 deletions

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using VAR.Json;
namespace BasicBlockChain.Core
{
public class Block
{
public int Index { get; set; }
public DateTime Date { get; set; }
public string PreviousHash { get; set; }
public List<Transaction> Transactions { get; } = new List<Transaction>();
public string Hash { get; set; }
public int Nonce { get; set; }
public Block(DateTime date, Block previousBlock, IList<Transaction> transactions)
{
Index = (previousBlock?.Index ?? -1) + 1;
Date = date;
PreviousHash = previousBlock?.Hash;
if (transactions != null) { Transactions.AddRange(transactions); }
Nonce = 0;
Hash = CalculateHash();
}
private string GetData()
{
return JsonWriter.WriteObject(Transactions);
}
public string CalculateHash(string data = null, SHA256 sha256 = null)
{
if (sha256 == null) { sha256 = SHA256.Create(); }
if (data == null)
{
data = GetData();
}
byte[] dataBytes = Encoding.UTF8.GetBytes(string.Format("{0}-{1}-{2}-{3}",
Date,
PreviousHash,
data,
Nonce));
byte[] hashBytes = sha256.ComputeHash(dataBytes);
string hash = Convert.ToBase64String(hashBytes);
return hash;
}
public void Mine(int difficulty)
{
SHA256 sha256 = SHA256.Create();
var leadingZeros = new string('0', difficulty);
string data = GetData();
while (Hash.Substring(0, difficulty) != leadingZeros)
{
Nonce++;
Hash = CalculateHash(data, sha256);
}
}
}
}