Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/extension/src/libs/name-resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class GenericNameResolver {
});
}

async resolveName(name: string, coins: CoinType[]): Promise<string | null> {
async resolveName(name: string, coins: CoinType[], providerChain?: string): Promise<string | null> {
let response: string | null = null;
for (const coin of coins) {
response = await this.nameResolver
.resolveAddress(name, coin)
.resolveAddress(name, coin, providerChain)
.catch(() => null);
if (response) return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ const isChecked = (address: string) => {
props.network.displayAddress(address)
);
} catch (err) {
console.error('Error checking if address is checked', err);
console.error(
'Error checking if address is checked, probably name resolver',
);
return false;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,11 @@ const inputAddressFrom = (text: string) => {
const inputAddressTo = async (text: string) => {
const debounceResolve = debounce(() => {
nameResolver
.resolveName(text, [props.network.name as CoinType, 'ETH'])
.resolveName(
text,
[props.network.name as CoinType, 'ETH'],
props.network.provider as string,
)
.then(resolved => {
if (resolved) {
addressTo.value = resolved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,11 @@ const inputAddressFrom = (text: string) => {
const inputAddressTo = async (text: string) => {
const debounceResolve = debounce(() => {
nameResolver
.resolveName(text, [props.network.name as CoinType, 'ETH'])
.resolveName(
text,
[props.network.name as CoinType, 'ETH'],
props.network.provider as string,
)
.then(resolved => {
if (resolved) {
addressTo.value = resolved;
Expand Down
8 changes: 6 additions & 2 deletions packages/extension/src/ui/action/views/swap/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ const setMax = () => {
const inputAddress = (text: string) => {
const debounceResolve = debounce(() => {
nameResolver
.resolveName(text, [props.network.name as CoinType, 'ETH'])
.resolveName(
text,
[props.network.name as CoinType, 'ETH'],
props.network.provider as string,
)
.then(resolved => {
if (resolved) {
inputAddress(resolved);
Expand Down Expand Up @@ -773,7 +777,7 @@ const isDisabled = computed(() => {
return true;
if (
BigNumber(fromAmount.value).gt(
fromBase(fromToken.value!.balance.toString(), fromToken.value.decimals),
fromBase(fromToken.value!.balance!.toString(), fromToken.value!.decimals),
)
)
return true;
Expand Down
3 changes: 3 additions & 0 deletions packages/name-resolution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"node": ">=14.15.0"
},
"devDependencies": {
"@bonfida/spl-name-service": "^3.0.10",
"@types/node": "^22.15.24",
"@typescript-eslint/eslint-plugin": "^8.33.0",
"@typescript-eslint/parser": "^8.33.0",
Expand All @@ -37,6 +38,7 @@
"tsup": "^8.5.0",
"typescript": "^5.8.3",
"typescript-eslint": "8.33.0",
"viem": "^2.29.2",
"vitest": "^3.1.4"
},
"repository": {
Expand All @@ -50,6 +52,7 @@
"@ensdomains/address-encoder": "^1.1.2",
"@siddomains/sidjs": "0.1.29",
"@unstoppabledomains/resolution": "^9.3.3",
"@web3-name-sdk/core": "^0.4.1",
"ethers": "^5.8.0"
}
}
8 changes: 5 additions & 3 deletions packages/name-resolution/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ class NameResolver {
if (name !== null) return name;
}
return null;
})
}),
);
}

public async resolveAddress(
name: string,
coin: CoinType = "ETH"
coin: CoinType = "ETH",
paymentIdChain?: string,
): Promise<string | null> {
return this.initDone.then(() => {
if (this.sid.isSupportedName(name)) return this.sid.resolveAddress(name);
if (this.rns.isSupportedName(name))
return this.rns.resolveAddress(name, coin);
if (this.ud.isSupportedName(name))
return this.ud.resolveAddress(name, coin);
if (this.sid.isSupportedName(name))
return this.sid.resolveAddress(name, paymentIdChain);
if (this.ens.isSupportedName(name))
return this.ens.resolveAddress(name, coin);

Expand Down
140 changes: 93 additions & 47 deletions packages/name-resolution/src/sid/index.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,118 @@
import { ethers } from "ethers";
import SID, { getSidAddress } from "@siddomains/sidjs";
import { createWeb3Name } from "@web3-name-sdk/core";
import { BaseResolver } from "../types";
import {
PAYMENT_ID_CHAINS_MAP,
PaymentIdChain,
SIDOptions,
TIMEOUT_PRESETS,
} from "./types";
import { createSolName } from "@web3-name-sdk/core/solName";
import { createPaymentIdName } from "@web3-name-sdk/core/paymentIdName";
import { isValidPaymentId } from "./utils";
import { getTLD } from "../utils";
import { SIDOptions } from "./types";
// demo: https://sdk-demo-git-main-space-id.vercel.app/

const evm_tlds = [
"bnb",
"arb",
"wod",
"mph",
"g",
"btc",
"burger",
"alien",
"zkf",
"merlin",
"taiko",
"tomo",
"gno",
"floki",
"ll",
"ip",
"mode",
"mint",
"manta",
"cake",
"zeta",
"ail",
"duck",
];

class SIDResolver implements BaseResolver {
name: string;

supportedTLDs = ["bnb", "arb"];

timeout: number;
// The supported tlds for sid in evm and solana
supportedTLDs = ["sol", ...evm_tlds];
rpc: SIDOptions;
solanaNameResolver: ReturnType<typeof createSolName>;
paymentIdNameResolver: ReturnType<typeof createPaymentIdName>;
web3NameResolver: ReturnType<typeof createWeb3Name>;

constructor(options: SIDOptions) {
this.rpc = options;
this.timeout = options.timeout || TIMEOUT_PRESETS.normal;
this.name = "spaceid";
this.solanaNameResolver = createSolName({ timeout: this.timeout });
this.paymentIdNameResolver = createPaymentIdName();
this.web3NameResolver = createWeb3Name();
}

// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-empty-function
public async init(): Promise<void> {}

public async resolveReverseName(address: string): Promise<string | null> {
const bnbProvider = new ethers.providers.JsonRpcProvider({
url: this.rpc.node.bnb,
headers: {
"user-agent": "enkrypt/name-resolver",
},
});
const sidBNB = new SID({
provider: bnbProvider,
sidAddress: getSidAddress("56"),
});
const nameBnb = await sidBNB.getName(address);
if (nameBnb) return nameBnb.name;
const arbProvider = new ethers.providers.JsonRpcProvider({
url: this.rpc.node.arb,
headers: {
"user-agent": "enkrypt/name-resolver",
},
});
const sidArb = new SID({
provider: arbProvider,
sidAddress: getSidAddress("42161"),
// The PaymentId only supports getAddress resolution.
public async handlePaymentIdGetAddress(
name: string,
paymentIdChain?: string,
): Promise<string | null> {
return await this.paymentIdNameResolver.getAddress({
name,
chainId:
PAYMENT_ID_CHAINS_MAP[paymentIdChain?.toLowerCase()] ||
PaymentIdChain.Ethereum,
});
const nameArb = await sidArb.getName(address);
if (nameArb) return nameArb.name;
return null;
}

public async resolveAddress(name: string): Promise<string | null> {
const provider = new ethers.providers.JsonRpcProvider({
url: this.rpc.node[getTLD(name)],
headers: {
"user-agent": "enkrypt/name-resolver",
},
});
const sid = new SID({
provider,
sidAddress: getSidAddress(getTLD(name) === "bnb" ? "56" : "42161"),
});
const address = await sid.name(name).getAddress();
if (parseInt(address, 16) === 0) {
public async resolveReverseName(address: string): Promise<string | null> {
try {
let name = await this.web3NameResolver.getDomainName({
timeout: this.timeout,
address,
});
if (!name) {
name = await this.solanaNameResolver.getDomainName({
address,
});
}
return name;
} catch (error) {
return null;
}
return address;
}

public async resolveAddress(
name: string,
paymentIdChain?: string,
): Promise<string | null> {
if (isValidPaymentId(name)) {
return this.handlePaymentIdGetAddress(name, paymentIdChain);
}
const tld = getTLD(name);
switch (tld) {
case "sol":
const solAddress = await this.solanaNameResolver.getAddress({
name,
});
return solAddress;
default:
const address = await this.web3NameResolver.getAddress(name, {
timeout: this.timeout,
});
return parseInt(address, 16) === 0 ? null : address;
}
}
public isSupportedName(name: string): boolean {
return this.supportedTLDs.includes(getTLD(name));
// Compatible with TLD and paymentId
return this.supportedTLDs.includes(getTLD(name)) || isValidPaymentId(name);
}
}

Expand Down
32 changes: 32 additions & 0 deletions packages/name-resolution/src/sid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,36 @@ export interface SIDOptions {
bnb: string;
arb: string;
};
timeout?: number
}

export type Protocol = "EVM" | "Solana" | "PaymentID";
export enum PaymentIdChain {
Bitcoin = 0,
Ethereum = 1,
Solana = 2,
Tron = 3,
Aptos = 4,
Sui = 5,
}
export const PAYMENT_ID_CHAINS_MAP = {
"bitcoin": 0,
"ethereum": 1,
"solana": 2,
"tron": 3,
"aptos": 4,
"sui": 5,
};
export type Method =
| "getAddress"
| "getDomainName"
| "batchGetDomainNameByChainId"
| "getMetadata"
| "getContentHash";


export const TIMEOUT_PRESETS = {
veryShort: 100, // Intentionally short to test timeout
normal: 5000, // Normal timeout (5s)
long: 15000, // Long timeout (15s)
};
3 changes: 3 additions & 0 deletions packages/name-resolution/src/sid/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const paymentIdRegex = /^[a-zA-Z0-9]+@[a-zA-Z0-9]+$/
export const isValidPaymentId = (id: string) => paymentIdRegex.test(id)

5 changes: 3 additions & 2 deletions packages/name-resolution/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ENSOptions } from "./ens/types";
import { SIDOptions } from "./sid/types";
import { SIDOptions } from "./sid/types";

export type CoinType =
| "BTC"
Expand Down Expand Up @@ -146,7 +146,8 @@ export abstract class BaseResolver {

public abstract resolveAddress(
name: string,
coint: CoinType
coint: CoinType,
paymentIdChain?: string
): Promise<string | null>;

public abstract resolveReverseName(address: string): Promise<string | null>;
Expand Down
Loading
Loading