+
+ #[test]
+ fn parses_invoice_with_payment_paths() {
+ 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+
+ let mut buffer = Vec::new();
+ invoice.write(&mut buffer).unwrap();
+
+ if let Err(e) = Invoice::try_from(buffer) {
+ panic!("error parsing invoice: {:?}", e);
+ }
+
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.paths = None;
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPaths)),
+ }
+
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.blindedpay = None;
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)),
+ }
+
+ let empty_payment_paths = vec![];
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.paths = Some(Iterable(empty_payment_paths.iter().map(|(path, _)| path)));
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPaths)),
+ }
+
+ let mut payment_paths = payment_paths();
+ payment_paths.pop();
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.blindedpay = Some(Iterable(payment_paths.iter().map(|(_, payinfo)| payinfo)));
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)),
+ }
+ }
+
+ #[test]
+ fn parses_invoice_with_created_at() {
+ 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+
+ let mut buffer = Vec::new();
+ invoice.write(&mut buffer).unwrap();
+
+ if let Err(e) = Invoice::try_from(buffer) {
+ panic!("error parsing invoice: {:?}", e);
+ }
+
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.created_at = None;
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => {
+ assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingCreationTime));
+ },
+ }
+ }
+
+ #[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_no_std(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())
+ .amount_msats(1000)
+ .build().unwrap()
+ .request_invoice(vec![1; 32], payer_pubkey()).unwrap()
+ .build().unwrap()
+ .sign(payer_sign).unwrap()
+ .respond_with_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+
+ let mut buffer = Vec::new();
+ invoice.write(&mut buffer).unwrap();
+
+ if let Err(e) = Invoice::try_from(buffer) {
+ panic!("error parsing invoice: {:?}", e);
+ }
+
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.payment_hash = None;
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => {
+ assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingPaymentHash));
+ },
+ }
+ }
+
+ #[test]
+ fn parses_invoice_with_amount() {
+ 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+
+ let mut buffer = Vec::new();
+ invoice.write(&mut buffer).unwrap();
+
+ if let Err(e) = Invoice::try_from(buffer) {
+ panic!("error parsing invoice: {:?}", e);
+ }
+
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.amount = None;
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingAmount)),
+ }
+ }
+
+ #[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_no_std(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_no_std(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();
+
+ // Only standard addresses will be included.
+ let fallbacks = unsigned_invoice.invoice.fields_mut().fallbacks.as_mut().unwrap();
+ // Non-standard addresses
+ fallbacks.push(FallbackAddress { version: 1, program: vec![0u8; 41] });
+ fallbacks.push(FallbackAddress { version: 2, program: vec![0u8; 1] });
+ fallbacks.push(FallbackAddress { version: 17, program: vec![0u8; 40] });
+ // Standard address
+ fallbacks.push(FallbackAddress { version: 1, program: vec![0u8; 33] });
+ fallbacks.push(FallbackAddress { version: 2, program: vec![0u8; 40] });
+
+ 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),
+ Address {
+ payload: Payload::WitnessProgram {
+ version: WitnessVersion::V1,
+ program: vec![0u8; 33],
+ },
+ network: Network::Bitcoin,
+ },
+ Address {
+ payload: Payload::WitnessProgram {
+ version: WitnessVersion::V2,
+ program: vec![0u8; 40],
+ },
+ network: Network::Bitcoin,
+ },
+ ],
+ );
+ },
+ Err(e) => panic!("error parsing invoice: {:?}", e),
+ }
+ }
+
+ #[test]
+ fn parses_invoice_with_node_id() {
+ 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+
+ let mut buffer = Vec::new();
+ invoice.write(&mut buffer).unwrap();
+
+ if let Err(e) = Invoice::try_from(buffer) {
+ panic!("error parsing invoice: {:?}", e);
+ }
+
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.node_id = None;
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => {
+ assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey));
+ },
+ }
+
+ let invalid_pubkey = payer_pubkey();
+ let mut tlv_stream = invoice.as_tlv_stream();
+ tlv_stream.3.node_id = Some(&invalid_pubkey);
+
+ match Invoice::try_from(tlv_stream.to_bytes()) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => {
+ assert_eq!(e, ParseError::InvalidSemantics(SemanticError::InvalidSigningPubkey));
+ },
+ }
+ }
+
+ #[test]
+ fn fails_parsing_invoice_without_signature() {
+ let mut buffer = Vec::new();
+ 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .invoice
+ .write(&mut buffer).unwrap();
+
+ match Invoice::try_from(buffer) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::InvalidSemantics(SemanticError::MissingSignature)),
+ }
+ }
+
+ #[test]
+ fn fails_parsing_invoice_with_invalid_signature() {
+ let mut 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+ let last_signature_byte = invoice.bytes.last_mut().unwrap();
+ *last_signature_byte = last_signature_byte.wrapping_add(1);
+
+ let mut buffer = Vec::new();
+ invoice.write(&mut buffer).unwrap();
+
+ match Invoice::try_from(buffer) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => {
+ assert_eq!(e, ParseError::InvalidSignature(secp256k1::Error::InvalidSignature));
+ },
+ }
+ }
+
+ #[test]
+ fn fails_parsing_invoice_with_extra_tlv_records() {
+ 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_no_std(payment_paths(), payment_hash(), now()).unwrap()
+ .build().unwrap()
+ .sign(recipient_sign).unwrap();
+
+ let mut encoded_invoice = Vec::new();
+ invoice.write(&mut encoded_invoice).unwrap();
+ BigSize(1002).write(&mut encoded_invoice).unwrap();
+ BigSize(32).write(&mut encoded_invoice).unwrap();
+ [42u8; 32].write(&mut encoded_invoice).unwrap();
+
+ match Invoice::try_from(encoded_invoice) {
+ Ok(_) => panic!("expected error"),
+ Err(e) => assert_eq!(e, ParseError::Decode(DecodeError::InvalidValue)),
+ }
+ }