ethers.js/packages/json-wallets/lib/utils.js

70 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.uuidV4 = exports.searchPath = exports.getPassword = exports.zpad = exports.looseArrayify = void 0;
2019-05-14 18:48:48 -04:00
var bytes_1 = require("@ethersproject/bytes");
var strings_1 = require("@ethersproject/strings");
function looseArrayify(hexString) {
if (typeof (hexString) === 'string' && hexString.substring(0, 2) !== '0x') {
hexString = '0x' + hexString;
}
2021-10-16 02:29:27 -04:00
return (0, bytes_1.arrayify)(hexString);
2019-05-14 18:48:48 -04:00
}
exports.looseArrayify = looseArrayify;
function zpad(value, length) {
value = String(value);
while (value.length < length) {
value = '0' + value;
}
return value;
}
exports.zpad = zpad;
function getPassword(password) {
if (typeof (password) === 'string') {
2021-10-16 02:29:27 -04:00
return (0, strings_1.toUtf8Bytes)(password, strings_1.UnicodeNormalizationForm.NFKC);
2019-05-14 18:48:48 -04:00
}
2021-10-16 02:29:27 -04:00
return (0, bytes_1.arrayify)(password);
2019-05-14 18:48:48 -04:00
}
exports.getPassword = getPassword;
function searchPath(object, path) {
var currentChild = object;
var comps = path.toLowerCase().split('/');
for (var i = 0; i < comps.length; i++) {
// Search for a child object with a case-insensitive matching key
var matchingChild = null;
for (var key in currentChild) {
if (key.toLowerCase() === comps[i]) {
matchingChild = currentChild[key];
break;
}
}
// Didn't find one. :'(
if (matchingChild === null) {
return null;
}
// Now check this child...
currentChild = matchingChild;
}
return currentChild;
}
exports.searchPath = searchPath;
2020-07-20 02:27:26 -04:00
// See: https://www.ietf.org/rfc/rfc4122.txt (Section 4.4)
function uuidV4(randomBytes) {
2021-10-16 02:29:27 -04:00
var bytes = (0, bytes_1.arrayify)(randomBytes);
2020-07-20 02:27:26 -04:00
// Section: 4.1.3:
// - time_hi_and_version[12:16] = 0b0100
bytes[6] = (bytes[6] & 0x0f) | 0x40;
// Section 4.4
// - clock_seq_hi_and_reserved[6] = 0b0
// - clock_seq_hi_and_reserved[7] = 0b1
bytes[8] = (bytes[8] & 0x3f) | 0x80;
2021-10-16 02:29:27 -04:00
var value = (0, bytes_1.hexlify)(bytes);
2020-07-20 02:27:26 -04:00
return [
value.substring(2, 10),
value.substring(10, 14),
value.substring(14, 18),
value.substring(18, 22),
value.substring(22, 34),
].join("-");
}
exports.uuidV4 = uuidV4;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=utils.js.map