https://github.com/aymen94/simple-blockchain
small tutorial with code to try to create your blockchain 🔝
https://github.com/aymen94/simple-blockchain
blockchain javascript learn tutorial typescript
Last synced: about 1 month ago
JSON representation
small tutorial with code to try to create your blockchain 🔝
- Host: GitHub
- URL: https://github.com/aymen94/simple-blockchain
- Owner: aymen94
- Created: 2018-01-21T00:11:11.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-15T21:43:57.000Z (over 6 years ago)
- Last Synced: 2025-04-18T16:27:12.209Z (about 2 months ago)
- Topics: blockchain, javascript, learn, tutorial, typescript
- Language: TypeScript
- Homepage: https://medium.com/@aymennaghmouchi/how-to-work-blockchain-2b8631052335
- Size: 42 KB
- Stars: 23
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# How to Work BlockChain
how to work blockchain, this is only example structure. :smiley:small tutorial with code to try to create your blockchain.
I used typescript because is very easy to read/understand.
-----------------------------------------------------------------------------------------------------
## Block.ts
### Instance variable
- index ===> index block.- timestamp ===> date of block instance.
- data ===> any type of data to save in block
- precedentHash ===>hash of the last block (lock added before this block).
- hashBlock ===> hash of this block.
- nonce ===>it has nothing to do with blocking, change to something random (example random number).
#### constructor block
```
public constructor(index,data)
```#### methods of the block.
```
public getHashBlock();
public getprecedentHash();public setPrecedentHash(hash);
public generateHash();
```> Proof-of-work is used to make secure the blockchain(similar bitcoin Hashcash)
> more info https://en.wikipedia.org/wiki/Proof-of-work_system
```
public mineBlock(miningDifficulty:number){
while(this.hashBlock.substring(0,miningDifficulty)!=Array(miningDifficulty+1).join('0')){
this.nonce++;
this.generateHash();
}
```-----------------------------------------------------------------------------------------------------------
## BlockChain.ts
### Instance variable
- chain:Array ===> array of block,(better chained list).
- miningDifficulty ===> the difficulty of mining expressed in numbers(the more the number is high the more the difficulty increases).
#### constructor BlockChain
> as a parameter it takes the difficulty of mining.
> when to start the blockchain, add the first block (this block is without the previous hash block because is the first block)
```
constructor(miningD){
this.chain=new Array(new Block(0,{ firstBlock:"Created By Aymen Naghmouchi"}));
this.miningDifficulty=miningD;
}
```
#### methods of the BlockChain.
```
public getLastBlock();
public checkBlockChain();
```> set the precedent block hash in the last added and regenerate her hash
```
public addBlock(newBlock){
newBlock.setPrecedentHash(this.getLastBlock().getHashBlock());
newBlock.mineBlock(this.miningDifficulty);
this.chain.push(newBlock);
}
```
> check all blocks are linked among them.
```public checkBlockChain():boolean{
for(let i:number=1;i