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

✨ Added resolveName method to provider #139

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"eslint": "^8.16.0",
"eslint-plugin-jest": "^26.2.2",
"eslint-plugin-jsdoc": "38.0.2",
"eth-ens-namehash": "^2.0.8",
"ethers": "^5.6.8",
"express": "^4.17.1",
"husky": "^7.0.4",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { splitSignature } from './utils/split-signature';
import { toChecksumAddress } from './utils/to-checksum-address';
import { toUtf8Bytes } from './utils/to-utf8-bytes';
import { weiToEther } from './utils/wei-to-ether';
import { namehash } from './utils/namehash';

export * from './providers/types';
export * from './utils/bytes';
Expand All @@ -55,6 +56,7 @@ export {
toUtf8Bytes,
computeAddress,
computePublicKey,
namehash,
/* classes */
Contract,
TinyBig,
Expand Down
18 changes: 18 additions & 0 deletions src/providers/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,22 @@ export abstract class BaseProvider {
const logs = rpcLogs.map((log) => cleanLog(log, false));
return logs;
}

/**
* Resolves an ENS name to a full Ethereum address.
*
* * [Identical](/docs/api#isd) to [`ethers.provider.resolveName`](https://docs.ethers.io/v5/api/providers/provider/#Provider-ResolveName) in ethers.js
* * [Identical](/docs/api#isd) to [`web3.eth.ens.getAddress`](https://web3js.readthedocs.io/en/v1.7.3/web3-eth-ens.html#getaddress) in web3.js
*
* @param name the ENS name to resolve to an Ethereum address
* @returns the full Ethereum address matching the ENS name specified
* @example
* ```javascript
* await provider.resolveName('daws.eth');
* // '0xc0DEAF6bD3F0c6574a6a625EF2F22f62A5150EAB'
* ```
*/
public async resolveName(name: string) // : Promise<string>
{
}
}
33 changes: 33 additions & 0 deletions src/providers/test/json-rpc-provider/resolve-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ethers } from 'ethers';
import Web3 from 'web3';
import { JsonRpcProvider } from '../../..';
import { rpcUrls } from './../rpc-urls';

const rpcUrl = rpcUrls.mainnet;

const names = ['daws.eth', 'ricmoo.eth', 'vitalik.eth'];

describe('provider.resolveName', () => {
const essentialEthProvider = new JsonRpcProvider(rpcUrl);
const web3Provider = new Web3(rpcUrl);
const ethersProvider = new ethers.providers.StaticJsonRpcProvider(rpcUrl);

it('should match ethers.js', async () => {
await names.forEach(async (name) => {
const [eeName, ethersName] = await Promise.all([
essentialEthProvider.resolveName(name),
ethersProvider.resolveName(name),
]);
expect(eeName).toBe(ethersName);
});
});
it('should match web3.js', async () => {
await names.forEach(async (name) => {
const [eeName, web3Name] = await Promise.all([
essentialEthProvider.resolveName(name),
web3Provider.eth.ens.getAddress(name),
]);
expect(eeName).toBe(web3Name);
});
});
});
22 changes: 22 additions & 0 deletions src/utils/namehash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { keccak256 } from './keccak256';

/**
* Hashs a name according to the [EIP 137](https://eips.ethereum.org/EIPS/eip-137) specification.
* Function based off of [danfinlay/eth-ens-namehash](https://github.com/danfinlay/eth-ens-namehash).
*
* @param name the name to hash
* @returns the hashed name
*/
export function namehash(name: string) {
if (!name)
return '0x0000000000000000000000000000000000000000000000000000000000000000';

name = name.normalize();
const labels = name.split('.');
let node;
for (var i = labels.length - 1; i >= 0; i--) {
const labelSha = keccak256(labels[i]);
node = keccak256(Buffer.from(node + labelSha, 'hex'));
}
return `0x${node}`;
}
19 changes: 19 additions & 0 deletions src/utils/tests/namehash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @ts-ignore
import { hash as ethHash } from 'eth-ens-namehash';
import { namehash as eeHash } from '../..';

const names = [
'faceboоk.eth',
'facebook.eth',
'\u017F\u0323\u0307.eth',
'\u0073\u0323\u0307.eth',
];

describe('utils.namehash', () => {
it('should match eth-ens-namehash', () => {
names.forEach((name) => {
const [eeNamehash, ethNamehash] = [eeHash(name), ethHash(name)];
expect(eeNamehash).toBe(ethNamehash);
});
});
});