36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.parseBytes32String = exports.formatBytes32String = void 0;
|
||
|
const data_js_1 = require("../utils/data.js");
|
||
|
const logger_js_1 = require("../utils/logger.js");
|
||
|
const utf8_js_1 = require("../utils/utf8.js");
|
||
|
function formatBytes32String(text) {
|
||
|
// Get the bytes
|
||
|
const bytes = (0, utf8_js_1.toUtf8Bytes)(text);
|
||
|
// Check we have room for null-termination
|
||
|
if (bytes.length > 31) {
|
||
|
throw new Error("bytes32 string must be less than 32 bytes");
|
||
|
}
|
||
|
// Zero-pad (implicitly null-terminates)
|
||
|
return (0, data_js_1.zeroPadBytes)(bytes, 32);
|
||
|
}
|
||
|
exports.formatBytes32String = formatBytes32String;
|
||
|
function parseBytes32String(_bytes) {
|
||
|
const data = logger_js_1.logger.getBytes(_bytes, "bytes");
|
||
|
// Must be 32 bytes with a null-termination
|
||
|
if (data.length !== 32) {
|
||
|
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, utf8_js_1.toUtf8String)(data.slice(0, length));
|
||
|
}
|
||
|
exports.parseBytes32String = parseBytes32String;
|
||
|
//# sourceMappingURL=bytes32.js.map
|