github.com/status-im/status-go@v1.1.0/services/ens/strings.go (about)

     1  package ens
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/ethereum/go-ethereum/common"
     9  	"github.com/ethereum/go-ethereum/crypto"
    10  )
    11  
    12  func NameHash(name string) common.Hash {
    13  	node := common.Hash{}
    14  
    15  	if len(name) > 0 {
    16  		labels := strings.Split(name, ".")
    17  
    18  		for i := len(labels) - 1; i >= 0; i-- {
    19  			labelSha := crypto.Keccak256Hash([]byte(labels[i]))
    20  			node = crypto.Keccak256Hash(node.Bytes(), labelSha.Bytes())
    21  		}
    22  	}
    23  
    24  	return node
    25  }
    26  
    27  func ValidateENSUsername(username string) error {
    28  	if !strings.HasSuffix(username, ".eth") {
    29  		return fmt.Errorf("username must end with .eth")
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  func UsernameToLabel(username string) [32]byte {
    36  	usernameHashed := crypto.Keccak256([]byte(username))
    37  	var label [32]byte
    38  	copy(label[:], usernameHashed)
    39  
    40  	return label
    41  }
    42  
    43  func ExtractCoordinates(pubkey string) ([32]byte, [32]byte) {
    44  	x, _ := hex.DecodeString(pubkey[4:68])
    45  	y, _ := hex.DecodeString(pubkey[68:132])
    46  
    47  	var xByte [32]byte
    48  	copy(xByte[:], x)
    49  
    50  	var yByte [32]byte
    51  	copy(yByte[:], y)
    52  
    53  	return xByte, yByte
    54  }