/
packet.go
84 lines (71 loc) · 2.75 KB
/
packet.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
package types
import (
"strings"
"time"
newsdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
// DefaultRelativePacketTimeoutHeight is the default packet timeout height (in blocks) relative
// to the current block height of the counterparty chain provided by the client state. The
// timeout is disabled when set to 0.
DefaultRelativePacketTimeoutHeight = "0-1000"
// DefaultRelativePacketTimeoutTimestamp is the default packet timeout timestamp (in nanoseconds)
// relative to the current block timestamp of the counterparty chain provided by the client
// state. The timeout is disabled when set to 0. The default is currently set to a 10 minute
// timeout.
// nolint: gosec
DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())
)
// NewNonFungibleTokenPacketData constructs a new NonFungibleTokenPacketData instance
func NewNonFungibleTokenPacketData(
classID string,
classURI string,
tokenIDs []string,
tokenURI []string,
sender string,
receiver string,
) NonFungibleTokenPacketData {
return NonFungibleTokenPacketData{
ClassId: classID,
ClassUri: classURI,
TokenIds: tokenIDs,
TokenUris: tokenURI,
Sender: sender,
Receiver: receiver,
}
}
// ValidateBasic is used for validating the nft transfer.
// NOTE: The addresses formats are not validated as the sender and recipient can have different
// formats defined by their corresponding chains that are not known to IBC.
func (nftpd NonFungibleTokenPacketData) ValidateBasic() error {
if strings.TrimSpace(nftpd.ClassId) == "" {
return newsdkerrors.Wrap(ErrInvalidClassID, "classId cannot be blank")
}
if len(nftpd.TokenIds) == 0 {
return newsdkerrors.Wrap(ErrInvalidTokenID, "tokenId cannot be blank")
}
if len(nftpd.TokenIds) != len(nftpd.TokenUris) {
return newsdkerrors.Wrap(ErrInvalidPacket, "tokenIds and tokenUris lengths do not match")
}
if strings.TrimSpace(nftpd.Sender) == "" {
return newsdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "sender address cannot be blank")
}
// decode the sender address
if _, err := sdk.AccAddressFromBech32(nftpd.Sender); err != nil {
return newsdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid sender address")
}
if strings.TrimSpace(nftpd.Receiver) == "" {
return newsdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "receiver address cannot be blank")
}
// decode the receiver address
if _, err := sdk.AccAddressFromBech32(nftpd.Receiver); err != nil {
return newsdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid receiver address")
}
return nil
}
// GetBytes is a helper for serializing
func (nftpd NonFungibleTokenPacketData) GetBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&nftpd))
}