[
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2017 Kassius \n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# NoobChain-Tutorial-Part-1\nA Simple Java Blockchain for educational purposes. \n\nThis is for https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa tutorial.\n*If you have any other questions you can message me on the [Blockchain Developers Club](https://discord.gg/ZsyQqyk) discord server.*\n\n# Dependencies: You will need to import GSON:\n- gson [gson-2.8.2.jar](http://central.maven.org/maven2/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar)\n\n# Java Version:\n- JDK1.8.0_77\n\ncontact: kassCrypto@gmail.com\n"
  },
  {
    "path": "src/noobchain/Block.java",
    "content": "package noobchain;\r\n\r\nimport java.util.Date;\r\n\r\npublic class Block {\r\n\t\r\n\tpublic String hash;\r\n\tpublic String previousHash; \r\n\tprivate String data; //our data will be a simple message.\r\n\tprivate long timeStamp; //as number of milliseconds since 1/1/1970.\r\n\tprivate int nonce;\r\n\t\r\n\t//Block Constructor.  \r\n\tpublic Block(String data,String previousHash ) {\r\n\t\tthis.data = data;\r\n\t\tthis.previousHash = previousHash;\r\n\t\tthis.timeStamp = new Date().getTime();\r\n\t\t\r\n\t\tthis.hash = calculateHash(); //Making sure we do this after we set the other values.\r\n\t}\r\n\t\r\n\t//Calculate new hash based on blocks contents\r\n\tpublic String calculateHash() {\r\n\t\tString calculatedhash = StringUtil.applySha256( \r\n\t\t\t\tpreviousHash +\r\n\t\t\t\tLong.toString(timeStamp) +\r\n\t\t\t\tInteger.toString(nonce) + \r\n\t\t\t\tdata \r\n\t\t\t\t);\r\n\t\treturn calculatedhash;\r\n\t}\r\n\t\r\n\t//Increases nonce value until hash target is reached.\r\n\tpublic void mineBlock(int difficulty) {\r\n\t\tString target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * \"0\" \r\n\t\twhile(!hash.substring( 0, difficulty).equals(target)) {\r\n\t\t\tnonce ++;\r\n\t\t\thash = calculateHash();\r\n\t\t}\r\n\t\tSystem.out.println(\"Block Mined!!! : \" + hash);\r\n\t}\r\n\t\r\n}\r\n"
  },
  {
    "path": "src/noobchain/NoobChain.java",
    "content": "package noobchain;\r\nimport java.util.ArrayList;\r\nimport com.google.gson.GsonBuilder;\r\n\r\npublic class NoobChain {\r\n\t\r\n\tpublic static ArrayList<Block> blockchain = new ArrayList<Block>();\r\n\tpublic static int difficulty = 5;\r\n\r\n\tpublic static void main(String[] args) {\t\r\n\t\t//add our blocks to the blockchain ArrayList:\r\n\t\t\r\n\t\tSystem.out.println(\"Trying to Mine block 1... \");\r\n\t\taddBlock(new Block(\"Hi im the first block\", \"0\"));\r\n\t\t\r\n\t\tSystem.out.println(\"Trying to Mine block 2... \");\r\n\t\taddBlock(new Block(\"Yo im the second block\",blockchain.get(blockchain.size()-1).hash));\r\n\t\t\r\n\t\tSystem.out.println(\"Trying to Mine block 3... \");\r\n\t\taddBlock(new Block(\"Hey im the third block\",blockchain.get(blockchain.size()-1).hash));\t\r\n\t\t\r\n\t\tSystem.out.println(\"\\nBlockchain is Valid: \" + isChainValid());\r\n\t\t\r\n\t\tString blockchainJson = StringUtil.getJson(blockchain);\r\n\t\tSystem.out.println(\"\\nThe block chain: \");\r\n\t\tSystem.out.println(blockchainJson);\r\n\t}\r\n\t\r\n\tpublic static Boolean isChainValid() {\r\n\t\tBlock currentBlock; \r\n\t\tBlock previousBlock;\r\n\t\tString hashTarget = new String(new char[difficulty]).replace('\\0', '0');\r\n\t\t\r\n\t\t//loop through blockchain to check hashes:\r\n\t\tfor(int i=1; i < blockchain.size(); i++) {\r\n\t\t\tcurrentBlock = blockchain.get(i);\r\n\t\t\tpreviousBlock = blockchain.get(i-1);\r\n\t\t\t//compare registered hash and calculated hash:\r\n\t\t\tif(!currentBlock.hash.equals(currentBlock.calculateHash()) ){\r\n\t\t\t\tSystem.out.println(\"Current Hashes not equal\");\t\t\t\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t//compare previous hash and registered previous hash\r\n\t\t\tif(!previousBlock.hash.equals(currentBlock.previousHash) ) {\r\n\t\t\t\tSystem.out.println(\"Previous Hashes not equal\");\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t//check if hash is solved\r\n\t\t\tif(!currentBlock.hash.substring( 0, difficulty).equals(hashTarget)) {\r\n\t\t\t\tSystem.out.println(\"This block hasn't been mined\");\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\t\r\n\tpublic static void addBlock(Block newBlock) {\r\n\t\tnewBlock.mineBlock(difficulty);\r\n\t\tblockchain.add(newBlock);\r\n\t}\r\n}\r\n"
  },
  {
    "path": "src/noobchain/StringUtil.java",
    "content": "package noobchain;\r\nimport java.security.MessageDigest;\r\n\r\nimport com.google.gson.GsonBuilder;\r\n\r\npublic class StringUtil {\r\n\t\r\n\t//Applies Sha256 to a string and returns the result. \r\n\tpublic static String applySha256(String input){\r\n\t\t\r\n\t\ttry {\r\n\t\t\tMessageDigest digest = MessageDigest.getInstance(\"SHA-256\");\r\n\t        \r\n\t\t\t//Applies sha256 to our input, \r\n\t\t\tbyte[] hash = digest.digest(input.getBytes(\"UTF-8\"));\r\n\t        \r\n\t\t\tStringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal\r\n\t\t\tfor (int i = 0; i < hash.length; i++) {\r\n\t\t\t\tString hex = Integer.toHexString(0xff & hash[i]);\r\n\t\t\t\tif(hex.length() == 1) hexString.append('0');\r\n\t\t\t\thexString.append(hex);\r\n\t\t\t}\r\n\t\t\treturn hexString.toString();\r\n\t\t}\r\n\t\tcatch(Exception e) {\r\n\t\t\tthrow new RuntimeException(e);\r\n\t\t}\r\n\t}\r\n\t\r\n\t//Short hand helper to turn Object into a json string\r\n\tpublic static String getJson(Object o) {\r\n\t\treturn new GsonBuilder().setPrettyPrinting().create().toJson(o);\r\n\t}\r\n\t\r\n\t//Returns difficulty string target, to compare to hash. eg difficulty of 5 will return \"00000\"  \r\n\tpublic static String getDificultyString(int difficulty) {\r\n\t\treturn new String(new char[difficulty]).replace('\\0', '0');\r\n\t}\r\n\t\r\n\t\r\n}\r\n"
  }
]