Skip to content

Commit

Permalink
Merge #572: Reduce line count
Browse files Browse the repository at this point in the history
26bb52e Set struct_lit_width to 80 (Tobin C. Harding)
ced7d77 Set fn_call_with to 80 (Tobin C. Harding)
6fe7b20 Set fn_single_line to true (Tobin C. Harding)

Pull request description:

  This PR is formatting changes only, totally non-urgent.

  Reduce line count by setting a couple of rustfmt config options to be the same as they are in `rust-bitcoin`. Final patch removes whitespace in trait impl blocks, and was the impetus for this PR.

ACKs for top commit:
  apoelstra:
    ACK 26bb52e

Tree-SHA512: 185006a09cf8fe8f8258a19183dbd7fc1c860f125ca9a55f7903aef7bb48b8fbc3a5319f81bfa1fcb6c1fbeb47345f1c251f27ee416d3132e8322cae9c8e213f
  • Loading branch information
apoelstra committed Sep 23, 2023
2 parents 780dbc6 + 26bb52e commit 3c6ae26
Show file tree
Hide file tree
Showing 39 changed files with 656 additions and 2,180 deletions.
55 changes: 11 additions & 44 deletions bitcoind-tests/tests/setup/test_util.rs
Original file line number Diff line number Diff line change
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 @@ -287,43 +277,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
58 changes: 21 additions & 37 deletions bitcoind-tests/tests/test_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ mod setup;
use rand::RngCore;
use setup::test_util::{self, TestData};
/// 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 spk
fn get_vout(cl: &Client, txid: Txid, value: u64, spk: ScriptBuf) -> (OutPoint, TxOut) {
Expand Down Expand Up @@ -137,10 +135,9 @@ pub fn test_desc_satisfy(
.assume_checked();
// Had to decrease 'value', so that fees can be increased
// (Was getting insufficient fees error, for deep script trees)
psbt.unsigned_tx.output.push(TxOut {
value: 99_997_000,
script_pubkey: addr.script_pubkey(),
});
psbt.unsigned_tx
.output
.push(TxOut { value: 99_997_000, script_pubkey: addr.script_pubkey() });
let mut input = psbt::Input::default();
input
.update_with_descriptor_unchecked(&definite_desc)
Expand Down Expand Up @@ -180,10 +177,8 @@ pub fn test_desc_satisfy(
rand::thread_rng().fill_bytes(&mut aux_rand);
let schnorr_sig =
secp.sign_schnorr_with_aux_rand(&msg, &internal_keypair, &aux_rand);
psbt.inputs[0].tap_key_sig = Some(taproot::Signature {
sig: schnorr_sig,
hash_ty: hash_ty,
});
psbt.inputs[0].tap_key_sig =
Some(taproot::Signature { sig: schnorr_sig, hash_ty: hash_ty });
} else {
// No internal key
}
Expand All @@ -208,13 +203,9 @@ pub fn test_desc_satisfy(
let sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &aux_rand);
let x_only_pk =
x_only_pks[xonly_keypairs.iter().position(|&x| x == keypair).unwrap()];
psbt.inputs[0].tap_script_sigs.insert(
(x_only_pk, leaf_hash),
taproot::Signature {
sig,
hash_ty: hash_ty,
},
);
psbt.inputs[0]
.tap_script_sigs
.insert((x_only_pk, leaf_hash), taproot::Signature { sig, hash_ty: hash_ty });
}
}
_ => {
Expand Down Expand Up @@ -263,33 +254,26 @@ pub fn test_desc_satisfy(
let sig = secp.sign_ecdsa(&msg, &sk);
let pk = pks[sks.iter().position(|&x| x == sk).unwrap()];
assert!(secp.verify_ecdsa(&msg, &sig, &pk.inner).is_ok());
psbt.inputs[0].partial_sigs.insert(
pk,
ecdsa::Signature {
sig,
hash_ty: hash_ty,
},
);
psbt.inputs[0]
.partial_sigs
.insert(pk, ecdsa::Signature { sig, hash_ty: hash_ty });
}
}
}
// Add the hash preimages to the psbt
psbt.inputs[0].sha256_preimages.insert(
testdata.pubdata.sha256,
testdata.secretdata.sha256_pre.to_vec(),
);
psbt.inputs[0]
.sha256_preimages
.insert(testdata.pubdata.sha256, testdata.secretdata.sha256_pre.to_vec());
psbt.inputs[0].hash256_preimages.insert(
sha256d::Hash::from_byte_array(testdata.pubdata.hash256.to_byte_array()),
testdata.secretdata.hash256_pre.to_vec(),
);
psbt.inputs[0].hash160_preimages.insert(
testdata.pubdata.hash160,
testdata.secretdata.hash160_pre.to_vec(),
);
psbt.inputs[0].ripemd160_preimages.insert(
testdata.pubdata.ripemd160,
testdata.secretdata.ripemd160_pre.to_vec(),
);
psbt.inputs[0]
.hash160_preimages
.insert(testdata.pubdata.hash160, testdata.secretdata.hash160_pre.to_vec());
psbt.inputs[0]
.ripemd160_preimages
.insert(testdata.pubdata.ripemd160, testdata.secretdata.ripemd160_pre.to_vec());
println!("Testing descriptor: {}", definite_desc);
// Finalize the transaction using psbt
// Let miniscript do it's magic!
Expand Down
54 changes: 15 additions & 39 deletions examples/psbt_sign_finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ fn main() {
let bridge_descriptor = Descriptor::from_str(&s).unwrap();
//let bridge_descriptor = Descriptor::<bitcoin::PublicKey>::from_str(&s).expect("parse descriptor string");
assert!(bridge_descriptor.sanity_check().is_ok());
println!(
"Bridge pubkey script: {}",
bridge_descriptor.script_pubkey()
);
println!(
"Bridge address: {}",
bridge_descriptor.address(Network::Regtest).unwrap()
);
println!("Bridge pubkey script: {}", bridge_descriptor.script_pubkey());
println!("Bridge address: {}", bridge_descriptor.address(Network::Regtest).unwrap());
println!(
"Weight for witness satisfaction cost {}",
bridge_descriptor.max_weight_to_satisfy().unwrap()
Expand All @@ -37,37 +31,25 @@ fn main() {
let master_private_key_str = "cQhdvB3McbBJdx78VSSumqoHQiSXs75qwLptqwxSQBNBMDxafvaw";
let _master_private_key =
PrivateKey::from_str(master_private_key_str).expect("Can't create private key");
println!(
"Master public key: {}",
_master_private_key.public_key(&secp256k1)
);
println!("Master public key: {}", _master_private_key.public_key(&secp256k1));

let backup1_private_key_str = "cWA34TkfWyHa3d4Vb2jNQvsWJGAHdCTNH73Rht7kAz6vQJcassky";
let backup1_private =
PrivateKey::from_str(backup1_private_key_str).expect("Can't create private key");

println!(
"Backup1 public key: {}",
backup1_private.public_key(&secp256k1)
);
println!("Backup1 public key: {}", backup1_private.public_key(&secp256k1));

let backup2_private_key_str = "cPJFWUKk8sdL7pcDKrmNiWUyqgovimmhaaZ8WwsByDaJ45qLREkh";
let backup2_private =
PrivateKey::from_str(backup2_private_key_str).expect("Can't create private key");

println!(
"Backup2 public key: {}",
backup2_private.public_key(&secp256k1)
);
println!("Backup2 public key: {}", backup2_private.public_key(&secp256k1));

let backup3_private_key_str = "cT5cH9UVm81W5QAf5KABXb23RKNSMbMzMx85y6R2mF42L94YwKX6";
let _backup3_private =
PrivateKey::from_str(backup3_private_key_str).expect("Can't create private key");

println!(
"Backup3 public key: {}",
_backup3_private.public_key(&secp256k1)
);
println!("Backup3 public key: {}", _backup3_private.public_key(&secp256k1));

let spend_tx = Transaction {
version: 2,
Expand Down Expand Up @@ -104,15 +86,13 @@ fn main() {
txin.sequence = Sequence::from_height(26); //Sequence::MAX; //
psbt.unsigned_tx.input.push(txin);

psbt.unsigned_tx.output.push(TxOut {
script_pubkey: receiver.script_pubkey(),
value: amount / 5 - 500,
});
psbt.unsigned_tx
.output
.push(TxOut { script_pubkey: receiver.script_pubkey(), value: amount / 5 - 500 });

psbt.unsigned_tx.output.push(TxOut {
script_pubkey: bridge_descriptor.script_pubkey(),
value: amount * 4 / 5,
});
psbt.unsigned_tx
.output
.push(TxOut { script_pubkey: bridge_descriptor.script_pubkey(), value: amount * 4 / 5 });

// Generating signatures & witness data

Expand Down Expand Up @@ -148,13 +128,9 @@ fn main() {
let pk2 = backup2_private.public_key(&secp256k1);
assert!(secp256k1.verify_ecdsa(&msg, &sig2, &pk2.inner).is_ok());

psbt.inputs[0].partial_sigs.insert(
pk1,
bitcoin::ecdsa::Signature {
sig: sig1,
hash_ty: hash_ty,
},
);
psbt.inputs[0]
.partial_sigs
.insert(pk1, bitcoin::ecdsa::Signature { sig: sig1, hash_ty: hash_ty });

println!("{:#?}", psbt);

Expand Down
Loading

0 comments on commit 3c6ae26

Please sign in to comment.