{"id":19068779,"url":"https://github.com/aymen94/simple-blockchain","last_synced_at":"2025-04-28T13:46:27.501Z","repository":{"id":107480973,"uuid":"118291325","full_name":"aymen94/simple-Blockchain","owner":"aymen94","description":"small tutorial with code to try to create your blockchain 🔝","archived":false,"fork":false,"pushed_at":"2018-09-15T21:43:57.000Z","size":43,"stargazers_count":23,"open_issues_count":0,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-18T16:27:12.209Z","etag":null,"topics":["blockchain","javascript","learn","tutorial","typescript"],"latest_commit_sha":null,"homepage":"https://medium.com/@aymennaghmouchi/how-to-work-blockchain-2b8631052335","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aymen94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-21T00:11:11.000Z","updated_at":"2024-02-17T19:49:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"a26f1241-b70d-4aaa-98a1-506c393bdf15","html_url":"https://github.com/aymen94/simple-Blockchain","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymen94%2Fsimple-Blockchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymen94%2Fsimple-Blockchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymen94%2Fsimple-Blockchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymen94%2Fsimple-Blockchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aymen94","download_url":"https://codeload.github.com/aymen94/simple-Blockchain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251321808,"owners_count":21570798,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","javascript","learn","tutorial","typescript"],"created_at":"2024-11-09T01:11:56.072Z","updated_at":"2025-04-28T13:46:27.478Z","avatar_url":"https://github.com/aymen94.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# How to Work BlockChain  \nhow to work blockchain, this is only example structure. :smiley:\n\nsmall tutorial with code to try to create your blockchain.\n\nI used typescript because is very easy to read/understand.\n\n-----------------------------------------------------------------------------------------------------\n\n## Block.ts\n\n### Instance variable\n- index ===\u003e index block.\n\n- timestamp ===\u003e date of block instance.\n\n- data ===\u003e any type of  data to save in block\n\n- precedentHash ===\u003ehash of the last block (lock added before this block).\n\n- hashBlock ===\u003e hash of this block.\n\n- nonce ===\u003eit has nothing to do with blocking, change to something random (example random number).\n\n\n#### constructor block\n```\npublic constructor(index,data)\n```\n\n#### methods of the block.\n```\n public getHashBlock();\n \n public getprecedentHash();\n\n public setPrecedentHash(hash);\n \n public  generateHash();\n \n```\n\n\u003e Proof-of-work  is used to  make secure the blockchain(similar bitcoin Hashcash)\n\n\u003e more info https://en.wikipedia.org/wiki/Proof-of-work_system\n```\npublic mineBlock(miningDifficulty:number){\n    while(this.hashBlock.substring(0,miningDifficulty)!=Array(miningDifficulty+1).join('0')){\n      this.nonce++;\n      this.generateHash();\n  }\n```\n\n-----------------------------------------------------------------------------------------------------------\n## BlockChain.ts\n### Instance variable\n- chain:Array\u003cBlock\u003e ===\u003e  array of block,(better chained list).\n- miningDifficulty ===\u003e the difficulty of mining expressed in numbers(the more the number is high the more the difficulty increases).\n \n #### constructor BlockChain\n \u003e as a parameter it takes the difficulty of mining. \n \u003e when to start the blockchain, add the first block (this block is without the previous hash block because is the first block)\n  ```\n constructor(miningD){\n   this.chain=new Array\u003cBlock\u003e(new Block(0,{ firstBlock:\"Created By Aymen Naghmouchi\"}));\n   this.miningDifficulty=miningD;\n }\n ```\n \n #### methods of the BlockChain.\n```\n  public getLastBlock();\n  \n  public checkBlockChain();\n  \n```\n\n\u003e set the precedent block hash in the last added and regenerate her hash\n```\n public addBlock(newBlock){\n   newBlock.setPrecedentHash(this.getLastBlock().getHashBlock());\n   newBlock.mineBlock(this.miningDifficulty);\n   this.chain.push(newBlock);\n  }\n```\n![Alt text](https://raw.githubusercontent.com/aymen94/simple-Blockchain/master/blockchain-aymen%20.jpg?raw=true \"Blockchain\")\n\n\n\u003e check all blocks are linked among them.\n```\n\n  public checkBlockChain():boolean{\n    for(let i:number=1;i\u003cthis.chain.length;i++){\n      let actualblock:Block=this.chain[i];\n      let precedentblock:Block=this.chain[i-1];\n      if(actualblock.getPrecedentHash()!==precedentblock.getHashBlock())\n        return false;\n    }\n    return true;\n  }\n```\n\n# OUTPUT\n```\n{\n  \"chain\": [\n    {\n      \"index\": 0,\n      \"timestamp\": \"2018-01-21T14:23:12.424Z\",\n      \"data\": {\n        \"firstBlock\": \"Created By Aymen Naghmouchi\"\n      },\n      \"hashBlock\": \"47234aa1697b3fd20bc5db17b899ac0d1d6adc7608cbca7fbdcd515d89c81954\",\n      \"nonce\": 0\n    },\n    {\n      \"index\": 1,\n      \"timestamp\": \"2018-01-21T14:23:12.428Z\",\n      \"data\": {\n        \"user\": \"Aymen Naghmouchi\",\n        \"Amount\": 100\n      },\n      \"hashBlock\": \"0008d50dfd76575392335a0df6b82d52826c75ee827b0c639c77405dc40b22cc\",\n      \"nonce\": 4636,\n      \"precedentHash\": \"47234aa1697b3fd20bc5db17b899ac0d1d6adc7608cbca7fbdcd515d89c81954\"\n    },\n    {\n      \"index\": 2,\n      \"timestamp\": \"2018-01-21T14:23:12.515Z\",\n      \"data\": {\n        \"user\": \"Aymen Naghmouchi\",\n        \"Amount\": 2500\n      },\n      \"hashBlock\": \"0000c505fb52117656109daff8c9d18325b782b9674eb6f0d64840de43f0f0cc\",\n      \"nonce\": 4302,\n      \"precedentHash\": \"0008d50dfd76575392335a0df6b82d52826c75ee827b0c639c77405dc40b22cc\"\n    },\n    {\n      \"index\": 3,\n      \"timestamp\": \"2018-01-21T14:23:12.566Z\",\n      \"data\": {\n        \"user\": \"Aymen Naghmouchi\",\n        \"Amount\": 50\n      },\n      \"hashBlock\": \"000fd901ae1c2622cb9b48a801099d8f5c884d73b86402078cdf9eea78c787eb\",\n      \"nonce\": 1479,\n      \"precedentHash\": \"0000c505fb52117656109daff8c9d18325b782b9674eb6f0d64840de43f0f0cc\"\n    }\n  ],\n  \"miningDifficulty\": 3\n}\nall blocks are linked? true\n```\n\n\n\n## How to use\n* git clone or fork it\n* npm install\n* npm start\n\n### compile\n```\ntsc main.ts\n```\n### execute\n```\nnode main.js\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faymen94%2Fsimple-blockchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faymen94%2Fsimple-blockchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faymen94%2Fsimple-blockchain/lists"}