Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3-eth-ens add gettext and getname #6914

Merged
merged 4 commits into from
Mar 25, 2024
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
6 changes: 5 additions & 1 deletion packages/web3-eth-ens/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ Documentation:

- Added function `setAddress` in ENS and Resolver classes (#5956)

## [Unreleased]
## [Unreleased]

### Add

- Added function getText and getName in ENS and resolver classes
4 changes: 3 additions & 1 deletion packages/web3-eth-ens/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const interfaceIds: { [T: string]: string } = {
};

/**
* An object holding the functionsthat are supported by the ENS resolver contracts/interfaces.
* An object holding the functions that are supported by the ENS resolver contracts/interfaces.
*/
export const methodsInInterface: { [T: string]: string } = {
setAddr: 'addr',
Expand All @@ -38,6 +38,8 @@ export const methodsInInterface: { [T: string]: string } = {
pubkey: 'pubkey',
setContenthash: 'contenthash',
contenthash: 'contenthash',
text: 'text',
name: 'name',
};

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
return this._resolver.getAddress(ENSName, coinType);
}

/**
* ERC-634 - Returns the text content stored in the resolver for the specified key.
* @param ENSName - The ENS name to resolve
* @param key - The key to resolve https://github.com/ethereum/ercs/blob/master/ERCS/erc-634.md#global-keys
* @returns - The value content stored in the resolver for the specified key
*/
public async getText(ENSName: string, key: string): Promise<string> {
return this._resolver.getText(ENSName, key);
}


/**
* Resolves the name of an ENS node.
* @param ENSName - The node to resolve
* @returns - The name
*/
public async getName(ENSName: string): Promise<string> {
return this._resolver.getName(ENSName);
}

/**
* Returns the X and Y coordinates of the curve point for the public key.
* @param ENSName - The ENS name
Expand Down
21 changes: 21 additions & 0 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,25 @@ export class Resolver {
.setAddr(namehash(ENSName), address)
.send(txConfig);
}

public async getText(
ENSName: string,
key: string,
) {
const resolverContract = await this.getResolverContractAdapter(ENSName);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.text);

return resolverContract.methods
.text(namehash(ENSName), key).call()
}

public async getName(
address: string
) {
const resolverContract = await this.getResolverContractAdapter(address);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.name);

return resolverContract.methods
.name(namehash(address)).call()
}
}
54 changes: 54 additions & 0 deletions packages/web3-eth-ens/test/unit/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,60 @@ describe('resolver', () => {
});
});

describe('text', () => {
it('getText', async () => {
const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
call: async () => Promise.resolve(true),
} as unknown as NonPayableMethodObject<any, any>);

const textMock = jest.spyOn(contract.methods, 'text').mockReturnValue({
call: jest.fn(),
} as unknown as NonPayableMethodObject<any, any>);

jest.spyOn(registry, 'getResolver').mockImplementation(async () => {
return new Promise(resolve => {
resolve(contract);
});
});

await resolver.getText(ENS_NAME, "key");
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.text],
);
expect(textMock).toHaveBeenCalledWith(namehash(ENS_NAME), "key");
})
})

describe('name', () => {
it('getName', async () => {
const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
call: async () => Promise.resolve(true),
} as unknown as NonPayableMethodObject<any, any>);

const nameMock = jest.spyOn(contract.methods, 'name').mockReturnValue({
call: jest.fn(),
} as unknown as NonPayableMethodObject<any, any>);

jest.spyOn(registry, 'getResolver').mockImplementation(async () => {
return new Promise(resolve => {
resolve(contract);
});
});

await resolver.getName(ENS_NAME);
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.name],
);

expect(nameMock).toHaveBeenCalledWith(namehash(ENS_NAME));
})
})


describe('supportsInterface', () => {
it('check supportsInterface for non strict hex id', async () => {
const interfaceId = 'setAddr';
Expand Down
Loading