Skip to content

Commit 9f82b4b

Browse files
committed
serde: remove impls for PSET and Transaction
Leave the impl for BlockHeader (which is used by the functionary in a couple of places) (though I'd also like to delete this), the impls for the hash types (which are manually implemented and have regression tests), and the confidential stuff (which are also manually implemented and fairly straightforward). But for Transaction and PSET these impls are untenable. They are: * based on serde-derives (though 'manually' through the serde_struct_impl macro) of extremely complicated and fragile types, exposing implementation details and probably bypassing invariants on deserialization * not specified or documented anywhere * have very poor error messages on deserialization * not tested anywhere (not a single test broke with this change!!) If people are depending on these, we should provide some sort of compat crate/module. We can wait until they are trying to upgrade rust-elements and file complaints to do this. I'm skeptical that anybody *is*, given the above problems, at least not on purpose. If people want serde impls but don't need backward compatibility, we can provide that via the bitcoin-consensus-encoding crate, which will have general-purpose "serde-encode consensus objects in hex/bytes" adaptors in an upcoming version.
1 parent c13d392 commit 9f82b4b

11 files changed

Lines changed: 0 additions & 374 deletions

File tree

src/block.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ pub struct Block {
360360
/// Complete list of transaction in the block
361361
pub txdata: Vec<Transaction>,
362362
}
363-
serde_struct_impl!(Block, header, txdata);
364363
impl_consensus_encoding!(Block, header, txdata);
365364

366365
impl Block {

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ mod parse;
6969
pub mod pset;
7070
pub mod schnorr;
7171
pub mod script;
72-
#[cfg(feature = "serde")]
73-
mod serde_utils;
7472
pub mod sighash;
7573
pub mod taproot;
7674
mod transaction;

src/locktime.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ pub const LOCK_TIME_THRESHOLD: u32 = 500_000_000;
6666
/// ```
6767
#[allow(clippy::derive_ord_xor_partial_ord)]
6868
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
69-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7069
pub enum LockTime {
7170
/// A block height lock time value.
7271
///
@@ -292,7 +291,6 @@ impl Decodable for LockTime {
292291

293292
/// An absolute block height, guaranteed to always contain a valid height value.
294293
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
295-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
296294
pub struct Height(u32);
297295

298296
impl Height {
@@ -377,7 +375,6 @@ impl TryFrom<String> for Height {
377375
/// `to_consensus_u32()`. Said another way, `Time(x)` means 'x seconds since epoch' _not_ '(x -
378376
/// threshold) seconds since epoch'.
379377
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
380-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
381378
pub struct Time(u32);
382379

383380
impl Time {

src/opcodes.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
2121
#![allow(non_camel_case_types)]
2222

23-
#[cfg(feature = "serde")] use serde;
24-
2523
use std::fmt;
2624

2725
// Note: I am deliberately not implementing PartialOrd or Ord on the
@@ -862,16 +860,6 @@ impl fmt::Display for All {
862860
}
863861
}
864862

865-
#[cfg(feature = "serde")]
866-
impl serde::Serialize for All {
867-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
868-
where
869-
S: serde::Serializer,
870-
{
871-
serializer.serialize_str(&self.to_string())
872-
}
873-
}
874-
875863
/// Empty stack is also FALSE
876864
pub static OP_FALSE: All = all::OP_PUSHBYTES_0;
877865
/// Number 1 is also TRUE

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 & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ const PSBT_ELEMENTS_IN_BLINDED_ISSUANCE: u8 = 0x15;
172172
/// A key-value map for an input of the corresponding index in the unsigned
173173
/// transaction.
174174
#[derive(Clone, Debug, PartialEq)]
175-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
176175
pub struct Input {
177176
/// The non-witness transaction this input spends from. Should only be
178177
/// [`std::option::Option::Some`] for inputs which spend non-segwit outputs or
@@ -184,10 +183,6 @@ pub struct Input {
184183
pub witness_utxo: Option<TxOut>,
185184
/// A map from public keys to their corresponding signature as would be
186185
/// pushed to the stack from a scriptSig or witness.
187-
#[cfg_attr(
188-
feature = "serde",
189-
serde(with = "crate::serde_utils::btreemap_byte_values")
190-
)]
191186
pub partial_sigs: BTreeMap<PublicKey, Vec<u8>>,
192187
/// The sighash type to be used for this input. Signatures for this input
193188
/// must use the sighash type.
@@ -198,7 +193,6 @@ pub struct Input {
198193
pub witness_script: Option<Script>,
199194
/// A map from public keys needed to sign this input to their corresponding
200195
/// master key fingerprints and derivation paths.
201-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
202196
pub bip32_derivation: BTreeMap<PublicKey, KeySource>,
203197
/// The finalized, fully-constructed scriptSig with signatures and any other
204198
/// scripts necessary for this input to pass validation.
@@ -208,28 +202,12 @@ pub struct Input {
208202
pub final_script_witness: Option<Vec<Vec<u8>>>,
209203
/// TODO: Proof of reserves commitment
210204
/// RIPEMD160 hash to preimage map
211-
#[cfg_attr(
212-
feature = "serde",
213-
serde(with = "crate::serde_utils::btreemap_byte_values")
214-
)]
215205
pub ripemd160_preimages: BTreeMap<ripemd160::Hash, Vec<u8>>,
216206
/// SHA256 hash to preimage map
217-
#[cfg_attr(
218-
feature = "serde",
219-
serde(with = "crate::serde_utils::btreemap_byte_values")
220-
)]
221207
pub sha256_preimages: BTreeMap<sha256::Hash, Vec<u8>>,
222208
/// HSAH160 hash to preimage map
223-
#[cfg_attr(
224-
feature = "serde",
225-
serde(with = "crate::serde_utils::btreemap_byte_values")
226-
)]
227209
pub hash160_preimages: BTreeMap<hash160::Hash, Vec<u8>>,
228210
/// HAS256 hash to preimage map
229-
#[cfg_attr(
230-
feature = "serde",
231-
serde(with = "crate::serde_utils::btreemap_byte_values")
232-
)]
233211
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
234212
/// (PSET) Prevout TXID of the input
235213
pub previous_txid: Txid,
@@ -244,13 +222,10 @@ pub struct Input {
244222
/// Serialized schnorr signature with sighash type for key spend
245223
pub tap_key_sig: Option<schnorr::SchnorrSig>,
246224
/// Map of `<xonlypubkey>|<leafhash>` with signature
247-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
248225
pub tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), schnorr::SchnorrSig>,
249226
/// Map of Control blocks to Script version pair
250-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
251227
pub tap_scripts: BTreeMap<ControlBlock, (Script, LeafVersion)>,
252228
/// Map of tap root x only keys to origin info and leaf hashes contained in it
253-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::btreemap_as_seq"))]
254229
pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>,
255230
/// Taproot Internal key
256231
pub tap_internal_key: Option<XOnlyPublicKey>,
@@ -303,16 +278,8 @@ pub struct Input {
303278
/// Whether the issuance is blinded
304279
pub blinded_issuance: Option<u8>,
305280
/// Other fields
306-
#[cfg_attr(
307-
feature = "serde",
308-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
309-
)]
310281
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
311282
/// Unknown key-value pairs for this input.
312-
#[cfg_attr(
313-
feature = "serde",
314-
serde(with = "crate::serde_utils::btreemap_as_seq_byte_values")
315-
)]
316283
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
317284
}
318285

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<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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ pub use self::map::{Global, GlobalTxData, Input, Output, PsbtSighashType, TapTre
5858

5959
/// A Partially Signed Transaction.
6060
#[derive(Debug, Clone, PartialEq)]
61-
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6261
pub struct PartiallySignedTransaction {
6362
/// The key-value pairs for all global data.
6463
pub global: Global,

src/pset/raw.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@ use crate::encode::{self, deserialize, serialize, Decodable, Encodable, VarInt,
2424
use hex::DisplayHex as _;
2525
/// A PSET key in its raw byte form.
2626
#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)]
27-
#[cfg_attr(
28-
feature = "serde",
29-
derive(serde::Serialize, serde::Deserialize),
30-
)]
3127
pub struct Key {
3228
/// The type of this PSET key.
3329
pub type_value: u8,
3430
/// The key itself in raw byte form.
35-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
3631
pub key: Vec<u8>,
3732
}
3833

@@ -50,15 +45,10 @@ impl Key {
5045

5146
/// A PSET key-value pair in its raw byte form.
5247
#[derive(Debug, PartialEq)]
53-
#[cfg_attr(
54-
feature = "serde",
55-
derive(serde::Serialize, serde::Deserialize),
56-
)]
5748
pub struct Pair {
5849
/// The key of this key-value pair.
5950
pub key: Key,
6051
/// The value of this key-value pair in raw byte form.
61-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
6252
pub value: Vec<u8>,
6353
}
6454

@@ -67,23 +57,17 @@ pub type ProprietaryType = u8;
6757

6858
/// Proprietary keys (i.e. keys starting with 0xFC byte) with their internal
6959
/// structure according to BIP 174.
70-
#[cfg_attr(
71-
feature = "serde",
72-
derive(serde::Serialize, serde::Deserialize),
73-
)]
7460
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
7561
pub struct ProprietaryKey<Subtype = ProprietaryType>
7662
where
7763
Subtype: Copy + From<u8> + Into<u8>,
7864
{
7965
/// Proprietary type prefix used for grouping together keys under some
8066
/// application and avoid namespace collision
81-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
8267
pub prefix: Vec<u8>,
8368
/// Custom proprietary subtype
8469
pub subtype: Subtype,
8570
/// Additional key bytes (like serialized public key data etc)
86-
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
8771
pub key: Vec<u8>,
8872
}
8973

0 commit comments

Comments
 (0)