Skip to content

Remove MiniscriptKey associated hash types #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 24 additions & 97 deletions bitcoind-tests/tests/setup/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bitcoin::secp256k1;
use internals::hex::exts::DisplayHex;
use miniscript::descriptor::{SinglePub, SinglePubKey};
use miniscript::{
bitcoin, hash256, Descriptor, DescriptorPublicKey, Error, Miniscript, ScriptContext,
bitcoin, hash256, Descriptor, DescriptorPublicKey, Error, Miniscript, ScriptContext, StringKey,
TranslatePk, Translator,
};
use rand::RngCore;
Expand Down Expand Up @@ -108,14 +108,7 @@ impl TestData {
let ripemd160_pre = [0x78 as u8; 32];
let ripemd160 = ripemd160::Hash::hash(&ripemd160_pre);

let pubdata = PubData {
pks,
sha256,
hash256,
ripemd160,
hash160,
x_only_pks,
};
let pubdata = PubData { pks, sha256, hash256, ripemd160, hash160, x_only_pks };
let secretdata = SecretData {
sks,
sha256_pre,
Expand All @@ -124,10 +117,7 @@ impl TestData {
hash160_pre,
x_only_keypairs,
};
Self {
pubdata,
secretdata,
}
Self { pubdata, secretdata }
}
}

Expand Down Expand Up @@ -156,7 +146,7 @@ pub fn parse_insane_ms<Ctx: ScriptContext>(
) -> Miniscript<DescriptorPublicKey, Ctx> {
let ms = subs_hash_frag(ms, pubdata);
let ms =
Miniscript::<String, Ctx>::from_str_insane(&ms).expect("only parsing valid minsicripts");
Miniscript::<StringKey, Ctx>::from_str_insane(&ms).expect("only parsing valid minsicripts");
let mut translator = StrTranslatorLoose(0, pubdata);
let ms = ms.translate_pk(&mut translator).unwrap();
ms
Expand All @@ -166,17 +156,17 @@ pub fn parse_insane_ms<Ctx: ScriptContext>(
#[derive(Debug, Clone)]
struct StrDescPubKeyTranslator<'a>(usize, &'a PubData);

impl<'a> Translator<String, DescriptorPublicKey, ()> for StrDescPubKeyTranslator<'a> {
fn pk(&mut self, pk_str: &String) -> Result<DescriptorPublicKey, ()> {
let avail = !pk_str.ends_with("!");
impl<'a> Translator<StringKey, DescriptorPublicKey, ()> for StrDescPubKeyTranslator<'a> {
fn pk(&mut self, pk_str: &StringKey) -> Result<DescriptorPublicKey, ()> {
let avail = !pk_str.string.ends_with("!");
if avail {
self.0 = self.0 + 1;
if pk_str.starts_with("K") {
if pk_str.string.starts_with("K") {
Ok(DescriptorPublicKey::Single(SinglePub {
origin: None,
key: SinglePubKey::FullKey(self.1.pks[self.0]),
}))
} else if pk_str.starts_with("X") {
} else if pk_str.string.starts_with("X") {
Ok(DescriptorPublicKey::Single(SinglePub {
origin: None,
key: SinglePubKey::XOnly(self.1.x_only_pks[self.0]),
Expand All @@ -191,26 +181,6 @@ impl<'a> Translator<String, DescriptorPublicKey, ()> for StrDescPubKeyTranslator
}))
}
}

fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, ()> {
let sha = sha256::Hash::from_str(sha256).unwrap();
Ok(sha)
}

fn hash256(&mut self, hash256: &String) -> Result<hash256::Hash, ()> {
let hash256 = hash256::Hash::from_str(hash256).unwrap();
Ok(hash256)
}

fn ripemd160(&mut self, ripemd160: &String) -> Result<ripemd160::Hash, ()> {
let ripemd160 = ripemd160::Hash::from_str(ripemd160).unwrap();
Ok(ripemd160)
}

fn hash160(&mut self, hash160: &String) -> Result<hash160::Hash, ()> {
let hash160 = hash160::Hash::from_str(hash160).unwrap();
Ok(hash160)
}
}

// Translate Str to DescriptorPublicKey
Expand All @@ -219,17 +189,17 @@ impl<'a> Translator<String, DescriptorPublicKey, ()> for StrDescPubKeyTranslator
#[derive(Debug, Clone)]
struct StrTranslatorLoose<'a>(usize, &'a PubData);

impl<'a> Translator<String, DescriptorPublicKey, ()> for StrTranslatorLoose<'a> {
fn pk(&mut self, pk_str: &String) -> Result<DescriptorPublicKey, ()> {
let avail = !pk_str.ends_with("!");
impl<'a> Translator<StringKey, DescriptorPublicKey, ()> for StrTranslatorLoose<'a> {
fn pk(&mut self, pk_str: &StringKey) -> Result<DescriptorPublicKey, ()> {
let avail = !pk_str.string.ends_with("!");
if avail {
self.0 = self.0 + 1;
if pk_str.starts_with("K") {
if pk_str.string.starts_with("K") {
Ok(DescriptorPublicKey::Single(SinglePub {
origin: None,
key: SinglePubKey::FullKey(self.1.pks[self.0]),
}))
} else if pk_str.starts_with("X") {
} else if pk_str.string.starts_with("X") {
Ok(DescriptorPublicKey::Single(SinglePub {
origin: None,
key: SinglePubKey::XOnly(self.1.x_only_pks[self.0]),
Expand All @@ -248,26 +218,6 @@ impl<'a> Translator<String, DescriptorPublicKey, ()> for StrTranslatorLoose<'a>
}))
}
}

fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, ()> {
let sha = sha256::Hash::from_str(sha256).unwrap();
Ok(sha)
}

fn hash256(&mut self, hash256: &String) -> Result<hash256::Hash, ()> {
let hash256 = hash256::Hash::from_str(hash256).unwrap();
Ok(hash256)
}

fn ripemd160(&mut self, ripemd160: &String) -> Result<ripemd160::Hash, ()> {
let ripemd160 = ripemd160::Hash::from_str(ripemd160).unwrap();
Ok(ripemd160)
}

fn hash160(&mut self, hash160: &String) -> Result<hash160::Hash, ()> {
let hash160 = hash160::Hash::from_str(hash160).unwrap();
Ok(hash160)
}
}

#[allow(dead_code)]
Expand All @@ -277,7 +227,7 @@ pub fn parse_test_desc(
pubdata: &PubData,
) -> Result<Descriptor<DescriptorPublicKey>, Error> {
let desc = subs_hash_frag(desc, pubdata);
let desc = Descriptor::<String>::from_str(&desc)?;
let desc = Descriptor::<StringKey>::from_str(&desc)?;
let mut translator = StrDescPubKeyTranslator(0, pubdata);
let desc = desc
.translate_pk(&mut translator)
Expand All @@ -287,43 +237,20 @@ pub fn parse_test_desc(

// substitute hash fragments in the string as the per rules
fn subs_hash_frag(ms: &str, pubdata: &PubData) -> String {
let ms = ms.replace(
"sha256(H)",
&format!("sha256({})", &pubdata.sha256.to_string()),
);
let ms = ms.replace(
"hash256(H)",
&format!("hash256({})", &pubdata.hash256.to_string()),
);
let ms = ms.replace(
"ripemd160(H)",
&format!("ripemd160({})", &pubdata.ripemd160.to_string()),
);
let ms = ms.replace(
"hash160(H)",
&format!("hash160({})", &pubdata.hash160.to_string()),
);
let ms = ms.replace("sha256(H)", &format!("sha256({})", &pubdata.sha256.to_string()));
let ms = ms.replace("hash256(H)", &format!("hash256({})", &pubdata.hash256.to_string()));
let ms = ms.replace("ripemd160(H)", &format!("ripemd160({})", &pubdata.ripemd160.to_string()));
let ms = ms.replace("hash160(H)", &format!("hash160({})", &pubdata.hash160.to_string()));

let mut rand_hash32 = [0u8; 32];
rand::thread_rng().fill_bytes(&mut rand_hash32);

let mut rand_hash20 = [0u8; 20];
rand::thread_rng().fill_bytes(&mut rand_hash20);
let ms = ms.replace(
"sha256(H!)",
&format!("sha256({})", rand_hash32.to_lower_hex_string()),
);
let ms = ms.replace(
"hash256(H!)",
&format!("hash256({})", rand_hash32.to_lower_hex_string()),
);
let ms = ms.replace(
"ripemd160(H!)",
&format!("ripemd160({})", rand_hash20.to_lower_hex_string()),
);
let ms = ms.replace(
"hash160(H!)",
&format!("hash160({})", rand_hash20.to_lower_hex_string()),
);
let ms = ms.replace("sha256(H!)", &format!("sha256({})", rand_hash32.to_lower_hex_string()));
let ms = ms.replace("hash256(H!)", &format!("hash256({})", rand_hash32.to_lower_hex_string()));
let ms =
ms.replace("ripemd160(H!)", &format!("ripemd160({})", rand_hash20.to_lower_hex_string()));
let ms = ms.replace("hash160(H!)", &format!("hash160({})", rand_hash20.to_lower_hex_string()));
ms
}
61 changes: 20 additions & 41 deletions bitcoind-tests/tests/test_cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ where
}

/// Quickly create a BTC amount.
fn btc<F: Into<f64>>(btc: F) -> Amount {
Amount::from_btc(btc.into()).unwrap()
}
fn btc<F: Into<f64>>(btc: F) -> Amount { Amount::from_btc(btc.into()).unwrap() }

// Find the Outpoint by value.
// Ideally, we should find by scriptPubkey, but this
Expand All @@ -72,10 +70,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
let pks = &testdata.pubdata.pks;
// Generate some blocks
let blocks = cl
.generate_to_address(
500,
&cl.get_new_address(None, None).unwrap().assume_checked(),
)
.generate_to_address(500, &cl.get_new_address(None, None).unwrap().assume_checked())
.unwrap();
assert_eq!(blocks.len(), 500);

Expand All @@ -98,10 +93,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
}
// Wait for the funds to mature.
let blocks = cl
.generate_to_address(
50,
&cl.get_new_address(None, None).unwrap().assume_checked(),
)
.generate_to_address(50, &cl.get_new_address(None, None).unwrap().assume_checked())
.unwrap();
assert_eq!(blocks.len(), 50);
// Create a PSBT for each transaction.
Expand Down Expand Up @@ -140,10 +132,9 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
.get_new_address(None, Some(json::AddressType::Bech32))
.unwrap()
.assume_checked();
psbt.unsigned_tx.output.push(TxOut {
value: 99_999_000,
script_pubkey: addr.script_pubkey(),
});
psbt.unsigned_tx
.output
.push(TxOut { value: 99_999_000, script_pubkey: addr.script_pubkey() });
let mut input = psbt::Input::default();
input.witness_utxo = Some(witness_utxo);
input.witness_script = Some(desc.explicit_script().unwrap());
Expand Down Expand Up @@ -187,32 +178,25 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
for sk in sks_reqd {
let sig = secp.sign_ecdsa(&msg, &sk);
let pk = pks[sks.iter().position(|&x| x == sk).unwrap()];
psbts[i].inputs[0].partial_sigs.insert(
pk,
bitcoin::ecdsa::Signature {
sig,
hash_ty: sighash_ty,
},
);
psbts[i].inputs[0]
.partial_sigs
.insert(pk, bitcoin::ecdsa::Signature { sig, hash_ty: sighash_ty });
}
// Add the hash preimages to the psbt
psbts[i].inputs[0].sha256_preimages.insert(
testdata.pubdata.sha256,
testdata.secretdata.sha256_pre.to_vec(),
);
psbts[i].inputs[0]
.sha256_preimages
.insert(testdata.pubdata.sha256, testdata.secretdata.sha256_pre.to_vec());
psbts[i].inputs[0].hash256_preimages.insert(
sha256d::Hash::from_byte_array(testdata.pubdata.hash256.to_byte_array()),
testdata.secretdata.hash256_pre.to_vec(),
);
println!("{}", ms);
psbts[i].inputs[0].hash160_preimages.insert(
testdata.pubdata.hash160,
testdata.secretdata.hash160_pre.to_vec(),
);
psbts[i].inputs[0].ripemd160_preimages.insert(
testdata.pubdata.ripemd160,
testdata.secretdata.ripemd160_pre.to_vec(),
);
psbts[i].inputs[0]
.hash160_preimages
.insert(testdata.pubdata.hash160, testdata.secretdata.hash160_pre.to_vec());
psbts[i].inputs[0]
.ripemd160_preimages
.insert(testdata.pubdata.ripemd160, testdata.secretdata.ripemd160_pre.to_vec());
// Finalize the transaction using psbt
// Let miniscript do it's magic!
if let Err(e) = psbts[i].finalize_mall_mut(&secp) {
Expand All @@ -232,10 +216,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
}
// Finally mine the blocks and await confirmations
let _blocks = cl
.generate_to_address(
10,
&cl.get_new_address(None, None).unwrap().assume_checked(),
)
.generate_to_address(10, &cl.get_new_address(None, None).unwrap().assume_checked())
.unwrap();
// Get the required transactions from the node mined in the blocks.
for txid in spend_txids {
Expand All @@ -247,9 +228,7 @@ pub fn test_from_cpp_ms(cl: &Client, testdata: &TestData) {
}

#[test]
fn test_setup() {
setup::setup();
}
fn test_setup() { setup::setup(); }

#[test]
fn tests_from_cpp() {
Expand Down
Loading