-
Notifications
You must be signed in to change notification settings - Fork 258
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
``` | ||
|
||
```rust {{ title: "namehash-rust", link: "https://github.com/InstateDev/namehash-rust" }} | ||
fn main() { | ||
let node = &namehash("name.eth"); | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So This fuctions returns (Hash.sha3(result)) which is keccak256 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I'm running a test script locally and 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;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" }} | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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