PSBT serialize/deserialize impl for EcdsaSig type

This commit is contained in:
Dr Maxim Orlovsky 2022-01-06 11:51:33 +01:00
parent 0af1c3f320
commit c92057d98f
1 changed files with 25 additions and 1 deletions

View File

@ -20,7 +20,7 @@
use prelude::*; use prelude::*;
use io; use ::{EcdsaSig, io};
use blockdata::script::Script; use blockdata::script::Script;
use blockdata::transaction::{EcdsaSigHashType, Transaction, TxOut}; use blockdata::transaction::{EcdsaSigHashType, Transaction, TxOut};
@ -89,6 +89,30 @@ impl Deserialize for PublicKey {
} }
} }
impl Serialize for EcdsaSig {
fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(72);
buf.extend(self.sig.serialize_der().iter());
buf.push(self.hash_ty as u8);
buf
}
}
impl Deserialize for EcdsaSig {
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
let (sighash_byte, signature) = bytes.split_last()
.ok_or(encode::Error::ParseFailed("empty partial signature data"))?;
Ok(EcdsaSig {
sig: secp256k1::ecdsa::Signature::from_der(signature)
.map_err(|_| encode::Error::ParseFailed("non-DER encoded signature"))?,
// NB: Since BIP-174 says "the signature as would be pushed to the stack from
// a scriptSig or witness" we should use a consensus deserialization and do
// not error on a non-standard values.
hash_ty: EcdsaSigHashType::from_u32_consensus(*sighash_byte as u32)
})
}
}
impl Serialize for KeySource { impl Serialize for KeySource {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
let mut rv: Vec<u8> = Vec::with_capacity(key_source_len(&self)); let mut rv: Vec<u8> = Vec::with_capacity(key_source_len(&self));