Skip to content

Commit 38b0fc2

Browse files
committed
pset: replace ad-hoc serde implementation with base64 string
There is a standard serialization of PSETs (and PSBTs, and PSBT2s): the base64 of the byte encoding. Rather than making up our own serialization based on the details of our rust-elements types, which makes basically every structural property of our types a property of our API, just use the standard encoding. This is a major breaking change; we may need a compatibility layer based on rust-elements 0.25.
1 parent 3003963 commit 38b0fc2

File tree

7 files changed

+18
-342
lines changed

7 files changed

+18
-342
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ mod parse;
6464
pub mod pset;
6565
pub mod schnorr;
6666
pub mod script;
67-
#[cfg(feature = "serde")]
68-
mod serde_utils;
6967
pub mod sighash;
7068
pub mod taproot;
7169
mod transaction;

src/pset/map/global.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const PSBT_ELEMENTS_GLOBAL_TX_MODIFIABLE: u8 = 0x01;
5858

5959
/// Global transaction data
6060
#[derive(Debug, Clone, PartialEq)]
61-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6261
pub struct TxData {
6362
/// Transaction version. Must be 2.
6463
pub version: u32,
@@ -93,10 +92,8 @@ impl Default for TxData {
9392

9493
/// A key-value map for global data.
9594
#[derive(Clone, Debug, PartialEq)]
96-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9795
pub struct Global {
9896
/// Global transaction data
99-
#[cfg_attr(feature = "serde", serde(flatten))]
10097
pub tx_data: TxData,
10198
/// The version number of this PSET. Must be present.
10299
pub version: u32,
@@ -109,16 +106,8 @@ pub struct Global {
109106
/// Elements tx modifiable flag
110107
pub elements_tx_modifiable_flag: Option<u8>,
111108
/// Other Proprietary fields
112-
#[cfg_attr(
113-
feature = "serde",
114-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
115-
)]
116109
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
117110
/// Unknown global key-value pairs.
118-
#[cfg_attr(
119-
feature = "serde",
120-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
121-
)]
122111
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
123112
}
124113

src/pset/map/input.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ const PSBT_ELEMENTS_IN_BLINDED_ISSUANCE: u8 = 0x15;
171171
/// A key-value map for an input of the corresponding index in the unsigned
172172
/// transaction.
173173
#[derive(Clone, Debug, PartialEq)]
174-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
175174
pub struct Input {
176175
/// The non-witness transaction this input spends from. Should only be
177176
/// [`std::option::Option::Some`] for inputs which spend non-segwit outputs or
@@ -183,10 +182,6 @@ pub struct Input {
183182
pub witness_utxo: Option<TxOut>,
184183
/// A map from public keys to their corresponding signature as would be
185184
/// pushed to the stack from a scriptSig or witness.
186-
#[cfg_attr(
187-
feature = "serde",
188-
serde(with = "crate::serde_utils::btreemap_byte_values")
189-
)]
190185
pub partial_sigs: BTreeMap<PublicKey, Vec<u8>>,
191186
/// The sighash type to be used for this input. Signatures for this input
192187
/// must use the sighash type.
@@ -197,7 +192,6 @@ pub struct Input {
197192
pub witness_script: Option<Script>,
198193
/// A map from public keys needed to sign this input to their corresponding
199194
/// master key fingerprints and derivation paths.
200-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
201195
pub bip32_derivation: BTreeMap<PublicKey, KeySource>,
202196
/// The finalized, fully-constructed scriptSig with signatures and any other
203197
/// scripts necessary for this input to pass validation.
@@ -207,28 +201,12 @@ pub struct Input {
207201
pub final_script_witness: Option<Vec<Vec<u8>>>,
208202
/// TODO: Proof of reserves commitment
209203
/// RIPEMD160 hash to preimage map
210-
#[cfg_attr(
211-
feature = "serde",
212-
serde(with = "crate::serde_utils::btreemap_byte_values")
213-
)]
214204
pub ripemd160_preimages: BTreeMap<ripemd160::Hash, Vec<u8>>,
215205
/// SHA256 hash to preimage map
216-
#[cfg_attr(
217-
feature = "serde",
218-
serde(with = "crate::serde_utils::btreemap_byte_values")
219-
)]
220206
pub sha256_preimages: BTreeMap<sha256::Hash, Vec<u8>>,
221207
/// HSAH160 hash to preimage map
222-
#[cfg_attr(
223-
feature = "serde",
224-
serde(with = "crate::serde_utils::btreemap_byte_values")
225-
)]
226208
pub hash160_preimages: BTreeMap<hash160::Hash, Vec<u8>>,
227209
/// HAS256 hash to preimage map
228-
#[cfg_attr(
229-
feature = "serde",
230-
serde(with = "crate::serde_utils::btreemap_byte_values")
231-
)]
232210
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
233211
/// (PSET) Prevout TXID of the input
234212
pub previous_txid: Txid,
@@ -243,13 +221,10 @@ pub struct Input {
243221
/// Serialized schnorr signature with sighash type for key spend
244222
pub tap_key_sig: Option<schnorr::SchnorrSig>,
245223
/// Map of `<xonlypubkey>|<leafhash>` with signature
246-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
247224
pub tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), schnorr::SchnorrSig>,
248225
/// Map of Control blocks to Script version pair
249-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
250226
pub tap_scripts: BTreeMap<ControlBlock, (Script, LeafVersion)>,
251227
/// Map of tap root x only keys to origin info and leaf hashes contained in it
252-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
253228
pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>,
254229
/// Taproot Internal key
255230
pub tap_internal_key: Option<XOnlyPublicKey>,
@@ -302,16 +277,8 @@ pub struct Input {
302277
/// Whether the issuance is blinded
303278
pub blinded_issuance: Option<u8>,
304279
/// Other fields
305-
#[cfg_attr(
306-
feature = "serde",
307-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
308-
)]
309280
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
310281
/// Unknown key-value pairs for this input.
311-
#[cfg_attr(
312-
feature = "serde",
313-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
314-
)]
315282
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
316283
}
317284

@@ -381,8 +348,6 @@ pub struct PsbtSighashType {
381348
pub(crate) inner: u32,
382349
}
383350

384-
serde_string_impl!(PsbtSighashType, "a PsbtSighashType data");
385-
386351
impl fmt::Display for PsbtSighashType {
387352
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
388353
match self.schnorr_hash_ty() {

src/pset/map/output.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,19 @@ const PSBT_ELEMENTS_OUT_BLIND_ASSET_PROOF: u8 = 0x0a;
8787
/// A key-value map for an output of the corresponding index in the unsigned
8888
/// transaction.
8989
#[derive(Clone, Default, Debug, PartialEq)]
90-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9190
pub struct Output {
9291
/// The redeem script for this output.
9392
pub redeem_script: Option<Script>,
9493
/// The witness script for this output.
9594
pub witness_script: Option<Script>,
9695
/// A map from public keys needed to spend this output to their
9796
/// corresponding master key fingerprints and derivation paths.
98-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
9997
pub bip32_derivation: BTreeMap<PublicKey, KeySource>,
10098
/// The internal pubkey
10199
pub tap_internal_key: Option<XOnlyPublicKey>,
102100
/// Taproot Output tree
103101
pub tap_tree: Option<TapTree>,
104102
/// Map of tap root x only keys to origin info and leaf hashes contained in it
105-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
106103
pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>,
107104
/// (PSET) The explicit amount of the output
108105
pub amount: Option<u64>,
@@ -131,22 +128,13 @@ pub struct Output {
131128
pub blind_asset_proof: Option<Box<SurjectionProof>>,
132129
/// Pset
133130
/// Other fields
134-
#[cfg_attr(
135-
feature = "serde",
136-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
137-
)]
138131
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
139132
/// Unknown key-value pairs for this output.
140-
#[cfg_attr(
141-
feature = "serde",
142-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
143-
)]
144133
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
145134
}
146135

147136
/// Taproot Tree representing a finalized [`TaprootBuilder`] (a complete binary tree)
148137
#[derive(Clone, Debug)]
149-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150138
pub struct TapTree(pub(crate) TaprootBuilder);
151139

152140
impl PartialEq for TapTree {

src/pset/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ pub use self::map::{Global, GlobalTxData, Input, Output, PsbtSighashType, TapTre
5656

5757
/// A Partially Signed Transaction.
5858
#[derive(Debug, Clone, PartialEq)]
59-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6059
pub struct PartiallySignedTransaction {
6160
/// The key-value pairs for all global data.
6261
pub global: Global,
@@ -769,6 +768,24 @@ impl Decodable for PartiallySignedTransaction {
769768
}
770769
}
771770

771+
#[cfg(feature = "base64")]
772+
impl serde::Serialize for PartiallySignedTransaction {
773+
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
774+
self.to_string().serialize(s)
775+
}
776+
}
777+
778+
#[cfg(feature = "base64")]
779+
impl<'de> serde::Deserialize<'de> for PartiallySignedTransaction {
780+
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
781+
use serde::de::Error as _;
782+
783+
let b64 = String::deserialize(d)?;
784+
b64.parse().map_err(D::Error::custom)
785+
}
786+
}
787+
788+
772789
#[cfg(test)]
773790
mod tests {
774791
use super::*;

src/pset/raw.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ use crate::encode::{self, deserialize, serialize, Decodable, Encodable, VarInt,
2424
use crate::hex;
2525
/// A PSET key in its raw byte form.
2626
#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)]
27-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2827
pub struct Key {
2928
/// The type of this PSET key.
3029
pub type_value: u8,
3130
/// The key itself in raw byte form.
32-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
3331
pub key: Vec<u8>,
3432
}
3533

@@ -47,12 +45,10 @@ impl Key {
4745

4846
/// A PSET key-value pair in its raw byte form.
4947
#[derive(Debug, PartialEq)]
50-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5148
pub struct Pair {
5249
/// The key of this key-value pair.
5350
pub key: Key,
5451
/// The value of this key-value pair in raw byte form.
55-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
5652
pub value: Vec<u8>,
5753
}
5854

@@ -61,20 +57,17 @@ pub type ProprietaryType = u8;
6157

6258
/// Proprietary keys (i.e. keys starting with 0xFC byte) with their internal
6359
/// structure according to BIP 174.
64-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6560
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
6661
pub struct ProprietaryKey<Subtype = ProprietaryType>
6762
where
6863
Subtype: Copy + From<u8> + Into<u8>,
6964
{
7065
/// Proprietary type prefix used for grouping together keys under some
7166
/// application and avoid namespace collision
72-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
7367
pub prefix: Vec<u8>,
7468
/// Custom proprietary subtype
7569
pub subtype: Subtype,
7670
/// Additional key bytes (like serialized public key data etc)
77-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
7871
pub key: Vec<u8>,
7972
}
8073

0 commit comments

Comments
 (0)