whoisens-lib
Version:
Whois for ENS (Ethereum Name Service) lookup service
127 lines • 4.12 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const js_sha3_1 = __importDefault(require("js-sha3"));
const eth_ens_namehash_1 = __importDefault(require("eth-ens-namehash"));
const index_js_1 = __importDefault(require("content-hash/dist/index.js"));
const types_js_1 = require("../lib/types.js");
const CONTENT_HASH_CODEC_MAP = {
'ipfs-ns': 'ipfs://',
'swarm-ns': 'bzz://'
};
exports.default = {
getMethodID(methodSignature) {
return this.add0x(this.hash(methodSignature).slice(0, 8));
},
trimHex(hex) {
if (!hex)
return;
return hex.replace(/0x0+/g, '');
},
/**
* Remove unneeded leading zeros.
*
* @param hex
*/
normalizeHex(hex) {
if (!hex)
return;
if (hex === '0x')
return hex;
return this.add0x(this.trimHex(hex));
},
add0x(hex) {
return '0x' + hex;
},
remove0x(hex) {
if (!hex || !hex.startsWith('0x'))
return;
return hex.slice(2);
},
node(string) {
return eth_ens_namehash_1.default.hash(string);
},
hash(string) {
return js_sha3_1.default.keccak256(string);
},
getLTDfromDomain(domain) {
return domain.split('.').pop();
},
getLabelsFromDomain(domain) {
return domain.split('.').slice(0, -1);
},
getAddressParent(name) {
return name.split('.').slice(1).join('.');
},
getNameMain(name) {
if (this.getAddressType(name) !== types_js_1.EthAddressType.name)
throw `parameter should be a name, got: ${name}`;
return name.split('.').slice(-2).join('.');
},
getAddressType(address) {
if ((address.endsWith('.eth') && address.length >= 5) || address === 'eth')
return types_js_1.EthAddressType.name;
if ((address.startsWith('0x') && !address.endsWith('.addr.reverse')) && address.length === 42 && this.trimHex(address) || (address.endsWith('.addr.reverse') && !address.startsWith('0x')))
return types_js_1.EthAddressType.address;
return types_js_1.EthAddressType.error;
},
// FIXME: need a library to convert it in correct way with no dependencies
byteToString(str, truncate = false) {
if (!str)
return;
let output = str.slice(130, 130 + 76);
if (truncate)
output = output.replace(/00+/, '');
return output;
},
decodeContentHash(string) {
if (!string)
return;
string = this.byteToString(string);
if (!string)
return;
return {
content: index_js_1.default.decode(string),
codec: index_js_1.default.getCodec(string)
};
},
getContentHashAsURL(contentHash) {
if (!contentHash)
return;
const codec = this.getContentHashCodec(contentHash.codec);
return `${codec}${contentHash.content}`;
},
getContentHashCodec(codec) {
return CONTENT_HASH_CODEC_MAP[codec];
},
hexToAscii(hex) {
if (!hex)
return;
let value = '';
let i = 0;
if (hex.substring(0, 2) === '0x')
i = 2;
for (; i < hex.length; i += 2) {
const code = parseInt(hex.substring(i, i + 2), 16);
value += String.fromCharCode(code);
}
return value;
},
/**
* Return address hex (0x....) from name.addr.reverse.
* If address name is not in revert form, then just passing string is returning.
*/
reverseAddressToHex(address) {
const reverseAddressEnding = '.addr.reverse';
if (address && address.endsWith(reverseAddressEnding)) {
return this.add0x(address.replace(reverseAddressEnding, ''));
}
return address;
},
isResult(str) {
return str && this.trimHex(str.toString()) && str !== '0x';
}
};
//# sourceMappingURL=index.js.map