que fait ce code const crypto = require('crypto'); const EC = require('elliptic').ec;…
- User
que fait ce code const crypto = require('crypto'); const EC = require('elliptic').ec; const ec = new EC('secp256k1'); // même courbe utilisée par Bitcoin, Ethereum
function isValidAddress(address) { if(address === null) return true; try { ec.keyFromPublic(address, 'hex'); return true; } catch (e) { return false; } }
class Transaction { constructor(fromAddress, toAddress, amount, fee = 0, nonce = 0) { this.fromAddress = fromAddress; this.toAddress = toAddress; this.amount = amount; this.fee = fee; this.nonce = nonce; this.timestamp = Date.now(); this.id = this.calculateHash(); this.signature = null; }
calculateHash() { return crypto.createHash('sha256') .update(this.fromAddress + this.toAddress + this.amount + this.fee + this.nonce + this.timestamp) .digest('hex'); }
signTransaction(signingKey) { if(signingKey.getPublic('hex') !== this.fromAddress) { throw new Error("Tu ne peux pas signer une transaction pour un autre portefeuille!"); } const hashTx = this.calculateHash(); const sig = signingKey.sign(hashTx, 'base64'); this.signature = sig.toDER('hex'); }
isValid() { if(this.fromAddress === null) return true; if(!this.signature || this.signature.length === 0) { throw new Error("Pas de signature dans cette transaction"); } const publicKey = ec.keyFromPublic(this.fromAddress, 'hex'); return publicKey.verify(this.calculateHash(), this.signature); } }
class Block { constructor(index, timestamp, transactions, previousHash = '') { this.index = index; this.timestamp = timestamp; this.transactions = transactions; this.previousHash = previousHash; this.nonce = 0; this.hash = this.calculateHash(); }
calculateHash() { return crypto.createHash('sha256') .update(this.index + this.previousHash + this.timestamp + JSON.stringify(this.transactions) + this.nonce) .digest('hex'); }
mineBlock(difficulty) { const target = Array(difficulty + 1).join("0"); while(this.hash.substring(0, difficulty) !== target) { this.nonce++; this.hash = this.calculateHash(); } console.log(
Block mined: ${this.hash} (nonce=${this.nonce})); }hasValidTransactions() { for(const tx of this.transactions) { if(!tx.isValid()) { return false; } } return true; } }
class Blockchain { constructor() { this.chain = [this.createGenesisBlock()]; this.difficulty = 3; this.pendingTransactions = []; this.miningReward = 100; this.blockTimeTarget = 30000; }
createGenesisBlock() { return new Block(0, Date.now().toString(), [], "0"); }
getLatestBlock() { return this.chain[this.chain.length -1]; }
getBalanceOfAddress(address) { let balance = 0; for(const block of this.chain) { for(const trans of block.transactions) { if(trans.fromAddress === address) { balance -= (trans.amount + (trans.fee || 0)); } if(trans.toAddress === address) { balance += trans.amount; } } } return balance; }
getPendingBalanceOfAddress(address) { let balance = this.getBalanceOfAddress(address); for (const tx of this.pendingTransactions) { if (tx.fromAddress === address) { balance -= (tx.amount + (tx.fee || 0)); } if (tx.toAddress === address) { balance += tx.amount; } } return balance; }
- Assistant