|
18 | 18 | //! imply it needs to fail HTLCs/payments/channels it manages). |
19 | 19 | //! |
20 | 20 |
|
| 21 | +use bitcoin::bech32; |
| 22 | +use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, WriteBase32, u5}; |
21 | 23 | use bitcoin::blockdata::block::{Block, BlockHeader}; |
22 | 24 | use bitcoin::blockdata::transaction::Transaction; |
23 | 25 | use bitcoin::blockdata::constants::genesis_block; |
@@ -209,6 +211,33 @@ pub struct PaymentPreimage(pub [u8;32]); |
209 | 211 | #[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)] |
210 | 212 | pub struct PaymentSecret(pub [u8;32]); |
211 | 213 |
|
| 214 | +impl FromBase32 for PaymentSecret { |
| 215 | + type Err = bech32::Error; |
| 216 | + |
| 217 | + fn from_base32(field_data: &[u5]) -> Result<PaymentSecret, bech32::Error> { |
| 218 | + if field_data.len() != 52 { |
| 219 | + return Err(bech32::Error::InvalidLength) |
| 220 | + } else { |
| 221 | + let data_bytes = Vec::<u8>::from_base32(field_data)?; |
| 222 | + let mut payment_secret = [0; 32]; |
| 223 | + payment_secret.copy_from_slice(&data_bytes); |
| 224 | + Ok(PaymentSecret(payment_secret)) |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +impl ToBase32 for PaymentSecret { |
| 230 | + fn write_base32<W: WriteBase32>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err> { |
| 231 | + (&self.0[..]).write_base32(writer) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +impl Base32Len for PaymentSecret { |
| 236 | + fn base32_len(&self) -> usize { |
| 237 | + 52 |
| 238 | + } |
| 239 | +} |
| 240 | + |
212 | 241 | type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource, PaymentHash)>); |
213 | 242 |
|
214 | 243 | /// Error type returned across the channel_state mutex boundary. When an Err is generated for a |
|
0 commit comments