package blockchain; import java.security.Security; import java.util.ArrayList; public class NoobChainV2 { public static ArrayList blockchain = new ArrayList(); public static int difficulty = 5; public static Wallet walletA; public static Wallet walletB; /** * @return Boolean */ public static Boolean isChainValid() { Block currentBlock; Block previousBlock; String hashTarget = new String(new char[difficulty]).replace('\0', '0'); // loop through blockchain to check hashes: for (int i = 1; i < blockchain.size(); i++) { currentBlock = blockchain.get(i); previousBlock = blockchain.get(i - 1); // compare registered hash and calculated hash: if (!currentBlock.hash.equals(currentBlock.calculateHash())) { System.out.println("Current Hashes not equal"); return false; } // compare previous hash and registered previous hash if (!previousBlock.hash.equals(currentBlock.previousHash)) { System.out.println("Previous Hashes not equal"); return false; } // check if hash is solved if (!currentBlock.hash.substring(0, difficulty).equals(hashTarget)) { System.out.println("This block hasn't been mined"); return false; } } return true; } /** * Noob blockchain. * * @param args no arguments */ public static void main(String[] args) { // Setup Bouncey castle as a Security Provider Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); // Create the new wallets walletA = new Wallet(); walletB = new Wallet(); // Test public and private keys System.out.println("Private key: " + StringUtil.getStringFromKey(walletA.privateKey)); System.out.println("Public key: " + StringUtil.getStringFromKey(walletA.publicKey)); // Create a test transaction from WalletA to walletB Transaction transaction = new Transaction(walletA.publicKey, walletB.publicKey, 5, null); transaction.generateSignature(walletA.privateKey); // Verify the signature works and verify it from the public key System.out.println("Is signature verified: " + transaction.verifiySignature()); } }