`rustfmt`: `lightning-transaction-sync/tests/integration_tests.rs`
[rust-lightning] / lightning-invoice / src / lib.rs
index 6f21af72dbed019a8af1caf214c0d6a0f3ab9677..e427bf3ccb99965a4ba50d8609c3dad186cf3658 100644 (file)
@@ -31,7 +31,6 @@ pub mod utils;
 
 extern crate bech32;
 #[macro_use] extern crate lightning;
-extern crate num_traits;
 extern crate secp256k1;
 extern crate alloc;
 #[cfg(any(test, feature = "std"))]
@@ -43,8 +42,8 @@ extern crate serde;
 use std::time::SystemTime;
 
 use bech32::u5;
-use bitcoin::{Address, Network, PubkeyHash, ScriptHash};
-use bitcoin::address::{Payload, WitnessProgram, WitnessVersion};
+use bitcoin::{Address, Network, PubkeyHash, ScriptHash, WitnessProgram, WitnessVersion};
+use bitcoin::address::Payload;
 use bitcoin::hashes::{Hash, sha256};
 use lightning::ln::features::Bolt11InvoiceFeatures;
 use lightning::util::invoice::construct_invoice_preimage;
@@ -66,7 +65,7 @@ use core::str;
 use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
 
 #[doc(no_inline)]
-pub use lightning::ln::PaymentSecret;
+pub use lightning::ln::types::PaymentSecret;
 #[doc(no_inline)]
 pub use lightning::routing::router::{RouteHint, RouteHintHop};
 #[doc(no_inline)]
@@ -77,15 +76,9 @@ mod de;
 mod ser;
 mod tb;
 
+#[allow(unused_imports)]
 mod prelude {
-       #[cfg(feature = "hashbrown")]
-       extern crate hashbrown;
-
        pub use alloc::{vec, vec::Vec, string::String};
-       #[cfg(not(feature = "hashbrown"))]
-       pub use std::collections::{HashMap, hash_map};
-       #[cfg(feature = "hashbrown")]
-       pub use self::hashbrown::{HashMap, HashSet, hash_map};
 
        pub use alloc::string::ToString;
 }
@@ -169,7 +162,7 @@ pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18;
 /// use secp256k1::Secp256k1;
 /// use secp256k1::SecretKey;
 ///
-/// use lightning::ln::PaymentSecret;
+/// use lightning::ln::types::PaymentSecret;
 ///
 /// use lightning_invoice::{Currency, InvoiceBuilder};
 ///
@@ -549,7 +542,7 @@ impl InvoiceBuilder<tb::False, tb::False, tb::False, tb::False, tb::False, tb::F
                        amount: None,
                        si_prefix: None,
                        timestamp: None,
-                       tagged_fields: Vec::new(),
+                       tagged_fields: Vec::with_capacity(8),
                        error: None,
 
                        phantom_d: core::marker::PhantomData,
@@ -584,7 +577,13 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Boo
 
        /// Sets the amount in millisatoshis. The optimal SI prefix is chosen automatically.
        pub fn amount_milli_satoshis(mut self, amount_msat: u64) -> Self {
-               let amount = amount_msat * 10; // Invoices are denominated in "pico BTC"
+               let amount = match amount_msat.checked_mul(10) { // Invoices are denominated in "pico BTC"
+                       Some(amt) => amt,
+                       None => {
+                               self.error = Some(CreationError::InvalidAmount);
+                               return self
+                       }
+               };
                let biggest_possible_si_prefix = SiPrefix::values_desc()
                        .iter()
                        .find(|prefix| amount % prefix.multiplier() == 0)
@@ -879,8 +878,7 @@ impl SignedRawBolt11Invoice {
 
        /// Recovers the public key used for signing the invoice from the recoverable signature.
        pub fn recover_payee_pub_key(&self) -> Result<PayeePubKey, secp256k1::Error> {
-               let hash = Message::from_slice(&self.hash[..])
-                       .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
+               let hash = Message::from_digest(self.hash);
 
                Ok(PayeePubKey(Secp256k1::new().recover_ecdsa(
                        &hash,
@@ -905,8 +903,7 @@ impl SignedRawBolt11Invoice {
                let pub_key = included_pub_key.or(recovered_pub_key.as_ref())
                        .expect("One is always present");
 
-               let hash = Message::from_slice(&self.hash[..])
-                       .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
+               let hash = Message::from_digest(self.hash);
 
                let secp_context = Secp256k1::new();
                let verification_result = secp_context.verify_ecdsa(
@@ -1000,8 +997,7 @@ impl RawBolt11Invoice {
                where F: FnOnce(&Message) -> Result<RecoverableSignature, E>
        {
                let raw_hash = self.signable_hash();
-               let hash = Message::from_slice(&raw_hash[..])
-                       .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
+               let hash = Message::from_digest(raw_hash);
                let signature = sign_method(&hash)?;
 
                Ok(SignedRawBolt11Invoice {
@@ -1075,9 +1071,10 @@ impl RawBolt11Invoice {
                find_all_extract!(self.known_tagged_fields(), TaggedField::PrivateRoute(ref x), x).collect()
        }
 
+       /// Returns `None` if no amount is set or on overflow.
        pub fn amount_pico_btc(&self) -> Option<u64> {
-               self.hrp.raw_amount.map(|v| {
-                       v * self.hrp.si_prefix.as_ref().map_or(1_000_000_000_000, |si| { si.multiplier() })
+               self.hrp.raw_amount.and_then(|v| {
+                       v.checked_mul(self.hrp.si_prefix.as_ref().map_or(1_000_000_000_000, |si| { si.multiplier() }))
                })
        }
 
@@ -1354,6 +1351,15 @@ impl Bolt11Invoice {
                self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor").0
        }
 
+       /// Recover the payee's public key if one was included in the invoice, otherwise return the
+       /// recovered public key from the signature
+       pub fn get_payee_pub_key(&self) -> PublicKey {
+               match self.payee_pub_key() {
+                       Some(pk) => *pk,
+                       None => self.recover_payee_pub_key()
+               }
+       }
+
        /// Returns the Duration since the Unix epoch at which the invoice expires.
        /// Returning None if overflow occurred.
        pub fn expires_at(&self) -> Option<Duration> {
@@ -1874,7 +1880,7 @@ mod test {
                         Bolt11SemanticError};
 
                let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
-               let payment_secret = lightning::ln::PaymentSecret([21; 32]);
+               let payment_secret = lightning::ln::types::PaymentSecret([21; 32]);
                let invoice_template = RawBolt11Invoice {
                        hrp: RawHrp {
                                currency: Currency::Bitcoin,
@@ -2061,7 +2067,7 @@ mod test {
                let route_1 = RouteHint(vec![
                        RouteHintHop {
                                src_node_id: public_key,
-                               short_channel_id: de::parse_int_be(&[123; 8], 256).expect("short chan ID slice too big?"),
+                               short_channel_id: u64::from_be_bytes([123; 8]),
                                fees: RoutingFees {
                                        base_msat: 2,
                                        proportional_millionths: 1,
@@ -2072,7 +2078,7 @@ mod test {
                        },
                        RouteHintHop {
                                src_node_id: public_key,
-                               short_channel_id: de::parse_int_be(&[42; 8], 256).expect("short chan ID slice too big?"),
+                               short_channel_id: u64::from_be_bytes([42; 8]),
                                fees: RoutingFees {
                                        base_msat: 3,
                                        proportional_millionths: 2,
@@ -2097,7 +2103,7 @@ mod test {
                        },
                        RouteHintHop {
                                src_node_id: public_key,
-                               short_channel_id: de::parse_int_be(&[1; 8], 256).expect("short chan ID slice too big?"),
+                               short_channel_id: u64::from_be_bytes([1; 8]),
                                fees: RoutingFees {
                                        base_msat: 5,
                                        proportional_millionths: 4,