From 238310e5149e11dad8dd19f123dcf6c13b08dc67 Mon Sep 17 00:00:00 2001 From: DanGould Date: Fri, 6 Dec 2024 17:26:06 -0500 Subject: [PATCH 1/2] Expose de/serialize for Sender, Receiver These structs need to be serialized and deserialized in order to be persisted in a downstream database. We use json to do this only because that decision was made in payjoin-cli. A binary encoding could be used as well, but that bikeshed is to be built at a later date. --- Cargo.lock | 1 + Cargo.toml | 1 + src/error.rs | 4 ++++ src/receive/mod.rs | 10 ++++++++++ src/receive/uni.rs | 9 +++++++++ src/send/mod.rs | 10 ++++++++++ src/send/uni.rs | 9 +++++++++ 7 files changed, 44 insertions(+) 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..cb9dd39 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(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..71985b0 100644 --- a/src/receive/uni.rs +++ b/src/receive/uni.rs @@ -87,6 +87,15 @@ impl Receiver { pub fn id(&self) -> String { self.0.id() } + + pub fn to_json(&self) -> Result { + self.0.to_json() + } + + #[uniffi::constructor] + pub fn from_json(json: &str) -> Result { + super::Receiver::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..46ba803 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(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..5595d84 100644 --- a/src/send/uni.rs +++ b/src/send/uni.rs @@ -125,6 +125,15 @@ impl Sender { Err(e) => Err(e), } } + + pub fn to_json(&self) -> Result { + self.0.to_json() + } + + #[uniffi::constructor] + pub fn from_json(json: &str) -> Result { + super::Sender::from_json(json).map(Into::into) + } } #[derive(uniffi::Object)] From 437bdac923d839775f542aafd4c197d3554164cc Mon Sep 17 00:00:00 2001 From: DanGould Date: Fri, 6 Dec 2024 17:28:05 -0500 Subject: [PATCH 2/2] Bump payjoin-ffi 0.21.1 --- CHANGELOG.md | 3 +++ Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e751d55..bf8160e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [0.21.1] +- Add `to_json` and `from_json` methods to `Sender` and `Receiver` UniFFI types. ([#39](https://github.com/LtbLightning/payjoin-ffi/pull/39)) + ## [0.21.0] This release updates the bindings libraries to `payjoin` version `0.21.0`. #### APIs changed diff --git a/Cargo.lock b/Cargo.lock index 96f89b2..fb0dc26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2042,7 +2042,7 @@ dependencies = [ [[package]] name = "payjoin_ffi" -version = "0.21.0" +version = "0.21.1" dependencies = [ "base64 0.22.1", "bdk", diff --git a/Cargo.toml b/Cargo.toml index e811c98..6fc5536 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "payjoin_ffi" -version = "0.21.0" +version = "0.21.1" edition = "2021" license = "MIT OR Apache-2.0" exclude = ["tests"]