UNPKG

@bsv/wallet-toolbox-client

Version:
67 lines 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BulkStorageBase = void 0; const HeightRange_1 = require("../util/HeightRange"); const utilityHelpers_noBuffer_1 = require("../../../../utility/utilityHelpers.noBuffer"); class BulkStorageBase { static createBulkStorageBaseOptions(chain, fs) { const options = { chain, fs }; return options; } constructor(options) { this.chain = options.chain; this.fs = options.fs; } async shutdown() { } async findHeaderForHeight(height) { const header = await this.findHeaderForHeightOrUndefined(height); if (!header) throw new Error(`No header found for height ${height}`); return header; } async getHeightRange() { return new HeightRange_1.HeightRange(0, await this.getMaxHeight()); } async setStorage(storage) { } async exportBulkHeaders(rootFolder, jsonFilename, maxPerFile) { const info = { rootFolder: rootFolder, jsonFilename: jsonFilename, files: [], headersPerFile: maxPerFile }; const maxHeight = await this.getMaxHeight(); const baseFilename = jsonFilename.slice(0, -5); // remove ".json" let prevHash = '00'.repeat(32); let prevChainWork = '00'.repeat(32); for (let height = 0; height <= maxHeight; height += maxPerFile) { const count = Math.min(maxPerFile, maxHeight - height + 1); let file = { fileName: `${baseFilename}_${info.files.length}.headers`, firstHeight: height, prevHash: prevHash, prevChainWork: prevChainWork, count: count, lastHash: null, fileHash: null, lastChainWork: '' }; const buffer = await this.headersToBuffer(height, count); await this.fs.writeFile(this.fs.pathJoin(rootFolder, file.fileName), buffer); /* file = await BulkFilesReader.validateHeaderFile(this.fs, rootFolder, file) if (!file.lastHash) throw new Error('Unexpected result.') prevHash = file.lastHash prevChainWork = file.lastChainWork info.files.push(file) */ } const bytes = (0, utilityHelpers_noBuffer_1.asUint8Array)(JSON.stringify(info), 'utf8'); await this.fs.writeFile(this.fs.pathJoin(rootFolder, jsonFilename), bytes); } } exports.BulkStorageBase = BulkStorageBase; //# sourceMappingURL=BulkStorageBase.js.map