psbt: Fix serde for maps with non-string keys and binary values
This commit is contained in:
parent
e5f3bca2b4
commit
a9c13272a0
|
@ -60,6 +60,78 @@ pub mod btreemap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod btreemap_byte_values {
|
||||||
|
//! Module for serialization of BTreeMaps with Vec<u8> values because
|
||||||
|
//! serde_json will not serialize hashmaps with non-string keys be default.
|
||||||
|
#![allow(missing_docs)]
|
||||||
|
|
||||||
|
// NOTE: This module can be exactly copied to use with HashMap.
|
||||||
|
|
||||||
|
use ::std::collections::BTreeMap;
|
||||||
|
use serde;
|
||||||
|
|
||||||
|
/// A custom key-value pair type that serialized the bytes as hex.
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct OwnedPair<T>(
|
||||||
|
T,
|
||||||
|
#[serde(deserialize_with = "::serde_utils::hex_bytes::deserialize")]
|
||||||
|
Vec<u8>,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// A custom key-value pair type that serialized the bytes as hex.
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
struct BorrowedPair<'a, T: 'static>(
|
||||||
|
&'a T,
|
||||||
|
#[serde(serialize_with = "::serde_utils::hex_bytes::serialize")]
|
||||||
|
&'a [u8],
|
||||||
|
);
|
||||||
|
|
||||||
|
pub fn serialize<S, T>(v: &BTreeMap<T, Vec<u8>>, s: S)
|
||||||
|
-> Result<S::Ok, S::Error> where
|
||||||
|
S: serde::Serializer,
|
||||||
|
T: serde::Serialize + ::std::hash::Hash + Eq + Ord + 'static,
|
||||||
|
{
|
||||||
|
use serde::ser::SerializeSeq;
|
||||||
|
|
||||||
|
let mut seq = s.serialize_seq(Some(v.len()))?;
|
||||||
|
for (key, value) in v.iter() {
|
||||||
|
seq.serialize_element(&BorrowedPair(key, value))?;
|
||||||
|
}
|
||||||
|
seq.end()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize<'de, D, T>(d: D)
|
||||||
|
-> Result<BTreeMap<T, Vec<u8>>, D::Error> where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
T: serde::Deserialize<'de> + ::std::hash::Hash + Eq + Ord,
|
||||||
|
{
|
||||||
|
use ::std::marker::PhantomData;
|
||||||
|
|
||||||
|
struct Visitor<T>(PhantomData<T>);
|
||||||
|
impl<'de, T> serde::de::Visitor<'de> for Visitor<T> where
|
||||||
|
T: serde::Deserialize<'de> + ::std::hash::Hash + Eq + Ord,
|
||||||
|
{
|
||||||
|
type Value = BTreeMap<T, Vec<u8>>;
|
||||||
|
|
||||||
|
fn expecting(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
|
write!(f, "a sequence of pairs")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut a: A)
|
||||||
|
-> Result<Self::Value, A::Error>
|
||||||
|
{
|
||||||
|
let mut ret = BTreeMap::new();
|
||||||
|
while let Option::Some(OwnedPair(key, value)) = a.next_element()? {
|
||||||
|
ret.insert(key, value);
|
||||||
|
}
|
||||||
|
Ok(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
d.deserialize_seq(Visitor(PhantomData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub mod hex_bytes {
|
pub mod hex_bytes {
|
||||||
//! Module for serialization of byte arrays as hex strings.
|
//! Module for serialization of byte arrays as hex strings.
|
||||||
#![allow(missing_docs)]
|
#![allow(missing_docs)]
|
||||||
|
|
|
@ -49,10 +49,10 @@ pub struct Global {
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
||||||
pub xpub: BTreeMap<ExtendedPubKey, KeySource>,
|
pub xpub: BTreeMap<ExtendedPubKey, KeySource>,
|
||||||
/// Global proprietary key-value pairs.
|
/// Global proprietary key-value pairs.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
|
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
|
||||||
/// Unknown global key-value pairs.
|
/// Unknown global key-value pairs.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ pub struct Input {
|
||||||
pub witness_utxo: Option<TxOut>,
|
pub witness_utxo: Option<TxOut>,
|
||||||
/// A map from public keys to their corresponding signature as would be
|
/// A map from public keys to their corresponding signature as would be
|
||||||
/// pushed to the stack from a scriptSig or witness.
|
/// pushed to the stack from a scriptSig or witness.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub partial_sigs: BTreeMap<PublicKey, Vec<u8>>,
|
pub partial_sigs: BTreeMap<PublicKey, Vec<u8>>,
|
||||||
/// The sighash type to be used for this input. Signatures for this input
|
/// The sighash type to be used for this input. Signatures for this input
|
||||||
/// must use the sighash type.
|
/// must use the sighash type.
|
||||||
|
@ -99,10 +99,10 @@ pub struct Input {
|
||||||
/// HAS256 hash to preimage map
|
/// HAS256 hash to preimage map
|
||||||
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
|
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
|
||||||
/// Proprietary key-value pairs for this input.
|
/// Proprietary key-value pairs for this input.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
|
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
|
||||||
/// Unknown key-value pairs for this input.
|
/// Unknown key-value pairs for this input.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,10 +47,10 @@ pub struct Output {
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
||||||
pub bip32_derivation: BTreeMap<PublicKey, KeySource>,
|
pub bip32_derivation: BTreeMap<PublicKey, KeySource>,
|
||||||
/// Proprietary key-value pairs for this output.
|
/// Proprietary key-value pairs for this output.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
|
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
|
||||||
/// Unknown key-value pairs for this output.
|
/// Unknown key-value pairs for this output.
|
||||||
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap"))]
|
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
|
||||||
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue