@bsv/sdk
Version:
BSV Blockchain Software Development Kit
91 lines • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockHeadersService = void 0;
const DefaultHttpClient_js_1 = require("../http/DefaultHttpClient.js");
/**
* Represents a chain tracker based on a BlockHeadersService API.
*
* @example
* ```typescript
* const chainTracker = new BlockHeadersService('https://headers.spv.money', {
* apiKey: '17JxRHcJerGBEbusx56W8o1m8Js73TFGo'
* })
* ```
*/
class BlockHeadersService {
/**
* Constructs an instance of the BlockHeadersService ChainTracker.
*
* @param {string} baseUrl - The base URL for the BlockHeadersService API (e.g. https://headers.spv.money)
* @param {BlockHeadersServiceConfig} config - Configuration options for the BlockHeadersService ChainTracker.
*/
constructor(baseUrl, config = {}) {
const { httpClient, apiKey } = config;
this.baseUrl = baseUrl;
this.httpClient = httpClient ?? (0, DefaultHttpClient_js_1.defaultHttpClient)();
this.apiKey = apiKey ?? '';
}
/**
* Verifies if a given merkle root is valid for a specific block height.
*
* @param {string} root - The merkle root to verify.
* @param {number} height - The block height to check against.
* @returns {Promise<boolean>} - A promise that resolves to true if the merkle root is valid for the specified block height, false otherwise.
*/
async isValidRootForHeight(root, height) {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
data: [
{
blockHeight: height,
merkleRoot: root
}
]
};
try {
const response = await this.httpClient.request(`${this.baseUrl}/api/v1/chain/merkleroot/verify`, requestOptions);
if (response.ok) {
return response.data.confirmationState === 'CONFIRMED';
}
else {
throw new Error(`Failed to verify merkleroot for height ${height} because of an error: ${JSON.stringify(response.data)}`);
}
}
catch (error) {
throw new Error(`Failed to verify merkleroot for height ${height} because of an error: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Gets the current block height from the BlockHeadersService API.
*
* @returns {Promise<number>} - A promise that resolves to the current block height.
*/
async currentHeight() {
const requestOptions = {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
}
};
try {
const response = await this.httpClient.request(`${this.baseUrl}/api/v1/chain/tip/longest`, requestOptions);
if (response.ok && response.data && typeof response.data.height === 'number') {
return response.data.height;
}
else {
throw new Error(`Failed to get current height because of an error: ${JSON.stringify(response.data)}`);
}
}
catch (error) {
throw new Error(`Failed to get current height because of an error: ${error instanceof Error ? error.message : String(error)}`);
}
}
}
exports.BlockHeadersService = BlockHeadersService;
//# sourceMappingURL=BlockHeadersService.js.map