Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var sender   = new Signature();
            var receiver = new Signature();

            var blockChain = new BlockChain(difficulty: 2);

            var transaction = new Transaction(sender, receiver.PublicKey, 42.42m);

            blockChain.AddContent(transaction.Serialize());
            var smallTransaction = new Transaction(sender, receiver.PublicKey, 0.42m);

            blockChain.AddContent(smallTransaction.Serialize());

            System.Console.WriteLine($"Valide Chain? {blockChain.IsChainValid()}");

            foreach (var block in blockChain)
            {
                Console.WriteLine($"Block:\n{block}\n---\n");

                var blockTransaktion = new Transaction(block.Content);
                Console.WriteLine($"Transaktion:\n{blockTransaktion}\n");
                Console.WriteLine($"valide? {blockTransaktion.Verify()}");
                Console.WriteLine("=================\n\n");
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Wallet walletA = new Wallet();
            Wallet walletB = new Wallet();

            Wallet coinbase = new Wallet();


            Transaction genesisTransaction = new Transaction(coinbase.PublicKey, walletA.PublicKey, 100, null);

            genesisTransaction.GenerateSignature(coinbase.PrivateKey);
            genesisTransaction.TransactionId = "0";
            genesisTransaction.Outputs.Add(new TransactionOutput(genesisTransaction.RecipientPublicKey, genesisTransaction.Value, genesisTransaction.TransactionId));
            BlockChain.AddUnspentTransactionOutput(genesisTransaction.Outputs[0].Id, genesisTransaction.Outputs[0]);

            Console.WriteLine("Creating and Mining Genesis block - Transferring 100 coins from coinbase to Wallet A");
            BlockChain.AddBlockTransaction(genesisTransaction);

            Console.WriteLine($"Wallet A Balance: {walletA.GetBalance()}");
            Console.WriteLine("Attempting to transfer 30 coins from Wallet A to Wallet B");
            var transaction = walletA.SendFunds(walletB.PublicKey, 30);

            if (transaction == null)
            {
                Console.WriteLine("Wallet A does not have funds to transfer");
            }
            else
            {
                BlockChain.AddBlockTransaction(transaction);
                Console.WriteLine($"Wallet A Balance: {walletA.GetBalance()}");
                Console.WriteLine($"Wallet B Balance: {walletB.GetBalance()}");
            }


            Console.WriteLine("Attempting to transfer 10 coins from Wallet B to Wallet A");
            var transaction1 = walletB.SendFunds(walletA.PublicKey, 10);

            if (transaction1 == null)
            {
                Console.WriteLine("Wallet B does not have funds to transfer");
            }
            else
            {
                BlockChain.AddBlockTransaction(transaction1);
                Console.WriteLine($"Wallet A Balance: {walletA.GetBalance()}");
                Console.WriteLine($"Wallet B Balance: {walletB.GetBalance()}");
            }

            Console.WriteLine("Check if Chain is valid");
            Console.WriteLine(BlockChain.IsChainValid());

            Console.WriteLine("BlockChain Display");
            Console.WriteLine(BlockChain.Display());
        }