diff --git a/Cargo.lock b/Cargo.lock index cad9079..96f89b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2058,6 +2058,7 @@ dependencies = [ "rcgen", "reqwest", "rustls 0.22.4", + "serde_json", "testcontainers", "testcontainers-modules", "thiserror 1.0.69", diff --git a/Cargo.toml b/Cargo.toml index eda73c7..e811c98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ bitcoin-ffi = { git = "https://github.com/bitcoindevkit/bitcoin-ffi.git", rev = hex = "0.4.3" ohttp = { package = "bitcoin-ohttp", version = "0.6.0" } payjoin = { version = "0.21.0", features = ["send", "receive", "base64", "v2", "io"] } +serde_json = "1.0.128" thiserror = "1.0.58" uniffi = { version = "0.28.0", optional = true } url = "2.5.0" diff --git a/src/error.rs b/src/error.rs index 7ca392e..da4763b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -74,6 +74,9 @@ pub enum PayjoinError { #[error("{message}")] InputPairError { message: String }, + + #[error("{message}")] + SerdeJsonError { message: String }, } macro_rules! impl_from_error { @@ -99,6 +102,7 @@ impl_from_error! { OutputSubstitutionError => OutputSubstitutionError, InputContributionError => InputContributionError, PsbtInputError => InputPairError, + serde_json::Error => SerdeJsonError, } #[cfg(feature = "uniffi")] diff --git a/src/receive/mod.rs b/src/receive/mod.rs index 985ba6a..9ee73bf 100644 --- a/src/receive/mod.rs +++ b/src/receive/mod.rs @@ -97,6 +97,16 @@ impl Receiver { pub fn id(&self) -> String { >::into(self.clone()).id().to_string() } + + pub fn to_json(&self) -> Result { + serde_json::to_string(&self.0).map_err(|e| e.into()) + } + + pub fn from_json(&self, json: &str) -> Result { + let receiver = serde_json::from_str::(json) + .map_err(>::into)?; + Ok(receiver.into()) + } } #[derive(Clone)] diff --git a/src/receive/uni.rs b/src/receive/uni.rs index a0cd5c8..686b091 100644 --- a/src/receive/uni.rs +++ b/src/receive/uni.rs @@ -87,6 +87,14 @@ impl Receiver { pub fn id(&self) -> String { self.0.id() } + + pub fn to_json(&self) -> Result { + self.0.to_json() + } + + pub fn from_json(&self, json: &str) -> Result { + self.0.from_json(json).map(Into::into) + } } #[cfg_attr(feature = "uniffi", derive(uniffi::Record))] diff --git a/src/send/mod.rs b/src/send/mod.rs index cba5cec..dc63c1c 100644 --- a/src/send/mod.rs +++ b/src/send/mod.rs @@ -124,6 +124,16 @@ impl Sender { Err(e) => Err(e.into()), } } + + pub fn to_json(&self) -> Result { + serde_json::to_string(&self.0).map_err(|e| e.into()) + } + + pub fn from_json(&self, json: &str) -> Result { + let sender = serde_json::from_str::(json) + .map_err(>::into)?; + Ok(sender.into()) + } } /// Data required for validation of response. diff --git a/src/send/uni.rs b/src/send/uni.rs index 3d4828b..5a01bac 100644 --- a/src/send/uni.rs +++ b/src/send/uni.rs @@ -125,6 +125,14 @@ impl Sender { Err(e) => Err(e), } } + + pub fn to_json(&self) -> Result { + self.0.to_json() + } + + pub fn from_json(&self, json: &str) -> Result { + self.0.from_json(json).map(Into::into) + } } #[derive(uniffi::Object)]