@@ -35,6 +35,8 @@ extern crate secp256k1;
35
35
extern crate alloc;
36
36
#[ cfg( any( test, feature = "std" ) ) ]
37
37
extern crate core;
38
+ #[ cfg( feature = "serde" ) ]
39
+ extern crate serde;
38
40
39
41
#[ cfg( feature = "std" ) ]
40
42
use std:: time:: SystemTime ;
@@ -61,6 +63,9 @@ use core::slice::Iter;
61
63
use core:: time:: Duration ;
62
64
use core:: str;
63
65
66
+ #[ cfg( feature = "serde" ) ]
67
+ use serde:: { Deserialize , Deserializer , Serialize , Serializer , de:: Error } ;
68
+
64
69
mod de;
65
70
mod ser;
66
71
mod tb;
@@ -1523,6 +1528,23 @@ impl<S> Display for SignOrCreationError<S> {
1523
1528
}
1524
1529
}
1525
1530
1531
+ #[ cfg( feature = "serde" ) ]
1532
+ impl Serialize for Invoice {
1533
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error > where S : Serializer {
1534
+ serializer. serialize_str ( self . to_string ( ) . as_str ( ) )
1535
+ }
1536
+ }
1537
+ #[ cfg( feature = "serde" ) ]
1538
+ impl < ' de > Deserialize < ' de > for Invoice {
1539
+ fn deserialize < D > ( deserializer : D ) -> Result < Invoice , D :: Error > where D : Deserializer < ' de > {
1540
+ let bolt11 = String :: deserialize ( deserializer) ?
1541
+ . parse :: < Invoice > ( )
1542
+ . map_err ( |e| D :: Error :: custom ( format ! ( "{:?}" , e) ) ) ?;
1543
+
1544
+ Ok ( bolt11)
1545
+ }
1546
+ }
1547
+
1526
1548
#[ cfg( test) ]
1527
1549
mod test {
1528
1550
use bitcoin_hashes:: hex:: FromHex ;
@@ -1974,4 +1996,26 @@ mod test {
1974
1996
1975
1997
assert ! ( invoice. would_expire( Duration :: from_secs( 1234567 + DEFAULT_EXPIRY_TIME + 1 ) ) ) ;
1976
1998
}
1999
+
2000
+ #[ cfg( feature = "serde" ) ]
2001
+ #[ test]
2002
+ fn test_serde ( ) {
2003
+ let invoice_str = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
2004
+ h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
2005
+ 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\
2006
+ h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\
2007
+ j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\
2008
+ ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\
2009
+ guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\
2010
+ ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\
2011
+ p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\
2012
+ 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\
2013
+ j5r6drg6k6zcqj0fcwg";
2014
+ let invoice = invoice_str. parse :: < super :: Invoice > ( ) . unwrap ( ) ;
2015
+ let serialized_invoice = serde_json:: to_string ( & invoice) . unwrap ( ) ;
2016
+ let deserialized_invoice: super :: Invoice = serde_json:: from_str ( serialized_invoice. as_str ( ) ) . unwrap ( ) ;
2017
+ assert_eq ! ( invoice, deserialized_invoice) ;
2018
+ assert_eq ! ( invoice_str, deserialized_invoice. to_string( ) . as_str( ) ) ;
2019
+ assert_eq ! ( invoice_str, serialized_invoice. as_str( ) . trim_matches( '\"' ) ) ;
2020
+ }
1977
2021
}
0 commit comments