Bump dependencies to bitcoin 0.27 and bech32 0.8
[rust-lightning] / lightning-invoice / src / ser.rs
index 2b4332f8633bea9c16fd943de8844d99b5b69e68..7d9ca9eb895cbd7a5f0566afa94553ac8448133e 100644 (file)
@@ -2,7 +2,8 @@ use std::fmt;
 use std::fmt::{Display, Formatter};
 use bech32::{ToBase32, u5, WriteBase32, Base32Len};
 
-use ::*;
+use super::{Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiry, Fallback, PayeePubKey, InvoiceSignature, PositiveTimestamp,
+       PrivateRoute, Description, RawTaggedField, Currency, RawHrp, SiPrefix, constants, SignedRawInvoice, RawDataPart};
 
 /// Converts a stream of bytes written to it to base32. On finalization the according padding will
 /// be applied. That means the results of writing two data blocks with one or two `BytesToBase32`
@@ -116,12 +117,13 @@ impl Display for SignedRawInvoice {
                let mut data  = self.raw_invoice.data.to_base32();
                data.extend_from_slice(&self.signature.to_base32());
 
-               bech32::encode_to_fmt(f, &hrp, data).expect("HRP is valid")?;
+               bech32::encode_to_fmt(f, &hrp, data, bech32::Variant::Bech32).expect("HRP is valid")?;
 
                Ok(())
        }
 }
 
+/// (C-not exported)
 impl Display for RawHrp {
        fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
                let amount = match self.raw_amount {
@@ -151,6 +153,7 @@ impl Display for Currency {
                        Currency::BitcoinTestnet => "tb",
                        Currency::Regtest => "bcrt",
                        Currency::Simnet => "sb",
+                       Currency::Signet => "tbs",
                };
                write!(f, "{}", currency_code)
        }
@@ -297,18 +300,6 @@ impl Base32Len for PayeePubKey {
        }
 }
 
-impl ToBase32 for PaymentSecret {
-       fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
-               (&self.0[..]).write_base32(writer)
-       }
-}
-
-impl Base32Len for PaymentSecret {
-       fn base32_len(&self) -> usize {
-               bytes_size_to_base32_size(32)
-       }
-}
-
 impl ToBase32 for ExpiryTime {
        fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
                writer.write(&encode_int_be_base32(self.as_seconds()))
@@ -365,22 +356,26 @@ impl Base32Len for Fallback {
        }
 }
 
-impl ToBase32 for Route {
+impl ToBase32 for PrivateRoute {
        fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
                let mut converter = BytesToBase32::new(writer);
 
-               for hop in self.iter() {
-                       converter.append(&hop.pubkey.serialize()[..])?;
-                       converter.append(&hop.short_channel_id[..])?;
+               for hop in (self.0).0.iter() {
+                       converter.append(&hop.src_node_id.serialize()[..])?;
+                       let short_channel_id = try_stretch(
+                               encode_int_be_base256(hop.short_channel_id),
+                               8
+                       ).expect("sizeof(u64) == 8");
+                       converter.append(&short_channel_id)?;
 
                        let fee_base_msat = try_stretch(
-                               encode_int_be_base256(hop.fee_base_msat),
+                               encode_int_be_base256(hop.fees.base_msat),
                                4
                        ).expect("sizeof(u32) == 4");
                        converter.append(&fee_base_msat)?;
 
                        let fee_proportional_millionths = try_stretch(
-                               encode_int_be_base256(hop.fee_proportional_millionths),
+                               encode_int_be_base256(hop.fees.proportional_millionths),
                                4
                        ).expect("sizeof(u32) == 4");
                        converter.append(&fee_proportional_millionths)?;
@@ -397,9 +392,9 @@ impl ToBase32 for Route {
        }
 }
 
-impl Base32Len for Route {
+impl Base32Len for PrivateRoute {
        fn base32_len(&self) -> usize {
-               bytes_size_to_base32_size(self.0.len() * 51)
+               bytes_size_to_base32_size((self.0).0.len() * 51)
        }
 }
 
@@ -444,18 +439,20 @@ impl ToBase32 for TaggedField {
                        TaggedField::Fallback(ref fallback_address) => {
                                write_tagged_field(writer, constants::TAG_FALLBACK, fallback_address)
                        },
-                       TaggedField::Route(ref route_hops) => {
-                               write_tagged_field(writer, constants::TAG_ROUTE, route_hops)
+                       TaggedField::PrivateRoute(ref route_hops) => {
+                               write_tagged_field(writer, constants::TAG_PRIVATE_ROUTE, route_hops)
                        },
                        TaggedField::PaymentSecret(ref payment_secret) => {
                                  write_tagged_field(writer, constants::TAG_PAYMENT_SECRET, payment_secret)
                        },
-
+                       TaggedField::Features(ref features) => {
+                               write_tagged_field(writer, constants::TAG_FEATURES, features)
+                       },
                }
        }
 }
 
-impl ToBase32 for Signature {
+impl ToBase32 for InvoiceSignature {
        fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> {
                let mut converter = BytesToBase32::new(writer);
                let (recovery_id, signature) = self.0.serialize_compact();
@@ -477,6 +474,7 @@ mod test {
                assert_eq!("tb", Currency::BitcoinTestnet.to_string());
                assert_eq!("bcrt", Currency::Regtest.to_string());
                assert_eq!("sb", Currency::Simnet.to_string());
+               assert_eq!("tbs", Currency::Signet.to_string());
        }
 
        #[test]