2022-09-05 16:57:11 -04:00
|
|
|
"use strict";
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* About bytes32 strings...
|
|
|
|
*
|
|
|
|
* @_docloc: api/utils:Bytes32 Strings
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-10-01 01:34:06 -04:00
|
|
|
exports.decodeBytes32String = exports.encodeBytes32String = void 0;
|
2022-09-15 22:58:45 -04:00
|
|
|
const index_js_1 = require("../utils/index.js");
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Encodes %%text%% as a Bytes32 string.
|
|
|
|
*/
|
2022-10-01 01:34:06 -04:00
|
|
|
function encodeBytes32String(text) {
|
2022-09-05 16:57:11 -04:00
|
|
|
// Get the bytes
|
2022-09-15 22:58:45 -04:00
|
|
|
const bytes = (0, index_js_1.toUtf8Bytes)(text);
|
2022-09-05 16:57:11 -04:00
|
|
|
// 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)
|
2022-09-15 22:58:45 -04:00
|
|
|
return (0, index_js_1.zeroPadBytes)(bytes, 32);
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2022-10-01 01:34:06 -04:00
|
|
|
exports.encodeBytes32String = encodeBytes32String;
|
2022-11-30 15:44:23 -05:00
|
|
|
/**
|
|
|
|
* Encodes the Bytes32-encoded %%bytes%% into a string.
|
|
|
|
*/
|
2022-10-01 01:34:06 -04:00
|
|
|
function decodeBytes32String(_bytes) {
|
2022-09-15 22:58:45 -04:00
|
|
|
const data = (0, index_js_1.getBytes)(_bytes, "bytes");
|
2022-09-05 16:57:11 -04:00
|
|
|
// 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
|
2022-09-15 22:58:45 -04:00
|
|
|
return (0, index_js_1.toUtf8String)(data.slice(0, length));
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
2022-10-01 01:34:06 -04:00
|
|
|
exports.decodeBytes32String = decodeBytes32String;
|
2022-09-05 16:57:11 -04:00
|
|
|
//# sourceMappingURL=bytes32.js.map
|