UNPKG

xud

Version:
40 lines 1.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNodePubKey = exports.pubKeyToAlias = void 0; const wordlist_1 = require("../constants/wordlist"); const errors_1 = __importDefault(require("../service/errors")); /** * Gets a two-word alias from a node pubkey. Throws an error if the pubkey is * not a valid 66 character hex string. * @param pubkey a public key in hex format to convert to an alias */ exports.pubKeyToAlias = (pubkey) => { const getAlias = (substring) => { const asNumber = parseInt(substring, 16); if (!isNaN(asNumber)) { const length = wordlist_1.wordlist.length; const index = asNumber % (length * length); const alias = wordlist_1.wordlist[Math.floor(index / length)] + wordlist_1.wordlist[index % length]; return alias; } return undefined; }; if (pubkey.length === 66) { const alias = getAlias(pubkey); if (alias) { return alias; } } throw errors_1.default.INVALID_ARGUMENT('Not a valid public key'); }; /** * Returns true if a given node identifier could be a node pub key, namely * if it is a hex string with the correct length. */ exports.isNodePubKey = (nodeIdentifier) => { return nodeIdentifier.length === 66 && /^[0-9a-fA-F]+$/.test(nodeIdentifier); }; //# sourceMappingURL=aliasUtils.js.map