-
Notifications
You must be signed in to change notification settings - Fork 31
/
htlc.go
159 lines (149 loc) · 4.98 KB
/
htlc.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
package types
import (
fmt "fmt"
time "time"
"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewHTLC constructs a new HTLC instance
func NewHTLC(
id tmbytes.HexBytes,
sender sdk.AccAddress,
to sdk.AccAddress,
receiverOnOtherChain string,
senderOnOtherChain string,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
secret tmbytes.HexBytes,
timestamp uint64,
expirationHeight uint64,
state HTLCState,
closedBlock uint64,
transfer bool,
direction SwapDirection,
) HTLC {
return HTLC{
Id: id.String(),
Sender: sender.String(),
To: to.String(),
ReceiverOnOtherChain: receiverOnOtherChain,
SenderOnOtherChain: senderOnOtherChain,
Amount: amount,
HashLock: hashLock.String(),
Secret: secret.String(),
Timestamp: timestamp,
ExpirationHeight: expirationHeight,
State: state,
ClosedBlock: closedBlock,
Transfer: transfer,
Direction: direction,
}
}
// Validate validates the HTLC
func (h HTLC) Validate() error {
if err := ValidateID(h.Id); err != nil {
return err
}
if err := ValidateHashLock(h.HashLock); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(h.Sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if _, err := sdk.AccAddressFromBech32(h.To); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address (%s)", err)
}
if err := ValidateReceiverOnOtherChain(h.ReceiverOnOtherChain); err != nil {
return err
}
if err := ValidateSenderOnOtherChain(h.SenderOnOtherChain); err != nil {
return err
}
if h.ExpirationHeight == 0 {
return sdkerrors.Wrapf(ErrInvalidExpirationHeight, "expire height cannot be 0")
}
if h.Timestamp == 0 {
return sdkerrors.Wrapf(ErrInvalidTimestamp, "timestamp cannot be 0")
}
if err := ValidateAmount(h.Transfer, h.Amount); err != nil {
return err
}
if h.State > Refunded {
return sdkerrors.Wrapf(ErrInvalidState, "invalid htlc status")
}
if h.State == Completed && h.ClosedBlock == 0 {
return sdkerrors.Wrapf(ErrInvalidClosedBlock, "closed block cannot be 0")
}
if !h.Transfer && h.Direction != 0 {
return sdkerrors.Wrapf(ErrInvalidDirection, "invalid htlc direction")
}
if h.Transfer && (h.Direction < Incoming || h.Direction > Outgoing) {
return sdkerrors.Wrapf(ErrInvalidDirection, "invalid htlt direction")
}
if h.State != Completed && len(h.Secret) > 0 {
return sdkerrors.Wrapf(ErrInvalidSecret, "secret must be empty when the HTLC has not be claimed")
}
if h.State == Completed && len(h.Secret) != SecretLength {
return sdkerrors.Wrapf(ErrInvalidSecret, "length of the secret must be %d", SecretLength)
}
return nil
}
// NewAssetSupply constructs a new AssetSupply instance
func NewAssetSupply(
incomingSupply sdk.Coin,
outgoingSupply sdk.Coin,
currentSupply sdk.Coin,
timeLimitedCurrentSupply sdk.Coin,
timeElapsed time.Duration,
) AssetSupply {
return AssetSupply{
IncomingSupply: incomingSupply,
OutgoingSupply: outgoingSupply,
CurrentSupply: currentSupply,
TimeLimitedCurrentSupply: timeLimitedCurrentSupply,
TimeElapsed: timeElapsed,
}
}
// DefaultAssetSupplies gets the raw asset supplies for testing
func DefaultAssetSupplies() []AssetSupply {
return []AssetSupply{}
}
// Validate performs a basic validation of an asset supply fields.
func (a AssetSupply) Validate() error {
if !a.IncomingSupply.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "incoming supply %s", a.IncomingSupply)
}
if !a.OutgoingSupply.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "outgoing supply %s", a.OutgoingSupply)
}
if !a.CurrentSupply.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "current supply %s", a.CurrentSupply)
}
if !a.TimeLimitedCurrentSupply.IsValid() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "time-limited current supply %s", a.CurrentSupply)
}
denom := a.CurrentSupply.Denom
if (a.IncomingSupply.Denom != denom) || (a.OutgoingSupply.Denom != denom) || (a.TimeLimitedCurrentSupply.Denom != denom) {
return fmt.Errorf("asset supply denoms do not match %s %s %s %s", a.CurrentSupply.Denom, a.IncomingSupply.Denom, a.OutgoingSupply.Denom, a.TimeLimitedCurrentSupply.Denom)
}
return nil
}
// GetHashLock calculates the hash lock from the given secret and timestamp
func GetHashLock(secret tmbytes.HexBytes, timestamp uint64) []byte {
if timestamp > 0 {
return tmhash.Sum(append(secret, sdk.Uint64ToBigEndian(timestamp)...))
}
return tmhash.Sum(secret)
}
func GetID(
sender sdk.AccAddress,
to sdk.AccAddress,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
) tmbytes.HexBytes {
return tmhash.Sum(
append(append(append(hashLock, sender...), to...), []byte(amount.Sort().String())...),
)
}