Last active
November 9, 2018 06:53
-
-
Save hewigovens/9d078eb6b4e028ec78bce6abab71d980 to your computer and use it in GitHub Desktop.
ens namehash in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CryptoSwift | |
//https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md | |
extension String { | |
public var namehash: String { | |
var node = Array<UInt8>.init(repeating: 0x0, count: 32) | |
if self.count > 0 { | |
node = self.split(separator: ".") | |
.map { Array($0.utf8).sha3(.keccak256) } | |
.reversed() | |
.reduce(node) { return ($0 + $1).sha3(.keccak256) } | |
} | |
return "0x" + node.toHexString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assert("".namehash == "0x0000000000000000000000000000000000000000000000000000000000000000")
assert("eth".namehash == "0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae")
assert("foo.eth".namehash == "0xde9b09fd7c5f901e23a3f19fecc54828e9c848539801e86591bd9801b019f84f")