Skip to content

Commit

Permalink
f - add more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jkczyz committed Jan 18, 2023
1 parent 0c8b3c9 commit 70a9967
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl_writeable!(BlindedPayInfo, {
});

/// Wire representation for an on-chain fallback address.
#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub(super) struct FallbackAddress {
version: WitnessVersion,
program: Vec<u8>,
Expand Down Expand Up @@ -1274,6 +1274,28 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_relative_expiry() {
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with(payment_paths(), payment_hash(), now()).unwrap()
.relative_expiry(3600)
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Invoice::try_from(buffer) {
Ok(invoice) => assert_eq!(invoice.relative_expiry(), Duration::from_secs(3600)),
Err(e) => panic!("error parsing invoice: {:?}", e),
}
}

#[test]
fn parses_invoice_with_payment_hash() {
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
Expand Down Expand Up @@ -1332,6 +1354,79 @@ mod tests {
}
}

#[test]
fn parses_invoice_with_allow_mpp() {
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap()
.respond_with(payment_paths(), payment_hash(), now()).unwrap()
.allow_mpp()
.build().unwrap()
.sign(recipient_sign).unwrap();

let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Invoice::try_from(buffer) {
Ok(invoice) => {
let mut features = Bolt12InvoiceFeatures::empty();
features.set_basic_mpp_optional();
assert_eq!(invoice.features(), &features);
},
Err(e) => panic!("error parsing invoice: {:?}", e),
}
}

#[test]
fn parses_invoice_with_fallback_address() {
let script = Script::new();
let pubkey = bitcoin::util::key::PublicKey::new(recipient_pubkey());
let x_only_pubkey = XOnlyPublicKey::from_keypair(&recipient_keys()).0;
let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(x_only_pubkey);

let offer = OfferBuilder::new("foo".into(), recipient_pubkey())
.amount_msats(1000)
.build().unwrap();
let invoice_request = offer
.request_invoice(vec![1; 32], payer_pubkey()).unwrap()
.build().unwrap()
.sign(payer_sign).unwrap();
let mut unsigned_invoice = invoice_request
.respond_with(payment_paths(), payment_hash(), now()).unwrap()
.fallback_v0_p2wsh(&script.wscript_hash())
.fallback_v0_p2wpkh(&pubkey.wpubkey_hash().unwrap())
.fallback_v1_p2tr_tweaked(&tweaked_pubkey)
.build().unwrap();

// Invalid address will not be included.
let address = FallbackAddress {
version: WitnessVersion::V1,
program: vec![0u8; 40],
};
unsigned_invoice.invoice.fields_mut().fallbacks.get_or_insert_with(Vec::new).push(address);

let invoice = unsigned_invoice.sign(recipient_sign).unwrap();
let mut buffer = Vec::new();
invoice.write(&mut buffer).unwrap();

match Invoice::try_from(buffer) {
Ok(invoice) => {
assert_eq!(
invoice.fallbacks(),
vec![
Address::p2wsh(&script, Network::Bitcoin),
Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(),
Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin),
],
);
},
Err(e) => panic!("error parsing invoice: {:?}", e),
}
}

#[test]
fn parses_invoice_with_node_id() {
let invoice = OfferBuilder::new("foo".into(), recipient_pubkey())
Expand Down

0 comments on commit 70a9967

Please sign in to comment.