-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_funding_complete.go
150 lines (130 loc) · 4.59 KB
/
single_funding_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
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
package lnwire
import (
"bytes"
"fmt"
"io"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
)
// SingleFundingComplete is the message Alice sends to Bob once she is able to
// fully assemble the funding transaction, and both versions of the commitment
// transaction. The purpose of this message is to present Bob with the items
// required for him to generate a signature for Alice's version of the
// commitment transaction.
type SingleFundingComplete struct {
// ChannelID serves to uniquely identify the future channel created by
// the initiated single funder workflow.
// TODO(roasbeef): change all to PendingChannelID, document schema
ChannelID uint64
// FundingOutPoint is the outpoint (txid:index) of the funding
// transaction. With this value, Bob will be able to generate a
// signature for Alice's version of the commitment transaction.
FundingOutPoint *wire.OutPoint
// CommitSignature is Alice's signature for Bob's version of the
// commitment transaction.
CommitSignature *btcec.Signature
// RevocationKey is the initial key to be used for the revocation
// clause within the self-output of the initiators's commitment
// transaction. Once an initial new state is created, the initiator
// will send a pre-image which will allow the initiator to sweep the
// initiator's funds if the violate the contract.
RevocationKey *btcec.PublicKey
}
// NewSingleFundingComplete creates, and returns a new empty
// SingleFundingResponse.
func NewSingleFundingComplete(chanID uint64, fundingPoint *wire.OutPoint,
commitSig *btcec.Signature, revokeKey *btcec.PublicKey) *SingleFundingComplete {
return &SingleFundingComplete{
ChannelID: chanID,
FundingOutPoint: fundingPoint,
CommitSignature: commitSig,
RevocationKey: revokeKey,
}
}
// Decode deserializes the serialized SingleFundingComplete stored in the passed
// io.Reader into the target SingleFundingComplete using the deserialization
// rules defined by the passed protocol version.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingComplete) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// FundingOutPoint (36)
// CommitmentSignature (73)
// RevocationKey (33)
err := readElements(r,
&s.ChannelID,
&s.FundingOutPoint,
&s.CommitSignature,
&s.RevocationKey)
if err != nil {
return err
}
return nil
}
// Encode serializes the target SingleFundingComplete into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed
// protocol version.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error {
// ChannelID (8)
// FundingOutPoint (36)
// Commitment Signature (73)
// RevocationKey (33)
err := writeElements(w,
s.ChannelID,
s.FundingOutPoint,
s.CommitSignature,
s.RevocationKey)
if err != nil {
return err
}
return nil
}
// Command returns the uint32 code which uniquely identifies this message as a
// SingleFundingRequest on the wire.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingComplete) Command() uint32 {
return CmdSingleFundingComplete
}
// MaxPayloadLength returns the maximum allowed payload length for a
// SingleFundingComplete. This is calculated by summing the max length of all
// the fields within a SingleFundingResponse. Therefore, the final breakdown
// is: 8 + 36 + 73 = 150
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingComplete) MaxPayloadLength(uint32) uint32 {
return 150
}
// Validate examines each populated field within the SingleFundingComplete for
// field sanity.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingComplete) Validate() error {
var zeroHash [32]byte
if bytes.Equal(zeroHash[:], s.FundingOutPoint.Hash[:]) {
return fmt.Errorf("funding outpoint hash must be non-zero")
}
if s.CommitSignature == nil {
return fmt.Errorf("commitment signature must be non-nil")
}
// TODO(roasbeef): fin validation
// We're good!
return nil
}
// String returns the string representation of the SingleFundingResponse.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingComplete) String() string {
var rk []byte
if s.RevocationKey != nil {
rk = s.RevocationKey.SerializeCompressed()
}
return fmt.Sprintf("\n--- Begin SingleFundingComplete ---\n") +
fmt.Sprintf("ChannelID:\t\t\t%d\n", s.ChannelID) +
fmt.Sprintf("FundingOutPoint:\t\t\t%x\n", s.FundingOutPoint) +
fmt.Sprintf("CommitSignature\t\t\t\t%x\n", s.CommitSignature) +
fmt.Sprintf("RevocationKey\t\t\t\t%x\n", rk) +
fmt.Sprintf("--- End SingleFundingComplete ---\n")
}