Compare commits
48 Commits
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | 48922db70f | |
Derrick Hammer | 3068dea37d | |
Derrick Hammer | 07afb90e7a | |
semantic-release-bot | 635806e600 | |
Derrick Hammer | 55c9e606e0 | |
Derrick Hammer | b33b70160e | |
semantic-release-bot | f6978c1974 | |
Derrick Hammer | 2f25241943 | |
Derrick Hammer | 536ed0fa4d | |
semantic-release-bot | adc35b49d6 | |
Derrick Hammer | 7a029edfba | |
Derrick Hammer | 240fdf0128 | |
Derrick Hammer | 20f6d84751 | |
semantic-release-bot | a63e2d1f97 | |
Derrick Hammer | ccef3131c2 | |
Derrick Hammer | 5a7694ab70 | |
Derrick Hammer | 097cc66285 | |
Derrick Hammer | 9f5a4d2ce0 | |
semantic-release-bot | 8307c5ea11 | |
Derrick Hammer | 11430b6e2a | |
Derrick Hammer | f3303d57a5 | |
semantic-release-bot | ef42fd0a5a | |
Derrick Hammer | 047e7d591c | |
Derrick Hammer | 8aeafee3e0 | |
semantic-release-bot | 80b16f11ed | |
Derrick Hammer | 8b5240d1b8 | |
Derrick Hammer | 5c374b64d2 | |
semantic-release-bot | eaefadd473 | |
Derrick Hammer | dd629edd8c | |
Derrick Hammer | b6d0c362c6 | |
Derrick Hammer | 0ba86078d4 | |
semantic-release-bot | 57564624c2 | |
Derrick Hammer | a5a9cce71b | |
Derrick Hammer | 01051a04e0 | |
semantic-release-bot | b2b753b6c9 | |
Derrick Hammer | b14c441198 | |
Derrick Hammer | effbcbe95a | |
semantic-release-bot | 28825f897b | |
Derrick Hammer | b739ecbcbf | |
Derrick Hammer | ea8aa7ab65 | |
Derrick Hammer | c7bf06a41f | |
Derrick Hammer | 1bfe7d9eed | |
semantic-release-bot | 8b8e08a07f | |
Derrick Hammer | c953bd7986 | |
Derrick Hammer | e1f527d7da | |
Derrick Hammer | 80e6b4aa7b | |
Derrick Hammer | 23714fba07 | |
Derrick Hammer | addc693fb3 |
|
@ -0,0 +1,13 @@
|
|||
name: Build/Publish
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- develop
|
||||
- develop-*
|
||||
|
||||
jobs:
|
||||
main:
|
||||
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
|
||||
secrets: inherit
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"preset": [
|
||||
"@lumeweb/presetter-kernel-module-preset"
|
||||
],
|
||||
"config": {
|
||||
"official": true
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
## [0.0.2-develop.13](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.12...v0.0.2-develop.13) (2023-10-12)
|
||||
|
||||
## [0.0.2-develop.12](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.11...v0.0.2-develop.12) (2023-09-13)
|
||||
|
||||
## [0.0.2-develop.11](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.10...v0.0.2-develop.11) (2023-09-11)
|
||||
|
||||
## [0.0.2-develop.10](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.9...v0.0.2-develop.10) (2023-09-04)
|
||||
|
||||
## [0.0.2-develop.9](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.8...v0.0.2-develop.9) (2023-09-03)
|
||||
|
||||
## [0.0.2-develop.8](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.7...v0.0.2-develop.8) (2023-09-02)
|
||||
|
||||
## [0.0.2-develop.7](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.6...v0.0.2-develop.7) (2023-08-04)
|
||||
|
||||
## [0.0.2-develop.6](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.5...v0.0.2-develop.6) (2023-08-03)
|
||||
|
||||
## [0.0.2-develop.5](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.4...v0.0.2-develop.5) (2023-08-02)
|
||||
|
||||
## [0.0.2-develop.4](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.3...v0.0.2-develop.4) (2023-07-29)
|
||||
|
||||
## [0.0.2-develop.3](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.2...v0.0.2-develop.3) (2023-07-29)
|
||||
|
||||
## [0.0.2-develop.2](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.2-develop.1...v0.0.2-develop.2) (2023-07-29)
|
||||
|
||||
## [0.0.2-develop.1](https://git.lumeweb.com/LumeWeb/resolver-module-handshake/compare/v0.0.1...v0.0.2-develop.1) (2023-07-15)
|
|
@ -0,0 +1,3 @@
|
|||
# resolver-module-handshake
|
||||
|
||||
Module CID: zdiKj8qrXfXVBzE9pk2AVQmiQnegRfXj4MTjYqwQYBZ3Lkbasvfm12tebH
|
14
build.js
14
build.js
|
@ -1,14 +0,0 @@
|
|||
import esbuild from "esbuild"
|
||||
|
||||
esbuild.buildSync({
|
||||
entryPoints: ['src-module/index.ts'],
|
||||
outfile: 'dist-module/index.js',
|
||||
format: 'iife',
|
||||
bundle: true,
|
||||
legalComments: 'external',
|
||||
// minify: true
|
||||
tsconfig: "tsconfig.module.json",
|
||||
define: {
|
||||
'global': 'self'
|
||||
}
|
||||
})
|
|
@ -1,17 +0,0 @@
|
|||
import {
|
||||
AbstractResolverModule,
|
||||
DNSResult,
|
||||
ResolverOptions,
|
||||
} from "@lumeweb/libresolver";
|
||||
export default class Handshake extends AbstractResolverModule {
|
||||
private buildBlacklist;
|
||||
resolve(
|
||||
domain: string,
|
||||
options: ResolverOptions,
|
||||
bypassCache: boolean
|
||||
): Promise<DNSResult>;
|
||||
private processNs;
|
||||
private processGlue;
|
||||
private query;
|
||||
private processTxt;
|
||||
}
|
|
@ -1,179 +0,0 @@
|
|||
import tldEnum from "@lumeweb/tld-enum";
|
||||
import { AbstractResolverModule, DNS_RECORD_TYPE, isDomain, isIp, isPromise, normalizeDomain, resolverEmptyResponse, resolveSuccess, ensureUniqueRecords, getTld, resolverError, } from "@lumeweb/libresolver";
|
||||
const HIP5_EXTENSIONS = ["eth", "_eth"];
|
||||
export default class Handshake extends AbstractResolverModule {
|
||||
async buildBlacklist() {
|
||||
const blacklist = new Set();
|
||||
let resolvers = this.resolver.resolvers;
|
||||
if (isPromise(resolvers)) {
|
||||
resolvers = await resolvers;
|
||||
}
|
||||
for (const resolver of resolvers) {
|
||||
let tlds = resolver.getSupportedTlds();
|
||||
if (isPromise(tlds)) {
|
||||
tlds = await tlds;
|
||||
}
|
||||
tlds.map((item) => blacklist.add(item));
|
||||
}
|
||||
return blacklist;
|
||||
}
|
||||
async resolve(domain, options, bypassCache) {
|
||||
options.options = options.options || {};
|
||||
const tld = getTld(domain);
|
||||
const blacklist = await this.buildBlacklist();
|
||||
if (blacklist.has(tld)) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
if (isIp(domain)) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
if (options?.options && "subquery" in options.options) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
const chainRecords = await this.query(tld, bypassCache);
|
||||
if (chainRecords.error) {
|
||||
return resolverError(chainRecords.error);
|
||||
}
|
||||
if (!chainRecords.data?.records.length) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
let records = [];
|
||||
for (const record of chainRecords.data?.records) {
|
||||
switch (record.type) {
|
||||
case "NS": {
|
||||
await this.processNs(domain, record, records, chainRecords.data?.records, options, bypassCache);
|
||||
break;
|
||||
}
|
||||
case "GLUE4": {
|
||||
await this.processGlue(domain, record, records, options, bypassCache);
|
||||
break;
|
||||
}
|
||||
case "TXT": {
|
||||
await this.processTxt(record, records, options);
|
||||
break;
|
||||
}
|
||||
case "SYNTH6": {
|
||||
if (options.type === DNS_RECORD_TYPE.A &&
|
||||
"ipv6" in options.options &&
|
||||
options.options.ipv6) {
|
||||
records.push({
|
||||
type: options.type,
|
||||
value: record.address,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "SYNTH4": {
|
||||
if (options.type === DNS_RECORD_TYPE.A) {
|
||||
records.push({
|
||||
type: options.type,
|
||||
value: record.address,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
records = ensureUniqueRecords(records);
|
||||
if (0 < records.length) {
|
||||
return resolveSuccess(records);
|
||||
}
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
// @ts-ignore
|
||||
async processNs(domain, record, records, hnsRecords, options, bypassCache) {
|
||||
if (![DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME, DNS_RECORD_TYPE.NS].includes(options.type)) {
|
||||
return;
|
||||
}
|
||||
// @ts-ignore
|
||||
const glue = hnsRecords.slice().find((item) =>
|
||||
// @ts-ignore
|
||||
["GLUE4", "GLUE6"].includes(item.type) && item.ns === record.ns);
|
||||
if (glue && options.type !== DNS_RECORD_TYPE.NS) {
|
||||
return this.processGlue(domain, glue, records, options, bypassCache);
|
||||
}
|
||||
if (options.type === DNS_RECORD_TYPE.NS) {
|
||||
records.push({ type: options.type, value: record.ns });
|
||||
return;
|
||||
}
|
||||
const foundDomain = normalizeDomain(record.ns);
|
||||
let isIcann = false;
|
||||
let isHip5 = false;
|
||||
let hip5Parts = foundDomain.split(".");
|
||||
if (hip5Parts.length >= 2 &&
|
||||
[...(options.options?.hip5 ?? []), ...HIP5_EXTENSIONS].includes(hip5Parts[hip5Parts.length - 1])) {
|
||||
isHip5 = true;
|
||||
}
|
||||
if ((isDomain(foundDomain) || /[a-zA-Z0-9\-]+/.test(foundDomain)) &&
|
||||
!isHip5) {
|
||||
if (foundDomain.includes(".")) {
|
||||
const tld = foundDomain.split(".")[foundDomain.split(".").length - 1];
|
||||
isIcann = tldEnum.list.includes(tld);
|
||||
}
|
||||
if (!isIcann) {
|
||||
const hnsNs = await this.resolver.resolve(foundDomain, options);
|
||||
if (hnsNs.records.length) {
|
||||
let icannRecords = await this.resolver.resolve(domain, {
|
||||
...options,
|
||||
options: {
|
||||
subquery: true,
|
||||
nameserver: hnsNs.records.pop()?.value,
|
||||
},
|
||||
});
|
||||
if (icannRecords.records.length) {
|
||||
records.push.apply(records, icannRecords.records);
|
||||
}
|
||||
}
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
let icannRecords = await this.resolver.resolve(domain, {
|
||||
...options,
|
||||
options: { subquery: true, nameserver: foundDomain },
|
||||
});
|
||||
if (icannRecords.records.length) {
|
||||
records.push.apply(records, icannRecords.records);
|
||||
return;
|
||||
}
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
let result = await this.resolver.resolve(record.ns, options, bypassCache);
|
||||
if (!result.records.length) {
|
||||
result.records.push({ type: DNS_RECORD_TYPE.NS, value: record.ns });
|
||||
return;
|
||||
}
|
||||
records.push.apply(records, result.records);
|
||||
}
|
||||
async processGlue(domain, record, records, options, bypassCache) {
|
||||
if (![DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME].includes(options.type)) {
|
||||
return;
|
||||
}
|
||||
if (isDomain(record.ns) && isIp(record.address)) {
|
||||
let results = await this.resolver.resolve(domain, {
|
||||
...options,
|
||||
options: {
|
||||
subquery: true,
|
||||
nameserver: record.address,
|
||||
},
|
||||
}, bypassCache);
|
||||
if (results.records.length) {
|
||||
records.push.apply(records, results.records);
|
||||
}
|
||||
}
|
||||
}
|
||||
async query(tld, bypassCache) {
|
||||
let query = this.resolver.rpcNetwork.wisdomQuery("getnameresource", "handshake", [tld], bypassCache);
|
||||
return (await query.result);
|
||||
}
|
||||
async processTxt(record, records, options) {
|
||||
const content = record.txt.slice().pop();
|
||||
if ([DNS_RECORD_TYPE.TEXT, DNS_RECORD_TYPE.CONTENT].includes(options.type)) {
|
||||
records.push({
|
||||
type: DNS_RECORD_TYPE.TEXT,
|
||||
value: content,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
36
package.json
36
package.json
|
@ -1,32 +1,24 @@
|
|||
{
|
||||
"name": "@lumeweb/resolver-module-handshake",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"version": "0.0.2-develop.13",
|
||||
"main": "lib/index.js",
|
||||
"type": "module",
|
||||
"readme": "ERROR: No README data found!",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "gitea@git.lumeweb.com:LumeWeb/resolver-module-handshake.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build-script": "tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs",
|
||||
"compile-module": "npm run build-script && node build.js",
|
||||
"build": "tsc --project tsconfig.json",
|
||||
"build-module": "npm run compile-module && node ./dist-build/build.mjs dev"
|
||||
"prepare": "presetter bootstrap",
|
||||
"build": "run build",
|
||||
"semantic-release": "semantic-release"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lumeweb/kernel-handshake-client": "git+https://git.lumeweb.com/LumeWeb/kernel-handshake-client.git",
|
||||
"@lumeweb/kernel-libresolver": "git+https://git.lumeweb.com/LumeWeb/kernel-libresolver.git",
|
||||
"@lumeweb/libresolver": "git+https://git.lumeweb.com/LumeWeb/libresolver.git",
|
||||
"@lumeweb/tld-enum": "git+https://git.lumeweb.com/LumeWeb/list-of-top-level-domains.git"
|
||||
"@lumeweb/kernel-handshake-client": "0.1.0-develop.9",
|
||||
"@lumeweb/kernel-libresolver": "^0.1.0-develop.9",
|
||||
"@lumeweb/tld-enum": "^0.1.0-develop.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@lumeweb/cfg": "git+https://git.lumeweb.com/LumeWeb/cfg.git",
|
||||
"@lumeweb/relay-types": "git+https://git.lumeweb.com/LumeWeb/relay-types.git",
|
||||
"@scure/bip39": "^1.1.1",
|
||||
"@skynetlabs/skynet-nodejs": "^2.9.0",
|
||||
"@types/node": "^18.14.0",
|
||||
"@types/read": "^0.0.29",
|
||||
"cli-progress": "^3.12.0",
|
||||
"esbuild": "^0.15.18",
|
||||
"libskynetnode": "^0.1.4",
|
||||
"node-cache": "^5.1.2",
|
||||
"read": "^1.0.7",
|
||||
"typescript": "^4.9.5"
|
||||
"@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.44"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,218 +0,0 @@
|
|||
// This is the standard build script for a kernel module.
|
||||
|
||||
import * as fs from "fs";
|
||||
import read from "read";
|
||||
import * as bip39 from "@scure/bip39";
|
||||
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
||||
//@ts-ignore
|
||||
import { SkynetClient } from "@skynetlabs/skynet-nodejs";
|
||||
|
||||
// Helper variables to make it easier to return empty values alongside errors.
|
||||
const nu8 = new Uint8Array(0);
|
||||
const nkp = {
|
||||
publicKey: nu8,
|
||||
secretKey: nu8,
|
||||
};
|
||||
|
||||
// readFile is a wrapper for fs.readFileSync that handles the try-catch for the
|
||||
// caller.
|
||||
function readFile(fileName: string): [string, string | null] {
|
||||
try {
|
||||
let data = fs.readFileSync(fileName, "utf8");
|
||||
return [data, null];
|
||||
} catch (err) {
|
||||
return ["", "unable to read file: " + JSON.stringify(err)];
|
||||
}
|
||||
}
|
||||
|
||||
// readFileBinary is a wrapper for fs.readFileSync that handles the try-catch
|
||||
// for the caller.
|
||||
function readFileBinary(fileName: string): [Uint8Array, string | null] {
|
||||
try {
|
||||
let data = fs.readFileSync(fileName, null);
|
||||
return [data, null];
|
||||
} catch (err) {
|
||||
return [nu8, "unable to read file: " + JSON.stringify(err)];
|
||||
}
|
||||
}
|
||||
|
||||
// writeFile is a wrapper for fs.writeFileSync which handles the try-catch in a
|
||||
// non-exception way.
|
||||
function writeFile(fileName: string, fileData: string): string | null {
|
||||
try {
|
||||
fs.writeFileSync(fileName, fileData);
|
||||
return null;
|
||||
} catch (err) {
|
||||
return "unable to write file: " + JSON.stringify(err);
|
||||
}
|
||||
}
|
||||
|
||||
// handlePass handles all portions of the script that occur after the password
|
||||
// has been requested. If no password needs to be requested, handlePass will be
|
||||
// called with a null input. We need to structure the code this way because the
|
||||
// password reader is async and we can only access the password when using a
|
||||
// callback.
|
||||
function handlePass(password: string) {
|
||||
try {
|
||||
// If we are running prod and the seed file does not exist, we
|
||||
// need to confirm the password and also warn the user to use a
|
||||
// secure password.
|
||||
if (!fs.existsSync(seedFile) && process.argv[2] === "prod") {
|
||||
// The file does not exist, we need to confirm the
|
||||
// password.
|
||||
console.log();
|
||||
console.log(
|
||||
"No production entry found for module. Creating new production module..."
|
||||
);
|
||||
console.log(
|
||||
"If someone can guess the password, they can push arbitrary changes to your module."
|
||||
);
|
||||
console.log("Please use a secure password.");
|
||||
console.log();
|
||||
read(
|
||||
{ prompt: "Confirm Password: ", silent: true },
|
||||
function (err: any, confirmPassword: string) {
|
||||
if (err) {
|
||||
console.error("unable to fetch password:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
if (password !== confirmPassword) {
|
||||
console.error("passwords do not match");
|
||||
process.exit(1);
|
||||
}
|
||||
handlePassConfirm(moduleSalt, password);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// If the seed file does exist, or if we are using dev,
|
||||
// there's no need to confirm the password but we do
|
||||
// need to pass the logic off to the handlePassConfirm
|
||||
// callback.
|
||||
handlePassConfirm(moduleSalt, password);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Unable to read seedFile:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// handlePassConfirm handles the full script after the confirmation password
|
||||
// has been provided. If not confirmation password is needed, this function
|
||||
// will be called anyway using the unconfirmed password as input.
|
||||
function handlePassConfirm(seed: string, password: string) {
|
||||
// Create the seedFile if it does not exist. For dev we just save the
|
||||
// seed to disk outright, because this is a dev build and therefore not
|
||||
// security sensitive. Also the dev seed does not get pushed to the
|
||||
// github repo.
|
||||
//
|
||||
// For prod, we use the seed to create a new seed (called the shield)
|
||||
// which allows us to verify that the developer has provided the right
|
||||
// password when deploying the module. The shield does get pushed to
|
||||
// the github repo so that the production module is the same on all
|
||||
// devices.
|
||||
if (!fs.existsSync(seedFile) && process.argv[2] !== "prod") {
|
||||
// Generate the seed phrase and write it to the file.
|
||||
let seedPhrase = bip39.generateMnemonic(wordlist);
|
||||
let errWF = writeFile(seedFile, seedPhrase);
|
||||
if (errWF !== null) {
|
||||
console.error("unable to write file:", errWF);
|
||||
process.exit(1);
|
||||
}
|
||||
} else if (!fs.existsSync(seedFile) && process.argv[2] === "prod") {
|
||||
// Generate the seed phrase.
|
||||
let seedPhrase = bip39.generateMnemonic(wordlist);
|
||||
// Write the registry link to the file.
|
||||
}
|
||||
|
||||
// Load or verify the seed. If this is prod, the password is used to
|
||||
// create and verify the seed. If this is dev, we just load the seed
|
||||
// with no password.
|
||||
let seedPhrase: string;
|
||||
let registryLink: string;
|
||||
if (process.argv[2] === "prod") {
|
||||
// Generate the seed phrase from the password.
|
||||
seedPhrase = bip39.generateMnemonic(wordlist);
|
||||
} else {
|
||||
let [sp, errRF] = readFile(seedFile);
|
||||
if (errRF !== null) {
|
||||
console.error("unable to read seed phrase for dev command from disk");
|
||||
process.exit(1);
|
||||
}
|
||||
seedPhrase = sp;
|
||||
}
|
||||
|
||||
let metadata = {
|
||||
Filename: "index.js",
|
||||
};
|
||||
const client = new SkynetClient("https://web3portal.com");
|
||||
client
|
||||
.uploadFile("dist-module/index.js")
|
||||
.then((result: any) => {
|
||||
console.log("Immutable Link for kernel:", result);
|
||||
})
|
||||
.catch((err: any) => {
|
||||
console.error("unable to upload file", err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
// Add a newline for readability.
|
||||
console.log();
|
||||
|
||||
// Check for a 'dev' or 'prod' input to the script.
|
||||
if (process.argv.length !== 3) {
|
||||
console.error("need to provide either 'dev' or 'prod' as an input");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Create the build folder if it does not exist.
|
||||
if (!fs.existsSync("build")) {
|
||||
fs.mkdirSync("build");
|
||||
}
|
||||
|
||||
// Determine the seed file.
|
||||
let seedFile: string;
|
||||
if (process.argv[2] === "prod") {
|
||||
seedFile = "module-skylink";
|
||||
} else if (process.argv[2] === "dev") {
|
||||
seedFile = "build/dev-seed";
|
||||
} else {
|
||||
console.error("need to provide either 'dev' or 'prod' as an input");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// If doing a prod deployment, check whether the salt file exists. If it does
|
||||
// not, create it.
|
||||
let moduleSalt: string;
|
||||
if (!fs.existsSync(".module-salt")) {
|
||||
moduleSalt = bip39.generateMnemonic(wordlist);
|
||||
let errWF = writeFile(".module-salt", moduleSalt);
|
||||
if (errWF !== null) {
|
||||
console.error("unable to write module salt file:", errWF);
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
let [ms, errRF] = readFile(".module-salt");
|
||||
if (errRF !== null) {
|
||||
console.error("unable to read moduleSalt");
|
||||
process.exit(1);
|
||||
}
|
||||
ms = ms.replace(/\n$/, "");
|
||||
moduleSalt = ms;
|
||||
}
|
||||
|
||||
// Need to get a password if this is a prod build.
|
||||
if (process.argv[2] === "prod") {
|
||||
read(
|
||||
{ prompt: "Password: ", silent: true },
|
||||
function (err: any, password: string) {
|
||||
if (err) {
|
||||
console.error("unable to fetch password:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
handlePass(password);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
handlePass("");
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
import { setup } from "@lumeweb/kernel-libresolver";
|
||||
import Handshake from "../src/index.js";
|
||||
|
||||
setup(new Handshake());
|
295
src/index.ts
295
src/index.ts
|
@ -1,292 +1,5 @@
|
|||
import tldEnum from "@lumeweb/tld-enum";
|
||||
import {
|
||||
AbstractResolverModule,
|
||||
DNS_RECORD_TYPE,
|
||||
DNSResult,
|
||||
isDomain,
|
||||
isIp,
|
||||
isPromise,
|
||||
normalizeDomain,
|
||||
resolverEmptyResponse,
|
||||
ResolverOptions,
|
||||
resolveSuccess,
|
||||
ensureUniqueRecords,
|
||||
DNSRecord,
|
||||
getTld,
|
||||
resolverError,
|
||||
} from "@lumeweb/libresolver";
|
||||
import {
|
||||
createClient,
|
||||
Response as HandshakeResponse,
|
||||
} from "@lumeweb/kernel-handshake-client";
|
||||
import { ResolverModule } from "@lumeweb/kernel-libresolver";
|
||||
import { setup } from "@lumeweb/kernel-libresolver";
|
||||
import Handshake from "./resolver.js";
|
||||
|
||||
const client = createClient();
|
||||
|
||||
const HIP5_EXTENSIONS = ["eth", "_eth"];
|
||||
|
||||
interface HnsRecord {
|
||||
type: string;
|
||||
address: string;
|
||||
txt: string[];
|
||||
ns: string;
|
||||
}
|
||||
|
||||
export default class Handshake extends AbstractResolverModule {
|
||||
private async buildBlacklist(): Promise<Set<string>> {
|
||||
const blacklist = new Set<string>();
|
||||
let resolvers = this.resolver.resolvers as unknown as Set<ResolverModule>;
|
||||
if (isPromise(resolvers as any)) {
|
||||
resolvers = await resolvers;
|
||||
}
|
||||
|
||||
for (const resolver of resolvers) {
|
||||
let tlds: string[] | Promise<string[]> = resolver.getSupportedTlds();
|
||||
if (isPromise(tlds as any)) {
|
||||
tlds = await tlds;
|
||||
}
|
||||
(tlds as string[]).map((item: string) => blacklist.add(item));
|
||||
}
|
||||
|
||||
return blacklist;
|
||||
}
|
||||
|
||||
async resolve(
|
||||
domain: string,
|
||||
options: ResolverOptions,
|
||||
bypassCache: boolean
|
||||
): Promise<DNSResult> {
|
||||
options.options = options.options || {};
|
||||
const tld = getTld(domain);
|
||||
|
||||
const blacklist = await this.buildBlacklist();
|
||||
|
||||
if (blacklist.has(tld)) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
if (isIp(domain)) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
if (options?.options && "subquery" in options.options) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
const chainRecords = await this.query(tld);
|
||||
if (chainRecords.error) {
|
||||
return resolverError(chainRecords.error);
|
||||
}
|
||||
|
||||
if (!chainRecords.result?.records.length) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
let records: DNSRecord[] = [];
|
||||
|
||||
for (const record of chainRecords.result?.records) {
|
||||
switch (record.type) {
|
||||
case "NS": {
|
||||
await this.processNs(
|
||||
domain,
|
||||
record,
|
||||
records,
|
||||
chainRecords.result?.records,
|
||||
options,
|
||||
bypassCache
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "GLUE4": {
|
||||
await this.processGlue(domain, record, records, options, bypassCache);
|
||||
break;
|
||||
}
|
||||
case "TXT": {
|
||||
await this.processTxt(record, records, options);
|
||||
break;
|
||||
}
|
||||
case "SYNTH6": {
|
||||
if (
|
||||
options.type === DNS_RECORD_TYPE.A &&
|
||||
"ipv6" in options.options &&
|
||||
options.options.ipv6
|
||||
) {
|
||||
records.push({
|
||||
type: options.type,
|
||||
value: record.address,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "SYNTH4": {
|
||||
if (options.type === DNS_RECORD_TYPE.A) {
|
||||
records.push({
|
||||
type: options.type,
|
||||
value: record.address,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
records = ensureUniqueRecords(records);
|
||||
|
||||
if (0 < records.length) {
|
||||
return resolveSuccess(records);
|
||||
}
|
||||
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
private async processNs(
|
||||
domain: string,
|
||||
record: HnsRecord,
|
||||
records: DNSRecord[],
|
||||
hnsRecords: HnsRecord[],
|
||||
options: ResolverOptions,
|
||||
bypassCache: boolean
|
||||
) {
|
||||
if (
|
||||
![DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME, DNS_RECORD_TYPE.NS].includes(
|
||||
options.type
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// @ts-ignore
|
||||
const glue = hnsRecords.slice().find(
|
||||
(item: object) =>
|
||||
// @ts-ignore
|
||||
["GLUE4", "GLUE6"].includes(item.type) && item.ns === record.ns
|
||||
);
|
||||
|
||||
if (glue && options.type !== DNS_RECORD_TYPE.NS) {
|
||||
return this.processGlue(domain, glue, records, options, bypassCache);
|
||||
}
|
||||
|
||||
if (options.type === DNS_RECORD_TYPE.NS) {
|
||||
records.push({ type: options.type, value: record.ns });
|
||||
return;
|
||||
}
|
||||
|
||||
const foundDomain = normalizeDomain(record.ns);
|
||||
|
||||
let isIcann = false;
|
||||
let isHip5 = false;
|
||||
|
||||
let hip5Parts = foundDomain.split(".");
|
||||
|
||||
if (
|
||||
hip5Parts.length >= 2 &&
|
||||
[...(options.options?.hip5 ?? []), ...HIP5_EXTENSIONS].includes(
|
||||
hip5Parts[hip5Parts.length - 1]
|
||||
)
|
||||
) {
|
||||
isHip5 = true;
|
||||
}
|
||||
|
||||
if (
|
||||
(isDomain(foundDomain) || /[a-zA-Z0-9\-]+/.test(foundDomain)) &&
|
||||
!isHip5
|
||||
) {
|
||||
if (foundDomain.includes(".")) {
|
||||
const tld = foundDomain.split(".")[foundDomain.split(".").length - 1];
|
||||
|
||||
isIcann = tldEnum.list.includes(tld);
|
||||
}
|
||||
|
||||
if (!isIcann) {
|
||||
const hnsNs = await this.resolver.resolve(foundDomain, options);
|
||||
|
||||
if (hnsNs.records.length) {
|
||||
let icannRecords = await this.resolver.resolve(domain, {
|
||||
...options,
|
||||
options: {
|
||||
subquery: true,
|
||||
nameserver: hnsNs.records.pop()?.value,
|
||||
},
|
||||
});
|
||||
if (icannRecords.records.length) {
|
||||
records.push.apply(records, icannRecords.records);
|
||||
}
|
||||
}
|
||||
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
let icannRecords = await this.resolver.resolve(domain, {
|
||||
...options,
|
||||
options: { subquery: true, nameserver: foundDomain },
|
||||
});
|
||||
if (icannRecords.records.length) {
|
||||
records.push.apply(records, icannRecords.records);
|
||||
return;
|
||||
}
|
||||
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
let result = await this.resolver.resolve(record.ns, options, bypassCache);
|
||||
|
||||
if (!result.records.length) {
|
||||
result.records.push({ type: DNS_RECORD_TYPE.NS, value: record.ns });
|
||||
return;
|
||||
}
|
||||
|
||||
records.push.apply(records, result.records);
|
||||
}
|
||||
|
||||
private async processGlue(
|
||||
domain: string,
|
||||
record: HnsRecord,
|
||||
records: DNSRecord[],
|
||||
options: ResolverOptions,
|
||||
bypassCache: boolean
|
||||
) {
|
||||
if (![DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME].includes(options.type)) {
|
||||
return;
|
||||
}
|
||||
if (isDomain(record.ns) && isIp(record.address)) {
|
||||
let results = await this.resolver.resolve(
|
||||
domain,
|
||||
{
|
||||
...options,
|
||||
options: {
|
||||
subquery: true,
|
||||
nameserver: record.address,
|
||||
},
|
||||
},
|
||||
bypassCache
|
||||
);
|
||||
if (results.records.length) {
|
||||
records.push.apply(records, results.records);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async query(tld: string): Promise<HandshakeResponse> {
|
||||
return client.query("getnameresource", [tld, true]);
|
||||
}
|
||||
|
||||
private async processTxt(
|
||||
record: HnsRecord,
|
||||
records: DNSRecord[],
|
||||
options: ResolverOptions
|
||||
) {
|
||||
const content = record.txt.slice().pop() as string;
|
||||
|
||||
if (
|
||||
[DNS_RECORD_TYPE.TEXT, DNS_RECORD_TYPE.CONTENT].includes(options.type)
|
||||
) {
|
||||
records.push({
|
||||
type: DNS_RECORD_TYPE.TEXT,
|
||||
value: content,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// @ts-ignore
|
||||
setup(new Handshake());
|
||||
|
|
|
@ -0,0 +1,250 @@
|
|||
import tldEnum from "@lumeweb/tld-enum";
|
||||
import {
|
||||
AbstractResolverModule,
|
||||
DNS_RECORD_TYPE,
|
||||
DNSResult,
|
||||
isDomain,
|
||||
isIp,
|
||||
isPromise,
|
||||
normalizeDomain,
|
||||
resolverEmptyResponse,
|
||||
ResolverOptions,
|
||||
resolveSuccess,
|
||||
ensureUniqueRecords,
|
||||
DNSRecord,
|
||||
getTld,
|
||||
resolverError,
|
||||
} from "@lumeweb/libresolver";
|
||||
import {
|
||||
createClient,
|
||||
Response as HandshakeResponse,
|
||||
} from "@lumeweb/kernel-handshake-client";
|
||||
import { ResolverModule } from "@lumeweb/kernel-dns-client";
|
||||
|
||||
const client = createClient();
|
||||
|
||||
const HIP5_EXTENSIONS = ["eth", "_eth"];
|
||||
|
||||
interface HnsRecord {
|
||||
type: string;
|
||||
address: string;
|
||||
txt: string[];
|
||||
ns: string;
|
||||
}
|
||||
|
||||
export default class Handshake extends AbstractResolverModule {
|
||||
async resolve(
|
||||
domain: string,
|
||||
options: ResolverOptions,
|
||||
bypassCache: boolean,
|
||||
): Promise<DNSResult> {
|
||||
options.options = options.options || {};
|
||||
|
||||
if (await this.shouldBypassResolution(domain)) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
const chainRecords = await this.query(getTld(domain));
|
||||
if (chainRecords.error) {
|
||||
return resolverError(chainRecords.error);
|
||||
}
|
||||
|
||||
const hnsRecords = chainRecords.result?.records;
|
||||
if (!hnsRecords || !hnsRecords.length) {
|
||||
return resolverEmptyResponse();
|
||||
}
|
||||
|
||||
let records = await this.processRecords(
|
||||
hnsRecords,
|
||||
options,
|
||||
domain,
|
||||
bypassCache,
|
||||
);
|
||||
records = ensureUniqueRecords(records);
|
||||
|
||||
return records.length > 0
|
||||
? resolveSuccess(records)
|
||||
: resolverEmptyResponse();
|
||||
}
|
||||
|
||||
private async buildBlacklist(): Promise<Set<string>> {
|
||||
const blacklist = new Set<string>();
|
||||
let resolvers = this.resolver.resolvers as unknown as Set<ResolverModule>;
|
||||
if (isPromise(resolvers as any)) {
|
||||
resolvers = await resolvers;
|
||||
}
|
||||
|
||||
for (const resolver of resolvers) {
|
||||
let tlds: string[] | Promise<string[]> = resolver.getSupportedTlds();
|
||||
if (isPromise(tlds as any)) {
|
||||
tlds = await tlds;
|
||||
}
|
||||
(tlds as string[]).map((item: string) => blacklist.add(item));
|
||||
}
|
||||
|
||||
return blacklist;
|
||||
}
|
||||
|
||||
async processRecords(
|
||||
hnsRecords: HnsRecord[],
|
||||
options: ResolverOptions,
|
||||
domain: string,
|
||||
bypassCache: boolean,
|
||||
): Promise<DNSRecord[]> {
|
||||
let records: DNSRecord[] = [];
|
||||
|
||||
const nsRecords = this.findRecordsByType(hnsRecords, "NS");
|
||||
const contentRecords = this.findRecordsByType(hnsRecords, "TXT");
|
||||
|
||||
// Scenario: Content and NS Records Found (HIP-5)
|
||||
if (
|
||||
nsRecords &&
|
||||
contentRecords &&
|
||||
options.type === DNS_RECORD_TYPE.CONTENT
|
||||
) {
|
||||
return this.handleContentRecords(contentRecords, options);
|
||||
}
|
||||
|
||||
// Scenario: HIP-5 Compliance
|
||||
if (nsRecords) {
|
||||
const hip5Record = nsRecords.find((record) =>
|
||||
this.isNSHip5(record, options),
|
||||
);
|
||||
if (hip5Record) {
|
||||
let result = await this.resolver.resolve(
|
||||
hip5Record.ns,
|
||||
{
|
||||
...options,
|
||||
options: {
|
||||
domain,
|
||||
},
|
||||
},
|
||||
bypassCache,
|
||||
);
|
||||
|
||||
if (result.records.length) {
|
||||
records.push(...result.records);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario: Delegated Lookup (via NS)
|
||||
if (nsRecords && this.isNSHip5(nsRecords[0], options)) {
|
||||
return await this.handleDelegatedLookup(
|
||||
nsRecords,
|
||||
options,
|
||||
domain,
|
||||
bypassCache,
|
||||
);
|
||||
}
|
||||
|
||||
// Scenario: Content Records
|
||||
if (contentRecords) {
|
||||
return this.handleContentRecords(contentRecords, options);
|
||||
}
|
||||
|
||||
// Scenario: Direct DNS Query
|
||||
return await this.handleWithNameserver(domain, options);
|
||||
}
|
||||
|
||||
// Handle Content Records
|
||||
handleContentRecords(
|
||||
contentRecords: HnsRecord[],
|
||||
options: ResolverOptions,
|
||||
): DNSRecord[] {
|
||||
let records: DNSRecord[] = [];
|
||||
if (options.type === DNS_RECORD_TYPE.CONTENT) {
|
||||
contentRecords.forEach((record) =>
|
||||
records.push({
|
||||
type: DNS_RECORD_TYPE.CONTENT,
|
||||
value: record.txt?.slice().pop() as string,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
// Handle HIP-5 NS Delegation
|
||||
async handleDelegatedLookup(
|
||||
nsRecords: HnsRecord[],
|
||||
options: ResolverOptions,
|
||||
domain: string,
|
||||
bypassCache: boolean,
|
||||
): Promise<DNSRecord[]> {
|
||||
let records: DNSRecord[] = [];
|
||||
const result = await this.resolver.resolve(
|
||||
nsRecords[0].ns as string,
|
||||
{ ...options, options: { domain } },
|
||||
bypassCache,
|
||||
);
|
||||
|
||||
if (result.records.length) {
|
||||
records.push(...result.records);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
// Check if the resolution should be bypassed
|
||||
async shouldBypassResolution(domain: string): Promise<boolean> {
|
||||
const tld = getTld(domain);
|
||||
const blacklist = await this.buildBlacklist();
|
||||
return blacklist.has(tld) || isIp(domain);
|
||||
}
|
||||
|
||||
// Handle the case where a nameserver is found
|
||||
async handleWithNameserver(
|
||||
domain: string,
|
||||
options: ResolverOptions,
|
||||
): Promise<DNSRecord[]> {
|
||||
let records: DNSRecord[] = [];
|
||||
|
||||
for (const type of [DNS_RECORD_TYPE.A, DNS_RECORD_TYPE.CNAME]) {
|
||||
if (type === options.type) {
|
||||
const ret = await this.dnsQuery(domain, type);
|
||||
if (ret.length) {
|
||||
records.push({ type, value: ret.slice().shift().data.address });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
|
||||
private async query(tld: string): Promise<HandshakeResponse> {
|
||||
return client.query("getnameresource", [tld, true]);
|
||||
}
|
||||
|
||||
private async dnsQuery(domain: string, type: string): Promise<any> {
|
||||
return client.dnsQuery(domain, type);
|
||||
}
|
||||
|
||||
async ready() {
|
||||
return ((await client.status()) as any)?.ready;
|
||||
}
|
||||
private findRecordsByType(
|
||||
records: HnsRecord[],
|
||||
type: "NS" | "SYNTH4" | "SYNTH6" | "GLUE4" | "GLUE6" | "TXT",
|
||||
) {
|
||||
const ret = records.filter((item) => item.type === type);
|
||||
|
||||
if (!ret.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private isNSHip5(record: HnsRecord, options: ResolverOptions) {
|
||||
const foundDomain = normalizeDomain(record.ns);
|
||||
|
||||
let hip5Parts = foundDomain.split(".");
|
||||
|
||||
return (
|
||||
hip5Parts.length >= 2 &&
|
||||
[...(options.options?.hip5 ?? []), ...HIP5_EXTENSIONS].includes(
|
||||
hip5Parts[hip5Parts.length - 1],
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2021",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"declaration": true,
|
||||
"outDir": "./dist-build",
|
||||
"strict": true,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"include": [
|
||||
"src-build"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"**/__tests__/*"
|
||||
]
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2021",
|
||||
"declaration": true,
|
||||
"moduleResolution": "node",
|
||||
"outDir": "./dist",
|
||||
"strict": true,
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "**/__tests__/*"]
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2021",
|
||||
"declaration": true,
|
||||
"moduleResolution": "node",
|
||||
"outDir": "./dist-module",
|
||||
"strict": true,
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": ["src-module"],
|
||||
"exclude": ["node_modules", "**/__tests__/*"]
|
||||
}
|
Loading…
Reference in New Issue