Skip to content

Commit cd5d775

Browse files
committed
f - convert directly from bytes to message
1 parent b4d13df commit cd5d775

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

lightning/src/offers/offer.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,14 @@ tlv_stream!(OfferTlvStream, OfferTlvStreamRef, {
466466
});
467467

468468
impl Bech32Encode for Offer {
469-
type TlvStream = OfferTlvStream;
470-
471469
const BECH32_HRP: &'static str = "lno";
472470
}
473471

474472
impl FromStr for Offer {
475473
type Err = ParseError;
476474

477475
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
478-
let (tlv_stream, bytes) = Offer::from_bech32_str(s)?;
479-
let contents = OfferContents::try_from(tlv_stream)?;
480-
Ok(Offer { bytes, contents })
476+
Self::from_bech32_str(s)
481477
}
482478
}
483479

lightning/src/offers/parse.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,19 @@
1111
1212
use bitcoin::bech32;
1313
use bitcoin::bech32::{FromBase32, ToBase32};
14+
use core::convert::TryFrom;
1415
use core::fmt;
1516
use ln::msgs::DecodeError;
16-
use util::ser::Readable;
1717

1818
use prelude::*;
1919

2020
/// Indicates a message can be encoded using bech32.
21-
pub(crate) trait Bech32Encode: AsRef<[u8]> {
22-
/// TLV stream that a bech32-encoded message is parsed into.
23-
type TlvStream: Readable;
24-
21+
pub(crate) trait Bech32Encode: AsRef<[u8]> + TryFrom<Vec<u8>, Error=ParseError> {
2522
/// Human readable part of the message's bech32 encoding.
2623
const BECH32_HRP: &'static str;
2724

2825
/// Parses a bech32-encoded message into a TLV stream.
29-
fn from_bech32_str(s: &str) -> Result<(Self::TlvStream, Vec<u8>), ParseError> {
26+
fn from_bech32_str(s: &str) -> Result<Self, ParseError> {
3027
// Offer encoding may be split by '+' followed by optional whitespace.
3128
for chunk in s.split('+') {
3229
let chunk = chunk.trim_start();
@@ -43,7 +40,7 @@ pub(crate) trait Bech32Encode: AsRef<[u8]> {
4340
}
4441

4542
let data = Vec::<u8>::from_base32(&data)?;
46-
Ok((Readable::read(&mut &data[..])?, data))
43+
Self::try_from(data)
4744
}
4845

4946
/// Formats the message using bech32-encoding.

0 commit comments

Comments
 (0)