Skip to content

Commit

Permalink
[DT-902] feature: adding ENS support for Resolution.email(), Resoluti…
Browse files Browse the repository at this point in the history
…on.httpUrl(), Resolution.ipfsHash()
  • Loading branch information
DChan0319 committed Sep 26, 2023
1 parent 38fc045 commit faa6b80
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
20 changes: 18 additions & 2 deletions src/Ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ export default class Ens extends NamingService {
this.proxyServiceApiKey,
);

const nodeHash = await this.callMethod(reverseRegistrarContract, 'node', [
const nodeHash = await this.reverseRegistrarCallToNode(
reverseRegistrarContract,
address,
]);
);
const resolverAddress = await this.callMethod(
this.registryContract,
'resolver',
Expand Down Expand Up @@ -314,6 +315,16 @@ export default class Ens extends NamingService {
return this.callMethod(resolverContract, 'name', [nodeHash]);
}

/**
* This was done to make automated tests more configurable
*/
private async reverseRegistrarCallToNode(
reverseRegistrarContract: EthereumContract,
address: string,
): Promise<string> {
return await this.callMethod(reverseRegistrarContract, 'node', [address]);
}

async twitter(domain: string): Promise<string> {
throw new ResolutionError(ResolutionErrorCode.UnsupportedMethod, {
domain,
Expand Down Expand Up @@ -420,16 +431,21 @@ export default class Ens extends NamingService {
return formatsByCoinType[coinType].encoder(data);
}

// @see https://docs.ens.domains/ens-improvement-proposals/ensip-5-text-records
private async getTextRecord(
domain: string,
key: string,
): Promise<string | undefined> {
if (key === 'contenthash') {
return await this.getContentHash(domain);
}
const nodeHash = this.namehash(domain);
const resolver = await this.getResolverContract(domain);
const textRecord = await this.callMethod(resolver, 'text', [nodeHash, key]);
return textRecord;
}

// @see https://docs.ens.domains/ens-improvement-proposals/ensip-7-contenthash-field
private async getContentHash(domain: string): Promise<string | undefined> {
const contentHash = requireOrFail('content-hash', 'content-hash', '^2.5.2');
const nodeHash = this.namehash(domain);
Expand Down
42 changes: 31 additions & 11 deletions src/Resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,18 @@ export default class Resolution {
*/
async ipfsHash(domain: string): Promise<string> {
domain = prepareAndValidateDomain(domain);
return this.getPreferableNewRecord(
domain,
'dweb.ipfs.hash',
'ipfs.html.value',
);
return await this.callServiceForDomain(domain, async (service) => {
if (service instanceof Ens) {
// @see https://docs.ens.domains/ens-improvement-proposals/ensip-7-contenthash-field
return await service.record(domain, 'contenthash');
}

return await this.getPreferableNewRecord(
domain,
'dweb.ipfs.hash',
'ipfs.html.value',
);
});
}

/**
Expand All @@ -595,11 +602,17 @@ export default class Resolution {
*/
async httpUrl(domain: string): Promise<string> {
domain = prepareAndValidateDomain(domain);
return this.getPreferableNewRecord(
domain,
'browser.redirect_url',
'ipfs.redirect_domain.value',
);
return await this.callServiceForDomain(domain, async (service) => {
if (service instanceof Ens) {
return await service.record(domain, 'url');
}

return await this.getPreferableNewRecord(
domain,
'browser.redirect_url',
'ipfs.redirect_domain.value',
);
});
}

/**
Expand All @@ -609,7 +622,14 @@ export default class Resolution {
* @returns A Promise that resolves in an email address configured for this domain whois
*/
async email(domain: string): Promise<string> {
return this.record(domain, 'whois.email.value');
domain = prepareAndValidateDomain(domain);
let key = 'whois.email.value';
const serviceName = findNamingServiceName(domain);
if (serviceName === 'ENS') {
key = 'email';
}

return await this.record(domain, key);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/tests/Ens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,28 @@ describe('ENS', () => {

skipItInLive('reverses address to ENS domain', async () => {
const eyes = mockAsyncMethods(ens, {
reverseRegistrarCallToNode:
'0x4da70a332a7a98a58486f551a455b1398ce309d9bd3a4f0800da4eec299829a4',
callMethod: '0xDa1756Bb923Af5d1a05E277CB1E54f1D0A127890',
resolverCallToName: 'adrian.argent.xyz',
resolver: '0xDa1756Bb923Af5d1a05E277CB1E54f1D0A127890',
});
const result = await ens?.reverse(
const result = await ens?.reverseOf(
'0xb0E7a465D255aE83eb7F8a50504F3867B945164C',
'ETH',
);
expectSpyToBeCalled(eyes);
expect(result).toEqual('adrian.argent.xyz');
});

it('reverses address to ENS domain null', async () => {
const spy = mockAsyncMethod(ens, 'resolver', NullAddress);
const result = await ens?.reverse(
const spy = mockAsyncMethods(ens, {
reverseRegistrarCallToNode:
'0x4da70a332a7a98a58486f551a455b1398ce309d9bd3a4f0800da4eec299829a4',
callMethod: NullAddress,
});
const result = await ens?.reverseOf(
'0x112234455c3a32fd11230c42e7bccd4a84e02010',
'ETH',
);
expectSpyToBeCalled([spy]);
expectSpyToBeCalled(spy);
expect(result).toEqual(null);
});

Expand Down

0 comments on commit faa6b80

Please sign in to comment.