From: NicolaLS Date: Thu, 16 Jun 2022 11:52:49 +0000 (+0200) Subject: add optional dependency on serde to implement serialize/deserialize for invoice X-Git-Tag: v0.0.109~9^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=6c15de1399f2c1a3f9b1c384af17e59135d7f335;p=rust-lightning add optional dependency on serde to implement serialize/deserialize for invoice --- diff --git a/lightning-invoice/Cargo.toml b/lightning-invoice/Cargo.toml index 85cf1b3fe..98282d0df 100644 --- a/lightning-invoice/Cargo.toml +++ b/lightning-invoice/Cargo.toml @@ -26,7 +26,9 @@ num-traits = { version = "0.2.8", default-features = false } bitcoin_hashes = { version = "0.10", default-features = false } hashbrown = { version = "0.11", optional = true } core2 = { version = "0.3.0", default-features = false, optional = true } +serde = { version = "1.0.118", optional = true } [dev-dependencies] lightning = { version = "0.0.108", path = "../lightning", default-features = false, features = ["_test_utils"] } hex = "0.4" +serde_json = { version = "1"} diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 609b33b2d..bad024c66 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -35,6 +35,8 @@ extern crate secp256k1; extern crate alloc; #[cfg(any(test, feature = "std"))] extern crate core; +#[cfg(feature = "serde")] +extern crate serde; #[cfg(feature = "std")] use std::time::SystemTime; @@ -61,6 +63,9 @@ use core::slice::Iter; use core::time::Duration; use core::str; +#[cfg(feature = "serde")] +use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error}; + mod de; mod ser; mod tb; @@ -1523,6 +1528,23 @@ impl Display for SignOrCreationError { } } +#[cfg(feature = "serde")] +impl Serialize for Invoice { + fn serialize(&self, serializer: S) -> Result where S: Serializer { + serializer.serialize_str(self.to_string().as_str()) + } +} +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for Invoice { + fn deserialize(deserializer: D) -> Result where D: Deserializer<'de> { + let bolt11 = String::deserialize(deserializer)? + .parse::() + .map_err(|e| D::Error::custom(format!("{:?}", e)))?; + + Ok(bolt11) + } +} + #[cfg(test)] mod test { use bitcoin_hashes::hex::FromHex; @@ -1974,4 +1996,26 @@ mod test { assert!(invoice.would_expire(Duration::from_secs(1234567 + DEFAULT_EXPIRY_TIME + 1))); } + + #[cfg(feature = "serde")] + #[test] + fn test_serde() { + let invoice_str = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\ + h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\ + 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\ + h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\ + j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\ + ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\ + guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\ + ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\ + p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\ + 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\ + j5r6drg6k6zcqj0fcwg"; + let invoice = invoice_str.parse::().unwrap(); + let serialized_invoice = serde_json::to_string(&invoice).unwrap(); + let deserialized_invoice: super::Invoice = serde_json::from_str(serialized_invoice.as_str()).unwrap(); + assert_eq!(invoice, deserialized_invoice); + assert_eq!(invoice_str, deserialized_invoice.to_string().as_str()); + assert_eq!(invoice_str, serialized_invoice.as_str().trim_matches('\"')); + } }