Skip to content

Commit

Permalink
fix(grpc): return error on invalid arguments for VerifyMessage (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Jul 13, 2024
1 parent 97fd788 commit 456cc09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
20 changes: 9 additions & 11 deletions www/grpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,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()
Expand All @@ -37,25 +39,21 @@ 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 {
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
}
9 changes: 5 additions & 4 deletions www/grpc/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down

0 comments on commit 456cc09

Please sign in to comment.