2019-09-26 07:01:17 +00:00
|
|
|
package alias
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-09-26 07:01:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const poly uint64 = 0xB8
|
|
|
|
|
|
|
|
func generate(seed uint64) string {
|
|
|
|
generator := newLSFR(poly, seed)
|
|
|
|
adjective1Index := generator.next() % uint64(len(adjectives))
|
|
|
|
adjective2Index := generator.next() % uint64(len(adjectives))
|
|
|
|
animalIndex := generator.next() % uint64(len(animals))
|
|
|
|
adjective1 := adjectives[adjective1Index]
|
|
|
|
adjective2 := adjectives[adjective2Index]
|
|
|
|
animal := animals[animalIndex]
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s %s %s", adjective1, adjective2, animal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateFromPublicKey returns the 3 words name given an *ecdsa.PublicKey
|
|
|
|
func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
|
2020-02-07 11:56:30 +00:00
|
|
|
// Here we truncate the public key to the least significant 64 bits
|
2019-09-26 07:01:17 +00:00
|
|
|
return generate(uint64(publicKey.X.Int64()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateFromPublicKeyString returns the 3 words name given a public key
|
|
|
|
// prefixed with 0x
|
|
|
|
func GenerateFromPublicKeyString(publicKeyString string) (string, error) {
|
|
|
|
publicKeyBytes, err := hex.DecodeString(publicKeyString[2:])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenerateFromPublicKey(publicKey), nil
|
|
|
|
}
|