-
Notifications
You must be signed in to change notification settings - Fork 45
/
models_chain_links.go
729 lines (602 loc) · 21.2 KB
/
models_chain_links.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package types
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gogo/protobuf/proto"
"github.com/mr-tron/base58"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewChainConfig allows to build a new ChainConfig instance
func NewChainConfig(name string) ChainConfig {
return ChainConfig{
Name: name,
}
}
// Validate checks the validity of the ChainConfig
func (c ChainConfig) Validate() error {
if strings.TrimSpace(c.Name) == "" {
return fmt.Errorf("chain name cannot be empty or blank")
}
if c.Name != strings.ToLower(c.Name) {
return fmt.Errorf("chain name must be lowercase")
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewProof is a constructor function for Proof
//
//nolint:interfacer
func NewProof(pubKey cryptotypes.PubKey, signature Signature, plainText string) Proof {
pubKeyAny, err := codectypes.NewAnyWithValue(pubKey)
if err != nil {
panic("failed to pack public key to any type")
}
signatureAny, err := codectypes.NewAnyWithValue(signature)
if err != nil {
panic("failed to pack signature to any type")
}
return Proof{
PubKey: pubKeyAny,
Signature: signatureAny,
PlainText: plainText,
}
}
// GetSignature returns the Signature associated to this proof
func (p Proof) GetSignature() (Signature, error) {
sigValue := p.Signature.GetCachedValue()
if sigValue == nil {
return nil, fmt.Errorf("nil signature")
}
signature, ok := sigValue.(Signature)
if !ok {
return nil, fmt.Errorf("invalid signature type: %T", sigValue)
}
return signature, nil
}
// Validate checks the validity of the Proof
func (p Proof) Validate() error {
if p.PubKey == nil {
return fmt.Errorf("public key field cannot be nil")
}
if p.Signature == nil {
return fmt.Errorf("signature field cannot be nil")
}
if strings.TrimSpace(p.PlainText) == "" {
return fmt.Errorf("plain text cannot be empty or blank")
}
_, err := hex.DecodeString(p.PlainText)
if err != nil {
return fmt.Errorf("invalid hex-encoded plain text")
}
return nil
}
// Verify verifies the signature using the given plain text and public key.
// It returns and error if something is invalid.
func (p Proof) Verify(cdc codec.BinaryCodec, amino *codec.LegacyAmino, owner string, address AddressData) error {
// Decode the value
value, err := hex.DecodeString(p.PlainText)
if err != nil {
return fmt.Errorf("error while decoding proof text: %s", err)
}
// Get the signature
signature, err := p.GetSignature()
if err != nil {
return err
}
// Validate the signature
err = signature.Validate(cdc, amino, value, owner)
if err != nil {
return fmt.Errorf("invalid signature: %s", err)
}
// Verify the signature
pubKey, err := signature.Verify(cdc, p.PubKey, value)
if err != nil {
return fmt.Errorf("error while verifying the signature: %s", err)
}
// Verify the public key
valid, err := address.VerifyPubKey(pubKey)
if err != nil {
return err
}
if !valid {
return fmt.Errorf("invalid address and public key combination provided")
}
return nil
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (p *Proof) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
err := unpacker.UnpackAny(p.PubKey, &pubKey)
if err != nil {
return err
}
var signatureData Signature
return unpacker.UnpackAny(p.Signature, &signatureData)
}
// --------------------------------------------------------------------------------------------------------------------
// Signature represents a generic signature data
type Signature interface {
proto.Message
// GetValueType returns the type of the signature
GetValueType() (SignatureValueType, error)
// Validate checks the validity of the Signature
Validate(cdc codec.BinaryCodec, amino *codec.LegacyAmino, plainText []byte, owner string) error
// Verify allows to verify this signature using the given public key against the given plain text.
// If the signature is valid, it returns the public key instance used to verify it
Verify(cdc codec.BinaryCodec, pubKey *codectypes.Any, plainText []byte) (cryptotypes.PubKey, error)
}
// --------------------------------------------------------------------------------------------------------------------
// ValidateRawValue tells whether the given value has been properly encoded as a raw value
func ValidateRawValue(value []byte, expectedValue string) error {
if string(value) != expectedValue {
return fmt.Errorf("invalid signed value: expected %s, got %s", expectedValue, value)
}
return nil
}
// ValidateDirectTxValue tells whether the given value has been properly encoded as a Protobuf transaction containing
// the expected value as the memo field value
func ValidateDirectTxValue(value []byte, expectedMemo string, cdc codec.BinaryCodec) error {
// Unmarshal the SignDoc
var signDoc tx.SignDoc
err := cdc.Unmarshal(value, &signDoc)
if err != nil {
return err
}
// Check to make sure the value was a SignDoc. If that's not the case, the two arrays will not match
if !bytes.Equal(value, cdc.MustMarshal(&signDoc)) {
return fmt.Errorf("invalid signed doc")
}
// Get the TxBody
var txBody tx.TxBody
err = cdc.Unmarshal(signDoc.BodyBytes, &txBody)
if err != nil {
return err
}
// Check the memo field
if txBody.Memo != expectedMemo {
return fmt.Errorf("invalid signed memo: expected %s, got %s", expectedMemo, txBody.Memo)
}
return nil
}
// ValidateAminoTxValue tells whether the given value has been properly encoded as an Amino transaction containing
// the expected value as the memo field value
func ValidateAminoTxValue(value []byte, expectedMemo string, cdc *codec.LegacyAmino) error {
// Unmarshal the StdSignDoc
var signDoc legacytx.StdSignDoc
err := cdc.UnmarshalJSON(value, &signDoc)
if err != nil {
return err
}
// Check the memo field
if signDoc.Memo != expectedMemo {
return fmt.Errorf("invalid signed memo: expected %s, got %s", expectedMemo, signDoc.Memo)
}
return nil
}
// ValidatePersonalSignValue tells whether the given value has been properly encoded using the EVM persona_sign specification
func ValidatePersonalSignValue(value []byte, expectedValue string) error {
expectedSignedValue := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(expectedValue), expectedValue)
if string(value) != expectedSignedValue {
return fmt.Errorf("invalid signed value: expected %s but got %s", expectedSignedValue, value)
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
var _ Signature = &SingleSignature{}
// NewSingleSignature returns a new CosmosSignature instance
func NewSingleSignature(valueType SignatureValueType, signature []byte) *SingleSignature {
return &SingleSignature{
ValueType: valueType,
Signature: signature,
}
}
// GetValueType implements CosmosSignature
func (s *SingleSignature) GetValueType() (SignatureValueType, error) {
return s.ValueType, nil
}
// Validate implements Signature
func (s *SingleSignature) Validate(cdc codec.BinaryCodec, amino *codec.LegacyAmino, plainText []byte, owner string) error {
// Validate the signature itself
switch s.ValueType {
case SIGNATURE_VALUE_TYPE_COSMOS_DIRECT:
return ValidateDirectTxValue(plainText, owner, cdc)
case SIGNATURE_VALUE_TYPE_COSMOS_AMINO:
return ValidateAminoTxValue(plainText, owner, amino)
case SIGNATURE_VALUE_TYPE_RAW:
return ValidateRawValue(plainText, owner)
case SIGNATURE_VALUE_TYPE_EVM_PERSONAL_SIGN:
return ValidatePersonalSignValue(plainText, owner)
default:
return fmt.Errorf("invalid signature type: %s", s.ValueType)
}
}
// Verify implements Signature
func (s *SingleSignature) Verify(cdc codec.BinaryCodec, pubKey *codectypes.Any, plainText []byte) (cryptotypes.PubKey, error) {
// Get the pub key
var pubkey cryptotypes.PubKey
err := cdc.UnpackAny(pubKey, &pubkey)
if err != nil {
return nil, fmt.Errorf("failed to unpack the public key")
}
// Verify the signature
if !pubkey.VerifySignature(plainText, s.Signature) {
return nil, fmt.Errorf("failed to verify the signature")
}
return pubkey, nil
}
// --------------------------------------------------------------------------------------------------------------------
var _ Signature = &CosmosMultiSignature{}
// NewCosmosMultiSignature returns a new CosmosMultiSignature instance
func NewCosmosMultiSignature(bitArray *cryptotypes.CompactBitArray, signatures []Signature) *CosmosMultiSignature {
sigsAnys := make([]*codectypes.Any, len(signatures))
for i, sig := range signatures {
sigAny, err := codectypes.NewAnyWithValue(sig)
if err != nil {
panic("failed to pack signature to any type")
}
sigsAnys[i] = sigAny
}
return &CosmosMultiSignature{
BitArray: bitArray,
Signatures: sigsAnys,
}
}
// GetValueType implements CosmosSignature
func (s *CosmosMultiSignature) GetValueType() (SignatureValueType, error) {
signMode := SIGNATURE_VALUE_TYPE_UNSPECIFIED
for i, signature := range s.Signatures {
// Unwrap the signature
cosmosSig, ok := signature.GetCachedValue().(Signature)
if !ok {
return SIGNATURE_VALUE_TYPE_UNSPECIFIED, fmt.Errorf("invalid signature type at index %d: %T", i, cosmosSig)
}
// Get the signature sign mode
signatureSignMode, err := cosmosSig.GetValueType()
if err != nil {
return SIGNATURE_VALUE_TYPE_UNSPECIFIED, err
}
if signatureSignMode == SIGNATURE_VALUE_TYPE_UNSPECIFIED {
return SIGNATURE_VALUE_TYPE_UNSPECIFIED, fmt.Errorf("invalid signature signing mode: %s", signatureSignMode)
}
if signMode != SIGNATURE_VALUE_TYPE_UNSPECIFIED && signMode != signatureSignMode {
return SIGNATURE_VALUE_TYPE_UNSPECIFIED, fmt.Errorf("signature at index %d has different signing mode than others", i)
}
signMode = signatureSignMode
}
return signMode, nil
}
// Validate implements Signature
func (s *CosmosMultiSignature) Validate(cdc codec.BinaryCodec, amino *codec.LegacyAmino, plainText []byte, owner string) error {
signMode, err := s.GetValueType()
if err != nil {
return err
}
// Validate the signature itself
switch signMode {
case SIGNATURE_VALUE_TYPE_RAW:
return ValidateRawValue(plainText, owner)
case SIGNATURE_VALUE_TYPE_COSMOS_DIRECT:
return ValidateDirectTxValue(plainText, owner, cdc)
case SIGNATURE_VALUE_TYPE_COSMOS_AMINO:
return ValidateAminoTxValue(plainText, owner, amino)
default:
return fmt.Errorf("invalid signing mode: %s", signMode)
}
}
// Verify implements Signature
func (s *CosmosMultiSignature) Verify(cdc codec.BinaryCodec, pubKey *codectypes.Any, plainText []byte) (cryptotypes.PubKey, error) {
// Convert the signature data to the Cosmos type
cosmosSigData, err := SignatureToCosmosSignatureData(cdc, s)
if err != nil {
return nil, err
}
// Make sure the sig data is of the correct type
sigData, ok := cosmosSigData.(*signing.MultiSignatureData)
if !ok {
return nil, fmt.Errorf("invalid signature data type: %T", sigData)
}
// Get the pub key
var multiPubkey multisig.PubKey
err = cdc.UnpackAny(pubKey, &multiPubkey)
if err != nil {
return nil, fmt.Errorf("failed to unpack the public key")
}
// Verify the signature
err = multiPubkey.VerifyMultisignature(func(mode signing.SignMode) ([]byte, error) {
return plainText, nil
}, sigData)
if err != nil {
return nil, err
}
return multiPubkey, nil
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (s *CosmosMultiSignature) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
for _, signature := range s.Signatures {
var signatureData Signature
err := unpacker.UnpackAny(signature, &signatureData)
if err != nil {
return err
}
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// SignatureToCosmosSignatureData allows to convert the given Signature to a Cosmos SignatureData instance
// unpacking the proto.Any instance using the given unpacker.
func SignatureToCosmosSignatureData(unpacker codectypes.AnyUnpacker, s Signature) (signing.SignatureData, error) {
switch data := s.(type) {
case *SingleSignature:
return &signing.SingleSignatureData{
Signature: data.Signature,
SignMode: signing.SignMode_SIGN_MODE_UNSPECIFIED, // This can be unknown since we don't use it anyway
}, nil
case *CosmosMultiSignature:
sigs, err := unpackSignatures(unpacker, data.Signatures)
if err != nil {
return nil, err
}
return &signing.MultiSignatureData{
BitArray: data.BitArray,
Signatures: sigs,
}, nil
default:
return nil, fmt.Errorf("signature type not supported: %T", s)
}
}
// CosmosSignatureDataToSignature allows to create a Signature instance from the given Cosmos signature data
func CosmosSignatureDataToSignature(data signing.SignatureData) (Signature, error) {
switch data := data.(type) {
case *signing.SingleSignatureData:
var signatureType SignatureValueType
switch data.SignMode {
case signing.SignMode_SIGN_MODE_DIRECT:
signatureType = SIGNATURE_VALUE_TYPE_COSMOS_DIRECT
case signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON:
signatureType = SIGNATURE_VALUE_TYPE_COSMOS_AMINO
case signing.SignMode_SIGN_MODE_TEXTUAL:
signatureType = SIGNATURE_VALUE_TYPE_RAW
default:
return nil, fmt.Errorf("unsupported signing mode: %s", data.SignMode)
}
return &SingleSignature{
ValueType: signatureType,
Signature: data.Signature,
}, nil
case *signing.MultiSignatureData:
sigAnys := make([]*codectypes.Any, len(data.Signatures))
for i, data := range data.Signatures {
sigData, err := CosmosSignatureDataToSignature(data)
if err != nil {
return nil, err
}
sigAny, err := codectypes.NewAnyWithValue(sigData)
if err != nil {
return nil, err
}
sigAnys[i] = sigAny
}
return &CosmosMultiSignature{
BitArray: data.BitArray,
Signatures: sigAnys,
}, nil
default:
return nil, fmt.Errorf("unexpected case %+v", data)
}
}
// unpackSignatures unpacks the given signatures using the provided unpacker
func unpackSignatures(unpacker codectypes.AnyUnpacker, sigs []*codectypes.Any) ([]signing.SignatureData, error) {
var signatures = make([]signing.SignatureData, len(sigs))
for i, sig := range sigs {
var signature Signature
if err := unpacker.UnpackAny(sig, &signature); err != nil {
return nil, err
}
cosmosSigData, err := SignatureToCosmosSignatureData(unpacker, signature)
if err != nil {
return nil, err
}
signatures[i] = cosmosSigData
}
return signatures, nil
}
// --------------------------------------------------------------------------------------------------------------------
// AddressData is an interface representing a generic external chain address
type AddressData interface {
proto.Message
// Validate checks the validity of the AddressData
Validate() error
// GetValue returns the address value
GetValue() string
// VerifyPubKey verifies that the given public key is associated with this address data
VerifyPubKey(key cryptotypes.PubKey) (bool, error)
}
// --------------------------------------------------------------------------------------------------------------------
var _ AddressData = &Bech32Address{}
// NewBech32Address returns a new Bech32Address instance
func NewBech32Address(value, prefix string) *Bech32Address {
return &Bech32Address{Value: value, Prefix: prefix}
}
// Validate implements AddressData
func (b Bech32Address) Validate() error {
if strings.TrimSpace(b.Value) == "" {
return fmt.Errorf("value cannot be empty or blank")
}
if strings.TrimSpace(b.Prefix) == "" {
return fmt.Errorf("prefix cannot be empty or blank")
}
_, err := sdk.GetFromBech32(b.Value, b.Prefix)
if err != nil {
return fmt.Errorf("invalid Bech32 value or wrong prefix")
}
return nil
}
// GetValue implements AddressData
func (b Bech32Address) GetValue() string {
return b.Value
}
// VerifyPubKey implements AddressData
func (b Bech32Address) VerifyPubKey(key cryptotypes.PubKey) (bool, error) {
_, bz, err := bech32.DecodeAndConvert(b.Value)
if err != nil {
return false, err
}
return bytes.Equal(bz, key.Address().Bytes()), nil
}
// --------------------------------------------------------------------------------------------------------------------
var _ AddressData = &Base58Address{}
// NewBase58Address returns a new Base58Address instance
func NewBase58Address(value string) *Base58Address {
return &Base58Address{Value: value}
}
// Validate implements AddressData
func (b Base58Address) Validate() error {
if strings.TrimSpace(b.Value) == "" {
return fmt.Errorf("address cannot be empty or blank")
}
if _, err := base58.Decode(b.Value); err != nil {
return fmt.Errorf("invalid Base58 address")
}
return nil
}
// GetValue implements AddressData
func (b Base58Address) GetValue() string {
return b.Value
}
// VerifyPubKey implements AddressData
func (b Base58Address) VerifyPubKey(key cryptotypes.PubKey) (bool, error) {
bz, err := base58.Decode(b.Value)
return bytes.Equal(tmhash.SumTruncated(bz), key.Address().Bytes()), err
}
// --------------------------------------------------------------------------------------------------------------------
var _ AddressData = &HexAddress{}
// NewHexAddress returns a new HexAddress instance
func NewHexAddress(value, prefix string) *HexAddress {
return &HexAddress{Value: value, Prefix: prefix}
}
// Validate implements AddressData
func (h HexAddress) Validate() error {
if strings.TrimSpace(h.Value) == "" {
return fmt.Errorf("value cannot be empty or blank")
}
if len(h.Value) <= len(h.Prefix) {
return fmt.Errorf("address cannot be smaller than prefix")
}
prefix, addrWithoutPrefix := h.Value[:len(h.Prefix)], h.Value[len(h.Prefix):]
if prefix != h.Prefix {
return fmt.Errorf("prefix does not match")
}
if _, err := hex.DecodeString(addrWithoutPrefix); err != nil {
return fmt.Errorf("invalid hex address")
}
return nil
}
// GetValue implements AddressData
func (h HexAddress) GetValue() string {
return h.Value
}
// VerifyPubKey implements AddressData
func (h HexAddress) VerifyPubKey(key cryptotypes.PubKey) (bool, error) {
addr := h.Value[len(h.Prefix):]
bz, err := hex.DecodeString(addr)
if err != nil {
return false, err
}
pubKey, err := btcec.ParsePubKey(key.Bytes())
if err != nil {
return false, err
}
uncompressedPubKey := pubKey.SerializeUncompressed()
return bytes.Equal(crypto.Keccak256(uncompressedPubKey[1:])[12:], bz), err
}
// --------------------------------------------------------------------------------------------------------------------
// UnpackAddressData deserializes the given any type value as an address data using the provided unpacker
func UnpackAddressData(unpacker codectypes.AnyUnpacker, addressAny *codectypes.Any) (AddressData, error) {
var address AddressData
if err := unpacker.UnpackAny(addressAny, &address); err != nil {
return nil, err
}
return address, nil
}
// --------------------------------------------------------------------------------------------------------------------
// NewChainLink returns a new ChainLink instance
//
//nolint:interfacer
func NewChainLink(user string, address AddressData, proof Proof, chainConfig ChainConfig, creationTime time.Time) ChainLink {
addressAny, err := codectypes.NewAnyWithValue(address)
if err != nil {
panic("failed to pack address data to any type")
}
return ChainLink{
User: user,
Address: addressAny,
Proof: proof,
ChainConfig: chainConfig,
CreationTime: creationTime,
}
}
// GetAddressData returns the AddressData associated with this chain link
func (link ChainLink) GetAddressData() AddressData {
return link.Address.GetCachedValue().(AddressData)
}
// Validate checks the validity of the ChainLink
func (link ChainLink) Validate() error {
if _, err := sdk.AccAddressFromBech32(link.User); err != nil {
return fmt.Errorf("invalid creator address: %s", link.User)
}
if link.Address == nil {
return fmt.Errorf("address cannot be nil")
}
err := link.Proof.Validate()
if err != nil {
return err
}
err = link.ChainConfig.Validate()
if err != nil {
return err
}
if link.CreationTime.IsZero() {
return fmt.Errorf("creation time cannot be zero")
}
return nil
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (link *ChainLink) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if link.Address != nil {
var address AddressData
err := unpacker.UnpackAny(link.Address, &address)
if err != nil {
return err
}
}
err := link.Proof.UnpackInterfaces(unpacker)
if err != nil {
return err
}
return nil
}
// MustMarshalChainLink serializes the given chain link using the provided BinaryCodec
func MustMarshalChainLink(cdc codec.BinaryCodec, link ChainLink) []byte {
return cdc.MustMarshal(&link)
}
// MustUnmarshalChainLink deserializes the given byte array as a chain link using
// the provided BinaryCodec
func MustUnmarshalChainLink(codec codec.BinaryCodec, bz []byte) ChainLink {
var link ChainLink
codec.MustUnmarshal(bz, &link)
return link
}