Pass sigs and associated types by value
We should pass `Copy` types by value not by reference. Currently this is not done in secp, but lets do it here in bitcoin. Pass by value: - `SerializedSignature` - bitcoin sigs - secp sigs - secp `Message`
This commit is contained in:
parent
c03e23b004
commit
5e8f204581
|
@ -221,7 +221,7 @@ fn main() {
|
|||
.enumerate()
|
||||
.map(|(idx, input)| {
|
||||
let (_, sig) = input.partial_sigs.iter().next().expect("we have one sig");
|
||||
Witness::p2wpkh(sig, pk_inputs[idx].0)
|
||||
Witness::p2wpkh(*sig, pk_inputs[idx].0)
|
||||
})
|
||||
.collect();
|
||||
psbt.inputs.iter_mut().enumerate().for_each(|(idx, input)| {
|
||||
|
|
|
@ -47,7 +47,7 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, value: u64) {
|
|||
let msg = secp256k1::Message::from(sighash);
|
||||
println!("Message is {:x}", msg);
|
||||
let secp = secp256k1::Secp256k1::verification_only();
|
||||
pk.verify(&secp, &msg, &sig).unwrap()
|
||||
pk.verify(&secp, msg, sig).unwrap()
|
||||
}
|
||||
|
||||
/// Computes sighash for a legacy multisig transaction input that spends either a p2sh or a p2ms output.
|
||||
|
|
|
@ -75,7 +75,7 @@ fn main() {
|
|||
// Update the witness stack.
|
||||
let signature = bitcoin::ecdsa::Signature { signature, sighash_type };
|
||||
let pk = sk.public_key(&secp);
|
||||
*sighasher.witness_mut(input_index).unwrap() = Witness::p2wpkh(&signature, pk);
|
||||
*sighasher.witness_mut(input_index).unwrap() = Witness::p2wpkh(signature, pk);
|
||||
|
||||
// Get the signed transaction.
|
||||
let tx = sighasher.into_transaction();
|
||||
|
|
|
@ -243,7 +243,7 @@ impl Witness {
|
|||
/// serialized public key. Also useful for spending a P2SH-P2WPKH output.
|
||||
///
|
||||
/// It is expected that `pubkey` is related to the secret key used to create `signature`.
|
||||
pub fn p2wpkh(signature: &ecdsa::Signature, pubkey: secp256k1::PublicKey) -> Witness {
|
||||
pub fn p2wpkh(signature: ecdsa::Signature, pubkey: secp256k1::PublicKey) -> Witness {
|
||||
let mut witness = Witness::new();
|
||||
witness.push_slice(&signature.serialize());
|
||||
witness.push_slice(&pubkey.serialize());
|
||||
|
@ -355,7 +355,7 @@ impl Witness {
|
|||
/// Pushes, as a new element on the witness, an ECDSA signature.
|
||||
///
|
||||
/// Pushes the DER encoded signature + sighash_type, requires an allocation.
|
||||
pub fn push_ecdsa_signature(&mut self, signature: &ecdsa::Signature) {
|
||||
pub fn push_ecdsa_signature(&mut self, signature: ecdsa::Signature) {
|
||||
self.push_slice(&signature.serialize())
|
||||
}
|
||||
|
||||
|
@ -657,7 +657,7 @@ mod test {
|
|||
let signature = secp256k1::ecdsa::Signature::from_der(&sig_bytes).unwrap();
|
||||
let mut witness = Witness::default();
|
||||
let signature = crate::ecdsa::Signature { signature, sighash_type: EcdsaSighashType::All };
|
||||
witness.push_ecdsa_signature(&signature);
|
||||
witness.push_ecdsa_signature(signature);
|
||||
let expected_witness = vec![hex!(
|
||||
"304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b54101")
|
||||
];
|
||||
|
|
|
@ -203,10 +203,10 @@ impl PublicKey {
|
|||
pub fn verify<C: secp256k1::Verification>(
|
||||
&self,
|
||||
secp: &Secp256k1<C>,
|
||||
msg: &secp256k1::Message,
|
||||
sig: &ecdsa::Signature,
|
||||
msg: secp256k1::Message,
|
||||
sig: ecdsa::Signature,
|
||||
) -> Result<(), secp256k1::Error> {
|
||||
secp.verify_ecdsa(msg, &sig.signature, &self.inner)
|
||||
secp.verify_ecdsa(&msg, &sig.signature, &self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -336,10 +336,10 @@ impl CompressedPublicKey {
|
|||
pub fn verify<C: secp256k1::Verification>(
|
||||
&self,
|
||||
secp: &Secp256k1<C>,
|
||||
msg: &secp256k1::Message,
|
||||
sig: &ecdsa::Signature,
|
||||
msg: secp256k1::Message,
|
||||
sig: ecdsa::Signature,
|
||||
) -> Result<(), secp256k1::Error> {
|
||||
Ok(secp.verify_ecdsa(msg, &sig.signature, &self.0)?)
|
||||
Ok(secp.verify_ecdsa(&msg, &sig.signature, &self.0)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -109,11 +109,11 @@ impl<'a> IntoIterator for &'a SerializedSignature {
|
|||
}
|
||||
|
||||
impl From<Signature> for SerializedSignature {
|
||||
fn from(value: Signature) -> Self { Self::from_signature(&value) }
|
||||
fn from(value: Signature) -> Self { Self::from_signature(value) }
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Signature> for SerializedSignature {
|
||||
fn from(value: &'a Signature) -> Self { Self::from_signature(value) }
|
||||
fn from(value: &'a Signature) -> Self { Self::from_signature(*value) }
|
||||
}
|
||||
|
||||
impl TryFrom<SerializedSignature> for Signature {
|
||||
|
@ -162,7 +162,7 @@ impl SerializedSignature {
|
|||
/// Create a SerializedSignature from a Signature.
|
||||
/// (this serializes it)
|
||||
#[inline]
|
||||
pub fn from_signature(sig: &Signature) -> SerializedSignature { sig.serialize() }
|
||||
pub fn from_signature(sig: Signature) -> SerializedSignature { sig.serialize() }
|
||||
|
||||
/// Writes this serialized signature to a `writer`.
|
||||
#[inline]
|
||||
|
|
Loading…
Reference in New Issue