Skip to content

Instantly share code, notes, and snippets.

@ljahier
Created September 20, 2019 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljahier/fb5d95feec4b4864a4f96064163223cb to your computer and use it in GitHub Desktop.
Save ljahier/fb5d95feec4b4864a4f96064163223cb to your computer and use it in GitHub Desktop.
const SHA256 = require("crypto-js/sha256")
const fs = require("fs")
// Une transaction est l'achat ou la vente de notre crypto
class Transaction {
// Le constructeur est la pour quand on initialise une nouvelle instance
// de la classe transaction on peu directement lui passer des parametres
constructor(index, timestamp, data, previousHash = '') {
this.index = index
this.timestamp = timestamp
this.data = data
this.previousHash = previousHash
this.hash = this.calculateHash()
this.nonce = 0
}
// calculateHash() permet de pouvoir généré un hash a chaque transaction qui nous permet par la suite
// de pouvoir verifier que la nouvelle transaction est bien une transaction possible
calculateHash() {
return SHA256(this.index + this.timestamp + JSON.stringify(this.data) + this.previousHash + this.nonce).toString()
}
proofOfWork(difficulty) {
while (this.hash.substr(0, difficulty) !== Array(difficulty + 1).join("0")) {
this.nonce++
this.hash = this.calculateHash()
console.log(this.nonce + " : " + this.hash)
}
}
}
// C'est la crypto créer
class Blockchain {
constructor() {
// On créer le block 1er (genèse)
this.chain = [this.createGenesisTransaction()]
this.difficulty = 1
}
// fonction de création de la premiere transaction de la crypto
createGenesisTransaction() {
return new Transaction(0, new Date, "Genesis transaction", "0")
}
// Pouvoir récuperé la derniere transaction créée
getLatestTransaction() {
return this.chain[this.chain.length - 1]
}
// Créer une nouvelle transaction
addTransaction(newTransaction) {
newTransaction.previousHash = this.getLatestTransaction().hash
newTransaction.proofOfWork(this.difficulty)
this.chain.push(newTransaction)
const data = new Uint8Array(Buffer.from(JSON.stringify(newTransaction)));
fs.appendFile('data.json', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
}
// tester si la blockchain est bien valide
validateTransaction() {
// Boucle qui permet de pouvoir itéré chacune des transaction faite
for (let i = 1; i < this.chain.length; i++) {
// la transaction courante
const currentTransaction = this.chain[i]
// la derniere transaction
const previousTransaction = this.chain[i - 1]
// On test que le hash de la transaction en cours est bien le meme que celui que l'on recalcul
if (currentTransaction.hash !== currentTransaction.calculateHash()) {
return false
}
// on test que la valeur de previousHash de la transaction en cours est bien le meme que le hash de la transaction précedente
if (currentTransaction.previousHash !== previousTransaction.hash) {
return false
}
}
return true
}
}
let myCoin = new Blockchain()
myCoin.addTransaction(new Transaction(1, new Date, { amount: 1 }))
myCoin.addTransaction(new Transaction(2, new Date, { amount: 10 }))
myCoin.addTransaction(new Transaction(3, new Date, { amount: 100 }))
myCoin.addTransaction(new Transaction(4, new Date, { amount: 1000 }))
console.log()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment