18
18
//! extern crate core;
19
19
//! extern crate lightning;
20
20
//!
21
+ //! use core::convert::TryFrom;
21
22
//! use core::num::NonZeroU64;
22
23
//! use core::time::Duration;
23
24
//!
24
25
//! use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey};
25
26
//! use lightning::offers::offer::{Amount, Offer, OfferBuilder};
26
27
//! use lightning::offers::parse::ParseError;
28
+ //! use lightning::util::ser::{Readable, Writeable};
27
29
//!
28
30
//! # use lightning::onion_message::BlindedPath;
29
31
//! # #[cfg(feature = "std")]
54
56
//!
55
57
//! // Parse from a bech32 string after scanning from a QR code.
56
58
//! let offer = encoded_offer.parse::<Offer>()?;
59
+ //!
60
+ //! // Encode offer as raw bytes.
61
+ //! let mut bytes = Vec::new();
62
+ //! offer.write(&mut bytes).unwrap();
63
+ //!
64
+ //! // Decode raw bytes into an offer.
65
+ //! let offer = Offer::try_from(bytes)?;
57
66
//! # Ok(())
58
67
//! # }
59
68
//! ```
@@ -71,7 +80,7 @@ use ln::features::OfferFeatures;
71
80
use ln:: msgs:: MAX_VALUE_MSAT ;
72
81
use offers:: parse:: { Bech32Encode , ParseError , SemanticError } ;
73
82
use onion_message:: BlindedPath ;
74
- use util:: ser:: { Writeable , Writer } ;
83
+ use util:: ser:: { Readable , Writeable , Writer } ;
75
84
76
85
use prelude:: * ;
77
86
@@ -398,12 +407,27 @@ impl OfferContents {
398
407
}
399
408
}
400
409
410
+ impl Writeable for Offer {
411
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
412
+ self . bytes . write ( writer)
413
+ }
414
+ }
415
+
401
416
impl Writeable for OfferContents {
402
417
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
403
418
self . as_tlv_stream ( ) . write ( writer)
404
419
}
405
420
}
406
421
422
+ impl TryFrom < Vec < u8 > > for Offer {
423
+ type Error = ParseError ;
424
+
425
+ fn try_from ( bytes : Vec < u8 > ) -> Result < Self , Self :: Error > {
426
+ let tlv_stream: OfferTlvStream = Readable :: read ( & mut & bytes[ ..] ) ?;
427
+ Offer :: try_from ( ( bytes, tlv_stream) )
428
+ }
429
+ }
430
+
407
431
/// The minimum amount required for an item in an [`Offer`], denominated in either bitcoin or
408
432
/// another currency.
409
433
#[ derive( Clone , Debug , PartialEq ) ]
@@ -456,6 +480,8 @@ impl Bech32Encode for Offer {
456
480
const BECH32_HRP : & ' static str = "lno" ;
457
481
}
458
482
483
+ type ParsedOffer = ( Vec < u8 > , OfferTlvStream ) ;
484
+
459
485
impl FromStr for Offer {
460
486
type Err = ParseError ;
461
487
@@ -466,6 +492,16 @@ impl FromStr for Offer {
466
492
}
467
493
}
468
494
495
+ impl TryFrom < ParsedOffer > for Offer {
496
+ type Error = ParseError ;
497
+
498
+ fn try_from ( offer : ParsedOffer ) -> Result < Self , Self :: Error > {
499
+ let ( bytes, tlv_stream) = offer;
500
+ let contents = OfferContents :: try_from ( tlv_stream) ?;
501
+ Ok ( Offer { bytes, contents } )
502
+ }
503
+ }
504
+
469
505
impl TryFrom < OfferTlvStream > for OfferContents {
470
506
type Error = SemanticError ;
471
507
0 commit comments