Created
April 20, 2022 11:10
-
-
Save YashKarthik/9f0f7373a11266475fc8391e08338ff8 to your computer and use it in GitHub Desktop.
Sample blockchain from fireship
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as crypto from "crypto"; | |
class Transaction { | |
constructor( | |
public amount: number, | |
public payer: string, | |
public payee: string | |
) {} | |
toString() { | |
return JSON.stringify(this) | |
} | |
} | |
class Block { | |
public nonce = Math.round(Math.random() * 999999999) | |
constructor( | |
public prevHash: string, | |
public transaction: Transaction, | |
public ts = Date.now() | |
) {} | |
get hash() { | |
const str = JSON.stringify(this); | |
const hash = crypto.createHash('SHA256'); | |
hash.update(str).end(); | |
return hash.digest('hex');; | |
} | |
} | |
class Chain { | |
public static instance = new Chain(); | |
chain: Block[]; | |
constructor() { | |
this.chain = [new Block(null, new Transaction(100, 'genesis', 'sotashi'))]; | |
} | |
get lastBlock() { | |
return this.chain[this.chain.length - 1] | |
} | |
mine(nonce: number) { | |
let solution = 1; | |
console.log('Mining...') | |
while(true) { | |
const hash = crypto.createHash('MDS'); | |
hash.update((nonce + solution).toString()).end(); | |
const attempt = hash.digest('hex'); | |
if (attempt.substr(0, 4) === "0000") { | |
console.log(`Solved: ${solution}`); | |
return solution; | |
} | |
solution++; | |
} | |
} | |
addBlock( | |
transaction: Transaction, | |
senderPubicKey: string, | |
signature: string | |
) { | |
const verifier = crypto.createVerify('SHA256'); | |
verifier.update(transaction.toString()); | |
const isValid = verifier.verify(senderPubicKey, signature); | |
if (isValid) { | |
const newBlock = new Block(this.lastBlock.hash, transaction); | |
this.chain.push(newBlock); | |
} | |
} | |
} | |
class Wallet { | |
public publicKey: string; | |
public privateKey: string; | |
constructor() { | |
const keypair = crypto.generateKeyPairSync('rsa', { | |
modulusLength: 2048, | |
publicKeyencoding: { type: 'spki', format: 'pem' }, | |
privateKeyencoding: { type: 'pkcs8', format: 'pem' }, | |
}); | |
this.privateKey = keypair.privateKey; | |
this.publicKey = keypair.publicKey; | |
} | |
sendMoney(amount: number, payeePublicKey: string) { | |
const transaction = new Transaction(amount, this.publicKey, payeePublicKey); | |
const sign = crypto.createsign('SHA25'); | |
sign.update(transaction.toString()).end(); | |
const signature = sign.sign(this.privateKey); | |
Chain.instance.addBlock(transaction, this.publicKey, signature); | |
} | |
} | |
const satoshi = new Wallet(); | |
const bob = new Wallet(); | |
const alice = new Wallet(); | |
satoshi.sendMoney(50, bob.publicKey); | |
bob.sendMoney(20, alice.publicKey); | |
alice.sendMoney(30, bob.publicKey); | |
console.log(Chain.instance); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment