From: Valentine Wallace Date: Wed, 21 Apr 2021 21:53:00 +0000 (-0400) Subject: invoice: swap PaymentSecret for ChannelManager's PaymentSecret X-Git-Tag: v0.0.14~6^2~4 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=9529226adf3d0c34c2419968556761b4cf1d3b6e;p=rust-lightning invoice: swap PaymentSecret for ChannelManager's PaymentSecret --- diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index 52a2201bd..6b77520d5 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -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 { - if field_data.len() != 52 { - Err(ParseError::Skip) - } else { - let data_bytes = Vec::::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; diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index b33b1b374..c85239b62 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -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 /// diff --git a/lightning-invoice/src/ser.rs b/lightning-invoice/src/ser.rs index cfc4313b9..8f0ff31b9 100644 --- a/lightning-invoice/src/ser.rs +++ b/lightning-invoice/src/ser.rs @@ -297,18 +297,6 @@ impl Base32Len for PayeePubKey { } } -impl ToBase32 for PaymentSecret { - fn write_base32(&self, writer: &mut W) -> Result<(), ::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(&self, writer: &mut W) -> Result<(), ::Err> { writer.write(&encode_int_be_base32(self.as_seconds())) diff --git a/lightning-invoice/tests/ser_de.rs b/lightning-invoice/tests/ser_de.rs index 403f8f1f0..1ea4dbc66 100644 --- a/lightning-invoice/tests/ser_de.rs +++ b/lightning-invoice/tests/ser_de.rs @@ -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; diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 439c9b93c..e49a983c0 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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 { + if field_data.len() != 52 { + return Err(bech32::Error::InvalidLength) + } else { + let data_bytes = Vec::::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(&self, writer: &mut W) -> Result<(), ::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