UNPKG

chainpro

Version:
57 lines (45 loc) 1.56 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNewBlockValid = exports.create = exports.calcHash = undefined; var _cryptoJs = require('crypto-js'); var _cryptoJs2 = _interopRequireDefault(_cryptoJs); var _chain = require('./chain'); var _chain2 = _interopRequireDefault(_chain); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const calcHash = exports.calcHash = ({ index, prevHash, timestamp, data }) => _cryptoJs2.default.SHA256(index + prevHash + timestamp + data).toString(); const create = exports.create = data => { const prev = _chain2.default.last(); const index = prev.index + 1; const timestamp = new Date().getTime(); const prevHash = prev.hash; const hash = calcHash({ index, prevHash, timestamp, data }); return { index, timestamp, data, prevHash, hash }; }; const isNewBlockValid = exports.isNewBlockValid = (newBlock, prevBlock = _chain2.default.last()) => { let isValid = true; if (prevBlock.index + 1 !== newBlock.index) { console.log('New block has invalid index'); isValid = false; } else if (prevBlock.hash !== newBlock.prevHash) { console.log('New block has invalid prevHash'); isValid = false; } else if (calcHash(newBlock) !== newBlock.hash) { console.log('New block has invalid hash'); isValid = false; } return isValid; }; //# sourceMappingURL=block.js.map