]> git.bitcoin.ninja Git - rust-lightning/commitdiff
invoice: swap PaymentSecret for ChannelManager's PaymentSecret
authorValentine Wallace <vwallace@protonmail.com>
Wed, 21 Apr 2021 21:53:00 +0000 (17:53 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Thu, 29 Apr 2021 22:39:47 +0000 (18:39 -0400)
lightning-invoice/src/de.rs
lightning-invoice/src/lib.rs
lightning-invoice/src/ser.rs
lightning-invoice/tests/ser_de.rs
lightning/src/ln/channelmanager.rs

index 52a2201bdfd4c16eefb4e4d51d5d9ae0730a812a..6b77520d5bfa1abed5e805e05415c99cde836bae 100644 (file)
@@ -10,6 +10,7 @@ use bech32::{u5, FromBase32};
 
 use bitcoin_hashes::Hash;
 use bitcoin_hashes::sha256;
+use lightning::ln::channelmanager::PaymentSecret;
 use lightning::routing::network_graph::RoutingFees;
 use lightning::routing::router::RouteHintHop;
 
@@ -484,21 +485,6 @@ impl FromBase32 for PayeePubKey {
        }
 }
 
-impl FromBase32 for PaymentSecret {
-       type Err = ParseError;
-
-       fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, ParseError> {
-               if field_data.len() != 52 {
-                       Err(ParseError::Skip)
-               } else {
-                       let data_bytes = Vec::<u8>::from_base32(field_data)?;
-                       let mut payment_secret = [0; 32];
-                       payment_secret.copy_from_slice(&data_bytes);
-                       Ok(PaymentSecret(payment_secret))
-               }
-       }
-}
-
 impl FromBase32 for ExpiryTime {
        type Err = ParseError;
 
index b33b1b374d85b1bea79e5d38a984b7be36e8b0d0..c85239b62d7a4f041343d4db1a87c5671a10ed8a 100644 (file)
@@ -25,6 +25,7 @@ use bech32::u5;
 use bitcoin_hashes::Hash;
 use bitcoin_hashes::sha256;
 use lightning::ln::features::InvoiceFeatures;
+use lightning::ln::channelmanager::PaymentSecret;
 #[cfg(any(doc, test))]
 use lightning::routing::network_graph::RoutingFees;
 use lightning::routing::router::RouteHintHop;
@@ -358,10 +359,6 @@ pub struct Description(String);
 #[derive(Eq, PartialEq, Debug, Clone)]
 pub struct PayeePubKey(pub PublicKey);
 
-/// 256-bit payment secret
-#[derive(Eq, PartialEq, Debug, Clone)]
-pub struct PaymentSecret(pub [u8; 32]);
-
 /// Positive duration that defines when (relatively to the timestamp) in the future the invoice
 /// expires
 ///
index cfc4313b94b3561779430409fcd375afb0fa7ce2..8f0ff31b904b78b42f4708ea3af8c4b7fd099a04 100644 (file)
@@ -297,18 +297,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()))
index 403f8f1f0ee9f4f9aa395ddf6f6e503ecc559897..1ea4dbc6615d80ffd21352fc4a2229c39a780761 100644 (file)
@@ -1,9 +1,11 @@
 extern crate bitcoin_hashes;
+extern crate lightning;
 extern crate lightning_invoice;
 extern crate secp256k1;
 
 use bitcoin_hashes::hex::FromHex;
 use bitcoin_hashes::sha256;
+use lightning::ln::channelmanager::PaymentSecret;
 use lightning_invoice::*;
 use secp256k1::Secp256k1;
 use secp256k1::key::SecretKey;
index 439c9b93c27af37858a783147dd796f5b6aa7746..e49a983c0155630b1477bda54c34be546630994b 100644 (file)
@@ -18,6 +18,8 @@
 //! imply it needs to fail HTLCs/payments/channels it manages).
 //!
 
+use bitcoin::bech32;
+use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5};
 use bitcoin::blockdata::block::{Block, BlockHeader};
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::blockdata::constants::genesis_block;
@@ -209,6 +211,33 @@ pub struct PaymentPreimage(pub [u8;32]);
 #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
 pub struct PaymentSecret(pub [u8;32]);
 
+impl FromBase32 for PaymentSecret {
+       type Err = bech32::Error;
+
+       fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> {
+               if field_data.len() != 52 {
+                       return Err(bech32::Error::InvalidLength)
+               } else {
+                       let data_bytes = Vec::<u8>::from_base32(field_data)?;
+                       let mut payment_secret = [0; 32];
+                       payment_secret.copy_from_slice(&data_bytes);
+                       Ok(PaymentSecret(payment_secret))
+               }
+       }
+}
+
+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 {
+               52
+       }
+}
+
 type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>);
 
 /// Error type returned across the channel_state mutex boundary. When an Err is generated for a