Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat_: Update alias generated #6342

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

feat_: Update alias generated #6342

wants to merge 1 commit into from

Conversation

ulisesmac
Copy link

This PR is required to acomplish

The context about this change is mainly in this Discord thread.

Summary

This PR changes the implementation of GenerateFromPublicKeyString to return a compressed key with ellipsis instead of the 3-words name.

Important changes:

  • 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

@status-im-auto
Copy link
Member

status-im-auto commented Feb 13, 2025

Jenkins Builds

Click to see older builds (48)
Commit #️⃣ Finished (UTC) Duration Platform Result
✔️ 91989c3 #1 2025-02-13 19:02:44 ~3 min macos 📦zip
✖️ 91989c3 #1 2025-02-13 19:03:56 ~4 min tests 📄log
✔️ 91989c3 #1 2025-02-13 19:04:37 ~5 min windows 📦zip
✔️ 91989c3 #1 2025-02-13 19:04:43 ~5 min ios 📦zip
✔️ 91989c3 #1 2025-02-13 19:05:01 ~5 min macos 📦zip
✔️ 91989c3 #1 2025-02-13 19:05:08 ~6 min linux 📦zip
✔️ 91989c3 #1 2025-02-13 19:05:36 ~6 min android 📦aar
✔️ 91989c3 #1 2025-02-13 19:11:14 ~12 min tests-rpc 📄log
✔️ efe7105 #2 2025-02-13 20:20:33 ~5 min macos 📦zip
✔️ efe7105 #2 2025-02-13 20:20:44 ~5 min ios 📦zip
✔️ efe7105 #2 2025-02-13 20:20:54 ~5 min macos 📦zip
✔️ efe7105 #2 2025-02-13 20:21:09 ~5 min windows 📦zip
✔️ efe7105 #2 2025-02-13 20:21:30 ~6 min android 📦aar
✔️ efe7105 #2 2025-02-13 20:21:31 ~6 min linux 📦zip
✖️ efe7105 #2 2025-02-13 20:28:25 ~12 min tests-rpc 📄log
✔️ efe7105 #2 2025-02-13 20:46:43 ~31 min tests 📄log
✖️ 3635971 #3 2025-02-17 15:18:48 ~1 min tests-rpc 📄log
✔️ 3635971 #3 2025-02-17 15:21:03 ~4 min windows 📦zip
✔️ 3635971 #3 2025-02-17 15:21:19 ~4 min macos 📦zip
✔️ 3635971 #3 2025-02-17 15:21:38 ~4 min ios 📦zip
✔️ 3635971 #3 2025-02-17 15:22:03 ~5 min macos 📦zip
✔️ 3635971 #3 2025-02-17 15:22:55 ~6 min linux 📦zip
✔️ 3635971 #3 2025-02-17 15:23:47 ~6 min android 📦aar
✔️ 3635971 #3 2025-02-17 15:47:29 ~30 min tests 📄log
✖️ 07005bb #4 2025-02-17 15:22:35 ~1 min tests-rpc 📄log
✔️ 07005bb #4 2025-02-17 15:24:58 ~3 min windows 📦zip
✔️ 07005bb #4 2025-02-17 15:25:36 ~4 min macos 📦zip
✔️ 07005bb #4 2025-02-17 15:26:40 ~4 min ios 📦zip
✔️ 07005bb #4 2025-02-17 15:27:16 ~5 min macos 📦zip
✔️ 07005bb #4 2025-02-17 15:28:08 ~5 min linux 📦zip
✔️ 07005bb #4 2025-02-17 15:29:53 ~6 min android 📦aar
✔️ 07005bb #4 2025-02-17 16:17:24 ~29 min tests 📄log
✖️ d912c7d #5 2025-02-18 16:46:04 ~1 min tests-rpc 📄log
✔️ d912c7d #5 2025-02-18 16:48:47 ~4 min macos 📦zip
✔️ d912c7d #5 2025-02-18 16:49:10 ~4 min ios 📦zip
✔️ d912c7d #5 2025-02-18 16:49:39 ~5 min macos 📦zip
✔️ d912c7d #5 2025-02-18 16:49:46 ~5 min windows 📦zip
✔️ d912c7d #5 2025-02-18 16:50:17 ~5 min linux 📦zip
✔️ d912c7d #5 2025-02-18 16:50:27 ~6 min android 📦aar
✔️ d912c7d #5 2025-02-18 17:15:36 ~31 min tests 📄log
✔️ 4a938d1 #6 2025-02-18 16:52:14 ~2 min ios 📦zip
✔️ 4a938d1 #6 2025-02-18 16:53:14 ~4 min macos 📦zip
✔️ 4a938d1 #6 2025-02-18 16:53:27 ~2 min android 📦aar
✔️ 4a938d1 #6 2025-02-18 16:53:37 ~3 min windows 📦zip
✔️ 4a938d1 #6 2025-02-18 16:54:57 ~5 min macos 📦zip
✔️ 4a938d1 #6 2025-02-18 16:55:50 ~5 min linux 📦zip
✔️ 4a938d1 #6 2025-02-18 16:59:21 ~12 min tests-rpc 📄log
✔️ 4a938d1 #6 2025-02-18 17:46:57 ~31 min tests 📄log
Commit #️⃣ Finished (UTC) Duration Platform Result
✔️ 1283819 #7 2025-02-21 14:24:30 ~3 min android 📦aar
✔️ 1283819 #7 2025-02-21 14:25:24 ~4 min ios 📦zip
✔️ 1283819 #7 2025-02-21 14:25:35 ~4 min windows 📦zip
✖️ 1283819 #7 2025-02-21 14:26:13 ~4 min tests 📄log
✔️ 1283819 #7 2025-02-21 14:26:28 ~5 min macos 📦zip
✔️ 1283819 #7 2025-02-21 14:26:41 ~5 min macos 📦zip
✔️ 1283819 #7 2025-02-21 14:27:23 ~6 min linux 📦zip
✔️ 1283819 #7 2025-02-21 14:33:53 ~12 min tests-rpc 📄log
✔️ c1812f9 #8 2025-02-21 14:54:08 ~2 min android 📦aar
✔️ c1812f9 #8 2025-02-21 14:54:34 ~3 min ios 📦zip
✖️ c1812f9 #8 2025-02-21 14:55:38 ~4 min tests 📄log
✔️ c1812f9 #8 2025-02-21 14:56:38 ~5 min linux 📦zip
✔️ c1812f9 #8 2025-02-21 14:56:43 ~5 min macos 📦zip
✔️ c1812f9 #8 2025-02-21 14:56:53 ~5 min windows 📦zip
✔️ c1812f9 #8 2025-02-21 14:57:43 ~6 min macos 📦zip
✔️ c1812f9 #8 2025-02-21 15:04:07 ~12 min tests-rpc 📄log

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.

Project coverage is 59.77%. Comparing base (1bfb0ce) to head (4a938d1).

Files with missing lines Patch % Lines
protocol/identity/alias/generate.go 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6342      +/-   ##
===========================================
+ Coverage    59.69%   59.77%   +0.08%     
===========================================
  Files          865      865              
  Lines       113050   113053       +3     
===========================================
+ Hits         67484    67578      +94     
+ Misses       37731    37618     -113     
- Partials      7835     7857      +22     
Flag Coverage Δ
functional 0.45% <0.00%> (-0.01%) ⬇️
unit 59.77% <89.47%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
api/multiformat/utils.go 79.31% <100.00%> (ø)
protocol/contact.go 80.16% <100.00%> (-0.16%) ⬇️
protocol/identity/alias/generate.go 80.00% <87.50%> (-10.00%) ⬇️

... and 37 files with indirect coverage changes

@ulisesmac ulisesmac requested review from ilmotta and qfrank February 14, 2025 14:14
@ulisesmac
Copy link
Author

@igor-sirotin @ilmotta The comments have been addressed, could you please review again?

Thank you for the feedback!

return abbreviatedKey
}
return ""
func getShortenedCompressedKey(compressedKey string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can delete this function, which has only one usage and directly use alias.ShortenedCompressedKey(compressedKey)

// Fix a public key without the 0x prefix
if len(publicKeyString) == multiformat.LegacyKeyLength-2 && publicKeyString[:2] != "0x" {
fixedPublicKey = "0x" + publicKeyString
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could unnest a little bit and use else if.

Copy link
Collaborator

@igor-sirotin igor-sirotin Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ulisesmac mate, what was wrong with my suggestion? 😄

    // Attempt to fix a maybe public key without the 0x prefix
    if len(publicKeyString) >= 2 && publicKeyString[:2] != "0x" {
	    fixedPublicKey = "0x" + publicKeyString
    }
    if len(publicKeyString) != multiformat.LegacyKeyLength {
	      return fmt.Errorf("invalid length of public key")
    }

This implementation is short and simple.

  1. Prepend 0x if it's not yet there. No need to check the length of the given string at all at this stage.
  2. Check the string to be exactly 132 characters, already with 0x. If it's not, then we return an error.

Just 2 simple sequential conditions, no need complex conditions nesting here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@igor-sirotin

I think I can't take your suggestion as is since there's some unknowns to me with it.

You mentioned your suggestion has 2 sequential checks and no need to nest, but I think we need to nest or we lack some else clauses to make it work.

The code suggested won't fix a publicKeyString passed without the prefix Ox, instead, it will return the invalid key length error due to the always executed second if:

if len(publicKeyString) != multiformat.LegacyKeyLength {
 	 return fmt.Errorf("invalid length of public key") 	
}

So, I took your suggestion as an idea of what to check instead of as working code.

it'd be clearer if you provide the full function impl. because it seems the suggestion (as is) doesn't work, or is it that I am missing a language feature that will make it work? 🤔

(BTW, I'm new to Go, so I might lack some lang. features or preferred patterns)

Copy link
Collaborator

@igor-sirotin igor-sirotin Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code suggested won't fix a publicKeyString passed without the prefix 0x

Ok I see I did a typo there referencing fixedPublicKey, while it's not needed at all.

Actually, I simplified it even more 🙌. New simplifications are:

  1. Use strings.HasPrefix to check the prefix
  2. Remove the key length check, as it exists inside SerializeLegacyKey:
    if len(key) != LegacyKeyLength {
    return "", errors.New("invalid key length")
    }

Here's the full function:

func GenerateFromPublicKeyString(publicKeyString string) (string, error) {
	//  Ensure there's `0x` prefix
	if strings.HasPrefix(publicKeyString, "0x") {
		publicKeyString = "0x" + publicKeyString
	}

	compressedKey, err := multiformat.SerializeLegacyKey(publicKeyString)
	if err != nil {
		return "", err
	}

	return ShortenedCompressedKey(compressedKey), nil
}

Explanation

We know that by the point of calling multiformat.SerializeLegacyKey(fixedPublicKey) the public key must match 2 requirements:

  1. Start with 0x prefix
  2. Be of 132 characters length

Therefore, there's no nesting needed, we just need 2 sequential conditions to satisfy both requirements.
(knowing the 132 length check is inside, we don't need to duplicate it)

@ulisesmac, please let me know if it's still unclear

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied, just added a missing !.

Thanks for the suggestion @igor-sirotin

- 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
@ulisesmac
Copy link
Author

@igor-sirotin comments addressed, could you review again please?

Copy link
Collaborator

@igor-sirotin igor-sirotin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants