UNPKG

@layr-labs/hourglass-performer

Version:

TypeScript SDK for building Hourglass AVS performers

74 lines 2.1 kB
"use strict"; // Utility types and functions for working with protobuf in TypeScript Object.defineProperty(exports, "__esModule", { value: true }); exports.stringToBytes = stringToBytes; exports.bytesToString = bytesToString; exports.hexToBytes = hexToBytes; exports.bytesToHex = bytesToHex; exports.numberToBytes = numberToBytes; exports.bytesToNumber = bytesToNumber; exports.jsonToBytes = jsonToBytes; exports.bytesToJson = bytesToJson; /** * Convert a string to Uint8Array for protobuf bytes fields */ function stringToBytes(str) { return new TextEncoder().encode(str); } /** * Convert Uint8Array to string from protobuf bytes fields */ function bytesToString(bytes) { return new TextDecoder().decode(bytes); } /** * Convert a hex string to Uint8Array */ function hexToBytes(hex) { // Remove '0x' prefix if present const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex; // Ensure even length const paddedHex = cleanHex.length % 2 === 0 ? cleanHex : '0' + cleanHex; const bytes = new Uint8Array(paddedHex.length / 2); for (let i = 0; i < paddedHex.length; i += 2) { bytes[i / 2] = parseInt(paddedHex.substr(i, 2), 16); } return bytes; } /** * Convert Uint8Array to hex string */ function bytesToHex(bytes) { return '0x' + Array.from(bytes) .map(b => b.toString(16).padStart(2, '0')) .join(''); } /** * Convert a number to Uint8Array (big-endian) */ function numberToBytes(num) { const bytes = new Uint8Array(8); const view = new DataView(bytes.buffer); view.setBigUint64(0, BigInt(num), false); // big-endian return bytes; } /** * Convert Uint8Array to number (big-endian) */ function bytesToNumber(bytes) { const view = new DataView(bytes.buffer); return Number(view.getBigUint64(0, false)); // big-endian } /** * Convert JSON object to Uint8Array */ function jsonToBytes(obj) { return stringToBytes(JSON.stringify(obj)); } /** * Convert Uint8Array to JSON object */ function bytesToJson(bytes) { return JSON.parse(bytesToString(bytes)); } //# sourceMappingURL=protobuf.js.map