From ba8bc4f14a14aee21a770be60bc3997edfb6a87b Mon Sep 17 00:00:00 2001 From: Javad Date: Fri, 12 Jul 2024 15:15:05 +0330 Subject: [PATCH 1/4] fix: skip error on verify message when is invalid and return false --- www/grpc/utils.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/www/grpc/utils.go b/www/grpc/utils.go index 294a94fc1..8633e0655 100644 --- a/www/grpc/utils.go +++ b/www/grpc/utils.go @@ -49,13 +49,13 @@ func (*utilServer) VerifyMessage(_ context.Context, }, err } - if err := pub.Verify([]byte(req.Message), sig); err != nil { + if err := pub.Verify([]byte(req.Message), sig); err == nil { return &pactus.VerifyMessageResponse{ - IsValid: false, - }, err + IsValid: true, + }, nil } return &pactus.VerifyMessageResponse{ - IsValid: true, + IsValid: false, }, nil } From 07675e05a6b8585ab067e8b76b49c7c83d6a3f79 Mon Sep 17 00:00:00 2001 From: Javad Date: Fri, 12 Jul 2024 15:19:39 +0330 Subject: [PATCH 2/4] fix: update error on invalid signature and public key --- www/grpc/utils.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/www/grpc/utils.go b/www/grpc/utils.go index 8633e0655..be7d12e00 100644 --- a/www/grpc/utils.go +++ b/www/grpc/utils.go @@ -5,6 +5,8 @@ import ( "github.com/pactus-project/pactus/crypto/bls" pactus "github.com/pactus-project/pactus/www/grpc/gen/go" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) type utilServer struct { @@ -37,16 +39,12 @@ func (*utilServer) VerifyMessage(_ context.Context, ) (*pactus.VerifyMessageResponse, error) { sig, err := bls.SignatureFromString(req.Signature) if err != nil { - return &pactus.VerifyMessageResponse{ - IsValid: false, - }, err + return nil, status.Error(codes.InvalidArgument, "signature is invalid") } pub, err := bls.PublicKeyFromString(req.PublicKey) if err != nil { - return &pactus.VerifyMessageResponse{ - IsValid: false, - }, err + return nil, status.Error(codes.InvalidArgument, "public key is invalid") } if err := pub.Verify([]byte(req.Message), sig); err == nil { From 2a5317ee3a19ac6b471a4686e1c0ab945c78258e Mon Sep 17 00:00:00 2001 From: Javad Date: Fri, 12 Jul 2024 15:21:22 +0330 Subject: [PATCH 3/4] fix: status code invalid private key --- www/grpc/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/grpc/utils.go b/www/grpc/utils.go index be7d12e00..76e9734ca 100644 --- a/www/grpc/utils.go +++ b/www/grpc/utils.go @@ -24,7 +24,7 @@ func (*utilServer) SignMessageWithPrivateKey(_ context.Context, ) (*pactus.SignMessageWithPrivateKeyResponse, error) { prvKey, err := bls.PrivateKeyFromString(req.PrivateKey) if err != nil { - return nil, err + return nil, status.Error(codes.InvalidArgument, "invalid private key") } sig := prvKey.Sign([]byte(req.Message)).String() From ce41a89b51d0a343687d2bf42502a587f45abf5c Mon Sep 17 00:00:00 2001 From: Javad Date: Fri, 12 Jul 2024 15:25:19 +0330 Subject: [PATCH 4/4] fix: error in invalid message --- www/grpc/utils_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/www/grpc/utils_test.go b/www/grpc/utils_test.go index 5ab04d777..2060d2893 100644 --- a/www/grpc/utils_test.go +++ b/www/grpc/utils_test.go @@ -55,7 +55,7 @@ func TestVerifyMessage(t *testing.T) { sigStr := "923d67a8624cbb7972b29328e15ec76cc846076ccf00a9e94d991c677846f334ae4ba4551396fbcd6d1cab7593baf3b7" invalidSigStr := "113d67a8624cbb7972b29328e15ec76cc846076ccf00a9e94d991c677846f334ae4ba4551396fbcd6d1cab7593baf3c9" - t.Run("", func(t *testing.T) { + t.Run("valid message", func(t *testing.T) { res, err := client.VerifyMessage(context.Background(), &pactus.VerifyMessageRequest{ Message: msg, @@ -66,15 +66,16 @@ func TestVerifyMessage(t *testing.T) { assert.True(t, res.IsValid) }) - t.Run("", func(t *testing.T) { - _, err := client.VerifyMessage(context.Background(), + t.Run("invalid message", func(t *testing.T) { + res, err := client.VerifyMessage(context.Background(), &pactus.VerifyMessageRequest{ Message: msg, Signature: invalidSigStr, PublicKey: pubStr, }) - assert.NotNil(t, err) + assert.Nil(t, err) + assert.False(t, res.IsValid) }) assert.Nil(t, conn.Close(), "Error closing connection")