Skip to content
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

One shot cargo fmt #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
env:
DO_FUZZ: true
DO_INTEGRATION: true
DO_LINT: true
- rust: beta
env:
DUMMY: true
Expand Down
8 changes: 8 additions & 0 deletions contrib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ then
cargo test --features integration
)
fi
# Lint if told to
if [ "$DO_LINT" = true ]
then
(
rustup component add rustfmt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks it is already installed, CI prints:
info: component 'rustfmt' for target 'x86_64-unknown-linux-gnu' is up to date

cargo fmt --all -- --check
)
fi
17 changes: 6 additions & 11 deletions examples/pset_blind_coinjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use std::{collections::HashMap, str::FromStr};

use elements::confidential::{AssetBlindingFactor, ValueBlindingFactor};
use elements::{
bitcoin::PublicKey, pset::PartiallySignedTransaction as Pset, OutPoint,
Script, TxOutSecrets, TxOutWitness, Txid, WScriptHash,
bitcoin::PublicKey, pset::PartiallySignedTransaction as Pset, OutPoint, Script, TxOutSecrets,
TxOutWitness, Txid, WScriptHash,
};
use elements::{pset, secp256k1_zkp};

Expand Down Expand Up @@ -264,19 +264,14 @@ fn main() {
// ----------------------------------------------------------
// B Adds it's own outputs. Step 2 completed
// ----- Step 3: B to blind it's own outputs
let inp_txout_sec = [
None,
Some(&asset_txout_secrets.sec),
];
let inp_txout_sec = [None, Some(&asset_txout_secrets.sec)];

pset.blind_non_last(&mut rng, &secp, &inp_txout_sec).unwrap();
pset.blind_non_last(&mut rng, &secp, &inp_txout_sec)
.unwrap();
assert_eq!(pset, deser_pset(&tests["pset_coinjoined_B_blinded"]));

// Step 4: A blinds it's own inputs
let inp_txout_sec = [
Some(&btc_txout_secrets.sec),
None,
];
let inp_txout_sec = [Some(&btc_txout_secrets.sec), None];
pset.blind_last(&mut rng, &secp, &inp_txout_sec).unwrap();
assert_eq!(pset, deser_pset(&tests["pset_coinjoined_blinded"]));

Expand Down
10 changes: 2 additions & 8 deletions examples/raw_blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,9 @@ fn main() {

// Finalize(TODO in miniscript)
pset.inputs_mut()[0].partial_sigs.clear();
pset.inputs_mut()[0].final_script_witness = Some(vec![
inp0_sig,
inp0_pk.to_bytes(),
]);
pset.inputs_mut()[0].final_script_witness = Some(vec![inp0_sig, inp0_pk.to_bytes()]);
pset.inputs_mut()[1].partial_sigs.clear();
pset.inputs_mut()[1].final_script_witness = Some(vec![
inp1_sig,
inp1_pk.to_bytes(),
]);
pset.inputs_mut()[1].final_script_witness = Some(vec![inp1_sig, inp1_pk.to_bytes()]);
assert_eq!(pset, deser_pset(&tests["finalized"]));

// Extracted tx
Expand Down
17 changes: 9 additions & 8 deletions examples/tx.rs

Large diffs are not rendered by default.

62 changes: 43 additions & 19 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ use std::fmt;
use std::str::FromStr;

use bitcoin::bech32::{self, u5, FromBase32, ToBase32};
use bitcoin::hashes::Hash;
use bitcoin::util::base58;
use bitcoin::PublicKey;
use bitcoin::hashes::Hash;
use secp256k1_zkp;
#[cfg(feature = "serde")]
use serde;

use blech32;

use {PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};
use {opcodes, script};
use {PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash};

/// Encoding error
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -71,10 +71,18 @@ impl fmt::Display for AddressError {
write!(f, "invalid witness script version: {}", wver)
}
AddressError::InvalidWitnessProgramLength(ref len) => {
write!(f, "the witness program must be between 2 and 40 bytes in length, not {}", len)
write!(
f,
"the witness program must be between 2 and 40 bytes in length, not {}",
len
)
}
AddressError::InvalidSegwitV0ProgramLength(ref len) => {
write!(f, "a v0 witness program must be length 20 or 32, not {}", len)
write!(
f,
"a v0 witness program must be length 20 or 32, not {}",
len
)
}
AddressError::InvalidBlindingPubKey(ref e) => {
write!(f, "an invalid blinding pubkey was encountered: {}", e)
Expand Down Expand Up @@ -185,7 +193,8 @@ impl Address {
params: &'static AddressParams,
) -> Address {
let mut hash_engine = PubkeyHash::engine();
pk.write_into(&mut hash_engine).expect("engines don't error");
pk.write_into(&mut hash_engine)
.expect("engines don't error");

Address {
params,
Expand Down Expand Up @@ -217,7 +226,8 @@ impl Address {
params: &'static AddressParams,
) -> Address {
let mut hash_engine = WPubkeyHash::engine();
pk.write_into(&mut hash_engine).expect("engines don't error");
pk.write_into(&mut hash_engine)
.expect("engines don't error");

Address {
params,
Expand All @@ -237,7 +247,8 @@ impl Address {
params: &'static AddressParams,
) -> Address {
let mut hash_engine = ScriptHash::engine();
pk.write_into(&mut hash_engine).expect("engines don't error");
pk.write_into(&mut hash_engine)
.expect("engines don't error");

let builder = script::Builder::new()
.push_int(0)
Expand Down Expand Up @@ -335,7 +346,9 @@ impl Address {
Payload::WitnessProgram {
version: witver,
program: ref witprog,
} => script::Builder::new().push_int(witver.to_u8() as i64).push_slice(&witprog),
} => script::Builder::new()
.push_int(witver.to_u8() as i64)
.push_slice(&witprog),
}
.into_script()
}
Expand Down Expand Up @@ -393,9 +406,11 @@ impl Address {
return Err(AddressError::InvalidWitnessVersion(version.to_u8()));
}
if data.len() < 2 || data.len() > 40 + if blinded { 33 } else { 0 } {
return Err(AddressError::InvalidWitnessProgramLength(data.len() - if blinded { 33 } else { 0 }));
return Err(AddressError::InvalidWitnessProgramLength(
data.len() - if blinded { 33 } else { 0 },
));
}

// Specific segwit v0 check.
if !blinded && version.to_u8() == 0 && data.len() != 20 && data.len() != 32 {
return Err(AddressError::InvalidSegwitV0ProgramLength(data.len()));
Expand Down Expand Up @@ -423,10 +438,7 @@ impl Address {

Ok(Address {
params,
payload: Payload::WitnessProgram {
version,
program,
},
payload: Payload::WitnessProgram { version, program },
blinding_pubkey,
})
}
Expand Down Expand Up @@ -558,7 +570,11 @@ impl fmt::Display for Address {
blech32::encode_to_fmt(fmt, &hrp, &b32_data, blech32::Variant::Blech32m)
}
} else {
let var = if witver.to_u8() == 0 { bech32::Variant::Bech32 } else { bech32::Variant::Bech32m };
let var = if witver.to_u8() == 0 {
bech32::Variant::Bech32
} else {
bech32::Variant::Bech32m
};
let mut bech32_writer = bech32::Bech32Writer::new(hrp, var, fmt)?;
bech32::WriteBase32::write_u5(&mut bech32_writer, witver)?;
bech32::ToBase32::write_base32(&witprog, &mut bech32_writer)
Expand Down Expand Up @@ -700,9 +716,9 @@ mod test {
use super::*;
use bitcoin::util::key;
use secp256k1_zkp::{PublicKey, Secp256k1};
use Script;
#[cfg(feature = "serde")]
use serde_json;
use Script;

fn roundtrips(addr: &Address) {
assert_eq!(
Expand All @@ -719,7 +735,9 @@ mod test {
);
#[cfg(feature = "serde")]
assert_eq!(
serde_json::from_value::<Address>(serde_json::to_value(&addr).unwrap()).ok().as_ref(),
serde_json::from_value::<Address>(serde_json::to_value(&addr).unwrap())
.ok()
.as_ref(),
Some(addr)
);
}
Expand Down Expand Up @@ -788,7 +806,12 @@ mod test {

for &(a, blinded, ref params) in &addresses {
let result = a.parse();
assert!(result.is_ok(), "vector: {}, err: \"{}\"", a, result.unwrap_err());
assert!(
result.is_ok(),
"vector: {}, err: \"{}\"",
a,
result.unwrap_err()
);
let addr: Address = result.unwrap();
assert_eq!(a, &addr.to_string(), "vector: {}", a);
assert_eq!(blinded, addr.is_blinded());
Expand All @@ -815,7 +838,8 @@ mod test {
"v0 witness program must use b(l)ech32 not b(l)ech32m",
);

let address: Result<Address, _> = "ert130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqqu2tys".parse();
let address: Result<Address, _> =
"ert130xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqqu2tys".parse();
assert_eq!(
address.err().unwrap().to_string(),
"invalid witness script version: 17",
Expand Down
9 changes: 5 additions & 4 deletions src/blech32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ fn create_checksum(hrp: &[u8], data: &[u5], variant: Variant) -> Vec<u5> {
let mut checksum: Vec<u5> = Vec::new();
// ELEMENTS: 6->12
for p in 0..12 {
checksum.push(u5::try_from_u8(((plm >> (5 * (11 - p))) & 0x1f) as u8).unwrap()); // ELEMENTS: 5->11
checksum.push(u5::try_from_u8(((plm >> (5 * (11 - p))) & 0x1f) as u8).unwrap());
// ELEMENTS: 5->11
}
checksum
}
Expand Down Expand Up @@ -280,16 +281,16 @@ mod test {

let data2 = data.to_vec();
let mut data2_b32 = data2.to_base32();
data2_b32.extend(vec![u5::try_from_u8(0).unwrap(); 1023]);
data2_b32.extend(vec![u5::try_from_u8(0).unwrap(); 1023]);
let polymod2 = polymod(&data2_b32);
assert_eq!(polymod1, polymod2);
}

#[test]
fn test_checksum() {
let data = vec![7,2,3,4,5,6,7,8,9,234,123,213,16];
let data = vec![7, 2, 3, 4, 5, 6, 7, 8, 9, 234, 123, 213, 16];
let cs = create_checksum(b"lq", &data.to_base32(), Variant::Blech32);
let expected_cs = vec![22,13,13,5,4,4,23,7,28,21,30,12];
let expected_cs = vec![22, 13, 13, 5, 4, 4, 23, 7, 28, 21, 30, 12];
for i in 0..expected_cs.len() {
assert_eq!(expected_cs[i], *cs[i].as_ref());
}
Expand Down
Loading