-
Notifications
You must be signed in to change notification settings - Fork 30
/
validation.go
93 lines (83 loc) · 3.34 KB
/
validation.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
package types
import (
"encoding/hex"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// SecretLength is the length for the secret in hex string
SecretLength = 64
// HTLCIDLength is the length for the hash lock in hex string
HTLCIDLength = 64
// HashLockLength is the length for the hash lock in hex string
HashLockLength = 64
// MaxLengthForAddressOnOtherChain is the maximum length for the address on other chains
MaxLengthForAddressOnOtherChain = 128
// MinTimeLock is the minimum time span for HTLC in blocks
MinTimeLock = 50
// MaxTimeLock is the maximum time span for HTLC in blocks
MaxTimeLock = 34560
// MinDenomLength is the min length of the htlt token denom
MinDenomLength = 6
)
// ValidateReceiverOnOtherChain verifies if the receiver on the other chain is legal
func ValidateReceiverOnOtherChain(receiverOnOtherChain string) error {
if len(receiverOnOtherChain) > MaxLengthForAddressOnOtherChain {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "length of the receiver on other chain must be between [0,%d]", MaxLengthForAddressOnOtherChain)
}
return nil
}
// ValidateSenderOnOtherChain verifies if the receiver on the other chain is legal
func ValidateSenderOnOtherChain(senderOnOtherChain string) error {
if len(senderOnOtherChain) > MaxLengthForAddressOnOtherChain {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "length of the sender on other chain must be between [0,%d]", MaxLengthForAddressOnOtherChain)
}
return nil
}
// ValidateAmount verifies whether the given amount is legal
func ValidateAmount(transfer bool, amount sdk.Coins) error {
if transfer && len(amount) != 1 {
return sdkerrors.Wrapf(ErrInvalidAmount, "amount %s must contain exactly one coin", amount.String())
}
if !(amount.IsValid() && amount.IsAllPositive()) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "the transferred amount must be valid")
}
return nil
}
// ValidateID verifies whether the given ID lock is legal
func ValidateID(id string) error {
if len(id) != HTLCIDLength {
return sdkerrors.Wrapf(ErrInvalidID, "length of the htlc id must be %d", HTLCIDLength)
}
if _, err := hex.DecodeString(id); err != nil {
return sdkerrors.Wrapf(ErrInvalidID, "id must be a hex encoded string")
}
return nil
}
// ValidateHashLock verifies whether the given hash lock is legal
func ValidateHashLock(hashLock string) error {
if len(hashLock) != HashLockLength {
return sdkerrors.Wrapf(ErrInvalidHashLock, "length of the hash lock must be %d", HashLockLength)
}
if _, err := hex.DecodeString(hashLock); err != nil {
return sdkerrors.Wrapf(ErrInvalidHashLock, "hash lock must be a hex encoded string")
}
return nil
}
// ValidateTimeLock verifies whether the given time lock is legal
func ValidateTimeLock(timeLock uint64) error {
if timeLock < MinTimeLock || timeLock > MaxTimeLock {
return sdkerrors.Wrapf(ErrInvalidTimeLock, "the time lock must be between [%d,%d]", MinTimeLock, MaxTimeLock)
}
return nil
}
// ValidateSecret verifies whether the given secret is legal
func ValidateSecret(secret string) error {
if len(secret) != SecretLength {
return sdkerrors.Wrapf(ErrInvalidSecret, "length of the secret must be %d", SecretLength)
}
if _, err := hex.DecodeString(secret); err != nil {
return sdkerrors.Wrapf(ErrInvalidSecret, "secret must be a hex encoded string")
}
return nil
}