{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-stable/src/blockchain/model/block/miningProof.js"],"names":["forge","C_MIN_MINING_DIFFICULTY","C_MAX_MINING_DIFFICULTY","MiningProof","constructor","miningDifficulty","Math","max","min","round","startTimestamp","finishTimestamp","hash","nonce","displayName","_hash","substring","work","Date","now","_solveCryptoPuzzle","isFinalized","zeroString","repeat","calculateHash","length","sha256","md","create","update","digest","toHex"],"mappings":";;;;;;AAAOA,W;;;;;;;;;;;;;;;;;;;;AAEP,YAAMC,0BAA0B,CAAhC;;;;;;;;;;;;;;AACA,YAAMC,0BAA0B,EAAhC;;;;;;;;;;;;;;;AAEe,YAAMC,WAAN,CAAkB;AAC/BC,oBAAYC,gBAAZ,EAA8B;AAC5B,eAAKA,gBAAL,GAAwBC,KAAKC,GAAL,CAASN,uBAAT,EACSK,KAAKE,GAAL,CAASN,uBAAT,EAAkCI,KAAKG,KAAL,CAAWJ,gBAAX,CAAlC,CADT,CAAxB;AAEA,eAAKK,cAAL,GAAsB,IAAtB;AACA,eAAKC,eAAL,GAAuB,IAAvB;AACA,eAAKC,IAAL,GAAY,IAAZ;AACA,eAAKC,KAAL,GAAa,CAAb;AACD;;AAED,YAAIC,WAAJ,GAAkB;AAChB,cAAI,CAAC,KAAKC,KAAV,EAAiB;AACf,mBAAO,WAAP;AACD;;AAED,iBAAO,MAAM,KAAKA,KAAL,CAAWC,SAAX,CAAqB,CAArB,EAAwB,EAAxB,CAAb;AACD;;AAED,cAAMC,IAAN,GAAa;AACX,eAAKP,cAAL,GAAsBQ,KAAKC,GAAL,EAAtB;AACA,gBAAM,KAAKC,kBAAL,EAAN;AACA,eAAKT,eAAL,GAAuBO,KAAKC,GAAL,EAAvB;AACA,eAAKP,IAAL,GAAY,KAAKG,KAAL,EAAZ;AACD;;AAEDM,sBAAc;AACZ,iBAAO,CAAC,CAAC,KAAKT,IAAd;AACD;;AAEDQ,6BAAqB;AACnB,gBAAME,aAAa,IAAIC,MAAJ,CAAW,KAAKlB,gBAAhB,CAAnB;AACA,aAAG;AACD,iBAAKmB,aAAL,CAAmB,KAAKX,KAAL,GAAa,CAAhC;AACD,WAFD,QAEQ,KAAKD,IAAL,CAAUI,SAAV,CAAoB,KAAKJ,IAAL,CAAUa,MAAV,GAAmB,KAAKpB,gBAA5C,KAAiEiB,UAFzE;AAGD;;AAEDE,sBAAcX,KAAd,EAAqB;AACnB,eAAKA,KAAL,GAAaA,KAAb;AACA,eAAKD,IAAL,GAAY,KAAKG,KAAL,EAAZ;AACA,iBAAO,KAAKH,IAAZ;AACD;;AAEDG,gBAAQ;AACN,gBAAMW,SAAS1B,MAAM2B,EAAN,CAASD,MAAT,CAAgBE,MAAhB,EAAf;AACAF,iBAAOG,MAAP,CACE,KAAKhB,KAAL,GACA,KAAKR,gBADL,GAEA,KAAKK,cAFL,GAGA,KAAKC,eAJP;AAMA,iBAAOe,OAAOI,MAAP,GAAgBC,KAAhB,EAAP;AACD;AAnD8B;;yBAAZ5B,W","file":"miningProof.js","sourcesContent":["import forge from 'node_modules/node-forge/dist/forge.min.js';\n\nconst C_MIN_MINING_DIFFICULTY = 3;\nconst C_MAX_MINING_DIFFICULTY = 10;\n\nexport default class MiningProof {\n  constructor(miningDifficulty) {\n    this.miningDifficulty = Math.max(C_MIN_MINING_DIFFICULTY,\n                                     Math.min(C_MAX_MINING_DIFFICULTY, Math.round(miningDifficulty)));\n    this.startTimestamp = null;\n    this.finishTimestamp = null;\n    this.hash = null;\n    this.nonce = 0;\n  }\n  \n  get displayName() {\n    if (!this._hash) {\n      return \"#NotAName\";\n    }\n    \n    return \"#\" + this._hash.substring(0, 10);\n  }\n  \n  async work() {\n    this.startTimestamp = Date.now();\n    await this._solveCryptoPuzzle();\n    this.finishTimestamp = Date.now();\n    this.hash = this._hash();\n  }\n  \n  isFinalized() {\n    return !!this.hash;\n  }\n  \n  _solveCryptoPuzzle() {\n    const zeroString = \"0\".repeat(this.miningDifficulty);\n    do {\n      this.calculateHash(this.nonce + 1);\n    } while(this.hash.substring(this.hash.length - this.miningDifficulty) != zeroString);\n  }\n  \n  calculateHash(nonce) {\n    this.nonce = nonce;\n    this.hash = this._hash();\n    return this.hash;\n  }\n  \n  _hash() {\n    const sha256 = forge.md.sha256.create();\n    sha256.update(\n      this.nonce +\n      this.miningDifficulty +\n      this.startTimestamp +\n      this.finishTimestamp\n    );\n    return sha256.digest().toHex();\n  }\n}"]}