Skip to content

Commit 07005bb

Browse files
committed
feat_: Update alias generated
- Changed from the 3-words name to be the compressed key with an ellipsis - Update the shortened compressed key to be consistent with the alias
1 parent 157d6eb commit 07005bb

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

api/multiformat/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
secp256k1KeyType = 0xe7
1919
bls12p381g1KeyType = 0xea
2020
bls12p381g2KeyType = 0xeb
21-
legacyKeyLength = 132
21+
LegacyKeyLength = 132
2222
)
2323

2424
// SerializePublicKey serialises a non-serialised multibase encoded multicodec identified EC public key
@@ -70,7 +70,7 @@ func DeserializePublicKey(key, outputBase string) (string, error) {
7070
// SerializeLegacyKey converts a secp251k1 uncompressed key to
7171
// a base58 compressed key
7272
func SerializeLegacyKey(key string) (string, error) {
73-
if len(key) != legacyKeyLength {
73+
if len(key) != LegacyKeyLength {
7474
return "", errors.New("invalid key length")
7575
}
7676

protocol/contact.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
multiaccountscommon "github.com/status-im/status-go/multiaccounts/common"
1616
"github.com/status-im/status-go/multiaccounts/settings"
1717
"github.com/status-im/status-go/protocol/common"
18+
"github.com/status-im/status-go/protocol/identity/alias"
1819
"github.com/status-im/status-go/protocol/protobuf"
1920
"github.com/status-im/status-go/protocol/verification"
2021
)
@@ -363,16 +364,8 @@ func BuildContactFromPublicKey(publicKey *ecdsa.PublicKey) (*Contact, error) {
363364
return buildContact(id, publicKey)
364365
}
365366

366-
func getShortenedCompressedKey(publicKey string) string {
367-
if len(publicKey) > 9 {
368-
firstPart := publicKey[0:3]
369-
ellipsis := "..."
370-
publicKeySize := len(publicKey)
371-
lastPart := publicKey[publicKeySize-6 : publicKeySize]
372-
abbreviatedKey := fmt.Sprintf("%s%s%s", firstPart, ellipsis, lastPart)
373-
return abbreviatedKey
374-
}
375-
return ""
367+
func getShortenedCompressedKey(compressedKey string) string {
368+
return alias.ShortenedCompressedKey(compressedKey)
376369
}
377370

378371
func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact, error) {

protocol/identity/alias/generate.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package alias
22

33
import (
44
"crypto/ecdsa"
5-
"encoding/hex"
65
"fmt"
7-
"strings"
86

9-
"github.com/status-im/status-go/eth-node/crypto"
7+
"github.com/status-im/status-go/api/multiformat"
108
)
119

1210
const poly uint64 = 0xB8
@@ -32,16 +30,31 @@ func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
3230
// GenerateFromPublicKeyString returns the 3 words name given a public key
3331
// prefixed with 0x
3432
func GenerateFromPublicKeyString(publicKeyString string) (string, error) {
35-
pk := strings.TrimPrefix(publicKeyString, "0x")
36-
publicKeyBytes, err := hex.DecodeString(pk)
37-
if err != nil {
38-
return "", err
33+
var fixedPublicKey string
34+
if len(publicKeyString) == multiformat.LegacyKeyLength {
35+
fixedPublicKey = publicKeyString
36+
} else {
37+
// Attempt to fix a maybe public key without the 0x prefix
38+
if len(publicKeyString) >= 2 && publicKeyString[:2] != "0x" {
39+
fixedPublicKey = "0x" + publicKeyString
40+
} else {
41+
return "", fmt.Errorf("invalid length of public key")
42+
}
3943
}
4044

41-
publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
45+
compressedKey, err := multiformat.SerializeLegacyKey(fixedPublicKey)
4246
if err != nil {
4347
return "", err
4448
}
49+
return ShortenedCompressedKey(compressedKey), nil
50+
}
4551

46-
return GenerateFromPublicKey(publicKey), nil
52+
func ShortenedCompressedKey(compressedKey string) string {
53+
if len(compressedKey) > 12 {
54+
prefix := compressedKey[0:8]
55+
suffix := compressedKey[len(compressedKey)-5:]
56+
return prefix + "…" + suffix
57+
} else {
58+
return ""
59+
}
4760
}

protocol/identity/alias/generate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ func TestGenerateFromPublicKeyString(t *testing.T) {
2424
{
2525
name: "valid public key - start with 0x",
2626
publicKey: "0x04eedbaafd6adf4a9233a13e7b1c3c14461fffeba2e9054b8d456ce5f6ebeafadcbf3dce3716253fbc391277fa5a086b60b283daf61fb5b1f26895f456c2f31ae3",
27-
alias: "Darkorange Blue Bubblefish",
27+
alias: "zQ3shviW…k2bWB",
2828
errorExpected: false,
2929
},
3030
{
3131
name: "valid public key - without 0x",
3232
publicKey: "04eedbaafd6adf4a9233a13e7b1c3c14461fffeba2e9054b8d456ce5f6ebeafadcbf3dce3716253fbc391277fa5a086b60b283daf61fb5b1f26895f456c2f31ae3",
33-
alias: "Darkorange Blue Bubblefish",
33+
alias: "zQ3shviW…k2bWB",
3434
errorExpected: false,
3535
},
3636
{

0 commit comments

Comments
 (0)