use bitcoin::blockdata::script;
use bitcoin::hashes::Hash;
use bitcoin::{PubkeyHash, Script};
use crate::miniscript::context;
use crate::prelude::*;
use crate::{ScriptContext, ToPublicKey};
pub(crate) fn varint_len(n: usize) -> usize {
bitcoin::VarInt(n as u64).len()
}
pub(crate) fn witness_size(wit: &[Vec<u8>]) -> usize {
wit.iter().map(Vec::len).sum::<usize>() + varint_len(wit.len())
}
pub(crate) fn witness_to_scriptsig(witness: &[Vec<u8>]) -> Script {
let mut b = script::Builder::new();
for wit in witness {
if let Ok(n) = script::read_scriptint(wit) {
b = b.push_int(n);
} else {
b = b.push_slice(wit);
}
}
b.into_script()
}
pub(crate) trait MsKeyBuilder {
fn push_ms_key<Pk, Ctx>(self, key: &Pk) -> Self
where
Pk: ToPublicKey,
Ctx: ScriptContext;
fn push_ms_key_hash<Pk, Ctx>(self, key: &Pk) -> Self
where
Pk: ToPublicKey,
Ctx: ScriptContext;
}
impl MsKeyBuilder for script::Builder {
fn push_ms_key<Pk, Ctx>(self, key: &Pk) -> Self
where
Pk: ToPublicKey,
Ctx: ScriptContext,
{
match Ctx::sig_type() {
context::SigType::Ecdsa => self.push_key(&key.to_public_key()),
context::SigType::Schnorr => self.push_slice(&key.to_x_only_pubkey().serialize()),
}
}
fn push_ms_key_hash<Pk, Ctx>(self, key: &Pk) -> Self
where
Pk: ToPublicKey,
Ctx: ScriptContext,
{
match Ctx::sig_type() {
context::SigType::Ecdsa => self.push_slice(&key.to_public_key().pubkey_hash()),
context::SigType::Schnorr => {
self.push_slice(&PubkeyHash::hash(&key.to_x_only_pubkey().serialize()))
}
}
}
}