@@ -2,9 +2,12 @@ package grpc
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
8
+ "github.com/pactus-project/pactus/crypto"
7
9
"github.com/pactus-project/pactus/crypto/bls"
10
+ "github.com/pactus-project/pactus/crypto/ed25519"
8
11
pactus "github.com/pactus-project/pactus/www/grpc/gen/go"
9
12
"google.golang.org/grpc/codes"
10
13
"google.golang.org/grpc/status"
@@ -20,35 +23,28 @@ func newUtilsServer(server *Server) *utilServer {
20
23
}
21
24
}
22
25
23
- func (* utilServer ) SignMessageWithPrivateKey (_ context.Context ,
26
+ func (u * utilServer ) SignMessageWithPrivateKey (_ context.Context ,
24
27
req * pactus.SignMessageWithPrivateKeyRequest ,
25
28
) (* pactus.SignMessageWithPrivateKeyResponse , error ) {
26
- prvKey , err := bls . PrivateKeyFromString (req .PrivateKey )
29
+ prvKey , err := u . privateKeyFromString (req .PrivateKey )
27
30
if err != nil {
28
- return nil , status .Error (codes .InvalidArgument , "invalid private key" )
31
+ return nil , status .Error (codes .InvalidArgument , err . Error () )
29
32
}
30
-
31
33
sig := prvKey .Sign ([]byte (req .Message )).String ()
32
34
33
35
return & pactus.SignMessageWithPrivateKeyResponse {
34
36
Signature : sig ,
35
37
}, nil
36
38
}
37
39
38
- func (* utilServer ) VerifyMessage (_ context.Context ,
40
+ func (u * utilServer ) VerifyMessage (_ context.Context ,
39
41
req * pactus.VerifyMessageRequest ,
40
42
) (* pactus.VerifyMessageResponse , error ) {
41
- sig , err := bls .SignatureFromString (req .Signature )
42
- if err != nil {
43
- return nil , status .Error (codes .InvalidArgument , "signature is invalid" )
44
- }
45
-
46
- pub , err := bls .PublicKeyFromString (req .PublicKey )
43
+ pubKey , sig , err := u .publicKeyAndSigFromString (req .PublicKey , req .Signature )
47
44
if err != nil {
48
- return nil , status .Error (codes .InvalidArgument , "public key is invalid" )
45
+ return nil , status .Error (codes .InvalidArgument , err . Error () )
49
46
}
50
-
51
- if err := pub .Verify ([]byte (req .Message ), sig ); err == nil {
47
+ if err = pubKey .Verify ([]byte (req .Message ), sig ); err == nil {
52
48
return & pactus.VerifyMessageResponse {
53
49
IsValid : true ,
54
50
}, nil
@@ -101,3 +97,41 @@ func (*utilServer) BLSSignatureAggregation(_ context.Context,
101
97
Signature : bls .SignatureAggregate (sigs ... ).String (),
102
98
}, nil
103
99
}
100
+
101
+ func (* utilServer ) privateKeyFromString (prvStr string ) (crypto.PrivateKey , error ) {
102
+ blsPrv , err := bls .PrivateKeyFromString (prvStr )
103
+ if err == nil {
104
+ return blsPrv , nil
105
+ }
106
+
107
+ ed25519Prv , err := ed25519 .PrivateKeyFromString (prvStr )
108
+ if err == nil {
109
+ return ed25519Prv , nil
110
+ }
111
+
112
+ return nil , errors .New ("invalid Private Key" )
113
+ }
114
+
115
+ func (* utilServer ) publicKeyAndSigFromString (pubStr , sigStr string ) (crypto.PublicKey , crypto.Signature , error ) {
116
+ blsPub , err := bls .PublicKeyFromString (pubStr )
117
+ if err == nil {
118
+ blsSig , err := bls .SignatureFromString (sigStr )
119
+ if err != nil {
120
+ return nil , nil , errors .New ("invalid BLS signature" )
121
+ }
122
+
123
+ return blsPub , blsSig , nil
124
+ }
125
+
126
+ ed25519Pub , err := ed25519 .PublicKeyFromString (pubStr )
127
+ if err == nil {
128
+ ed25519Sig , err := ed25519 .SignatureFromString (sigStr )
129
+ if err != nil {
130
+ return nil , nil , errors .New ("invalid Ed25519 signature" )
131
+ }
132
+
133
+ return ed25519Pub , ed25519Sig , nil
134
+ }
135
+
136
+ return nil , nil , errors .New ("invalid Public Key" )
137
+ }
0 commit comments