Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit b188714

Browse files
authored
ENS Package tutorial (#6687)
* ens package tutorial * Update docs/docs/guides/ens/index.md
1 parent 2a40b66 commit b188714

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

docs/docs/guides/ens/index.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
sidebar_position: 9
3+
sidebar_label: 'Web3 ENS'
4+
---
5+
6+
# Using web3.js ENS Package
7+
8+
In this tutorial, we'll explore how to use the web3.js ENS (Ethereum Name Service) package. The Ethereum Name Service (ENS) is a decentralized domain system built on the Ethereum blockchain. It serves as a distributed, secure, and human-readable naming system designed to map Ethereum addresses, smart contracts, and various other services to easily understandable names.
9+
10+
## Installing web3.js
11+
12+
First, install web3.js version 4 in your project using npm:
13+
14+
```bash
15+
npm install web3
16+
```
17+
18+
## Setting up web3 and ENS
19+
20+
Now, let's set up web3 and ENS in a TypeScript file:
21+
22+
```typescript
23+
import Web3 from 'web3';
24+
25+
// Assuming you have a provider, replace 'http://localhost:8545' with your Web3 provider
26+
const web3 = new Web3('http://localhost:8545');
27+
28+
// You can use ENS with web3 object:
29+
const ens = await web3.eth.ens.getAddress('alice.eth');
30+
31+
```
32+
33+
## Installing web3.js ENS
34+
35+
For directly using ENS package first install ENS package and import `ENS`.
36+
37+
```bash
38+
npm install web3-eth-ens
39+
```
40+
41+
```typescript
42+
import { ENS } from 'web3-eth-ens';
43+
44+
const ens = new ENS(undefined,'https://127.0.0.1:4545');
45+
46+
console.log(await ens.getAddress('vitalik.eth'));
47+
48+
```
49+
50+
## ENS Examples
51+
52+
### getAddress
53+
54+
The getAddress function retrieves the Ethereum address associated with the given ENS name. It resolves the address by querying the ENS resolver for the provided ENS name and returns the resolved Ethereum address.
55+
56+
```typescript
57+
const address = await web3.eth.ens.getAddress('ethereum.eth');
58+
console.log(address);
59+
```
60+
61+
### getContenthash
62+
63+
The getContenthash function retrieves the content hash associated with the provided ENS name. It communicates with the ENS resolver to obtain the content hash value and returns the resolved content hash.
64+
65+
```typescript
66+
const hash = await web3.eth.ens.getContenthash('ethereum.eth');
67+
console.log(hash);
68+
```
69+
70+
### getOwner
71+
72+
The getOwner function obtains the owner of the specified ENS name. It queries the ENS registry to fetch the owner of the ENS name and returns the owner's Ethereum address.
73+
74+
```typescript
75+
const owner = await web3.eth.ens.getOwner('ethereum.eth');
76+
console.log(owner);
77+
```
78+
79+
### getPubKey
80+
81+
The getPubKey function fetches the public key x and y associated with the provided ENS name using the ENS resolver.
82+
83+
```typescript
84+
const key = await web3.eth.ens.getPubkey('xyz.eth');
85+
console.log(key);
86+
```
87+
88+
### getResolver
89+
90+
The getResolver function retrieves the resolver for the given ENS name.
91+
92+
```typescript
93+
const resolver = await web3.eth.ens.getResolver('xyz.eth');
94+
console.log(resolver.options.address);
95+
```
96+
97+
### getTTL
98+
99+
The getTTL function retrieves the Time-to-Live (TTL) value associated with the specified ENS name.
100+
101+
```typescript
102+
const ttl = await web3.eth.ens.getTTL('xyz.eth');
103+
console.log(ttl);
104+
```
105+
106+
### recordExists
107+
108+
The recordExists function checks whether a record exists for the given ENS name.
109+
110+
```typescript
111+
const result = await web3.eth.ens.recordExists('ethereum.eth');
112+
console.log(result);
113+
```
114+
115+
## Conclusion
116+
117+
In this tutorial, we've covered how to use the web3.js ENS package to interact with Ethereum Name Service. You should now be able to perform various ENS-related operations using web3.js version 4. For more details visit web3.js ENS [documentation](/libdocs/ENS) section.

0 commit comments

Comments
 (0)