Compare commits
No commits in common. "develop" and "master" have entirely different histories.
|
@ -1,13 +0,0 @@
|
||||||
name: Build/Publish
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- develop
|
|
||||||
- develop-*
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
main:
|
|
||||||
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
|
|
||||||
secrets: inherit
|
|
|
@ -1,13 +0,0 @@
|
||||||
{
|
|
||||||
"preset": [
|
|
||||||
"@lumeweb/presetter-kernel-module-preset"
|
|
||||||
],
|
|
||||||
"config": {
|
|
||||||
"official": true,
|
|
||||||
"vite": {
|
|
||||||
"define": {
|
|
||||||
"globalThis.process.env.NODE_ENV": null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
22
CHANGELOG.md
22
CHANGELOG.md
|
@ -1,22 +0,0 @@
|
||||||
# [1.0.0-develop.9](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.8...v1.0.0-develop.9) (2023-10-26)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
* only push a record if we have a content response ([65dffe2](https://git.lumeweb.com/LumeWeb/resolver-module-eth/commit/65dffe202ec82a7bcfd16e959445825ffa136938))
|
|
||||||
|
|
||||||
# [1.0.0-develop.8](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.7...v1.0.0-develop.8) (2023-10-12)
|
|
||||||
|
|
||||||
# [1.0.0-develop.7](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.6...v1.0.0-develop.7) (2023-09-04)
|
|
||||||
|
|
||||||
# [1.0.0-develop.6](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.5...v1.0.0-develop.6) (2023-09-03)
|
|
||||||
|
|
||||||
# [1.0.0-develop.5](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.4...v1.0.0-develop.5) (2023-09-02)
|
|
||||||
|
|
||||||
# [1.0.0-develop.4](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.3...v1.0.0-develop.4) (2023-07-29)
|
|
||||||
|
|
||||||
# [1.0.0-develop.3](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.2...v1.0.0-develop.3) (2023-07-29)
|
|
||||||
|
|
||||||
# [1.0.0-develop.2](https://git.lumeweb.com/LumeWeb/resolver-module-eth/compare/v1.0.0-develop.1...v1.0.0-develop.2) (2023-07-29)
|
|
||||||
|
|
||||||
# 1.0.0-develop.1 (2023-07-15)
|
|
|
@ -1,3 +1,2 @@
|
||||||
# resolver-module-eth
|
# resolver-module-eth
|
||||||
|
|
||||||
Module CID: zdiKvnZYNjDqXaM8uF3pGEs7Tt6jqGc7t7M4eqbvJwpkTnrZymncfUW9Cj
|
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
import esbuild from "esbuild";
|
||||||
|
|
||||||
|
esbuild.buildSync({
|
||||||
|
entryPoints: ["src-module/index.ts"],
|
||||||
|
outfile: "dist-module/index.js",
|
||||||
|
format: "esm",
|
||||||
|
bundle: true,
|
||||||
|
legalComments: "external",
|
||||||
|
// minify: true
|
||||||
|
inject: ["./polyfill.js"],
|
||||||
|
tsconfig: "tsconfig.module.json",
|
||||||
|
define: {
|
||||||
|
global: "self",
|
||||||
|
},
|
||||||
|
});
|
File diff suppressed because it is too large
Load Diff
52
package.json
52
package.json
|
@ -1,28 +1,42 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/resolver-module-eth",
|
"name": "@lumeweb/resolver-module-eip137",
|
||||||
"version": "1.0.0-develop.9",
|
"version": "0.1.0",
|
||||||
"main": "lib/index.js",
|
"main": "dist/index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"readme": "ERROR: No README data found!",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "gitea@git.lumeweb.com:LumeWeb/resolver-module-eth.git"
|
|
||||||
},
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "presetter bootstrap",
|
"build-script": "tsc --project tsconfig.build.json && mv dist-build/build.js dist-build/build.mjs",
|
||||||
"build": "run build",
|
"compile-module": "npm run build-script && node build.js",
|
||||||
"semantic-release": "semantic-release"
|
"build": "tsc --project tsconfig.json",
|
||||||
|
"build-module": "npm run compile-module && node ./dist-build/build.mjs dev"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ensdomains/ensjs": "^3.0.0-alpha.65",
|
"@lumeweb/ensjs": "^2.1.2",
|
||||||
"@lumeweb/kernel-eth-client": "^0.1.0-develop.17",
|
"@lumeweb/kernel-eth-client": "git+https://git.lumeweb.com/LumeWeb/kernel-eth-client.git",
|
||||||
"@lumeweb/kernel-libresolver": "^0.1.0-develop.9",
|
"@lumeweb/kernel-libresolver": "git+https://git.lumeweb.com/LumeWeb/kernel-libresolver.git",
|
||||||
"crypto-addr-codec": "github:e2xlabs/crypto-addr-serialize",
|
"@lumeweb/libresolver": "git+https://git.lumeweb.com/LumeWeb/libresolver.git",
|
||||||
"ethers": "^6.6.3"
|
"ethers": "^6.3.0",
|
||||||
|
"libskynet": "^0.1.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.44",
|
"@scure/bip39": "^1.2.0",
|
||||||
"vite-plugin-commonjs": "^0.8.1",
|
"@skynetlabs/skynet-nodejs": "^2.9.0",
|
||||||
"vite-plugin-dynamic-import": "^1.5.0"
|
"@types/node": "^18.15.11",
|
||||||
|
"@types/read": "^0.0.29",
|
||||||
|
"buffer": "^6.0.3",
|
||||||
|
"cli-progress": "^3.12.0",
|
||||||
|
"crypto-browserify": "^3.12.0",
|
||||||
|
"esbuild": "^0.15.18",
|
||||||
|
"libskynetnode": "^0.1.4",
|
||||||
|
"os-browserify": "^0.3.0",
|
||||||
|
"prettier": "^2.8.7",
|
||||||
|
"process": "^0.11.10",
|
||||||
|
"read": "^1.0.7",
|
||||||
|
"stream-browserify": "^3.0.0",
|
||||||
|
"typescript": "^5.0.4"
|
||||||
|
},
|
||||||
|
"browser": {
|
||||||
|
"crypto": "crypto-browserify",
|
||||||
|
"stream": "stream-browserify",
|
||||||
|
"os": "os-browserify"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
export const process = require("process");
|
||||||
|
export const Buffer = require("buffer").Buffer;
|
|
@ -0,0 +1,218 @@
|
||||||
|
// 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("");
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { setup } from "@lumeweb/kernel-libresolver";
|
||||||
|
import Eth from "../src/index.js";
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
setup(new Eth());
|
135
src/index.ts
135
src/index.ts
|
@ -1,5 +1,134 @@
|
||||||
import { setup } from "@lumeweb/kernel-libresolver";
|
import {
|
||||||
import Eth from "./resolver.js";
|
AbstractResolverModule,
|
||||||
|
DNS_RECORD_TYPE,
|
||||||
|
DNSRecord,
|
||||||
|
DNSResult,
|
||||||
|
isPromise,
|
||||||
|
resolverEmptyResponse,
|
||||||
|
resolverError,
|
||||||
|
ResolverOptions,
|
||||||
|
resolveSuccess,
|
||||||
|
} from "@lumeweb/libresolver";
|
||||||
|
import { ethers } from "ethers";
|
||||||
|
import { createProvider } from "@lumeweb/kernel-eth-client";
|
||||||
|
// @ts-ignore
|
||||||
|
import ENSRoot, { getEnsAddress } from "@lumeweb/ensjs";
|
||||||
|
|
||||||
|
const ENS = ENSRoot.default;
|
||||||
|
|
||||||
|
const ETH_PROVIDER = createProvider();
|
||||||
|
// @ts-ignore
|
||||||
|
ETH_PROVIDER._isProvider = true;
|
||||||
|
|
||||||
|
const ETH_SIGNER = new ethers.VoidSigner(ethers.ZeroAddress, ETH_PROVIDER);
|
||||||
|
// @ts-ignore
|
||||||
|
ETH_SIGNER._isSigner = true;
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
setup(new Eth());
|
ETH_PROVIDER.getSigner = () => ETH_SIGNER;
|
||||||
|
|
||||||
|
export default class Eth extends AbstractResolverModule {
|
||||||
|
// @ts-ignore
|
||||||
|
getSupportedTlds(): string[] {
|
||||||
|
return ["eth"];
|
||||||
|
}
|
||||||
|
|
||||||
|
async resolve(
|
||||||
|
domain: string,
|
||||||
|
options: ResolverOptions,
|
||||||
|
bypassCache: boolean
|
||||||
|
): Promise<DNSResult> {
|
||||||
|
const hip5Data = domain.split(".");
|
||||||
|
if (
|
||||||
|
2 <= hip5Data.length &&
|
||||||
|
options.options &&
|
||||||
|
"domain" in options.options
|
||||||
|
) {
|
||||||
|
if (ethers.isAddress(hip5Data[0])) {
|
||||||
|
let chain = hip5Data[1].replace("_", "");
|
||||||
|
domain = options.options.domain;
|
||||||
|
|
||||||
|
if (chain !== "eth") {
|
||||||
|
return resolverError("HIP5 chain not supported");
|
||||||
|
}
|
||||||
|
return this.resolve137(domain, options, bypassCache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await this.isTldSupported(domain)) {
|
||||||
|
return this.resolve137(domain, options, bypassCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolverEmptyResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async resolve137(
|
||||||
|
domain: string,
|
||||||
|
options: ResolverOptions,
|
||||||
|
bypassCache: boolean
|
||||||
|
): Promise<DNSResult> {
|
||||||
|
const records: DNSRecord[] = [];
|
||||||
|
|
||||||
|
const ens = new ENS({
|
||||||
|
provider: ETH_PROVIDER,
|
||||||
|
ensAddress: getEnsAddress(1),
|
||||||
|
});
|
||||||
|
|
||||||
|
let name;
|
||||||
|
try {
|
||||||
|
name = await ens.name(domain);
|
||||||
|
} catch (e: any) {
|
||||||
|
return resolverError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
let content;
|
||||||
|
if (
|
||||||
|
[DNS_RECORD_TYPE.CONTENT, DNS_RECORD_TYPE.TEXT].includes(options.type)
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
content = maybeGetContentHash(await name.getContent());
|
||||||
|
} catch (e: any) {
|
||||||
|
return resolverError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
records.push({
|
||||||
|
type: DNS_RECORD_TYPE.CONTENT,
|
||||||
|
value: content as string,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([DNS_RECORD_TYPE.CUSTOM].includes(options.type)) {
|
||||||
|
let text;
|
||||||
|
try {
|
||||||
|
text = await name.getText(options.customType);
|
||||||
|
} catch (e: any) {
|
||||||
|
return resolverError(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
records.push({
|
||||||
|
type: options.type,
|
||||||
|
customType: options.customType,
|
||||||
|
value: content as string,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 < records.length) {
|
||||||
|
return resolveSuccess(records);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolverEmptyResponse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function maybeGetContentHash(contentResult: any): string | boolean {
|
||||||
|
let content = false;
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof contentResult === "object" &&
|
||||||
|
"contenthash" === contentResult.contentType
|
||||||
|
) {
|
||||||
|
content = contentResult.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
129
src/resolver.ts
129
src/resolver.ts
|
@ -1,129 +0,0 @@
|
||||||
import {
|
|
||||||
AbstractResolverModule,
|
|
||||||
DNS_RECORD_TYPE,
|
|
||||||
DNSRecord,
|
|
||||||
DNSResult,
|
|
||||||
resolverEmptyResponse,
|
|
||||||
resolverError,
|
|
||||||
ResolverOptions,
|
|
||||||
resolveSuccess,
|
|
||||||
} from "@lumeweb/libresolver";
|
|
||||||
import { ethers } from "ethers";
|
|
||||||
import { createClient, createProvider } from "@lumeweb/kernel-eth-client";
|
|
||||||
// @ts-ignore
|
|
||||||
import { ENS } from "@ensdomains/ensjs";
|
|
||||||
|
|
||||||
const ETH_PROVIDER = createProvider();
|
|
||||||
// @ts-ignore
|
|
||||||
ETH_PROVIDER._isProvider = true;
|
|
||||||
|
|
||||||
const ETH_SIGNER = new ethers.VoidSigner(ethers.ZeroAddress, ETH_PROVIDER);
|
|
||||||
// @ts-ignore
|
|
||||||
ETH_SIGNER._isSigner = true;
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
ETH_PROVIDER.getSigner = () => ETH_SIGNER;
|
|
||||||
|
|
||||||
let ENSInstance: ENS;
|
|
||||||
const ETH_CLIENT = createClient();
|
|
||||||
|
|
||||||
async function getEns() {
|
|
||||||
if (!ENSInstance) {
|
|
||||||
ENSInstance = new ENS({
|
|
||||||
graphURI: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
await ENSInstance.setProvider(ETH_PROVIDER as any);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ENSInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Eth extends AbstractResolverModule {
|
|
||||||
// @ts-ignore
|
|
||||||
getSupportedTlds(): string[] {
|
|
||||||
return ["eth"];
|
|
||||||
}
|
|
||||||
|
|
||||||
async resolve(
|
|
||||||
domain: string,
|
|
||||||
options: ResolverOptions,
|
|
||||||
bypassCache: boolean,
|
|
||||||
): Promise<DNSResult> {
|
|
||||||
const hip5Data = domain.split(".");
|
|
||||||
if (
|
|
||||||
2 <= hip5Data.length &&
|
|
||||||
options.options &&
|
|
||||||
"domain" in options.options
|
|
||||||
) {
|
|
||||||
if (ethers.isAddress(hip5Data[0])) {
|
|
||||||
let chain = hip5Data[1].replace("_", "");
|
|
||||||
domain = options.options.domain;
|
|
||||||
|
|
||||||
if (chain !== "eth") {
|
|
||||||
return resolverError("HIP5 chain not supported");
|
|
||||||
}
|
|
||||||
return this.resolve137(domain, options, bypassCache);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await this.isTldSupported(domain)) {
|
|
||||||
return this.resolve137(domain, options, bypassCache);
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolverEmptyResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async resolve137(
|
|
||||||
domain: string,
|
|
||||||
options: ResolverOptions,
|
|
||||||
bypassCache: boolean,
|
|
||||||
): Promise<DNSResult> {
|
|
||||||
const records: DNSRecord[] = [];
|
|
||||||
|
|
||||||
const ens = await getEns();
|
|
||||||
|
|
||||||
let content;
|
|
||||||
if (
|
|
||||||
[DNS_RECORD_TYPE.CONTENT, DNS_RECORD_TYPE.TEXT].includes(options.type)
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
content = await ens.getContentHash(domain);
|
|
||||||
} catch (e: any) {
|
|
||||||
return resolverError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (content) {
|
|
||||||
records.push({
|
|
||||||
type: DNS_RECORD_TYPE.CONTENT,
|
|
||||||
value: `${content.protocolType}://${content.decoded}` as string,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ([DNS_RECORD_TYPE.CUSTOM].includes(options.type)) {
|
|
||||||
let text;
|
|
||||||
try {
|
|
||||||
text = await ens.getText(domain, options.customType as string);
|
|
||||||
} catch (e: any) {
|
|
||||||
return resolverError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
records.push({
|
|
||||||
type: options.type,
|
|
||||||
customType: options.customType,
|
|
||||||
value: text as string,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (0 < records.length) {
|
|
||||||
return resolveSuccess(records);
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolverEmptyResponse();
|
|
||||||
}
|
|
||||||
|
|
||||||
async ready(): Promise<void> {
|
|
||||||
return ((await ETH_CLIENT.status()) as any)?.ready;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2021",
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"declaration": true,
|
||||||
|
"outDir": "./dist-build",
|
||||||
|
"strict": true
|
||||||
|
},
|
||||||
|
"include": ["src-build"],
|
||||||
|
"exclude": ["node_modules", "**/__tests__/*"]
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2021",
|
||||||
|
"declaration": true,
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"outDir": "./dist",
|
||||||
|
"strict": true,
|
||||||
|
"allowSyntheticDefaultImports": true
|
||||||
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"exclude": ["node_modules", "**/__tests__/*"]
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2021",
|
||||||
|
"declaration": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"outDir": "./dist-module",
|
||||||
|
"strict": true,
|
||||||
|
"allowSyntheticDefaultImports": true
|
||||||
|
},
|
||||||
|
"include": ["src-module"],
|
||||||
|
"exclude": ["node_modules", "**/__tests__/*"]
|
||||||
|
}
|
|
@ -1,50 +0,0 @@
|
||||||
import { defineConfig } from "vite";
|
|
||||||
import optimizer from "vite-plugin-optimizer";
|
|
||||||
import { nodePolyfills } from "vite-plugin-node-polyfills";
|
|
||||||
import dynamicImport from "vite-plugin-dynamic-import";
|
|
||||||
import commonjs from "vite-plugin-commonjs";
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
build: {
|
|
||||||
outDir: "lib",
|
|
||||||
lib: {
|
|
||||||
entry: "build/index.js",
|
|
||||||
name: "main",
|
|
||||||
formats: ["cjs"],
|
|
||||||
fileName: "index",
|
|
||||||
},
|
|
||||||
minify: false,
|
|
||||||
rollupOptions: { output: { inlineDynamicImports: true } },
|
|
||||||
},
|
|
||||||
define: {
|
|
||||||
"globalThis.process.env.NODE_ENV": null,
|
|
||||||
"window.": "globalThis.",
|
|
||||||
},
|
|
||||||
resolve: {
|
|
||||||
dedupe: ["@lumeweb/libportal", "@lumeweb/libweb", "@lumeweb/libkernel"],
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
dynamicImport({
|
|
||||||
filter(id) {
|
|
||||||
if (id.includes("@ensdomains/ensjs/dist/esm")) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
optimizer({
|
|
||||||
"node-fetch":
|
|
||||||
"const e = undefined; export default e;export {e as Response, e as FormData, e as Blob};",
|
|
||||||
}),
|
|
||||||
nodePolyfills({
|
|
||||||
exclude: ["fs"],
|
|
||||||
globals: { Buffer: true, global: true, process: true },
|
|
||||||
}),
|
|
||||||
commonjs({
|
|
||||||
filter(id) {
|
|
||||||
if (id.includes("@ensdomains/address-encoder")) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
});
|
|
Loading…
Reference in New Issue