{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-stable/src/blockchain/model/block/block.js"],"names":["forge","Transaction","TransactionInputCollection","TransactionOutputCollection","C_BLOCK_REWARD","Block","constructor","minerWallet","transactions","miningProof","previousHash","isFinalized","Error","timestamp","Date","now","minerHash","hash","minerPublicKey","publicKey","reward","fees","_sendReward","_hash","digest","toHex","signature","_generateSignature","displayName","substring","isSigned","isVerified","verify","bytes","inputs","addMiningReward","finalize","outputs","add","transaction","forEach","input","transactionHash","sign","sha256","md","create","update"],"mappings":";;;;;;AAAOA,W;;AACAC,iB;;AACAC,gC;;AACAC,iC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEP,YAAMC,iBAAiB,EAAvB;;;;;;;;;;;;;;;AAEe,YAAMC,KAAN,CAAY;AACzBC,oBAAYC,WAAZ,EAAyBC,YAAzB,EAAuCC,WAAvC,EAAoDC,YAApD,EAAkE;AAChE,cAAI,CAACD,YAAYE,WAAZ,EAAL,EAAgC;AAC9B,kBAAM,IAAIC,KAAJ,CAAU,4BAAV,CAAN;AACD;;AAED,cAAIJ,aAAaG,WAAb,EAAJ,EAAgC;AAC9B,kBAAM,IAAIC,KAAJ,CAAU,wCAAV,CAAN;AACD;;AAED,eAAKC,SAAL,GAAiBC,KAAKC,GAAL,EAAjB;AACA,eAAKC,SAAL,GAAiBT,YAAYU,IAA7B;AACA,eAAKC,cAAL,GAAsBX,YAAYY,SAAlC;AACA,eAAKX,YAAL,GAAoBA,YAApB;AACA,eAAKC,WAAL,GAAmBA,WAAnB;AACA,eAAKW,MAAL,GAAcZ,aAAaa,IAAb,GAAoBjB,cAAlC;;AAEA,eAAKkB,WAAL,CAAiBf,WAAjB;;AAEA,eAAKG,YAAL,GAAoBA,YAApB;AACA,eAAKO,IAAL,GAAY,KAAKM,KAAL,GAAaC,MAAb,GAAsBC,KAAtB,EAAZ;AACA,eAAKC,SAAL,GAAiB,KAAKC,kBAAL,CAA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cAAI,CAAC,KAAKH,WAAL,CAAiBE,WAAjB,EAAL,EAAqC;AACnC,kBAAM,IAAIC,KAAJ,CAAU,4BAAV,CAAN;AACD;;AAED;AACA;AACA;AACA,iBAAOL,YAAYoC,IAAZ,CAAiB,KAAKpB,KAAL,EAAjB,CAAP;AACD;;AAEDA,gBAAQ;AACN,gBAAMqB,SAAS5C,MAAM6C,EAAN,CAASD,MAAT,CAAgBE,MAAhB,EAAf;AACA,iBAAOF,OAAOG,MAAP,CACL,KAAKlC,SAAL,GACA,KAAKG,SADL,GAEA,KAAKR,YAAL,CAAkBS,IAFlB,GAGA,KAAKR,WAAL,CAAiBQ,IAHjB,GAIA,KAAKP,YAJL,GAKA,KAAKU,MANA,CAAP;AAQD;AAhGwB;;yBAANf,K","file":"block.js","sourcesContent":["import forge from 'node_modules/node-forge/dist/forge.min.js';\nimport Transaction from '../transaction/transaction.js';\nimport TransactionInputCollection from '../transaction/transactionInputCollection.js';\nimport TransactionOutputCollection from '../transaction/transactionOutputCollection.js';\n\nconst C_BLOCK_REWARD = 10;\n\nexport default class Block {\n  constructor(minerWallet, transactions, miningProof, previousHash) {\n    if (!miningProof.isFinalized()) {\n      throw new Error(\"a block needs to be proven\");\n    }\n    \n    if (transactions.isFinalized()) {\n      throw new Error(\"cannot append block reward transaction\");\n    }\n    \n    this.timestamp = Date.now();\n    this.minerHash = minerWallet.hash;\n    this.minerPublicKey = minerWallet.publicKey;\n    this.transactions = transactions;\n    this.miningProof = miningProof;\n    this.reward = transactions.fees + C_BLOCK_REWARD;\n    \n    this._sendReward(minerWallet);\n    \n    this.previousHash = previousHash;\n    this.hash = this._hash().digest().toHex();\n    this.signature = this._generateSignature(minerWallet);\n  }\n  \n  get displayName() {\n    if (!this.hash) {\n      return \"#NotAName\";\n    }\n    \n    return \"#\" + this.hash.substring(0, 10);\n  }\n  \n  isSigned() {\n    return !!this.signature;\n  }\n  \n  isVerified() {\n    if (!this.isSigned()) {\n      return false;\n    }\n    \n    // recalculate hash for comparison\n    // if anything was changed, this hash will not match the encrypted hash (signature)\n    var hash = this._hash();\n    \n    // decrypt the signature using the public key (==> hash) and compare with the hash we just calculated\n    return this.minerPublicKey.verify(hash.digest().bytes(), this.signature);\n  }\n  \n  _sendReward(minerWallet) {\n    var inputs = new TransactionInputCollection(minerWallet)\n                      .addMiningReward(this)\n                      .finalize();\n    \n    var outputs = new TransactionOutputCollection()\n                      .add(minerWallet, this.reward)\n                      .finalize();\n    \n    var transaction = new Transaction(minerWallet, inputs, outputs);\n    \n    // set input transaction to the transaction itself\n    inputs.forEach((input) => input.transactionHash = transaction.hash);    \n    \n    this.transactions.add(transaction);\n    this.transactions.finalize();\n  }\n  \n  _generateSignature(minerWallet) {\n    if (this.isSigned()) {\n      return this;\n    }\n    \n    if (!this.transactions.isFinalized()) {\n      throw new Error(\"transactions need to be finallized\");\n    }\n    \n    if (!this.miningProof.isFinalized()) {\n      throw new Error(\"a block needs to be proven\");\n    }\n    \n    // encrypt the hash using the given private key\n    // this allows us to decrypt the signature later\n    // on using the matching public key\n    return minerWallet.sign(this._hash());\n  }\n  \n  _hash() {\n    const sha256 = forge.md.sha256.create();\n    return sha256.update(\n      this.timestamp +\n      this.minerHash +\n      this.transactions.hash +\n      this.miningProof.hash + \n      this.previousHash +\n      this.reward\n    );\n  }\n}"]}