+ #[test]
+ fn test_check_feature_bits() {
+ use crate::TaggedField::*;
+ use lightning::ln::features::Bolt11InvoiceFeatures;
+ use secp256k1::Secp256k1;
+ use secp256k1::SecretKey;
+ use crate::{Bolt11Invoice, RawBolt11Invoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp,
+ Bolt11SemanticError};
+
+ let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
+ let payment_secret = lightning::ln::PaymentSecret([21; 32]);
+ let invoice_template = RawBolt11Invoice {
+ hrp: RawHrp {
+ currency: Currency::Bitcoin,
+ raw_amount: None,
+ si_prefix: None,
+ },
+ data: RawDataPart {
+ timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
+ tagged_fields: vec ! [
+ PaymentHash(Sha256(sha256::Hash::from_hex(
+ "0001020304050607080900010203040506070809000102030405060708090102"
+ ).unwrap())).into(),
+ Description(
+ crate::Description::new(
+ "Please consider supporting this project".to_owned()
+ ).unwrap()
+ ).into(),
+ ],
+ },
+ };
+
+ // Missing features
+ let invoice = {
+ let mut invoice = invoice_template.clone();
+ invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert_eq!(Bolt11Invoice::from_signed(invoice), Err(Bolt11SemanticError::InvalidFeatures));
+
+ // Missing feature bits
+ let invoice = {
+ let mut invoice = invoice_template.clone();
+ invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
+ invoice.data.tagged_fields.push(Features(Bolt11InvoiceFeatures::empty()).into());
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert_eq!(Bolt11Invoice::from_signed(invoice), Err(Bolt11SemanticError::InvalidFeatures));
+
+ let mut payment_secret_features = Bolt11InvoiceFeatures::empty();
+ payment_secret_features.set_payment_secret_required();
+
+ // Including payment secret and feature bits
+ let invoice = {
+ let mut invoice = invoice_template.clone();
+ invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
+ invoice.data.tagged_fields.push(Features(payment_secret_features.clone()).into());
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert!(Bolt11Invoice::from_signed(invoice).is_ok());
+
+ // No payment secret or features
+ let invoice = {
+ let invoice = invoice_template.clone();
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert_eq!(Bolt11Invoice::from_signed(invoice), Err(Bolt11SemanticError::NoPaymentSecret));
+
+ // No payment secret or feature bits
+ let invoice = {
+ let mut invoice = invoice_template.clone();
+ invoice.data.tagged_fields.push(Features(Bolt11InvoiceFeatures::empty()).into());
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert_eq!(Bolt11Invoice::from_signed(invoice), Err(Bolt11SemanticError::NoPaymentSecret));
+
+ // Missing payment secret
+ let invoice = {
+ let mut invoice = invoice_template.clone();
+ invoice.data.tagged_fields.push(Features(payment_secret_features).into());
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert_eq!(Bolt11Invoice::from_signed(invoice), Err(Bolt11SemanticError::NoPaymentSecret));
+
+ // Multiple payment secrets
+ let invoice = {
+ let mut invoice = invoice_template;
+ invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
+ invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
+ invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
+ }.unwrap();
+ assert_eq!(Bolt11Invoice::from_signed(invoice), Err(Bolt11SemanticError::MultiplePaymentSecrets));
+ }
+