9
9
"github.com/pkg/errors"
10
10
"github.com/rs/zerolog/log"
11
11
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
12
-
13
12
"github.com/shutter-network/shutter/shlib/puredkg"
14
13
"github.com/shutter-network/shutter/shlib/shcrypto"
15
14
@@ -56,7 +55,7 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
56
55
}
57
56
58
57
dkgResultDB , err := queries .GetDKGResultForKeyperConfigIndex (ctx , eon )
59
- if err == pgx .ErrNoRows {
58
+ if errors . Is ( err , pgx .ErrNoRows ) {
60
59
return pubsub .ValidationReject , errors .Errorf ("no DKG result found for eon %d" , eon )
61
60
}
62
61
if err != nil {
@@ -81,16 +80,31 @@ func (handler *DecryptionKeyHandler) ValidateMessage(ctx context.Context, msg p2
81
80
)
82
81
}
83
82
84
- validationResult , err := checkKeysErrors (key . Keys , pureDKGResult )
83
+ validationResult , err := checkKeysErrors (ctx , key , pureDKGResult , queries )
85
84
return validationResult , err
86
85
}
87
86
88
- func checkKeysErrors (keys []* p2pmsg.Key , pureDKGResult * puredkg.Result ) (pubsub.ValidationResult , error ) {
89
- for i , k := range keys {
87
+ func checkKeysErrors (ctx context.Context , msg * p2pmsg.DecryptionKeys , pureDKGResult * puredkg.Result , queries * database.Queries ) (pubsub.ValidationResult , error ) {
88
+
89
+ for i , k := range msg .Keys {
90
90
epochSecretKey , err := k .GetEpochSecretKey ()
91
91
if err != nil {
92
92
return pubsub .ValidationReject , err
93
93
}
94
+ eon , err := medley .Uint64ToInt64Safe (msg .Eon )
95
+ if err != nil {
96
+ return pubsub .ValidationReject , errors .Wrapf (err , "overflow error while converting eon to int64 %d" , msg .Eon )
97
+ }
98
+ decryptionKey , err := queries .GetDecryptionKey (ctx , database.GetDecryptionKeyParams {
99
+ Eon : eon ,
100
+ EpochID : k .GetIdentity (),
101
+ })
102
+ if err != nil && ! errors .Is (err , pgx .ErrNoRows ) {
103
+ return pubsub .ValidationReject , errors .Wrapf (err , "failed to get decryption key for identity %x from db" , k .Identity )
104
+ }
105
+ if bytes .Equal (k .Key , decryptionKey .DecryptionKey ) {
106
+ continue
107
+ }
94
108
ok , err := shcrypto .VerifyEpochSecretKey (epochSecretKey , pureDKGResult .PublicKey , k .Identity )
95
109
if err != nil {
96
110
return pubsub .ValidationReject , errors .Wrapf (err , "error while checking epoch secret key for identity %x" , k .Identity )
@@ -99,7 +113,7 @@ func checkKeysErrors(keys []*p2pmsg.Key, pureDKGResult *puredkg.Result) (pubsub.
99
113
return pubsub .ValidationReject , errors .Errorf ("epoch secret key for identity %x is not valid" , k .Identity )
100
114
}
101
115
102
- if i > 0 && bytes .Compare (k .Identity , keys [i - 1 ].Identity ) < 0 {
116
+ if i > 0 && bytes .Compare (k .Identity , msg . Keys [i - 1 ].Identity ) < 0 {
103
117
return pubsub .ValidationReject , errors .Errorf ("keys not ordered" )
104
118
}
105
119
}
0 commit comments