Skip to content

Commit

Permalink
Expose de/serialize for Sender, Receiver
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DanGould committed Dec 6, 2024
1 parent 5230f19 commit 26d1774
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub enum PayjoinError {

#[error("{message}")]
InputPairError { message: String },

#[error("{message}")]
SerdeJsonError { message: String },
}

macro_rules! impl_from_error {
Expand All @@ -99,6 +102,7 @@ impl_from_error! {
OutputSubstitutionError => OutputSubstitutionError,
InputContributionError => InputContributionError,
PsbtInputError => InputPairError,
serde_json::Error => SerdeJsonError,
}

#[cfg(feature = "uniffi")]
Expand Down
10 changes: 10 additions & 0 deletions src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ impl Receiver {
pub fn id(&self) -> String {
<Self as Into<payjoin::receive::v2::Receiver>>::into(self.clone()).id().to_string()
}

pub fn to_json(&self) -> Result<String, PayjoinError> {
serde_json::to_string(&self.0).map_err(|e| e.into())
}

pub fn from_json(&self, json: &str) -> Result<Self, PayjoinError> {
let receiver = serde_json::from_str::<payjoin::receive::v2::Receiver>(json)
.map_err(<serde_json::Error as Into<PayjoinError>>::into)?;
Ok(receiver.into())
}
}

#[derive(Clone)]
Expand Down
8 changes: 8 additions & 0 deletions src/receive/uni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ impl Receiver {
pub fn id(&self) -> String {
self.0.id()
}

pub fn to_json(&self) -> Result<String, PayjoinError> {
self.0.to_json()
}

pub fn from_json(&self, json: &str) -> Result<Self, PayjoinError> {
self.0.from_json(json).map(Into::into)
}
}

#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
Expand Down
10 changes: 10 additions & 0 deletions src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ impl Sender {
Err(e) => Err(e.into()),
}
}

pub fn to_json(&self) -> Result<String, PayjoinError> {
serde_json::to_string(&self.0).map_err(|e| e.into())
}

pub fn from_json(&self, json: &str) -> Result<Self, PayjoinError> {
let sender = serde_json::from_str::<payjoin::send::Sender>(json)
.map_err(<serde_json::Error as Into<PayjoinError>>::into)?;
Ok(sender.into())
}
}

/// Data required for validation of response.
Expand Down
8 changes: 8 additions & 0 deletions src/send/uni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ impl Sender {
Err(e) => Err(e),
}
}

pub fn to_json(&self) -> Result<String, PayjoinError> {
self.0.to_json()
}

pub fn from_json(&self, json: &str) -> Result<Self, PayjoinError> {
self.0.from_json(json).map(Into::into)
}
}

#[derive(uniffi::Object)]
Expand Down

0 comments on commit 26d1774

Please sign in to comment.