public byte[] ComputeHash(IHashFactory hashFactory) { IByteConverter converter = hashFactory.GetByteConverter(); string rawData = converter.ConvertToString(Nonce) + converter.ConvertToString(Sender) + converter.ConvertToString(Recipient) + Amount; byte[] data = Encoding.ASCII.GetBytes(rawData); return(hashFactory.GetDigest().GetHash(data)); }
public byte[] ComputeHash(IHashFactory hashFactory) { IByteConverter converter = hashFactory.GetByteConverter(); IDigest digest = hashFactory.GetDigest(); string data = converter.ConvertToString(Nonce) + converter.ConvertToString(PreviousHash) + CreationTime.ToString() + converter.ConvertToString(MerkleRoot) + converter.ConvertToString(MinerAddress) + HashAlgorithmId + Difficulty; byte[] rawData = Encoding.ASCII.GetBytes(data); return(digest.GetHash(rawData)); }
static void Main(string[] args) { #region Configs. BlockchainStorageManagerParameters managerParameters = new BlockchainStorageManagerParameters() { BlocksInUnit = 2 }; UnitRepositoryParameters repositoryParameters = new UnitRepositoryParameters() { DirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "blockchain") }; WalletManagerParameters walletManagerParameters = new WalletManagerParameters() { WalletDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "wallets") }; NetworkParameters networkParametersFirst = new NetworkParameters() { AddressBookPath = Path.Combine(Directory.GetCurrentDirectory(), AddressBookPath), PeerHostName = Dns.GetHostName(), PeerPort = 8910, ClientTimeout = new TimeSpan(0, 0, 4), ServerTimeout = new TimeSpan(0, 0, 4) }; BasicMiningFactoryParameters parameters = new BasicMiningFactoryParameters() { HashLeastUpFactor = 1.25, HashMostUpFactor = 1.75, HashLeastDownFactor = 0.25, HashMostDownFactor = 0.75, DifficultyLeastUpFactor = 1.25, DifficultyMostUpFactor = 1.75, DifficultyLeastDownFactor = 0.25, DifficultyMostDownFactor = 0.75, MinDifficulty = Difficulty }; JsonSerializer jsonSerializer = new JsonSerializer() { Formatting = Formatting.Indented }; using (Stream jsonFile = File.Open(MiningConfigPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (StreamWriter writer = new StreamWriter(jsonFile)) using (JsonWriter jsonWriter = new JsonTextWriter(writer)) jsonSerializer.Serialize(jsonWriter, parameters); using (Stream jsonFile = File.Open(ManagerConfigPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (StreamWriter writer = new StreamWriter(jsonFile)) using (JsonWriter jsonWriter = new JsonTextWriter(writer)) jsonSerializer.Serialize(jsonWriter, managerParameters); using (Stream jsonFile = File.Open(RepositoryConfigPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (StreamWriter writer = new StreamWriter(jsonFile)) using (JsonWriter jsonWriter = new JsonTextWriter(writer)) jsonSerializer.Serialize(jsonWriter, repositoryParameters); using (Stream jsonFile = File.Open(WalletManagerConfigPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (StreamWriter writer = new StreamWriter(jsonFile)) using (JsonWriter jsonWriter = new JsonTextWriter(writer)) jsonSerializer.Serialize(jsonWriter, walletManagerParameters); using (Stream jsonFile = File.Open(NetworkConfigPath, FileMode.Create, FileAccess.Write, FileShare.None)) using (StreamWriter writer = new StreamWriter(jsonFile)) using (JsonWriter jsonWriter = new JsonTextWriter(writer)) jsonSerializer.Serialize(jsonWriter, networkParametersFirst); JsonBasicMiningFactoryConfig miningConfig = new JsonBasicMiningFactoryConfig(MiningConfigPath); JsonBlockchainStorageManagerConfig managerConfig = new JsonBlockchainStorageManagerConfig(ManagerConfigPath); JsonUnitRepositoryConfig repositoryConfig = new JsonUnitRepositoryConfig(RepositoryConfigPath); JsonWalletManagerConfig walletManagerConfig = new JsonWalletManagerConfig(WalletManagerConfigPath); JsonNetworkConfig networkConfig = new JsonNetworkConfig(NetworkConfigPath); #endregion IMiningFactory miningFactory = new BasicMiningFactory(miningConfig); IHashFactory hashFactory = miningFactory.GetMiningHashFactoryById(3); IHashFactory transactionHashFactory = new TransactionHashFactory(); ISignatureFactory signatureFactory = new ECDSAFactory(); IWalletManager walletManager = new WalletManager(walletManagerConfig, signatureFactory, transactionHashFactory); Wallet wallet = walletManager.GetWallets().First(); UnitRepository repository = new UnitRepository(repositoryConfig); BlockchainStorageManager storageManager = new BlockchainStorageManager(managerConfig, repository); Blockchain blockchain = new Blockchain(wallet, walletManager, transactionHashFactory, signatureFactory, miningFactory, storageManager); wallet.Blockchain = blockchain; #region Network. AddressBook addressBook = new AddressBook(AddressBookPath); IBroadcaster network = new Network(blockchain, addressBook, networkConfig, wallet.Signer); blockchain.NetworkBroadcaster = network; network.Start(); #endregion Console.WriteLine("Second"); Console.WriteLine("0 - send transaction;\n1 - mine new block;\n2 - validate blockchain;\n3 - count your balance;\n4 - quit\n5 - create new wallet"); string input; do { input = Console.ReadLine(); switch (input) { case "0": wallet.SendTokens(8, addressBook.First().Key.PublicKey); break; case "1": blockchain.ProcessPendingTransactions(); break; case "2": Console.WriteLine(blockchain.IsValid ? "Blockchain is valid" : "Blockchain is invalid"); break; case "3": Console.WriteLine(blockchain.CountBalanceForWallet(wallet.PublicKey)); break; case "4": Console.WriteLine("Bye!"); break; case "5": Wallet newOne = walletManager.AddNewWallet(); Console.WriteLine("New wallet created!"); Console.WriteLine($"Public key: {hashFactory.GetByteConverter().ConvertToString(newOne.PublicKey)}"); break; default: Console.WriteLine("Wrong input!"); break; } }while (input != "4"); network.Stop(); }