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

fix(grpc): return error on invalid arguments for VerifyMessage #1411

Merged
merged 5 commits into from
Jul 13, 2024
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
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
Loading