Skip to content

Commit eac4403

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 - Add more tests about the public key length received
1 parent a3412cc commit eac4403

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

protocol/contact.go

Lines changed: 2 additions & 13 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,18 +364,6 @@ 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 ""
376-
}
377-
378367
func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact, error) {
379368
compressedKey, err := multiformat.SerializeLegacyKey(common.PubkeyToHex(publicKey))
380369
if err != nil {
@@ -385,7 +374,7 @@ func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact,
385374

386375
contact := &Contact{
387376
ID: publicKeyString,
388-
Alias: getShortenedCompressedKey(compressedKey),
377+
Alias: alias.ShortenedCompressedKey(compressedKey),
389378
Address: types.EncodeHex(address[:]),
390379
CustomizationColor: multiaccountscommon.CustomizationColorBlue,
391380
}

protocol/identity/alias/generate.go

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

33
import (
44
"crypto/ecdsa"
5-
"encoding/hex"
65
"fmt"
76
"strings"
87

9-
"github.com/status-im/status-go/eth-node/crypto"
8+
"github.com/status-im/status-go/api/multiformat"
109
)
1110

1211
const poly uint64 = 0xB8
@@ -29,19 +28,27 @@ func GenerateFromPublicKey(publicKey *ecdsa.PublicKey) string {
2928
return generate(uint64(publicKey.X.Int64()))
3029
}
3130

32-
// GenerateFromPublicKeyString returns the 3 words name given a public key
33-
// prefixed with 0x
31+
// GenerateFromPublicKeyString calculates the compressed key for the given publicKeyString
32+
// and returns its first 8 chars followed by an ellipsis char and its last 5 chars.
3433
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
34+
// Ensure there's `0x` prefix
35+
if !strings.HasPrefix(publicKeyString, "0x") {
36+
publicKeyString = "0x" + publicKeyString
3937
}
4038

41-
publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
39+
compressedKey, err := multiformat.SerializeLegacyKey(publicKeyString)
4240
if err != nil {
4341
return "", err
4442
}
4543

46-
return GenerateFromPublicKey(publicKey), nil
44+
return ShortenedCompressedKey(compressedKey), nil
45+
}
46+
47+
func ShortenedCompressedKey(compressedKey string) string {
48+
if len(compressedKey) <= 12 {
49+
return ""
50+
}
51+
prefix := compressedKey[0:8]
52+
suffix := compressedKey[len(compressedKey)-5:]
53+
return prefix + "…" + suffix
4754
}

protocol/identity/alias/generate_test.go

Lines changed: 20 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
{
@@ -45,6 +45,24 @@ func TestGenerateFromPublicKeyString(t *testing.T) {
4545
alias: "",
4646
errorExpected: true,
4747
},
48+
{
49+
name: "invalid public key length - 2 chars",
50+
publicKey: "0X",
51+
alias: "",
52+
errorExpected: true,
53+
},
54+
{
55+
name: "invalid public key length - public key length minus 1 char",
56+
publicKey: "0x04eedbaafd6adf4a9233a13e7b1c3c14461fffeba2e9054b8d456ce5f6ebeafadcbf3dce3716253fbc391277fa5a086b60b283daf61fb5b1f26895f456c2f31ae",
57+
alias: "",
58+
errorExpected: true,
59+
},
60+
{
61+
name: "invalid public key length - more chars than the public key",
62+
publicKey: "0x04eedbaafd6adf4a9233a13e7b1c3c14461fffeba2e9054b8d456ce5f6ebeafadcbf3dce3716253fbc391277fa5a086b60b283daf61fb5b1f26895f456c2f31ae304eedbaafd6adf4a9233a13e7b1c3c14461fffeba2e9054b8d456ce5f6ebeafadcbf3dce3716253fbc391277fa5a086b60b283daf61fb5b1f26895f456c2f31ae3",
63+
alias: "",
64+
errorExpected: true,
65+
},
4866
}
4967

5068
for _, tt := range tests {

0 commit comments

Comments
 (0)