use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256;
+use lightning::ln::PaymentSecret;
+use lightning::routing::network_graph::RoutingFees;
+use lightning::routing::router::{RouteHint, RouteHintHop};
use num_traits::{CheckedAdd, CheckedMul};
use secp256k1::recovery::{RecoveryId, RecoverableSignature};
use secp256k1::key::PublicKey;
-use super::*;
+use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiry, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp,
+ SemanticError, PrivateRoute, Description, RawTaggedField, Currency, RawHrp, SiPrefix, RawInvoice, constants, SignedRawInvoice,
+ RawDataPart, CreationError, InvoiceFeatures};
use self::hrp_sm::parse_hrp;
"tb" => Ok(Currency::BitcoinTestnet),
"bcrt" => Ok(Currency::Regtest),
"sb" => Ok(Currency::Simnet),
+ "tbs" => Ok(Currency::Signet),
_ => Err(ParseError::UnknownCurrency)
}
}
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
- let (hrp, data) = bech32::decode(s)?;
+ let (hrp, data, var) = bech32::decode(s)?;
+
+ if var == bech32::Variant::Bech32m {
+ // Consider Bech32m addresses to be "Invalid Checksum", since that is what we'd get if
+ // we didn't support Bech32m (which lightning does not use).
+ return Err(ParseError::Bech32Error(bech32::Error::InvalidChecksum));
+ }
if data.len() < 104 {
return Err(ParseError::TooShortDataPart);
hrp.as_bytes(),
&data[..data.len()-104]
),
- signature: Signature::from_base32(&data[data.len()-104..])?,
+ signature: InvoiceSignature::from_base32(&data[data.len()-104..])?,
})
}
}
}
}
-impl FromBase32 for Signature {
+impl FromBase32 for InvoiceSignature {
type Err = ParseError;
fn from_base32(signature: &[u5]) -> Result<Self, Self::Err> {
if signature.len() != 104 {
- return Err(ParseError::InvalidSliceLength("Signature::from_base32()".into()));
+ return Err(ParseError::InvalidSliceLength("InvoiceSignature::from_base32()".into()));
}
let recoverable_signature_bytes = Vec::<u8>::from_base32(signature)?;
let signature = &recoverable_signature_bytes[0..64];
let recovery_id = RecoveryId::from_i32(recoverable_signature_bytes[64] as i32)?;
- Ok(Signature(RecoverableSignature::from_compact(
+ Ok(InvoiceSignature(RecoverableSignature::from_compact(
signature,
recovery_id
)?))
}
}
-fn parse_int_be<T, U>(digits: &[U], base: T) -> Option<T>
+pub(crate) fn parse_int_be<T, U>(digits: &[U], base: T) -> Option<T>
where T: CheckedAdd + CheckedMul + From<u8> + Default,
U: Into<u8> + Copy
{
Ok(TaggedField::MinFinalCltvExpiry(MinFinalCltvExpiry::from_base32(field_data)?)),
constants::TAG_FALLBACK =>
Ok(TaggedField::Fallback(Fallback::from_base32(field_data)?)),
- constants::TAG_ROUTE =>
- Ok(TaggedField::Route(Route::from_base32(field_data)?)),
+ constants::TAG_PRIVATE_ROUTE =>
+ Ok(TaggedField::PrivateRoute(PrivateRoute::from_base32(field_data)?)),
constants::TAG_PAYMENT_SECRET =>
Ok(TaggedField::PaymentSecret(PaymentSecret::from_base32(field_data)?)),
+ constants::TAG_FEATURES =>
+ Ok(TaggedField::Features(InvoiceFeatures::from_base32(field_data)?)),
_ => {
// "A reader MUST skip over unknown fields"
Err(ParseError::Skip)
}
}
-impl FromBase32 for PaymentSecret {
- type Err = ParseError;
-
- fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, ParseError> {
- if field_data.len() != 52 {
- Err(ParseError::Skip)
- } else {
- let data_bytes = Vec::<u8>::from_base32(field_data)?;
- let mut payment_secret = [0; 32];
- payment_secret.copy_from_slice(&data_bytes);
- Ok(PaymentSecret(payment_secret))
- }
- }
-}
-
impl FromBase32 for ExpiryTime {
type Err = ParseError;
}
}
-impl FromBase32 for Route {
+impl FromBase32 for PrivateRoute {
type Err = ParseError;
- fn from_base32(field_data: &[u5]) -> Result<Route, ParseError> {
+ fn from_base32(field_data: &[u5]) -> Result<PrivateRoute, ParseError> {
let bytes = Vec::<u8>::from_base32(field_data)?;
if bytes.len() % 51 != 0 {
return Err(ParseError::UnexpectedEndOfTaggedFields);
}
- let mut route_hops = Vec::<RouteHop>::new();
+ let mut route_hops = Vec::<RouteHintHop>::new();
let mut bytes = bytes.as_slice();
while !bytes.is_empty() {
let mut channel_id: [u8; 8] = Default::default();
channel_id.copy_from_slice(&hop_bytes[33..41]);
- let hop = RouteHop {
- pubkey: PublicKey::from_slice(&hop_bytes[0..33])?,
- short_channel_id: channel_id,
- fee_base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
- fee_proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
- cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?")
+ let hop = RouteHintHop {
+ src_node_id: PublicKey::from_slice(&hop_bytes[0..33])?,
+ short_channel_id: parse_int_be(&channel_id, 256).expect("short chan ID slice too big?"),
+ fees: RoutingFees {
+ base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
+ proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
+ },
+ cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?"),
+ htlc_minimum_msat: None,
+ htlc_maximum_msat: None,
};
route_hops.push(hop);
}
- Ok(Route(route_hops))
+ Ok(PrivateRoute(RouteHint(route_hops)))
}
}
assert_eq!("tb".parse::<Currency>(), Ok(Currency::BitcoinTestnet));
assert_eq!("bcrt".parse::<Currency>(), Ok(Currency::Regtest));
assert_eq!("sb".parse::<Currency>(), Ok(Currency::Simnet));
+ assert_eq!("tbs".parse::<Currency>(), Ok(Currency::Signet));
assert_eq!("something_else".parse::<Currency>(), Err(ParseError::UnknownCurrency))
}
#[test]
fn test_parse_route() {
- use RouteHop;
- use ::Route;
+ use lightning::routing::network_graph::RoutingFees;
+ use lightning::routing::router::{RouteHint, RouteHintHop};
+ use ::PrivateRoute;
use bech32::FromBase32;
+ use de::parse_int_be;
let input = from_bech32(
"q20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqa\
fqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzq".as_bytes()
);
- let mut expected = Vec::<RouteHop>::new();
- expected.push(RouteHop {
- pubkey: PublicKey::from_slice(
+ let mut expected = Vec::<RouteHintHop>::new();
+ expected.push(RouteHintHop {
+ src_node_id: PublicKey::from_slice(
&[
0x02u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
][..]
).unwrap(),
- short_channel_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
- fee_base_msat: 1,
- fee_proportional_millionths: 20,
- cltv_expiry_delta: 3
+ short_channel_id: parse_int_be(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 256).expect("short chan ID slice too big?"),
+ fees: RoutingFees {
+ base_msat: 1,
+ proportional_millionths: 20,
+ },
+ cltv_expiry_delta: 3,
+ htlc_minimum_msat: None,
+ htlc_maximum_msat: None
});
- expected.push(RouteHop {
- pubkey: PublicKey::from_slice(
+ expected.push(RouteHintHop {
+ src_node_id: PublicKey::from_slice(
&[
0x03u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
][..]
).unwrap(),
- short_channel_id: [0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a],
- fee_base_msat: 2,
- fee_proportional_millionths: 30,
- cltv_expiry_delta: 4
+ short_channel_id: parse_int_be(&[0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a], 256).expect("short chan ID slice too big?"),
+ fees: RoutingFees {
+ base_msat: 2,
+ proportional_millionths: 30,
+ },
+ cltv_expiry_delta: 4,
+ htlc_minimum_msat: None,
+ htlc_maximum_msat: None
});
- assert_eq!(Route::from_base32(&input), Ok(Route(expected)));
+ assert_eq!(PrivateRoute::from_base32(&input), Ok(PrivateRoute(RouteHint(expected))));
assert_eq!(
- Route::from_base32(&[u5::try_from_u8(0).unwrap(); 40][..]),
+ PrivateRoute::from_base32(&[u5::try_from_u8(0).unwrap(); 40][..]),
Err(ParseError::UnexpectedEndOfTaggedFields)
);
}
#[test]
- fn test_payment_secret_deserialization() {
- use bech32::CheckBase32;
+ fn test_payment_secret_and_features_de_and_ser() {
+ use lightning::ln::features::InvoiceFeatures;
use secp256k1::recovery::{RecoveryId, RecoverableSignature};
use TaggedField::*;
- use {SiPrefix, SignedRawInvoice, Signature, RawInvoice, RawTaggedField, RawHrp, RawDataPart,
+ use {SiPrefix, SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart,
Currency, Sha256, PositiveTimestamp};
- assert_eq!( // BOLT 11 payment secret invoice. The unknown fields are invoice features.
- "lnbc25m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdeessp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygs9q5sqqqqqqqqqqqqqqqpqsq67gye39hfg3zd8rgc80k32tvy9xk2xunwm5lzexnvpx6fd77en8qaq424dxgt56cag2dpt359k3ssyhetktkpqh24jqnjyw6uqd08sgptq44qu".parse(),
- Ok(SignedRawInvoice {
+ // Feature bits 9, 15, and 99 are set.
+ let expected_features = InvoiceFeatures::from_le_bytes(vec![0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8]);
+ let invoice_str = "lnbc25m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdeessp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygs9q5sqqqqqqqqqqqqqqqpqsq67gye39hfg3zd8rgc80k32tvy9xk2xunwm5lzexnvpx6fd77en8qaq424dxgt56cag2dpt359k3ssyhetktkpqh24jqnjyw6uqd08sgptq44qu";
+ let invoice = SignedRawInvoice {
raw_invoice: RawInvoice {
hrp: RawHrp {
currency: Currency::Bitcoin,
).unwrap())).into(),
Description(::Description::new("coffee beans".to_owned()).unwrap()).into(),
PaymentSecret(::PaymentSecret([17; 32])).into(),
- RawTaggedField::UnknownSemantics(vec![5, 0, 20, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 1, 0, 16,
- 0].check_base32().unwrap())],
- }
+ Features(expected_features).into()]}
},
hash: [0xb1, 0x96, 0x46, 0xc3, 0xbc, 0x56, 0x76, 0x1d, 0x20, 0x65, 0x6e, 0x0e, 0x32,
0xec, 0xd2, 0x69, 0x27, 0xb7, 0x62, 0x6e, 0x2a, 0x8b, 0xe6, 0x97, 0x71, 0x9f,
0xf8, 0x7e, 0x44, 0x54, 0x55, 0xb9],
- signature: Signature(RecoverableSignature::from_compact(
+ signature: InvoiceSignature(RecoverableSignature::from_compact(
&[0xd7, 0x90, 0x4c, 0xc4, 0xb7, 0x4a, 0x22, 0x26, 0x9c, 0x68, 0xc1, 0xdf, 0x68,
0xa9, 0x6c, 0x21, 0x4d, 0x65, 0x1b, 0x93, 0x76, 0xe9, 0xf1, 0x64, 0xd3, 0x60,
0x4d, 0xa4, 0xb7, 0xde, 0xcc, 0xce, 0x0e, 0x82, 0xaa, 0xab, 0x4c, 0x85, 0xd3,
0x60, 0x82, 0xea, 0xac, 0x81, 0x39, 0x11, 0xda, 0xe0, 0x1a, 0xf3, 0xc1],
RecoveryId::from_i32(1).unwrap()
).unwrap()),
- })
- )
+ };
+ assert_eq!(invoice_str, invoice.to_string());
+ assert_eq!(
+ invoice_str.parse(),
+ Ok(invoice)
+ );
}
#[test]
fn test_raw_signed_invoice_deserialization() {
use TaggedField::*;
use secp256k1::recovery::{RecoveryId, RecoverableSignature};
- use {SignedRawInvoice, Signature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
+ use {SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
PositiveTimestamp};
assert_eq!(
0x7b, 0x1d, 0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7,
0x83, 0x5d, 0xb2, 0xec, 0xd5, 0x18, 0xe1, 0xc9
],
- signature: Signature(RecoverableSignature::from_compact(
+ signature: InvoiceSignature(RecoverableSignature::from_compact(
& [
0x38u8, 0xec, 0x68, 0x91, 0x34, 0x5e, 0x20, 0x41, 0x45, 0xbe, 0x8a,
0x3a, 0x99, 0xde, 0x38, 0xe9, 0x8a, 0x39, 0xd6, 0xa5, 0x69, 0x43,