@@ -14,6 +14,7 @@ import (
14
14
obskeyperdatabase "github.com/shutter-network/rolling-shutter/rolling-shutter/chainobserver/db/keyper"
15
15
corekeyperdatabase "github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/database"
16
16
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis/database"
17
+ "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
17
18
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
18
19
"github.com/shutter-network/rolling-shutter/rolling-shutter/shdb"
19
20
)
@@ -26,7 +27,7 @@ func (h *DecryptionKeySharesHandler) MessagePrototypes() []p2pmsg.Message {
26
27
return []p2pmsg.Message {& p2pmsg.DecryptionKeyShares {}}
27
28
}
28
29
29
- func (h * DecryptionKeySharesHandler ) ValidateMessage (_ context.Context , msg p2pmsg.Message ) (pubsub.ValidationResult , error ) {
30
+ func (h * DecryptionKeySharesHandler ) ValidateMessage (ctx context.Context , msg p2pmsg.Message ) (pubsub.ValidationResult , error ) {
30
31
keyShares := msg .(* p2pmsg.DecryptionKeyShares )
31
32
extra , ok := keyShares .Extra .(* p2pmsg.DecryptionKeyShares_Gnosis )
32
33
if ! ok {
@@ -42,7 +43,54 @@ func (h *DecryptionKeySharesHandler) ValidateMessage(_ context.Context, msg p2pm
42
43
if extra .Gnosis .TxPointer > math .MaxInt64 {
43
44
return pubsub .ValidationReject , errors .New ("tx pointer too large" )
44
45
}
45
- // TODO: check signature
46
+
47
+ keyperDB := corekeyperdatabase .New (h .dbpool )
48
+ eon , err := keyperDB .GetEon (ctx , int64 (keyShares .Eon ))
49
+ if err != nil {
50
+ return pubsub .ValidationReject , errors .Wrapf (err , "failed to get eon from database for eon %d" , keyShares .Eon )
51
+ }
52
+ obsKeyperDB := obskeyperdatabase .New (h .dbpool )
53
+ keyperSet , err := obsKeyperDB .GetKeyperSetByKeyperConfigIndex (ctx , eon .KeyperConfigIndex )
54
+ if err != nil {
55
+ return pubsub .ValidationReject , errors .Wrapf (err ,
56
+ "failed to get keyper set from database for keyper set index %d (eon %d)" ,
57
+ eon .KeyperConfigIndex ,
58
+ keyShares .Eon ,
59
+ )
60
+ }
61
+ if keyShares .KeyperIndex >= uint64 (len (keyperSet .Keypers )) {
62
+ return pubsub .ValidationReject , errors .Errorf (
63
+ "keyper index %d out of range for keyper set %d (eon %d)" ,
64
+ keyShares .KeyperIndex ,
65
+ eon .KeyperConfigIndex ,
66
+ keyShares .Eon ,
67
+ )
68
+ }
69
+ keyperAddressStr := keyperSet .Keypers [keyShares .KeyperIndex ]
70
+ keyperAddress , err := shdb .DecodeAddress (keyperAddressStr )
71
+ if err != nil {
72
+ return pubsub .ValidationReject , errors .Wrap (err , "failed to decode keyper address from database" )
73
+ }
74
+
75
+ identityPreimages := []identitypreimage.IdentityPreimage {}
76
+ for _ , share := range keyShares .Shares {
77
+ identityPreimage := identitypreimage .IdentityPreimage (share .EpochID )
78
+ identityPreimages = append (identityPreimages , identityPreimage )
79
+ }
80
+ slotDecryptionSignatureData := SlotDecryptionSignatureData {
81
+ InstanceID : keyShares .InstanceID ,
82
+ Eon : keyShares .Eon ,
83
+ Slot : extra .Gnosis .Slot ,
84
+ TxPointer : extra .Gnosis .TxPointer ,
85
+ IdentityPreimages : identityPreimages ,
86
+ }
87
+ signatureValid , err := CheckSlotDecryptionSignature (& slotDecryptionSignatureData , extra .Gnosis .Signature , keyperAddress )
88
+ if err != nil {
89
+ return pubsub .ValidationReject , errors .Wrap (err , "failed to check slot decryption signature" )
90
+ }
91
+ if ! signatureValid {
92
+ return pubsub .ValidationReject , errors .New ("slot decryption signature invalid" )
93
+ }
46
94
47
95
return pubsub .ValidationAccept , nil
48
96
}
@@ -193,7 +241,29 @@ func (h *DecryptionKeysHandler) ValidateMessage(ctx context.Context, msg p2pmsg.
193
241
signers = append (signers , signer )
194
242
}
195
243
196
- // TODO: check signatures
244
+ identityPreimages := []identitypreimage.IdentityPreimage {}
245
+ for _ , key := range keys .Keys {
246
+ identityPreimage := identitypreimage .IdentityPreimage (key .Identity )
247
+ identityPreimages = append (identityPreimages , identityPreimage )
248
+ }
249
+ slotDecryptionSignatureData := SlotDecryptionSignatureData {
250
+ InstanceID : keys .InstanceID ,
251
+ Eon : keys .Eon ,
252
+ Slot : extra .Gnosis .Slot ,
253
+ TxPointer : extra .Gnosis .TxPointer ,
254
+ IdentityPreimages : identityPreimages ,
255
+ }
256
+ for signatureIndex := 0 ; signatureIndex < len (extra .Gnosis .Signatures ); signatureIndex ++ {
257
+ signature := extra .Gnosis .Signatures [signatureIndex ]
258
+ signer := signers [signatureIndex ]
259
+ signatureValid , err := CheckSlotDecryptionSignature (& slotDecryptionSignatureData , signature , signer )
260
+ if err != nil {
261
+ return pubsub .ValidationReject , errors .Wrap (err , "failed to check slot decryption signature" )
262
+ }
263
+ if ! signatureValid {
264
+ return pubsub .ValidationReject , errors .New ("slot decryption signature invalid" )
265
+ }
266
+ }
197
267
198
268
return pubsub .ValidationAccept , nil
199
269
}
0 commit comments