Skip to content

Commit

Permalink
Update utils.go
Browse files Browse the repository at this point in the history
I decided to use error checking instead of prefix checking. Using error checking makes this much simpler, reducing the code by almost 20 lines.
  • Loading branch information
b00f authored Jan 15, 2025
1 parent 20b4156 commit c426114
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions www/grpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package grpc

import (
"context"
"errors"
"fmt"
"strings"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
Expand Down Expand Up @@ -31,6 +31,7 @@ func (u *utilServer) SignMessageWithPrivateKey(_ context.Context,
return nil, status.Error(codes.InvalidArgument, err.Error())
}
sig := prvKey.Sign([]byte(req.Message)).String()

return &pactus.SignMessageWithPrivateKeyResponse{
Signature: sig,
}, nil
Expand All @@ -48,6 +49,7 @@ func (u *utilServer) VerifyMessage(_ context.Context,
IsValid: true,
}, nil
}

return &pactus.VerifyMessageResponse{
IsValid: false,
}, nil
Expand Down Expand Up @@ -97,59 +99,39 @@ func (*utilServer) BLSSignatureAggregation(_ context.Context,
}

func (*utilServer) privateKeyFromString(prvStr string) (crypto.PrivateKey, error) {
maybeBLSPrivateKey := func(str string) bool {
return strings.Contains(strings.ToLower(str), "secret1p")
}
maybeEd25519PrivateKey := func(str string) bool {
return strings.Contains(strings.ToLower(str), "secret1r")
}
switch {
case maybeBLSPrivateKey(prvStr):
blsPrv, err := bls.PrivateKeyFromString(prvStr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid private key")
}
blsPrv, err := bls.PrivateKeyFromString(prvStr)
if err == nil {
return blsPrv, nil
case maybeEd25519PrivateKey(prvStr):
ed25519Prv, err := ed25519.PrivateKeyFromString(prvStr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid private key")
}
}

ed25519Prv, err := ed25519.PrivateKeyFromString(prvStr)
if err == nil {
return ed25519Prv, nil
default:
return nil, status.Error(codes.InvalidArgument, "invalid private key")
}

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

func (*utilServer) publicKeyAndSigFromString(pubStr, sigStr string) (crypto.PublicKey, crypto.Signature, error) {
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(pubStr):
blsPub, err := bls.PublicKeyFromString(pubStr)
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, "invalid public key")
}
sig, err := bls.SignatureFromString(sigStr)
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, "signature is invalid")
}
return blsPub, sig, nil
case maybeEd25519PublicKey(pubStr):
ed25519Pub, err := ed25519.PublicKeyFromString(pubStr)
blsPub, err := bls.PublicKeyFromString(pubStr)
if err == nil {
blsSig, err := bls.SignatureFromString(sigStr)
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, "invalid public key")
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
sig, err := ed25519.SignatureFromString(sigStr)

return blsPub, blsSig, nil
}

ed25519Pub, err := ed25519.PublicKeyFromString(pubStr)
if err == nil {
ed25519Sig, err := ed25519.SignatureFromString(sigStr)
if err != nil {
return nil, nil, status.Error(codes.InvalidArgument, "signature is invalid")
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, sig, nil
default:
return nil, nil, status.Error(codes.InvalidArgument, "invalid public key")

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
}

0 comments on commit c426114

Please sign in to comment.