UNPKG

@zlattice/lattice-js

Version:

Lattice blockchain TypeScript SDK with dual module support (CJS + ESM)

110 lines 3.53 kB
"use strict"; /** * About bytes32 strings... * * @_docloc: api/utils:Bytes32 Strings */ Object.defineProperty(exports, "__esModule", { value: true }); exports.encodeBytes32String = encodeBytes32String; exports.decodeBytes32String = decodeBytes32String; exports.encodeBytes32Array = encodeBytes32Array; exports.decodeBytes32Array = decodeBytes32Array; exports.hexStringsToBufferWithTrimmedZeros = hexStringsToBufferWithTrimmedZeros; const strings_1 = require("@ethersproject/strings"); const index_1 = require("./index.js"); /** * Encodes %%text%% as a Bytes32 string. */ function encodeBytes32String(text) { /** * Get the bytes */ const bytes = (0, strings_1.toUtf8Bytes)(text); if (bytes.length > 31) { /** * Check we have room for null-termination */ throw new Error("bytes32 string must be less than 32 bytes"); } /** * Zero-pad (implicitly null-terminates) */ return (0, index_1.zeroPadBytes)(bytes, 32); } /** * Encodes the Bytes32-encoded %%bytes%% into a string. */ function decodeBytes32String(_bytes) { const data = (0, index_1.getBytes)(_bytes, "bytes"); if (data.length !== 32) { /** * Must be 32 bytes with a null-termination */ throw new Error("invalid bytes32 - not 32 bytes long"); } if (data[31] !== 0) { throw new Error("invalid bytes32 string - no null terminator"); } /** * Find the null termination */ let length = 31; while (data[length - 1] === 0) { length--; } /** * Determine the string value */ return (0, strings_1.toUtf8String)(data.slice(0, length)); } /** * Encodes a Buffer into an array of Bytes32 strings. */ function encodeBytes32Array(bytes) { const result = []; for (let i = 0; i < bytes.length; i += 32) { const chunk = Buffer.alloc(32); bytes.copy(chunk, 0, i, Math.min(i + 32, bytes.length)); result.push((0, index_1.zeroPadBytes)(chunk, 32)); } return result; } function decodeBytes32Array(bytes) { const result = []; for (const b of bytes) { const decoded = decodeBytes32String(b); const chunk = Buffer.from(decoded, "hex"); result.push(chunk); } return result; } /** * Convert an array of hex strings to a buffer with trimmed zeros. * For example: ["0x1234567890abcdef", "0xabcdef1234567890"] -> Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) * @param hexStrings - An array of hex strings. * @returns A buffer containing the concatenated hex strings. */ function hexStringsToBufferWithTrimmedZeros(hexStrings) { if (!hexStrings || hexStrings.length === 0) { return Buffer.alloc(0); } const buffers = hexStrings.map((hexString) => { const cleanHexString = hexString.startsWith("0x") ? hexString.slice(2) : hexString; // Assuming each string is a valid hex representation of 32 bytes, // so cleanHexString.length should be 64. // No explicit validation here as per problem description focusing on the conversion and trimming. return Buffer.from(cleanHexString, "hex"); }); const combinedBuffer = Buffer.concat(buffers); if (combinedBuffer.length === 0) { return combinedBuffer; } let end = combinedBuffer.length; while (end > 0 && combinedBuffer[end - 1] === 0) { end--; } return combinedBuffer.subarray(0, end); } //# sourceMappingURL=bytes32.js.map