2019-07-28 01:02:24 +03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-08 02:24:04 +03:00
|
|
|
exports.parseBytes32String = exports.formatBytes32String = void 0;
|
2019-07-28 01:02:24 +03:00
|
|
|
var constants_1 = require("@ethersproject/constants");
|
|
|
|
var bytes_1 = require("@ethersproject/bytes");
|
|
|
|
var utf8_1 = require("./utf8");
|
|
|
|
function formatBytes32String(text) {
|
|
|
|
// Get the bytes
|
2021-10-16 09:29:27 +03:00
|
|
|
var bytes = (0, utf8_1.toUtf8Bytes)(text);
|
2019-07-28 01:02:24 +03: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)
|
2021-10-16 09:29:27 +03:00
|
|
|
return (0, bytes_1.hexlify)((0, bytes_1.concat)([bytes, constants_1.HashZero]).slice(0, 32));
|
2019-07-28 01:02:24 +03:00
|
|
|
}
|
|
|
|
exports.formatBytes32String = formatBytes32String;
|
|
|
|
function parseBytes32String(bytes) {
|
2021-10-16 09:29:27 +03:00
|
|
|
var data = (0, bytes_1.arrayify)(bytes);
|
2019-07-28 01:02:24 +03: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
|
|
|
|
var length = 31;
|
|
|
|
while (data[length - 1] === 0) {
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
// Determine the string value
|
2021-10-16 09:29:27 +03:00
|
|
|
return (0, utf8_1.toUtf8String)(data.slice(0, length));
|
2019-07-28 01:02:24 +03:00
|
|
|
}
|
|
|
|
exports.parseBytes32String = parseBytes32String;
|
2020-07-13 15:03:56 +03:00
|
|
|
//# sourceMappingURL=bytes32.js.map
|