forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htlc_addrequest.go
130 lines (111 loc) · 3.03 KB
/
htlc_addrequest.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
package lnwire
import (
"fmt"
"io"
)
// HTLCAddRequest ...
// Multiple Clearing Requests are possible by putting this inside an array of
// clearing requests
type HTLCAddRequest struct {
// We can use a different data type for this if necessary...
ChannelID uint64
// ID of this request
HTLCKey HTLCKey
// When the HTLC expires
Expiry uint32
// Amount to pay in the hop
// Difference between hop and first item in blob is the fee to complete
Amount CreditsAmount
// RefundContext is for payment cancellation
// TODO (j): not currently in use, add later
RefundContext HTLCKey
// Contract Type
// first 4 bits is n, second for is m, in n-of-m "multisig"
ContractType uint8
// Redemption Hashes
RedemptionHashes []*[20]byte
// Data to parse&pass on to the next node
// Eventually, we need to make this into a group of 2 nested structs?
Blob []byte
}
// Decode ...
func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// HTLCKey(8)
// Expiry(4)
// Amount(4)
// ContractType(1)
// RedemptionHashes (numOfHashes * 20 + numOfHashes)
// Blob(2+blobsize)
err := readElements(r,
&c.ChannelID,
&c.HTLCKey,
&c.Expiry,
&c.Amount,
&c.ContractType,
&c.RedemptionHashes,
&c.Blob,
)
if err != nil {
return err
}
return nil
}
// NewHTLCAddRequest creates a new HTLCAddRequest
func NewHTLCAddRequest() *HTLCAddRequest {
return &HTLCAddRequest{}
}
// Encode serializes the item from the HTLCAddRequest struct
// Writes the data to w
func (c *HTLCAddRequest) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.HTLCKey,
c.Expiry,
c.Amount,
c.ContractType,
c.RedemptionHashes,
c.Blob,
)
if err != nil {
return err
}
return nil
}
// Command ...
func (c *HTLCAddRequest) Command() uint32 {
return CmdHTLCAddRequest
}
// MaxPayloadLength ...
func (c *HTLCAddRequest) MaxPayloadLength(uint32) uint32 {
// base size ~110, but blob can be variable.
// shouldn't be bigger than 8K though...
return 8192
}
// Validate makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *HTLCAddRequest) Validate() error {
if c.Amount < 0 {
// While fees can be negative, it's too confusing to allow
// negative payments. Maybe for some wallets, but not this one!
return fmt.Errorf("Amount paid cannot be negative.")
}
// We're good!
return nil
}
func (c *HTLCAddRequest) String() string {
var redemptionHashes string
for i, rh := range c.RedemptionHashes {
redemptionHashes += fmt.Sprintf("\n\tSlice\t%d\n", i)
redemptionHashes += fmt.Sprintf("\t\tRedemption Hash: %x\n", *rh)
}
return fmt.Sprintf("\n--- Begin HTLCAddRequest ---\n") +
fmt.Sprintf("ChannelID:\t%d\n", c.ChannelID) +
fmt.Sprintf("HTLCKey:\t%d\n", c.HTLCKey) +
fmt.Sprintf("Expiry:\t\t%d\n", c.Expiry) +
fmt.Sprintf("Amount\t\t%d\n", c.Amount) +
fmt.Sprintf("ContractType:\t%d (%b)\n", c.ContractType, c.ContractType) +
fmt.Sprintf("RedemptionHashes:") +
redemptionHashes +
fmt.Sprintf("Blob:\t\t\t\t%x\n", c.Blob) +
fmt.Sprintf("--- End HTLCAddRequest ---\n")
}