opnet
Version:
The perfect library for building Bitcoin-based applications.
27 lines (26 loc) • 851 B
JavaScript
import protobuf from 'protobufjs';
let cachedRoot = null;
export async function loadProtobufSchema(baseUrl) {
if (cachedRoot) {
return cachedRoot;
}
let url = baseUrl.trim();
if (url.endsWith('/')) {
url = url.slice(0, -1);
}
const schemaUrl = `${url}/api/v1/protobuf/api-schema`;
const response = await fetch(schemaUrl);
if (!response.ok) {
throw new Error(`Failed to fetch protobuf schema: ${response.status} ${response.statusText}`);
}
const protoContent = await response.text();
cachedRoot = protobuf.parse(protoContent, { keepCase: true }).root;
return cachedRoot;
}
export function getProtobufType(root, typeName) {
const fullPath = `OPNetAPIProtocol.${typeName}`;
return root.lookupType(fullPath);
}
export function clearProtobufCache() {
cachedRoot = null;
}