-
Notifications
You must be signed in to change notification settings - Fork 555
/
proof.go
477 lines (412 loc) · 11.7 KB
/
proof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package types
import (
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v5/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v5/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v5/modules/core/exported"
)
// VerifySignature verifies if the the provided public key generated the signature
// over the given data. Single and Multi signature public keys are supported.
// The signature data type must correspond to the public key type. An error is
// returned if signature verification fails or an invalid SignatureData type is
// provided.
func VerifySignature(pubKey cryptotypes.PubKey, signBytes []byte, sigData signing.SignatureData) error {
switch pubKey := pubKey.(type) {
case multisig.PubKey:
data, ok := sigData.(*signing.MultiSignatureData)
if !ok {
return sdkerrors.Wrapf(ErrSignatureVerificationFailed, "invalid signature data type, expected %T, got %T", (*signing.MultiSignatureData)(nil), data)
}
// The function supplied fulfills the VerifyMultisignature interface. No special
// adjustments need to be made to the sign bytes based on the sign mode.
if err := pubKey.VerifyMultisignature(func(signing.SignMode) ([]byte, error) {
return signBytes, nil
}, data); err != nil {
return err
}
default:
data, ok := sigData.(*signing.SingleSignatureData)
if !ok {
return sdkerrors.Wrapf(ErrSignatureVerificationFailed, "invalid signature data type, expected %T, got %T", (*signing.SingleSignatureData)(nil), data)
}
if !pubKey.VerifySignature(signBytes, data.Signature) {
return ErrSignatureVerificationFailed
}
}
return nil
}
// MisbehaviourSignBytes returns the sign bytes for verification of misbehaviour.
func MisbehaviourSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
dataType DataType,
data []byte,
) ([]byte, error) {
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: dataType,
Data: data,
}
return cdc.Marshal(signBytes)
}
// HeaderSignBytes returns the sign bytes for verification of misbehaviour.
func HeaderSignBytes(
cdc codec.BinaryCodec,
header *Header,
) ([]byte, error) {
data := &HeaderData{
NewPubKey: header.NewPublicKey,
NewDiversifier: header.NewDiversifier,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: header.Sequence,
Timestamp: header.Timestamp,
Diversifier: header.NewDiversifier,
DataType: HEADER,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// ClientStateSignBytes returns the sign bytes for verification of the
// client state.
func ClientStateSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
clientState exported.ClientState,
) ([]byte, error) {
dataBz, err := ClientStateDataBytes(cdc, path, clientState)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: CLIENT,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// ClientStateDataBytes returns the client state data bytes used in constructing
// SignBytes.
func ClientStateDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
clientState exported.ClientState,
) ([]byte, error) {
any, err := clienttypes.PackClientState(clientState)
if err != nil {
return nil, err
}
data := &ClientStateData{
Path: []byte(path.String()),
ClientState: any,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// ConsensusStateSignBytes returns the sign bytes for verification of the
// consensus state.
func ConsensusStateSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
consensusState exported.ConsensusState,
) ([]byte, error) {
dataBz, err := ConsensusStateDataBytes(cdc, path, consensusState)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: CONSENSUS,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// ConsensusStateDataBytes returns the consensus state data bytes used in constructing
// SignBytes.
func ConsensusStateDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
consensusState exported.ConsensusState,
) ([]byte, error) {
any, err := clienttypes.PackConsensusState(consensusState)
if err != nil {
return nil, err
}
data := &ConsensusStateData{
Path: []byte(path.String()),
ConsensusState: any,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// ConnectionStateSignBytes returns the sign bytes for verification of the
// connection state.
func ConnectionStateSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
connectionEnd exported.ConnectionI,
) ([]byte, error) {
dataBz, err := ConnectionStateDataBytes(cdc, path, connectionEnd)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: CONNECTION,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// ConnectionStateDataBytes returns the connection state data bytes used in constructing
// SignBytes.
func ConnectionStateDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
connectionEnd exported.ConnectionI,
) ([]byte, error) {
connection, ok := connectionEnd.(connectiontypes.ConnectionEnd)
if !ok {
return nil, sdkerrors.Wrapf(
connectiontypes.ErrInvalidConnection,
"expected type %T, got %T", connectiontypes.ConnectionEnd{}, connectionEnd,
)
}
data := &ConnectionStateData{
Path: []byte(path.String()),
Connection: &connection,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// ChannelStateSignBytes returns the sign bytes for verification of the
// channel state.
func ChannelStateSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
channelEnd exported.ChannelI,
) ([]byte, error) {
dataBz, err := ChannelStateDataBytes(cdc, path, channelEnd)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: CHANNEL,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// ChannelStateDataBytes returns the channel state data bytes used in constructing
// SignBytes.
func ChannelStateDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
channelEnd exported.ChannelI,
) ([]byte, error) {
channel, ok := channelEnd.(channeltypes.Channel)
if !ok {
return nil, sdkerrors.Wrapf(
channeltypes.ErrInvalidChannel,
"expected channel type %T, got %T", channeltypes.Channel{}, channelEnd)
}
data := &ChannelStateData{
Path: []byte(path.String()),
Channel: &channel,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// PacketCommitmentSignBytes returns the sign bytes for verification of the
// packet commitment.
func PacketCommitmentSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
commitmentBytes []byte,
) ([]byte, error) {
dataBz, err := PacketCommitmentDataBytes(cdc, path, commitmentBytes)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: PACKETCOMMITMENT,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// PacketCommitmentDataBytes returns the packet commitment data bytes used in constructing
// SignBytes.
func PacketCommitmentDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
commitmentBytes []byte,
) ([]byte, error) {
data := &PacketCommitmentData{
Path: []byte(path.String()),
Commitment: commitmentBytes,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// PacketAcknowledgementSignBytes returns the sign bytes for verification of
// the acknowledgement.
func PacketAcknowledgementSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
acknowledgement []byte,
) ([]byte, error) {
dataBz, err := PacketAcknowledgementDataBytes(cdc, path, acknowledgement)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: PACKETACKNOWLEDGEMENT,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// PacketAcknowledgementDataBytes returns the packet acknowledgement data bytes used in constructing
// SignBytes.
func PacketAcknowledgementDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
acknowledgement []byte,
) ([]byte, error) {
data := &PacketAcknowledgementData{
Path: []byte(path.String()),
Acknowledgement: acknowledgement,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// PacketReceiptAbsenceSignBytes returns the sign bytes for verification
// of the absence of an receipt.
func PacketReceiptAbsenceSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
) ([]byte, error) {
dataBz, err := PacketReceiptAbsenceDataBytes(cdc, path)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: PACKETRECEIPTABSENCE,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// PacketReceiptAbsenceDataBytes returns the packet receipt absence data bytes
// used in constructing SignBytes.
func PacketReceiptAbsenceDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
) ([]byte, error) {
data := &PacketReceiptAbsenceData{
Path: []byte(path.String()),
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}
// NextSequenceRecvSignBytes returns the sign bytes for verification of the next
// sequence to be received.
func NextSequenceRecvSignBytes(
cdc codec.BinaryCodec,
sequence, timestamp uint64,
diversifier string,
path commitmenttypes.MerklePath,
nextSequenceRecv uint64,
) ([]byte, error) {
dataBz, err := NextSequenceRecvDataBytes(cdc, path, nextSequenceRecv)
if err != nil {
return nil, err
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: diversifier,
DataType: NEXTSEQUENCERECV,
Data: dataBz,
}
return cdc.Marshal(signBytes)
}
// NextSequenceRecvDataBytes returns the next sequence recv data bytes used in constructing
// SignBytes.
func NextSequenceRecvDataBytes(
cdc codec.BinaryCodec,
path commitmenttypes.MerklePath, //nolint:interfacer
nextSequenceRecv uint64,
) ([]byte, error) {
data := &NextSequenceRecvData{
Path: []byte(path.String()),
NextSeqRecv: nextSequenceRecv,
}
dataBz, err := cdc.Marshal(data)
if err != nil {
return nil, err
}
return dataBz, nil
}