github.com/status-im/status-go@v1.1.0/protocol/identity/alias/generate.go (about)

     1  package alias
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"encoding/hex"
     6  	"fmt"
     7  
     8  	"github.com/status-im/status-go/eth-node/crypto"
     9  )
    10  
    11  const poly uint64 = 0xB8
    12  
    13  func generate(seed uint64) string {
    14  	generator := newLSFR(poly, seed)
    15  	adjective1Index := generator.next() % uint64(len(adjectives))
    16  	adjective2Index := generator.next() % uint64(len(adjectives))
    17  	animalIndex := generator.next() % uint64(len(animals))
    18  	adjective1 := adjectives[adjective1Index]
    19  	adjective2 := adjectives[adjective2Index]
    20  	animal := animals[animalIndex]
    21  
    22  	return fmt.Sprintf("%s %s %s", adjective1, adjective2, animal)
    23  }
    24  
    25  // GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
    26  func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
    27  	// Here we truncate the public key to the least significant 64 bits
    28  	return generate(uint64(publicKey.X.Int64()))
    29  }
    30  
    31  // GenerateFromPublicKeyString returns the 3 words name given a public key
    32  // prefixed with 0x
    33  func GenerateFromPublicKeyString(publicKeyString string) (string, error) {
    34  	publicKeyBytes, err := hex.DecodeString(publicKeyString[2:])
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  
    39  	publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
    40  	if err != nil {
    41  		return "", err
    42  	}
    43  
    44  	return GenerateFromPublicKey(publicKey), nil
    45  }