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(grpc): support Ed25519 message signing and verification #1667

Merged
merged 6 commits into from
Jan 15, 2025

Conversation

BuidlWithShivam
Copy link
Contributor

Copy link

codecov bot commented Jan 14, 2025

Codecov Report

Attention: Patch coverage is 74.19355% with 8 lines in your changes missing coverage. Please review.

Project coverage is 76.37%. Comparing base (86a8ba9) to head (c426114).
Report is 41 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1667      +/-   ##
==========================================
+ Coverage   75.07%   76.37%   +1.30%     
==========================================
  Files         234      241       +7     
  Lines       12156    18469    +6313     
==========================================
+ Hits         9126    14106    +4980     
- Misses       2582     3901    +1319     
- Partials      448      462      +14     

@Ja7ad Ja7ad requested a review from b00f January 14, 2025 13:42
@b00f
Copy link
Collaborator

b00f commented Jan 14, 2025

@BuidlWithShivam,

Thank you so much for this amazing PR. It’s great to see how smart you are in understanding the logic and fixing this issue. Good job!
By thee way, I feel we can improve it in some ways.

First of all, it’s great to see that you can detect the type of PrivateKey by their prefixes. This approach is better than checking for errors in the public key. We can do the same for the public key as well, like this:

	maybeBLSPublicKey := func(str string) bool {
		return strings.Contains(strings.ToLower(str), "public1p")
	}

	maybeEd25519PublicKey := func(str string) bool {
		return strings.Contains(strings.ToLower(str), "public1r")
	}
	switch {
	case maybeBLSPublicKey(req.PublicKey):
		/// ...

	case maybeEd25519PublicKey(req.PublicKey):
		/// ...

	default:
		return nil, status.Error(codes.InvalidArgument, "invalid public key")
	}

Now, we can make another improvement here. PublicKeys are interfaces. We can simplify the above code by creating a function that takes a string and returns a PublicKey as an interface.

We can define a function like this:

func (*utilServer) publicKeyFromString(pubStr string) (crypto.PublicKey, error) {
	maybeBLSPublicKey := func(str string) bool {
		return strings.Contains(strings.ToLower(str), "secret1p")
	}

	maybeEd25519PublicKey := func(str string) bool {
		return strings.Contains(strings.ToLower(str), "secret1e")
	}

	switch {
	case maybeBLSPublicKey(pubStr):
		blsPub, err := bls.PublicKeyFromString(pubStr)
		if err != nil {
			return nil, status.Error(codes.InvalidArgument, "invalid private key")
		}
		return blsPub, nil

	case maybeEd25519PublicKey(pubStr):
		ed25519Pub, err := ed25519.PublicKeyFromString(pubStr)
		if err != nil {
			return nil, status.Error(codes.InvalidArgument, "invalid private key")
		}
		return ed25519Pub, nil

	default:
		return nil, status.Error(codes.InvalidArgument, "invalid private key")
	}
}

Finally we can make the same for PrivateKey

@b00f b00f modified the milestones: v1.7.0, v1.8.0 Jan 14, 2025
@BuidlWithShivam
Copy link
Contributor Author

@b00f Sure. Thanks for the review. I was not aware that the public key also has the same structure. Tried to find across the code but couldn't. Thanks for the suggestion on the interface as well. I just saw two different structs and missed the interface. I will implement those.

@BuidlWithShivam
Copy link
Contributor Author

@b00f Made the changes. Can you please check.

@b00f
Copy link
Collaborator

b00f commented Jan 15, 2025

@BuidlWithShivam

You can use make devtools followed by make check to ensure that your changes have no formatting or linting issues. For more details, refer to the Contributing Guide.

I decided to use error checking instead of prefix checking. Using error checking makes this much simpler, reducing the code by almost 20 lines.
@b00f
Copy link
Collaborator

b00f commented Jan 15, 2025

@BuidlWithShivam
I decided to use error checking instead of prefix checking. Using error checking makes this much simpler, reducing the code by almost 20 lines.

@b00f b00f modified the milestones: v1.8.0, v1.7.0 Jan 15, 2025
@b00f
Copy link
Collaborator

b00f commented Jan 15, 2025

Good job @BuidlWithShivam

@b00f b00f changed the title fix(grpc): sign and verify message by ed25519 feat(grpc): support Ed25519 message signing and verification Jan 15, 2025
@b00f b00f merged commit d0e66e5 into pactus-project:main Jan 15, 2025
13 checks passed
@b00f
Copy link
Collaborator

b00f commented Jan 15, 2025

@BuidlWithShivam Do you like to join Pactus DEV team?

@BuidlWithShivam
Copy link
Contributor Author

@BuidlWithShivam Do you like to join Pactus DEV team?

@b00f I am looking for opportunities in Blockchain. Sure we can have a chat about it.

@b00f
Copy link
Collaborator

b00f commented Jan 16, 2025

@BuidlWithShivam Please join our Discord and let's have chat there: https://discord.gg/pactus

@BuidlWithShivam
Copy link
Contributor Author

BuidlWithShivam commented Jan 16, 2025

@BuidlWithShivam Please join our Discord and let's have chat there: https://discord.gg/pactus

@b00f Joined.

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.

2 participants