Skip to content

Commit

Permalink
feat: load tld naming service from /supported_tlds API
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyluo committed Sep 30, 2024
1 parent 75b9498 commit 1affbec
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 241 deletions.
8 changes: 4 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
L1_TEST_NET_RPC_URL=https://goerli.infura.io/v3/<your_key>
L1_TEST_NET_RPC_WSS_URL=wss://goerli.infura.io/ws/v3/<your_key>
L2_TEST_NET_RPC_URL=https://polygon-mumbai.infura.io/v3/<your_key>
L2_TEST_NET_RPC_WSS_URL=wss://polygon-mumbai.g.alchemy.com/v2/<your_key>
L1_TEST_NET_RPC_URL=https://sepolia.infura.io/v3/<your_key>
L1_TEST_NET_RPC_WSS_URL=wss://sepolia.infura.io/ws/v3/<your_key>
L2_TEST_NET_RPC_URL=https://polygon-amoy.infura.io/v3/<your_key>
L2_TEST_NET_RPC_WSS_URL=wss://polygon-amoy.g.alchemy.com/v2/<your_key>
18 changes: 0 additions & 18 deletions src/Ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
Locations,
NamingServiceName,
Provider,
TokenUriMetadata,
BlockchainType,
DnsRecordType,
DnsRecord,
Expand Down Expand Up @@ -289,7 +288,6 @@ export default class Ens extends NamingService {
let domainName = '';
const nameWrapperMetadataResponse = await Networking.fetch(
`https://metadata.ens.domains/${this.networkName}/${this.nameWrapperContract.address}/${hash}`,
{},
);
if (nameWrapperMetadataResponse.status === 200) {
const jsonResponse = await nameWrapperMetadataResponse.json();
Expand All @@ -299,7 +297,6 @@ export default class Ens extends NamingService {

const baseRegistrarMetadataResponse = await Networking.fetch(
`https://metadata.ens.domains/${this.networkName}/${this.baseRegistrarContract.address}/${hash}`,
{},
);

if (baseRegistrarMetadataResponse.status === 200) {
Expand Down Expand Up @@ -614,19 +611,4 @@ export default class Ens extends NamingService {
this.namehash(domain),
]);
}

private async getMetadataFromTokenURI(
tokenUri: string,
): Promise<TokenUriMetadata> {
const resp = await Networking.fetch(tokenUri, {});
if (resp.ok) {
return resp.json();
}

throw new ResolutionError(ResolutionErrorCode.ServiceProviderError, {
providerMessage: await resp.text(),
method: 'UDAPI',
methodName: 'tokenURIMetadata',
});
}
}
8 changes: 4 additions & 4 deletions src/Resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ export default class Resolution {
async email(domain: string): Promise<string> {
domain = prepareAndValidateDomain(domain);
let key = 'whois.email.value';
const serviceName = findNamingServiceName(domain);
const serviceName = await findNamingServiceName(domain);
if (serviceName === 'ENS') {
key = 'email';
}
Expand Down Expand Up @@ -1062,7 +1062,7 @@ export default class Resolution {
private async getMetadataFromTokenURI(
tokenUri: string,
): Promise<TokenUriMetadata> {
const resp = await Networking.fetch(tokenUri, {});
const resp = await Networking.fetch(tokenUri);
if (resp.ok) {
return resp.json();
}
Expand Down Expand Up @@ -1102,7 +1102,7 @@ export default class Resolution {
domain: string,
func: (service: NamingService) => T,
): Promise<UnwrapPromise<T>> {
const serviceName = findNamingServiceName(domain);
const serviceName = await findNamingServiceName(domain);
if (!serviceName) {
throw new ResolutionError(ResolutionErrorCode.UnsupportedDomain, {
domain,
Expand Down Expand Up @@ -1141,7 +1141,7 @@ export default class Resolution {
func: (service: NamingService) => Promise<boolean>,
options: {throwIfUnsupportedDomain: boolean; expectedValue: boolean},
): Promise<boolean> {
const serviceName = findNamingServiceName(domain);
const serviceName = await findNamingServiceName(domain);
if (!serviceName) {
if (!options.throwIfUnsupportedDomain) {
return !options.expectedValue;
Expand Down
8 changes: 4 additions & 4 deletions src/UdApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default class UdApi extends NamingService {

constructor(api?: Api) {
super();
this.url = api?.url || 'https://unstoppabledomains.com/api/v1';
this.url = api?.url || 'https://unstoppabledomains.com/api/v1'; // TODO: this is about to be deprecated
const DefaultUserAgent =
'cross-fetch/3.1.4 (+https://github.com/lquixada/cross-fetch)';
'cross-fetch/4.0.0 (+https://github.com/lquixada/cross-fetch)';
const CustomUserAgent = `${DefaultUserAgent} Resolution`;
this.headers = {'X-user-agent': CustomUserAgent};
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export default class UdApi extends NamingService {
}

async twitter(domain: string): Promise<string> {
const serviceName = findNamingServiceName(domain);
const serviceName = await findNamingServiceName(domain);
if (serviceName !== NamingServiceName.UNS) {
throw new ResolutionError(ResolutionErrorCode.UnsupportedMethod, {
domain,
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class UdApi extends NamingService {

private async getMetadata(tokenId: string): Promise<ResolutionResponse> {
const tokenUri = `${this.url}/${tokenId}`;
const resp = await Networking.fetch(tokenUri, {}).catch((err) => {
const resp = await Networking.fetch(tokenUri).catch((err) => {
throw new ResolutionError(ResolutionErrorCode.MetadataEndpointError, {
tokenUri: tokenUri || 'undefined',
errorMessage: err.message,
Expand Down
4 changes: 2 additions & 2 deletions src/Uns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class Uns extends NamingService {
this.unsl2 = new UnsInternal(
UnsLocation.Layer2,
source.locations.Layer2,
BlockchainType.MATIC,
BlockchainType.POL,
);
}

Expand Down Expand Up @@ -447,7 +447,7 @@ export default class Uns extends NamingService {

private async getMetadata(tokenId: string): Promise<DomainMetadata> {
const tokenUri = await this.getTokenUri(tokenId);
const resp = await Networking.fetch(tokenUri, {}).catch((err) => {
const resp = await Networking.fetch(tokenUri).catch((err) => {
throw new ResolutionError(ResolutionErrorCode.MetadataEndpointError, {
tokenUri: tokenUri || 'undefined',
errorMessage: err.message,
Expand Down
4 changes: 3 additions & 1 deletion src/tests/Ens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from './helpers';
import Ens from '../Ens';

const ETH_L1_TESTNET_NAME = 'sepolia';

let resolution: Resolution;
let ens: Ens;

Expand All @@ -24,7 +26,7 @@ beforeEach(() => {
ProviderProtocol.http,
NamingServiceName.ENS,
),
network: 'goerli',
network: ETH_L1_TESTNET_NAME,
},
},
});
Expand Down
Loading

0 comments on commit 1affbec

Please sign in to comment.