Skip to content

Instantly share code, notes, and snippets.

@joshleblanc
Forked from towc/some-file.js
Last active March 29, 2018 14:52
Show Gist options
  • Save joshleblanc/812d94a1226f9ac8639b150b45cfec64 to your computer and use it in GitHub Desktop.
Save joshleblanc/812d94a1226f9ac8639b150b45cfec64 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const { promisify } = require('util');
const hash = require('./hash');
const { dbs: dbsPath } = require('./paths');
class MahClass {
constructor() {
this.readFilePromise = promisify(fs.readFile);
this.originPath = this.getOriginHashFromFile();
}
getDBFileContentsFromName(name) {
return this.readFilePromise(`${dbsPath}/${name}`, 'utf-8');
}
getNodeContentsFromName(name) {
return this.getDBFileContentsFromName(`${name}.json`);
}
getOriginHashFromFile() {
return this.getDBFileContentsFromName('origin-hash.txt');
}
getLastHashFromFile() {
return this.getDBFileContentsFromName('last-hash.txt');
}
setOriginHashFromFile() {
this.originHash = this.getOriginHashFromFile();
}
checkBlockHash(blockHash) {
const blockContent = this.getNodeContentsFromName(blockHash);
return hash.checkMatchesContent(blockHash, blockContent);
}
checkBlockIntegrity(blockHash) {
if(!this.checkBlockHash(blockHash)) {
return false;
}
// more code to appear here
}
checkChainIntegrity() {
const lastBlockHash = this.getLastHashFromFile();
return this.checkBlockIntegrity(lastBlockHash);
}
}
// and still need to declare the exports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment