-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
bolt4.rs
225 lines (207 loc) · 8.18 KB
/
bolt4.rs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// LNP P2P library, plmeneting both bolt (BOLT) and Bifrost P2P messaging
// system for Lightning network protocol (LNP)
//
// Written in 2020-2022 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.
//! Lightning payment data as they are decrypted from Sphinx onion packet
use std::io;
use amplify::Wrapper;
use bitcoin_scripts::hlc::HashPreimage;
use internet2::presentation::sphinx::SphinxPayload;
use internet2::tlv;
use lightning_encoding::{BigSize, LightningDecode, LightningEncode};
use crate::bolt::ShortChannelId;
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "strict_encoding", derive(NetworkEncode, NetworkDecode))]
pub struct PaymentOnion {
pub realm: HopRealm,
pub amt_to_forward: u64,
pub outgoing_cltv_value: u32,
}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "strict_encoding", derive(NetworkEncode, NetworkDecode))]
pub enum HopRealm {
Legacy(ShortChannelId),
TlvIntermediary(ShortChannelId),
TlvReceiver(Option<PaymentData>),
}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(LightningEncode, LightningDecode)]
#[cfg_attr(feature = "strict_encoding", derive(NetworkEncode, NetworkDecode))]
pub struct PaymentData {
pub payment_secret: HashPreimage,
pub total_msat: u64,
}
// For internal use to simplify Tlv encoding/decoding implementation
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(LightningEncode, LightningDecode)]
#[lightning_encoding(use_tlv)]
#[cfg_attr(
feature = "strict_encoding",
derive(NetworkEncode, NetworkDecode),
network_encoding(use_tlv)
)]
struct TlvPayment {
#[lightning_encoding(tlv = 2)]
#[cfg_attr(feature = "strict_encoding", network_encoding(tlv = 2))]
amt_to_forward: Option<u64>,
#[lightning_encoding(tlv = 4)]
#[cfg_attr(feature = "strict_encoding", network_encoding(tlv = 4))]
outgoing_cltv_value: Option<u32>,
#[lightning_encoding(tlv = 6)]
#[cfg_attr(feature = "strict_encoding", network_encoding(tlv = 6))]
short_channel_id: Option<ShortChannelId>,
#[lightning_encoding(tlv = 8)]
#[cfg_attr(feature = "strict_encoding", network_encoding(tlv = 8))]
payment_data: Option<PaymentData>,
#[lightning_encoding(unknown_tlvs)]
#[cfg_attr(feature = "strict_encoding", network_encoding(unknown_tlvs))]
unknown: tlv::Stream,
}
impl LightningEncode for PaymentOnion {
fn lightning_encode<E: io::Write>(
&self,
mut e: E,
) -> Result<usize, lightning_encoding::Error> {
let tlv = match self.realm {
HopRealm::Legacy(short_channel_id) => {
0u8.lightning_encode(&mut e)?;
short_channel_id.lightning_encode(&mut e)?;
self.amt_to_forward.lightning_encode(&mut e)?;
self.outgoing_cltv_value.lightning_encode(&mut e)?;
e.write_all(&[0u8; 12])?;
return Ok(33);
}
HopRealm::TlvIntermediary(short_channel_id) => TlvPayment {
amt_to_forward: Some(self.amt_to_forward),
outgoing_cltv_value: Some(self.outgoing_cltv_value),
short_channel_id: Some(short_channel_id),
payment_data: None,
unknown: none!(),
},
HopRealm::TlvReceiver(payment_data) => TlvPayment {
amt_to_forward: Some(self.amt_to_forward),
outgoing_cltv_value: Some(self.outgoing_cltv_value),
short_channel_id: None,
payment_data,
unknown: none!(),
},
};
let stream = tlv.lightning_serialize()?;
BigSize::from(stream.len()).lightning_encode(&mut e)?;
e.write_all(&stream)?;
Ok(stream.len())
}
}
impl LightningDecode for PaymentOnion {
fn lightning_decode<D: io::Read>(
mut d: D,
) -> Result<Self, lightning_encoding::Error> {
let len = BigSize::lightning_decode(&mut d)?;
match len.into_inner() {
0 => {
let onion = PaymentOnion {
realm: HopRealm::Legacy(ShortChannelId::lightning_decode(
&mut d,
)?),
amt_to_forward: LightningDecode::lightning_decode(&mut d)?,
outgoing_cltv_value: LightningDecode::lightning_decode(
&mut d,
)?,
};
let mut padding = [0u8; 12];
d.read_exact(&mut padding)?;
Ok(onion)
}
// A single 0x01 byte for length is reserved for future use to
// signal a different payload format. This is safe since no TLV
// value can ever be shorter than 2 bytes. In this case the hop_
// payload_length MUST be defined in the future specification making
// use of this length.
1 => Err(lightning_encoding::Error::DataIntegrityError(s!(
"payment onion with reserved realm=0x01"
))),
len => {
let tlv = TlvPayment::lightning_decode(d.take(len))?;
match (
tlv.amt_to_forward,
tlv.outgoing_cltv_value,
tlv.short_channel_id,
tlv.payment_data,
) {
(None, _, _, _) => {
Err(lightning_encoding::Error::DataIntegrityError(s!(
"payment onion must contain amt_to_forward"
)))
}
(_, None, _, _) => {
Err(lightning_encoding::Error::DataIntegrityError(s!(
"payment onion must contain outgoing_cltv_value"
)))
}
(Some(_), Some(_), Some(_), Some(_)) => {
Err(lightning_encoding::Error::DataIntegrityError(s!(
"payment onion must not contain both \
short_channel_id and payment_data"
)))
}
(
Some(amt_to_forward),
Some(outgoing_cltv_value),
Some(short_channel_id),
None,
) => Ok(PaymentOnion {
realm: HopRealm::TlvIntermediary(short_channel_id),
amt_to_forward,
outgoing_cltv_value,
}),
(
Some(amt_to_forward),
Some(outgoing_cltv_value),
None,
payment_data,
) => Ok(PaymentOnion {
realm: HopRealm::TlvReceiver(payment_data),
amt_to_forward,
outgoing_cltv_value,
}),
}
}
}
}
}
// TODO: Remove Strict encode/decode requirement on SphinxPayload type
impl SphinxPayload for PaymentOnion {
type DecodeError = lightning_encoding::Error;
fn serialized_len(&self) -> usize {
match self.realm {
HopRealm::Legacy(_) => 33,
HopRealm::TlvIntermediary(_) => 27,
HopRealm::TlvReceiver(None) => 27 - 10,
HopRealm::TlvReceiver(Some(_)) => 27 + 32,
}
}
#[inline]
fn encode(&self, writer: impl io::Write) -> Result<usize, io::Error> {
self.lightning_encode(writer).map_err(|err| match err {
lightning_encoding::Error::Io(err) => err.into(),
_ => unreachable!(),
})
}
#[inline]
fn decode(reader: impl io::Read) -> Result<Self, Self::DecodeError>
where
Self: Sized,
{
PaymentOnion::lightning_decode(reader)
}
}