@bsv/wallet-toolbox
Version:
BRC100 conforming wallet, wallet storage and wallet signer components
95 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockChainTracker = void 0;
const sdk_1 = require("@bsv/sdk");
const Services_1 = require("../services/Services");
const WERR_errors_1 = require("../sdk/WERR_errors");
class MockChainTracker {
constructor(chain, storage) {
this.chain = chain;
this.storage = storage;
}
async currentHeight() {
const tip = await this.storage.getChainTip();
return tip ? tip.height : -1;
}
async isValidRootForHeight(root, height) {
const header = await this.storage.getBlockHeaderByHeight(height);
if (!header)
return false;
return header.merkleRoot === root;
}
async getChain() {
return this.chain;
}
async getInfo() {
const tip = await this.storage.getChainTip();
return {
chain: this.chain,
heightBulk: tip ? tip.height : -1,
heightLive: tip ? tip.height : -1,
storage: 'mockchain',
bulkIngestors: [],
liveIngestors: [],
packages: []
};
}
async getPresentHeight() {
return this.currentHeight();
}
async getHeaders(height, count) {
let hex = '';
for (let h = height; h < height + count; h++) {
const header = await this.storage.getBlockHeaderByHeight(h);
if (!header)
break;
const binary = (0, Services_1.toBinaryBaseBlockHeader)(header);
hex += sdk_1.Utils.toHex(binary);
}
return hex;
}
async findChainTipHeader() {
const tip = await this.storage.getChainTip();
if (!tip)
throw new Error('Mock chain has no blocks');
return tip;
}
async findChainTipHash() {
const tip = await this.storage.getChainTip();
if (!tip)
throw new Error('Mock chain has no blocks');
return tip.hash;
}
async findHeaderForHeight(height) {
return this.storage.getBlockHeaderByHeight(height);
}
async findHeaderForBlockHash(hash) {
return this.storage.getBlockHeaderByHash(hash);
}
async addHeader(_header) {
// no-op for mock chain
}
async startListening() {
// no-op
}
async listening() {
// no-op
}
async isListening() {
return true;
}
async isSynchronized() {
return true;
}
async subscribeHeaders(_listener) {
throw new WERR_errors_1.WERR_NOT_IMPLEMENTED('subscribeHeaders not supported on mock chain');
}
async subscribeReorgs(_listener) {
throw new WERR_errors_1.WERR_NOT_IMPLEMENTED('subscribeReorgs not supported on mock chain');
}
async unsubscribe(_subscriptionId) {
throw new WERR_errors_1.WERR_NOT_IMPLEMENTED('unsubscribe not supported on mock chain');
}
}
exports.MockChainTracker = MockChainTracker;
//# sourceMappingURL=MockChainTracker.js.map