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

Conversation

NickSneo
Copy link

This PR adds code samples for Web3j library - https://github.com/hyperledger-web3j/web3j

Signed-off-by: Nischal Sharma <[email protected]>
Copy link
Member

@gskril gskril left a comment

Choose a reason for hiding this comment

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

thanks for contributing and sorry for the review delay! just have one comment about labelhash

@@ -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 - hyperledger-web3j/web3j#2140

We will soon release new Web3j with this fix. Thanks

Copy link
Member

@gskril gskril left a comment

Choose a reason for hiding this comment

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

Sorry I missed this the first time — would be great if you can include the relevant imports for each example so they're truly copy/paste snippets!

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants