Skip to content

Commit c92057d

Browse files
committed
PSBT serialize/deserialize impl for EcdsaSig type
1 parent 0af1c3f commit c92057d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/util/psbt/serialize.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use prelude::*;
2222

23-
use io;
23+
use ::{EcdsaSig, io};
2424

2525
use blockdata::script::Script;
2626
use blockdata::transaction::{EcdsaSigHashType, Transaction, TxOut};
@@ -89,6 +89,30 @@ impl Deserialize for PublicKey {
8989
}
9090
}
9191

92+
impl Serialize for EcdsaSig {
93+
fn serialize(&self) -> Vec<u8> {
94+
let mut buf = Vec::with_capacity(72);
95+
buf.extend(self.sig.serialize_der().iter());
96+
buf.push(self.hash_ty as u8);
97+
buf
98+
}
99+
}
100+
101+
impl Deserialize for EcdsaSig {
102+
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
103+
let (sighash_byte, signature) = bytes.split_last()
104+
.ok_or(encode::Error::ParseFailed("empty partial signature data"))?;
105+
Ok(EcdsaSig {
106+
sig: secp256k1::ecdsa::Signature::from_der(signature)
107+
.map_err(|_| encode::Error::ParseFailed("non-DER encoded signature"))?,
108+
// NB: Since BIP-174 says "the signature as would be pushed to the stack from
109+
// a scriptSig or witness" we should use a consensus deserialization and do
110+
// not error on a non-standard values.
111+
hash_ty: EcdsaSigHashType::from_u32_consensus(*sighash_byte as u32)
112+
})
113+
}
114+
}
115+
92116
impl Serialize for KeySource {
93117
fn serialize(&self) -> Vec<u8> {
94118
let mut rv: Vec<u8> = Vec::with_capacity(key_source_len(&self));

0 commit comments

Comments
 (0)