├── src ├── key.txt ├── models │ ├── keygenerator.js │ ├── block.js │ ├── transaction.js │ └── block_chain.js └── app.js ├── package.json └── LICENSE /src/key.txt: -------------------------------------------------------------------------------- 1 | Private key is ffe4260ff4b9845ddf973bb802f7bcd0257c47687abceba1ae6a04a71c1b8afa 2 | 3 | Public key is 04776e0301c3ed70c39db0ec014c2b24ad420df35df6b1b735af2344a4556c576c7b9c88a78adce5adbda3bc727c58484f7e25114472ae076037f7477957e7cc40 -------------------------------------------------------------------------------- /src/models/keygenerator.js: -------------------------------------------------------------------------------- 1 | const EC = require('elliptic').ec; 2 | const ec = new EC('secp256k1'); 3 | 4 | const key = ec.genKeyPair(); 5 | const publicKey = key.getPublic('hex'); 6 | const privateKey = key.getPrivate('hex'); 7 | 8 | console.log(); 9 | console.log('Public key is', publicKey); 10 | console.log(); 11 | console.log('Private key is', privateKey); -------------------------------------------------------------------------------- /src/app.js: -------------------------------------------------------------------------------- 1 | const Blockchain = require('./models/block_chain'); 2 | const Transaction = require('./models/transaction'); 3 | 4 | let lambiCoin = new Blockchain(); 5 | lambiCoin.createTransaction(new Transaction("add1", "add2", 100)); 6 | lambiCoin.createTransaction(new Transaction("add2", "add1", 510)); 7 | lambiCoin.createTransaction(new Transaction("add2", "add3", 510)); 8 | lambiCoin.createTransaction(new Transaction("add2", "add4", 510)); 9 | 10 | console.log("Starting the miner"); 11 | 12 | while (1) { 13 | lambiCoin.minePendingTransactions("lambi-address"); 14 | 15 | console.log( 16 | "My coin is: ", 17 | lambiCoin.getBalanceOfAddress("lambi-address") 18 | ); 19 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "crypto-js": "^4.0.0", 4 | "elliptic": "^6.5.4" 5 | }, 6 | "devDependencies": { 7 | "babel-cli": "^6.18.0", 8 | "babel-core": "^6.26.3", 9 | "nodemon": "^2.0.7" 10 | }, 11 | "name": "blockchain_js", 12 | "version": "1.0.0", 13 | "main": "index.js", 14 | "start": "src/app.js", 15 | "scripts": { 16 | "test": "echo \"Error: no test specified\" && exit 1", 17 | "start": "nodemon src/app.js" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/lambiengcode/blockchain_js.git" 22 | }, 23 | "keywords": [], 24 | "author": "lambiengcode", 25 | "license": "ISC", 26 | "bugs": { 27 | "url": "https://github.com/lambiengcode/blockchain_js/issues" 28 | }, 29 | "homepage": "https://github.com/lambiengcode/blockchain_js#readme", 30 | "description": "" 31 | } 32 | -------------------------------------------------------------------------------- /src/models/block.js: -------------------------------------------------------------------------------- 1 | const SHA256 = require("crypto-js/sha256"); 2 | 3 | class Block { 4 | constructor(timestamp, transactions, previousHash = "") { 5 | this.timestamp = timestamp; 6 | this.transactions = transactions; 7 | this.previousHash = previousHash; 8 | this.hash = this.calculateHash(); 9 | this.nonce = 0; 10 | } 11 | 12 | calculateHash() { 13 | return SHA256( 14 | this.index + 15 | this.previousHash + 16 | this.timestamp + 17 | JSON.stringify(this.data) + 18 | this.nonce 19 | ).toString(); 20 | } 21 | 22 | mineBlock(difficulty) { 23 | while ( 24 | this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0") 25 | ) { 26 | this.nonce++; 27 | this.hash = this.calculateHash(); 28 | } 29 | 30 | console.log(this.hash, this.nonce); 31 | } 32 | } 33 | 34 | module.exports = Block; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Dao Hong Vinh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/models/transaction.js: -------------------------------------------------------------------------------- 1 | const { SHA256 } = require("crypto-js"); 2 | 3 | const SHA256 = require("crypto-js/sha256"); 4 | 5 | class Transaction { 6 | constructor(fromAddress, toAddress, amount) { 7 | this.fromAddress = fromAddress; 8 | this.toAddress = toAddress; 9 | this.amount = amount; 10 | } 11 | 12 | calculateHash() { 13 | return SHA256(this.fromAddress + this.toAddress + this.amount).toString(); 14 | } 15 | 16 | signTransaction(signingKey) { 17 | if (signingKey.getPublic('hex') !== this.fromAddress) { 18 | throw new Error('You cannot sign transactions for other wallets!'); 19 | } 20 | 21 | const hashTx = this.calculateHash(); 22 | const sig = signingKey.sign(hashTx, 'base64'); 23 | this.signature = sig.toDER('hex'); 24 | } 25 | 26 | isValid() { 27 | if (this.fromAddress === null) return true; 28 | 29 | if (!this.signature || this.signature.length === 0) { 30 | throw new Error('No signature in this transaction'); 31 | } 32 | 33 | const publicKey = ec.keyFromPublic(this.fromAddress, 'hex'); 34 | return publicKey.verify(this.calculateHash(), this.signature); 35 | } 36 | } 37 | 38 | module.exports = Transaction; -------------------------------------------------------------------------------- /src/models/block_chain.js: -------------------------------------------------------------------------------- 1 | const Block = require('./block'); 2 | const Transaction = require('./transaction'); 3 | 4 | class Blockchain { 5 | constructor() { 6 | this.chain = [this.createGenesisBlock()]; 7 | this.difficulty = 3; 8 | this.pendingTransactions = []; 9 | this.miningReward = this.calculateReward(); 10 | } 11 | 12 | calculateReward() { 13 | var rewardStr = "0."; 14 | while (this.difficulty != rewardStr.length - 2) { 15 | rewardStr += "0"; 16 | } 17 | return parseFloat((rewardStr += "1")); 18 | } 19 | 20 | createGenesisBlock() { 21 | return new Block("01/01/2021", "Genesis Block", "0"); 22 | } 23 | 24 | getLatestBlock() { 25 | return this.chain[this.chain.length - 1]; 26 | } 27 | 28 | addBlock(newBlock) { 29 | newBlock.previousHash = this.getLatestBlock().hash; 30 | newBlock.mineBlock(this.difficulty); 31 | this.chain.push(newBlock); 32 | } 33 | 34 | minePendingTransactions(miningRewardAddress) { 35 | let block = new Block(Date.now(), this.pendingTransactions); 36 | block.mineBlock(this.difficulty); 37 | 38 | console.log("Block successfully mined!"); 39 | this.chain.push(block); 40 | 41 | this.pendingTransactions = [ 42 | new Transaction(null, miningRewardAddress, this.miningReward), 43 | ]; 44 | } 45 | 46 | createTransaction(transaction) { 47 | this.pendingTransactions.push(transaction); 48 | } 49 | 50 | getBalanceOfAddress(address) { 51 | let balance = 0; 52 | 53 | for (const block of this.chain) { 54 | for (const trans of block.transactions) { 55 | if (trans.fromAddress === address) { 56 | balance -= trans.amount; 57 | } 58 | 59 | if (trans.toAddress === address) { 60 | balance += trans.amount; 61 | } 62 | } 63 | } 64 | 65 | return balance; 66 | } 67 | 68 | isChainValid() { 69 | for (let i = 1; i < this.chain.length; i++) { 70 | const currentBlock = this.chain[i]; 71 | const previousBlock = this.chain[i - 1]; 72 | 73 | if (currentBlock.hash != currentBlock.calculateHash()) { 74 | return false; 75 | } 76 | 77 | if (currentBlock.previousHash != previousBlock.hash) { 78 | return false; 79 | } 80 | } 81 | return true; 82 | } 83 | } 84 | 85 | module.exports = Blockchain; 86 | --------------------------------------------------------------------------------