Skip to content

Commit 70a9967

Browse files
committed
f - add more test coverage
1 parent 0c8b3c9 commit 70a9967

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

lightning/src/offers/invoice.rs

+96-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ impl_writeable!(BlindedPayInfo, {
581581
});
582582

583583
/// Wire representation for an on-chain fallback address.
584-
#[derive(Debug, PartialEq)]
584+
#[derive(Clone, Debug, PartialEq)]
585585
pub(super) struct FallbackAddress {
586586
version: WitnessVersion,
587587
program: Vec<u8>,
@@ -1274,6 +1274,28 @@ mod tests {
12741274
}
12751275
}
12761276

1277+
#[test]
1278+
fn parses_invoice_with_relative_expiry() {
1279+
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
1280+
.amount_msats(1000)
1281+
.build().unwrap()
1282+
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1283+
.build().unwrap()
1284+
.sign(payer_sign).unwrap()
1285+
.respond_with(payment_paths(), payment_hash(), now()).unwrap()
1286+
.relative_expiry(3600)
1287+
.build().unwrap()
1288+
.sign(recipient_sign).unwrap();
1289+
1290+
let mut buffer = Vec::new();
1291+
invoice.write(&mut buffer).unwrap();
1292+
1293+
match Invoice::try_from(buffer) {
1294+
Ok(invoice) => assert_eq!(invoice.relative_expiry(), Duration::from_secs(3600)),
1295+
Err(e) => panic!("error parsing invoice: {:?}", e),
1296+
}
1297+
}
1298+
12771299
#[test]
12781300
fn parses_invoice_with_payment_hash() {
12791301
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
@@ -1332,6 +1354,79 @@ mod tests {
13321354
}
13331355
}
13341356

1357+
#[test]
1358+
fn parses_invoice_with_allow_mpp() {
1359+
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
1360+
.amount_msats(1000)
1361+
.build().unwrap()
1362+
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1363+
.build().unwrap()
1364+
.sign(payer_sign).unwrap()
1365+
.respond_with(payment_paths(), payment_hash(), now()).unwrap()
1366+
.allow_mpp()
1367+
.build().unwrap()
1368+
.sign(recipient_sign).unwrap();
1369+
1370+
let mut buffer = Vec::new();
1371+
invoice.write(&mut buffer).unwrap();
1372+
1373+
match Invoice::try_from(buffer) {
1374+
Ok(invoice) => {
1375+
let mut features = Bolt12InvoiceFeatures::empty();
1376+
features.set_basic_mpp_optional();
1377+
assert_eq!(invoice.features(), &features);
1378+
},
1379+
Err(e) => panic!("error parsing invoice: {:?}", e),
1380+
}
1381+
}
1382+
1383+
#[test]
1384+
fn parses_invoice_with_fallback_address() {
1385+
let script = Script::new();
1386+
let pubkey = bitcoin::util::key::PublicKey::new(recipient_pubkey());
1387+
let x_only_pubkey = XOnlyPublicKey::from_keypair(&recipient_keys()).0;
1388+
let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(x_only_pubkey);
1389+
1390+
let offer = OfferBuilder::new("foo".into(), recipient_pubkey())
1391+
.amount_msats(1000)
1392+
.build().unwrap();
1393+
let invoice_request = offer
1394+
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
1395+
.build().unwrap()
1396+
.sign(payer_sign).unwrap();
1397+
let mut unsigned_invoice = invoice_request
1398+
.respond_with(payment_paths(), payment_hash(), now()).unwrap()
1399+
.fallback_v0_p2wsh(&script.wscript_hash())
1400+
.fallback_v0_p2wpkh(&pubkey.wpubkey_hash().unwrap())
1401+
.fallback_v1_p2tr_tweaked(&tweaked_pubkey)
1402+
.build().unwrap();
1403+
1404+
// Invalid address will not be included.
1405+
let address = FallbackAddress {
1406+
version: WitnessVersion::V1,
1407+
program: vec![0u8; 40],
1408+
};
1409+
unsigned_invoice.invoice.fields_mut().fallbacks.get_or_insert_with(Vec::new).push(address);
1410+
1411+
let invoice = unsigned_invoice.sign(recipient_sign).unwrap();
1412+
let mut buffer = Vec::new();
1413+
invoice.write(&mut buffer).unwrap();
1414+
1415+
match Invoice::try_from(buffer) {
1416+
Ok(invoice) => {
1417+
assert_eq!(
1418+
invoice.fallbacks(),
1419+
vec![
1420+
Address::p2wsh(&script, Network::Bitcoin),
1421+
Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(),
1422+
Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin),
1423+
],
1424+
);
1425+
},
1426+
Err(e) => panic!("error parsing invoice: {:?}", e),
1427+
}
1428+
}
1429+
13351430
#[test]
13361431
fn parses_invoice_with_node_id() {
13371432
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())

0 commit comments

Comments
 (0)