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 web3j code samples #364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions docs/resolution/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ from ens.auto import ns
resolver = ns.resolver('alice.eth')
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
Web3j web3j = Web3j.build(new HttpService(<rpc_endpoint_url>));
EnsResolver ensResolver = new EnsResolver(web3j);

String resolver = ensResolver.getResolverAddress('luc.eth');
```

</CodeGroup>

To verify which specifications are implemented by a resolver you can call the `supportsInterface(bytes4 interfaceID)` on the resolver with the interfaceID you would like to test for.
Expand Down
16 changes: 16 additions & 0 deletions docs/resolution/names.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ from namehash import namehash
node = namehash('name.eth')
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
byte[] nameHash = NameHash.nameHashAsBytes('luc.eth');
String nameHashString = Numeric.toHexString(nameHash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like web3j also exposes NameHash.nameHash which returns a hex string directly, so no need to first get the bytes and convert it yourself

```

```rust {{ title: "namehash-rust", link: "https://github.com/InstateDev/namehash-rust" }}
fn main() {
let node = &namehash("name.eth");
Expand Down Expand Up @@ -177,6 +182,12 @@ const labelhash = keccak256(toUtf8Bytes(normalizedLabel))
string constant label = "label";
bytes32 constant labelhash = keccak256(bytes(label));
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
byte[] labelHash = NameHash.nameHashAsBytes('label');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Labelhash is the keccak256 hash of the label (e.g. "nick" in the name "nick.eth"), which is different than applying NameHash.nameHashAsBytes() on the label.

Web3j likely doesn't have a helper function specific to labelhash, but it may have a keccak256 function which can be used in this example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So NameHash.nameHash Function can be used for both full Name like nick.eth and just label like 'nickIt will give the correct result, thats why there is no ultitly function just for labels as user can use currentNameHash.nameHash` fucntion on label to get the labelHash.

This fuctions returns (Hash.sha3(result)) which is keccak256
Hope it makes sense, let me know if there is still any confusion

Copy link
Member

@gskril gskril Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I'm running a test script locally and NameHash.nameHashAsBytes('label') does not return the expected label hash. The expected value for label would be 0x1b036544434cea9770a413fd03e0fb240e1ccbd10a452f7dba85c8eca9ca3eda

I don't know Java well, but futzing around it looks like the code example will have to be a bit lower level like this:

public static String labelHash(String name) {
  // Get the label of the name (the part before the earliest '.')
  String[] parts = name.split("\\.");
  String label = parts[0];

  // Keccak256 hash the label and convert to hex string
  byte[] labelHash = Hash.sha3(label.getBytes(StandardCharsets.UTF_8));
  String labelHashString = Numeric.toHexString(labelHash);
  return labelHashString;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gskril Thanks for pointing out this. I was able to reproduce it, and created a PR for labelHash fucntion - LFDT-web3j/web3j#2140

We will soon release new Web3j with this fix. Thanks

String labelHashString = Numeric.toHexString(labelHash)
```

</CodeGroup>

## DNS Encoding {{ title: "DNS Encoding", id: "dns" }}
Expand Down Expand Up @@ -215,6 +226,11 @@ contract MyContract {
}
}
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
String dnsEncodedName = NameHash.dnsEncode('name.eth')
```

</CodeGroup>

### Algorithm
Expand Down
8 changes: 8 additions & 0 deletions docs/web/avatars.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ from ens.auto import ns
avatar = ns.get_text('alice.eth', 'avatar')
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
Web3j web3j = Web3j.build(new HttpService(<rpc_endpoint_url>));
EnsResolver ensResolver = new EnsResolver(web3j);

// Get ENS text
String avatar = ensResolver.getEnsText('nick.eth', 'avatar');
```

```go {{ title: "Go", variant: "go" }}
package main

Expand Down
12 changes: 12 additions & 0 deletions docs/web/records.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ url = ns.get_text('alice.eth', 'url')
assert url == 'https://example.com'
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
Web3j web3j = Web3j.build(new HttpService(<rpc_endpoint_url>));
EnsResolver ensResolver = new EnsResolver(web3j);

// Get ENS text
String url = ensResolver.getEnsText('nick.eth', 'url');

// Set ENS text
Credentials credentials = Credentials.create(<private_key>)
TransactionReceipt receiptResult = ensResolver.setEnsText('nick.eth', 'url', 'http://example.com', credentials);
```

```tsx {{ title: "Ethers.rs", variant: "rust" }}
// TODO: Not Implemented
```
Expand Down
8 changes: 8 additions & 0 deletions docs/web/resolution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ from ens.auto import ns
address = ns.address('alice.eth')
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
Web3j web3j = Web3j.build(new HttpService(<rpc_endpoint_url>));
EnsResolver ensResolver = new EnsResolver(web3j);

String ensName = ENSNormalize.ENSIP15.normalize("nick.eth");
System.out.println("ENS address = " + ensResolver.resolve(ensName));
```

```rust {{ variant: 'ethers-rs' }}
let provider = Provider::<Http>::try_from("https://mainnet.infura.io/v3/...")?;

Expand Down
31 changes: 28 additions & 3 deletions docs/web/reverse.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ from ens.auto import ns
name = ns.name('0x225f137127d9067788314bc7fcc1f36746a3c3B5')
```

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
Web3j web3j = Web3j.build(new HttpService(<rpc_endpoint_url>));
EnsResolver ensResolver = new EnsResolver(web3j);

# 0x225f137127d9067788314bc7fcc1f36746a3c3B5 -> luc.eth
String ensPrimaryName = ensResolver.reverseResolve('0x225f137127d9067788314bc7fcc1f36746a3c3B5');
```


```go {{ variant: 'go', title: 'Go' }}
package main
Expand Down Expand Up @@ -133,9 +141,9 @@ On these chains, you can set a primary name for the sender via `setName()` most

`setNameForAddrWithSignature()` can be used for EOAs or smart contracts with an ERC-1271 signature, while `setNameForAddrWithSignatureAndOwnable()` can be used when a smart contract has an explicit `owner()`.

{/* <CodeGroup title="Setting Primary Name">
<CodeGroup title="Setting Primary Name">

```tsx {{ meta: 'focus=4:9', variant: 'wagmi' }}
{/*```tsx {{ meta: 'focus=4:9', variant: 'wagmi' }}
```

```ts {{ variant: 'ethers-v5' }}
Expand Down Expand Up @@ -163,9 +171,26 @@ const hash = await setPrimaryName(wallet, {
from ens.auto import ns

ns.setup_name('myname.eth', my_address)
```*/}

```java {{ title: 'Web3j (Java)', variant: 'web3j', link: 'https://docs.web3j.io/4.11.0/advanced/ethereum_name_service/' }}
Web3j web3j = Web3j.build(new HttpService(<rpc_endpoint_url>));
EnsResolver ensResolver = new EnsResolver(web3j);
Credentials credentials = Credentials.create(<private_key>)

// Set Primary Name
TransactionReceipt receiptResult = ensResolver.setReverseName('nick.eth', credentials);

// Set Primary Name for Contract (or Address)
// for address = 0x225f137127d9067788314bc7fcc1f36746a3c3B5
// owner address (function caller) = credentials.getAddress()
// resolver address = 0x9010A27463717360cAD99CEA8bD39b8705CCA238
TransactionReceipt receiptResult = ensResolver.setReverseName('0x225f137127d9067788314bc7fcc1f36746a3c3B5',
credentials.getAddress(), '0x9010A27463717360cAD99CEA8bD39b8705CCA238',
'nick.eth', credentials);
```

</CodeGroup> */}
</CodeGroup>

### Do's and Dont's

Expand Down