UNPKG

@bsv/overlay-express

Version:
62 lines 2.14 kB
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. */ export class ChaintracksProvider { baseUrl; fetcher; constructor(url, config = {}) { this.baseUrl = `${trimUrl(url)}${trimPrefix(config.apiPrefix ?? '/chaintracks/v2')}`; this.fetcher = config.fetch ?? 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(); } } //# sourceMappingURL=ChaintracksProvider.js.map