@bsv/overlay-express
Version:
BSV Blockchain Overlay Express
65 lines • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChaintracksProvider = void 0;
function trimUrl(url) {
let trimmed = url;
while (trimmed.endsWith('/'))
trimmed = trimmed.slice(0, -1);
return trimmed;
}
function trimPrefix(prefix) {
if (prefix === '')
return '';
let trimmed = prefix.startsWith('/') ? prefix : `/${prefix}`;
while (trimmed.endsWith('/'))
trimmed = trimmed.slice(0, -1);
return trimmed;
}
/**
* Minimal go-chaintracks HTTP client for Overlay Express.
*
* It intentionally implements the SDK ChainTracker surface plus header lookup,
* which is enough for proof validation, BASM headers, and reorg stream URL
* construction without depending on wallet-toolbox client export timing.
*/
class ChaintracksProvider {
constructor(url, config = {}) {
var _a, _b;
this.baseUrl = `${trimUrl(url)}${trimPrefix((_a = config.apiPrefix) !== null && _a !== void 0 ? _a : '/chaintracks/v2')}`;
this.fetcher = (_b = config.fetch) !== null && _b !== void 0 ? _b : fetch;
}
async currentHeight() {
const response = await this.getJson('/height');
return response.height;
}
async isValidRootForHeight(root, height) {
const header = await this.findHeaderForHeight(height);
return header !== undefined && header.merkleRoot === root;
}
async findHeaderForHeight(height) {
return await this.getJsonOrUndefined(`/header/height/${height}`);
}
reorgStreamUrl() {
return `${this.baseUrl}/reorg/stream`;
}
async getJson(path) {
const value = await this.getJsonOrUndefined(path);
if (value === undefined) {
throw new Error(`Chaintracks returned no value for ${path}`);
}
return value;
}
async getJsonOrUndefined(path) {
const response = await this.fetcher(`${this.baseUrl}${path}`, {
headers: { Accept: 'application/json' }
});
if (response.status === 404)
return undefined;
if (!response.ok) {
throw new Error(`Chaintracks request failed for ${path}: ${response.status} ${response.statusText}`);
}
return await response.json();
}
}
exports.ChaintracksProvider = ChaintracksProvider;
//# sourceMappingURL=ChaintracksProvider.js.map