forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_funding_open_proof.go
106 lines (94 loc) · 3.32 KB
/
single_funding_open_proof.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
package lnwire
import (
"fmt"
"io"
)
// SingleFundingOpenProof is the message sent by the channel initiator to the
// responder after the previously constructed funding transaction has achieved
// a sufficient number of confirmations. It is the initiator's duty to present
// a proof of an open channel to the responder. Otherwise, responding node may
// be vulernable to a resource exhasution attack wherein the a requesting node
// repeatedly negotiates funding transactions which are never broadcast. If the
// responding node commits resources to watch the chain for each funding
// transaction, then this attack is very cheap for the initiator.
type SingleFundingOpenProof struct {
// ChannelID serves to uniquely identify the future channel created by
// the initiated single funder workflow.
ChannelID uint64
// SpvProof is an merkle proof of the inclusion of the funding
// transaction within a block.
// TODO(roasbeef): spec out format for SPV proof, only of single tx so
// can be compact
SpvProof []byte
}
// NewSingleFundingSignComplete creates a new empty SingleFundingOpenProof
// message.
func NewSingleFundingOpenProof(chanID uint64, spvProof []byte) *SingleFundingOpenProof {
return &SingleFundingOpenProof{
ChannelID: chanID,
SpvProof: spvProof,
}
}
// Decode deserializes the serialized SingleFundingOpenProof stored in the
// passed io.Reader into the target SingleFundingOpenProof using the
// deserialization rules defined by the passed protocol version.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingOpenProof) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// SpvProof (?)
err := readElements(r,
&s.ChannelID,
&s.SpvProof)
if err != nil {
return err
}
return nil
}
// Encode serializes the target SingleFundingOpenProof 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 *SingleFundingOpenProof) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
s.ChannelID,
s.SpvProof)
if err != nil {
return err
}
return nil
}
// Command returns the uint32 code which uniquely identifies this message as a
// SingleFundingSignComplete on the wire.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingOpenProof) Command() uint32 {
return CmdSingleFundingOpenProof
}
// MaxPayloadLength returns the maximum allowed payload length for a
// SingleFundingComplete. This is calculated by summing the max length of all
// the fields within a SingleFundingOpenProof.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingOpenProof) MaxPayloadLength(uint32) uint32 {
// 8 +
return 81
}
// Validate examines each populated field within the SingleFundingOpenProof for
// field sanity.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingOpenProof) Validate() error {
// We're good!
return nil
}
// String returns the string representation of the SingleFundingOpenProof.
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingOpenProof) String() string {
return fmt.Sprintf("\n--- Begin FundingSignComplete ---\n") +
fmt.Sprintf("ChannelID:\t\t%d\n", s.ChannelID) +
fmt.Sprintf("SpvProof\t\t%s\n", s.SpvProof) +
fmt.Sprintf("--- End FundingSignComplete ---\n")
}