├── blockchain-aymen .jpg ├── tsconfig.json ├── main.ts ├── package.json ├── BlockChain.ts ├── Block.ts └── README.md /blockchain-aymen .jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aymen94/simple-Blockchain/HEAD/blockchain-aymen .jpg -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "sourceMap": true 5 | } 6 | } -------------------------------------------------------------------------------- /main.ts: -------------------------------------------------------------------------------- 1 | import { BlockChain } from './BlockChain'; 2 | import { Block } from './block'; 3 | 4 | 5 | var AyCoin = new BlockChain(3); 6 | 7 | AyCoin.addBlock(new Block(1,{user:"Aymen Naghmouchi",Amount:100})); 8 | AyCoin.addBlock(new Block(2,{user:"Aymen Naghmouchi",Amount:2500})); 9 | AyCoin.addBlock(new Block(3,{user:"Aymen Naghmouchi",Amount:50})); 10 | 11 | 12 | 13 | console.log(JSON.stringify(AyCoin,null,2)); 14 | 15 | console.log("all blocks are linked? "+AyCoin.checkBlockChain()); 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "blockchain", 3 | "version": "1.0.0", 4 | "description": "blockchai tutorial", 5 | "main": "main.js", 6 | "dependencies": { 7 | "crypto-js": "^3.1.9-1" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "tsc main.ts", 12 | "start": "node main.js" 13 | }, 14 | "keywords": [ 15 | "blockchain", 16 | "javascript", 17 | "typescript", 18 | "learn", 19 | "structure" 20 | ], 21 | "author": "Aymen Naghmouchi", 22 | "license": "ISC" 23 | } 24 | -------------------------------------------------------------------------------- /BlockChain.ts: -------------------------------------------------------------------------------- 1 | import { Block } from './Block'; 2 | 3 | export class BlockChain{ 4 | private chain:Array; 5 | private miningDifficulty:number; 6 | 7 | constructor(miningD:number){ 8 | this.chain=new Array(new Block(0,{ firstBlock:"Created By Aymen Naghmouchi"})); 9 | this.miningDifficulty=miningD; 10 | } 11 | 12 | public getLastBlock():Block{ 13 | return this.chain[this.chain.length-1]; 14 | } 15 | 16 | public addBlock(newBlock:Block){ 17 | newBlock.setPrecedentHash(this.getLastBlock().getHashBlock()); 18 | newBlock.mineBlock(this.miningDifficulty); 19 | this.chain.push(newBlock); 20 | } 21 | 22 | 23 | 24 | public checkBlockChain():boolean{ 25 | for(let i:number=1;i index block. 14 | 15 | - timestamp ===> date of block instance. 16 | 17 | - data ===> any type of data to save in block 18 | 19 | - precedentHash ===>hash of the last block (lock added before this block). 20 | 21 | - hashBlock ===> hash of this block. 22 | 23 | - nonce ===>it has nothing to do with blocking, change to something random (example random number). 24 | 25 | 26 | #### constructor block 27 | ``` 28 | public constructor(index,data) 29 | ``` 30 | 31 | #### methods of the block. 32 | ``` 33 | public getHashBlock(); 34 | 35 | public getprecedentHash(); 36 | 37 | public setPrecedentHash(hash); 38 | 39 | public generateHash(); 40 | 41 | ``` 42 | 43 | > Proof-of-work is used to make secure the blockchain(similar bitcoin Hashcash) 44 | 45 | > more info https://en.wikipedia.org/wiki/Proof-of-work_system 46 | ``` 47 | public mineBlock(miningDifficulty:number){ 48 | while(this.hashBlock.substring(0,miningDifficulty)!=Array(miningDifficulty+1).join('0')){ 49 | this.nonce++; 50 | this.generateHash(); 51 | } 52 | ``` 53 | 54 | ----------------------------------------------------------------------------------------------------------- 55 | ## BlockChain.ts 56 | ### Instance variable 57 | - chain:Array ===> array of block,(better chained list). 58 | - miningDifficulty ===> the difficulty of mining expressed in numbers(the more the number is high the more the difficulty increases). 59 | 60 | #### constructor BlockChain 61 | > as a parameter it takes the difficulty of mining. 62 | > when to start the blockchain, add the first block (this block is without the previous hash block because is the first block) 63 | ``` 64 | constructor(miningD){ 65 | this.chain=new Array(new Block(0,{ firstBlock:"Created By Aymen Naghmouchi"})); 66 | this.miningDifficulty=miningD; 67 | } 68 | ``` 69 | 70 | #### methods of the BlockChain. 71 | ``` 72 | public getLastBlock(); 73 | 74 | public checkBlockChain(); 75 | 76 | ``` 77 | 78 | > set the precedent block hash in the last added and regenerate her hash 79 | ``` 80 | public addBlock(newBlock){ 81 | newBlock.setPrecedentHash(this.getLastBlock().getHashBlock()); 82 | newBlock.mineBlock(this.miningDifficulty); 83 | this.chain.push(newBlock); 84 | } 85 | ``` 86 | ![Alt text](https://raw.githubusercontent.com/aymen94/simple-Blockchain/master/blockchain-aymen%20.jpg?raw=true "Blockchain") 87 | 88 | 89 | > check all blocks are linked among them. 90 | ``` 91 | 92 | public checkBlockChain():boolean{ 93 | for(let i:number=1;i