-
Notifications
You must be signed in to change notification settings - Fork 45
/
models_packets.go
66 lines (56 loc) · 1.7 KB
/
models_packets.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
// Package types
// nolint: interfacer
package types
import (
"fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewLinkChainAccountPacketData returns a new LinkChainAccountPacketData instance
func NewLinkChainAccountPacketData(
sourceAddress AddressData,
sourceProof Proof,
sourceChainConfig ChainConfig,
destinationAddress string,
destinationProof Proof,
) LinkChainAccountPacketData {
addressAny, err := codectypes.NewAnyWithValue(sourceAddress)
if err != nil {
panic("failed to pack public key to any type")
}
return LinkChainAccountPacketData{
SourceAddress: addressAny,
SourceProof: sourceProof,
SourceChainConfig: sourceChainConfig,
DestinationAddress: destinationAddress,
DestinationProof: destinationProof,
}
}
// Validate validates the LinkChainAccountPacketData
func (p LinkChainAccountPacketData) Validate() error {
if p.SourceAddress == nil {
return fmt.Errorf("source address cannot be nil")
}
err := p.SourceProof.Validate()
if err != nil {
return fmt.Errorf("invalid source proof: %s", err)
}
err = p.SourceChainConfig.Validate()
if err != nil {
return fmt.Errorf("invalid source chain config: %s", err)
}
_, err = sdk.AccAddressFromBech32(p.DestinationAddress)
if err != nil {
return fmt.Errorf("invalid destination address: %s", p.DestinationAddress)
}
err = p.DestinationProof.Validate()
if err != nil {
return fmt.Errorf("invalid destination proof: %s", err)
}
return nil
}
// GetBytes is a helper for serialising
func (p LinkChainAccountPacketData) GetBytes() ([]byte, error) {
var modulePacket LinkChainAccountPacketData
return sdk.SortJSON(ModuleCdc.MustMarshalJSON(&modulePacket))
}