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
//! ```
@@ -70,7 +79,7 @@ use io;
70
79
use ln:: features:: OfferFeatures ;
71
80
use offers:: parse:: { Bech32Encode , ParseError , SemanticError } ;
72
81
use onion_message:: BlindedPath ;
73
- use util:: ser:: { Writeable , Writer } ;
82
+ use util:: ser:: { Readable , Writeable , Writer } ;
74
83
75
84
use prelude:: * ;
76
85
@@ -383,12 +392,27 @@ impl OfferContents {
383
392
}
384
393
}
385
394
395
+ impl Writeable for Offer {
396
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
397
+ self . bytes . write ( writer)
398
+ }
399
+ }
400
+
386
401
impl Writeable for OfferContents {
387
402
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
388
403
self . as_tlv_stream ( ) . write ( writer)
389
404
}
390
405
}
391
406
407
+ impl TryFrom < Vec < u8 > > for Offer {
408
+ type Error = ParseError ;
409
+
410
+ fn try_from ( bytes : Vec < u8 > ) -> Result < Self , Self :: Error > {
411
+ let tlv_stream: OfferTlvStream = Readable :: read ( & mut & bytes[ ..] ) ?;
412
+ Offer :: try_from ( ( bytes, tlv_stream) )
413
+ }
414
+ }
415
+
392
416
/// The amount required for an item in an [`Offer`] denominated in either bitcoin or another
393
417
/// currency.
394
418
#[ derive( Clone , Debug , PartialEq ) ]
@@ -431,6 +455,8 @@ impl Bech32Encode for Offer {
431
455
const BECH32_HRP : & ' static str = "lno" ;
432
456
}
433
457
458
+ type ParsedOffer = ( Vec < u8 > , OfferTlvStream ) ;
459
+
434
460
impl FromStr for Offer {
435
461
type Err = ParseError ;
436
462
@@ -441,6 +467,16 @@ impl FromStr for Offer {
441
467
}
442
468
}
443
469
470
+ impl TryFrom < ParsedOffer > for Offer {
471
+ type Error = ParseError ;
472
+
473
+ fn try_from ( offer : ParsedOffer ) -> Result < Self , Self :: Error > {
474
+ let ( bytes, tlv_stream) = offer;
475
+ let contents = OfferContents :: try_from ( tlv_stream) ?;
476
+ Ok ( Offer { bytes, contents } )
477
+ }
478
+ }
479
+
444
480
impl TryFrom < OfferTlvStream > for OfferContents {
445
481
type Error = SemanticError ;
446
482
0 commit comments