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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions www/grpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import (
"context"
"errors"
"fmt"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/ed25519"
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -20,35 +23,28 @@
}
}

func (*utilServer) SignMessageWithPrivateKey(_ context.Context,
func (u *utilServer) SignMessageWithPrivateKey(_ context.Context,
req *pactus.SignMessageWithPrivateKeyRequest,
) (*pactus.SignMessageWithPrivateKeyResponse, error) {
prvKey, err := bls.PrivateKeyFromString(req.PrivateKey)
prvKey, err := u.privateKeyFromString(req.PrivateKey)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid private key")
return nil, status.Error(codes.InvalidArgument, err.Error())
}

sig := prvKey.Sign([]byte(req.Message)).String()

return &pactus.SignMessageWithPrivateKeyResponse{
Signature: sig,
}, nil
}

func (*utilServer) VerifyMessage(_ context.Context,
func (u *utilServer) VerifyMessage(_ context.Context,
req *pactus.VerifyMessageRequest,
) (*pactus.VerifyMessageResponse, error) {
sig, err := bls.SignatureFromString(req.Signature)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "signature is invalid")
}

pub, err := bls.PublicKeyFromString(req.PublicKey)
pubKey, sig, err := u.publicKeyAndSigFromString(req.PublicKey, req.Signature)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "public key is invalid")
return nil, status.Error(codes.InvalidArgument, err.Error())

Check warning on line 45 in www/grpc/utils.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/utils.go#L45

Added line #L45 was not covered by tests
}

if err := pub.Verify([]byte(req.Message), sig); err == nil {
if err = pubKey.Verify([]byte(req.Message), sig); err == nil {
return &pactus.VerifyMessageResponse{
IsValid: true,
}, nil
Expand Down Expand Up @@ -101,3 +97,41 @@
Signature: bls.SignatureAggregate(sigs...).String(),
}, nil
}

func (*utilServer) privateKeyFromString(prvStr string) (crypto.PrivateKey, error) {
blsPrv, err := bls.PrivateKeyFromString(prvStr)
if err == nil {
return blsPrv, nil
}

ed25519Prv, err := ed25519.PrivateKeyFromString(prvStr)
if err == nil {
return ed25519Prv, nil
}

return nil, errors.New("invalid Private Key")
}

func (*utilServer) publicKeyAndSigFromString(pubStr, sigStr string) (crypto.PublicKey, crypto.Signature, error) {
blsPub, err := bls.PublicKeyFromString(pubStr)
if err == nil {
blsSig, err := bls.SignatureFromString(sigStr)
if err != nil {
return nil, nil, errors.New("invalid BLS signature")
}

Check warning on line 121 in www/grpc/utils.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/utils.go#L120-L121

Added lines #L120 - L121 were not covered by tests

return blsPub, blsSig, nil
}

ed25519Pub, err := ed25519.PublicKeyFromString(pubStr)
if err == nil {
ed25519Sig, err := ed25519.SignatureFromString(sigStr)
if err != nil {
return nil, nil, errors.New("invalid Ed25519 signature")
}

Check warning on line 131 in www/grpc/utils.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/utils.go#L130-L131

Added lines #L130 - L131 were not covered by tests

return ed25519Pub, ed25519Sig, nil
}

return nil, nil, errors.New("invalid Public Key")

Check warning on line 136 in www/grpc/utils.go

View check run for this annotation

Codecov / codecov/patch

www/grpc/utils.go#L136

Added line #L136 was not covered by tests
}
76 changes: 76 additions & 0 deletions www/grpc/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ func TestSignMessageWithPrivateKey(t *testing.T) {
td.StopServer()
}

func TestSignMessageWithED25519PrivateKey(t *testing.T) {
conf := testConfig()
td := setup(t, conf)
conn, client := td.utilClient(t)

msg := "pactus"
prvStr := "SECRET1RYY62A96X25ZAL4DPL5Z63G83GCSFCCQ7K0CMQD3MFNLYK3A6R26QUUK3Y0"
invalidPrvStr := "INVSECRET1RYY62A96X25ZAL4DPL5Z63G83GCSFCCQ7K0CMQD3MFNLYK3A6R26QUUK3Y0"
expectedSig := "361aaa09c408bfcf7e79dd90c583eeeaefe7c732ca5643cfb2ea7a6d22105b874a412080" +
"525a855bbd5df94110a7d0083d6e386e016ecf8b7f522c339f79d305"

t.Run("", func(t *testing.T) {
res, err := client.SignMessageWithPrivateKey(context.Background(),
&pactus.SignMessageWithPrivateKeyRequest{
Message: msg,
PrivateKey: prvStr,
})

assert.Nil(t, err)
assert.Equal(t, expectedSig, res.Signature)
})

t.Run("", func(t *testing.T) {
res, err := client.SignMessageWithPrivateKey(context.Background(),
&pactus.SignMessageWithPrivateKeyRequest{
Message: msg,
PrivateKey: invalidPrvStr,
})

assert.NotNil(t, err)
assert.Nil(t, res)
})

assert.Nil(t, conn.Close(), "Error closing connection")
td.StopServer()
}

func TestVerifyMessage(t *testing.T) {
conf := testConfig()
td := setup(t, conf)
Expand Down Expand Up @@ -84,6 +121,45 @@ func TestVerifyMessage(t *testing.T) {
td.StopServer()
}

func TestVerifyED25519Message(t *testing.T) {
conf := testConfig()
td := setup(t, conf)
conn, client := td.utilClient(t)

msg := "pactus"
pubStr := "public1rvqxnpfph8tnc3ck55z85w285t5jetylmmktr9wlzs0zvx7kx500szxfudh"
sigStr := "361aaa09c408bfcf7e79dd90c583eeeaefe7c732ca5643cfb2ea7a6d22105b874a41" +
"2080525a855bbd5df94110a7d0083d6e386e016ecf8b7f522c339f79d305"
invalidSigStr := "001aaa09c408bfcf7e79dd90c583eeeaefe7c732ca5643cfb2ea7a6d22105b" +
"874a412080525a855bbd5df94110a7d0083d6e386e016ecf8b7f522c339f79d305"

t.Run("valid message", func(t *testing.T) {
res, err := client.VerifyMessage(context.Background(),
&pactus.VerifyMessageRequest{
Message: msg,
Signature: sigStr,
PublicKey: pubStr,
})
assert.Nil(t, err)
assert.True(t, res.IsValid)
})

t.Run("invalid message", func(t *testing.T) {
res, err := client.VerifyMessage(context.Background(),
&pactus.VerifyMessageRequest{
Message: msg,
Signature: invalidSigStr,
PublicKey: pubStr,
})

assert.Nil(t, err)
assert.False(t, res.IsValid)
})

assert.Nil(t, conn.Close(), "Error closing connection")
td.StopServer()
}

func TestBLSPublicKeyAggregation(t *testing.T) {
ts := testsuite.NewTestSuite(t)
conf := testConfig()
Expand Down
Loading