forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
close_complete.go
90 lines (76 loc) · 1.95 KB
/
close_complete.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
package lnwire
import (
"fmt"
"io"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire"
)
// CloseComplete ...
type CloseComplete struct {
ReservationID uint64
ResponderCloseSig *btcec.Signature // Requester's Commitment
CloseShaHash *wire.ShaHash // TxID of the Close Tx
}
// Decode ...
func (c *CloseComplete) Decode(r io.Reader, pver uint32) error {
// ReservationID (8)
// ResponderCloseSig (73)
// First byte length then sig
// CloseShaHash (32)
err := readElements(r,
&c.ReservationID,
&c.ResponderCloseSig,
&c.CloseShaHash)
if err != nil {
return err
}
return nil
}
// NewCloseComplete creates a new CloseComplete
func NewCloseComplete() *CloseComplete {
return &CloseComplete{}
}
// Encode serializes the item from the CloseComplete struct
// Writes the data to w
func (c *CloseComplete) Encode(w io.Writer, pver uint32) error {
// ReservationID
// ResponderCloseSig
// CloseShaHash
err := writeElements(w,
c.ReservationID,
c.ResponderCloseSig,
c.CloseShaHash)
if err != nil {
return err
}
return nil
}
// Command ...
func (c *CloseComplete) Command() uint32 {
return CmdCloseComplete
}
// MaxPayloadLength ...
func (c *CloseComplete) MaxPayloadLength(uint32) uint32 {
// 8 + 73 + 32
return 113
}
// Validate makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *CloseComplete) Validate() error {
// We're good!
return nil
}
func (c *CloseComplete) String() string {
var serializedSig []byte
var shaString string
if c.ResponderCloseSig != nil && c.ResponderCloseSig.R != nil {
serializedSig = c.ResponderCloseSig.Serialize()
}
if c.CloseShaHash != nil {
shaString = (*c).CloseShaHash.String()
}
return fmt.Sprintf("\n--- Begin CloseComplete ---\n") +
fmt.Sprintf("ReservationID:\t\t%d\n", c.ReservationID) +
fmt.Sprintf("ResponderCloseSig:\t%x\n", serializedSig) +
fmt.Sprintf("CloseShaHash:\t\t%s\n", shaString) +
fmt.Sprintf("--- End CloseComplete ---\n")
}