2019-07-27 19:02:24 -03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-07 18:24:04 -05:00
|
|
|
exports.parseBytes32String = exports.formatBytes32String = void 0;
|
2019-07-27 19: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
|
|
|
|
var bytes = utf8_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 bytes_1.hexlify(bytes_1.concat([bytes, constants_1.HashZero]).slice(0, 32));
|
|
|
|
}
|
|
|
|
exports.formatBytes32String = formatBytes32String;
|
|
|
|
function parseBytes32String(bytes) {
|
|
|
|
var data = bytes_1.arrayify(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
|
|
|
|
var length = 31;
|
|
|
|
while (data[length - 1] === 0) {
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
// Determine the string value
|
|
|
|
return utf8_1.toUtf8String(data.slice(0, length));
|
|
|
|
}
|
|
|
|
exports.parseBytes32String = parseBytes32String;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=bytes32.js.map
|