status-go/services/ens/strings.go

55 lines
1.0 KiB
Go
Raw Normal View History

2021-12-21 15:05:09 +00:00
package ens
import (
2022-02-10 14:19:53 +00:00
"encoding/hex"
2021-12-21 15:05:09 +00:00
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func NameHash(name string) common.Hash {
2021-12-21 15:05:09 +00:00
node := common.Hash{}
if len(name) > 0 {
labels := strings.Split(name, ".")
for i := len(labels) - 1; i >= 0; i-- {
labelSha := crypto.Keccak256Hash([]byte(labels[i]))
node = crypto.Keccak256Hash(node.Bytes(), labelSha.Bytes())
}
}
return node
}
func ValidateENSUsername(username string) error {
2021-12-21 15:05:09 +00:00
if !strings.HasSuffix(username, ".eth") {
return fmt.Errorf("username must end with .eth")
}
return nil
}
2022-01-14 11:17:31 +00:00
func UsernameToLabel(username string) [32]byte {
2022-01-14 11:17:31 +00:00
usernameHashed := crypto.Keccak256([]byte(username))
var label [32]byte
copy(label[:], usernameHashed)
return label
}
func ExtractCoordinates(pubkey string) ([32]byte, [32]byte) {
2022-02-10 14:19:53 +00:00
x, _ := hex.DecodeString(pubkey[4:68])
y, _ := hex.DecodeString(pubkey[68:132])
2022-01-14 11:17:31 +00:00
var xByte [32]byte
copy(xByte[:], x)
var yByte [32]byte
copy(yByte[:], y)
return xByte, yByte
}