@justaname.id/hybrid-primary-name
Version:
**Hybrid Primary Name** is a Viem extension package that enables reverse ENS resolution to fetch the primary ENS name associated with a given Ethereum address. It integrates seamlessly with both Viem's `createPublicClient` and ENS's `createEnsPublicClient
83 lines (82 loc) • 3.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.primaryName = primaryName;
const public_1 = require("@ensdomains/ensjs/public");
const chains_1 = require("viem/chains");
const SUPPORTED_CHAIN_IDS = [chains_1.mainnet.id, chains_1.sepolia.id];
const API_URL = "https://api.justaname.id/ens/v1/primary-name/address";
/**
* Creates a plugin that extends the PublicClient with ENS resolution capabilities.
* This plugin adds the ability to perform reverse ENS lookups using both on-chain
* and API-based resolution methods.
*
* @returns A function that extends the client with the getEnsFromAddress method
*/
function primaryName() {
return function (client) {
return {
/**
* Performs reverse ENS resolution for a given Ethereum address.
* First attempts on-chain resolution, then falls back to API-based lookup.
*
* @param address - The Ethereum address to resolve
* @returns The primary ENS name associated with the address, or null if none found
* @throws Error if the current chain ID is not supported (must be mainnet or sepolia)
*
* @example
* ```typescript
* const ensName = await client.getEnsFromAddress('0x123...')
* if (ensName) {
* console.log(`ENS name: ${ensName}`)
* }
* ```
*/
getEnsFromAddress(address) {
return __awaiter(this, void 0, void 0, function* () {
const chainId = yield client.chain.id;
if (!SUPPORTED_CHAIN_IDS.includes(chainId)) {
throw new Error("Chain ID not supported");
}
try {
const reverseResult = yield (0, public_1.getName)(client, { address });
if (reverseResult === null || reverseResult === void 0 ? void 0 : reverseResult.name) {
return reverseResult.name;
}
}
catch (error) {
// Handle error if necessary
}
try {
const url = new URL(API_URL);
const params = new URLSearchParams({
address,
chainId: chainId.toString(),
});
url.search = params.toString();
const response = yield fetch(url);
if (response.ok) {
const { result } = yield response.json(); // TODO: Add proper type
const { name } = result.data;
if (name) {
return name;
}
}
}
catch (error) {
// Handle error if necessary
}
return null;
});
},
};
};
}