Skip to content

Commit

Permalink
Add serializer for Option<secp::key::Pubkey>
Browse files Browse the repository at this point in the history
Adds serde mod for an optional public key.

Useful for multisig output and atomic swap transaction flow
  • Loading branch information
GeneFerneau committed Jun 3, 2021
1 parent 3a268aa commit 01ce238
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/src/libtx/secp_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,51 @@ pub mod pubkey_serde {
}
}

/// Serializes an Option<secp::key::PublicKey> to and from hex
pub mod option_pubkey_serde {
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serializer};
use util::secp::key::PublicKey;
use util::{from_hex, static_secp_instance, ToHex};

///
pub fn serialize<S>(key: &Option<PublicKey>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match key {
Some(k) => {
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();
serializer.serialize_str(&k.serialize_vec(&static_secp, true).to_hex())
}
None => serializer.serialize_none(),
}
}

///
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<PublicKey>, D::Error>
where
D: Deserializer<'de>,
{
Option::<String>::deserialize(deserializer).and_then(|res| match res {
Some(string) => {
let static_secp = static_secp_instance();
let static_secp = static_secp.lock();

from_hex(&string)
.map_err(Error::custom)
.and_then(|bytes: Vec<u8>| {
Ok(Some(
PublicKey::from_slice(&static_secp, &bytes).map_err(Error::custom)?,
))
})
}
None => Ok(None),
})
}
}

/// Serializes an Option<secp::Signature> to and from hex
pub mod option_sig_serde {
use serde::de::Error;
Expand Down

0 comments on commit 01ce238

Please sign in to comment.