Less cloning and pattern simplifications (#3216)

* Less cloning and pattern simplifications

* Revert inclusive range and remove unecessary Error:From
This commit is contained in:
Quentin Le Sceller
2020-02-05 11:02:07 -05:00
committed by GitHub
parent a41965e024
commit c4e69717ab
34 changed files with 153 additions and 263 deletions
+3 -7
View File
@@ -382,7 +382,7 @@ impl ExtendedPrivKey {
passphrase: &str,
is_floo: bool,
) -> Result<ExtendedPrivKey, Error> {
let seed = mnemonic::to_seed(mnemonic, passphrase).map_err(|e| Error::MnemonicError(e))?;
let seed = mnemonic::to_seed(mnemonic, passphrase).map_err(Error::MnemonicError)?;
let mut hasher = BIP32GrinHasher::new(is_floo);
let key = ExtendedPrivKey::new_master(secp, &mut hasher, &seed)?;
Ok(key)
@@ -460,9 +460,7 @@ impl ExtendedPrivKey {
// Do SHA256 of just the ECDSA pubkey
let sha2_res = hasher.sha_256(&pk.public_key.serialize_vec(&secp, true)[..]);
// do RIPEMD160
let ripemd_res = hasher.ripemd_160(&sha2_res);
// Return
ripemd_res
hasher.ripemd_160(&sha2_res)
}
/// Returns the first four bytes of the identifier
@@ -567,9 +565,7 @@ impl ExtendedPubKey {
// Do SHA256 of just the ECDSA pubkey
let sha2_res = hasher.sha_256(&self.public_key.serialize_vec(secp, true)[..]);
// do RIPEMD160
let ripemd_res = hasher.ripemd_160(&sha2_res);
// Return
ripemd_res
hasher.ripemd_160(&sha2_res)
}
/// Returns the first four bytes of the identifier
+4 -4
View File
@@ -166,14 +166,14 @@ impl Keychain for ExtKeychain {
let keys = blind_sum
.positive_blinding_factors
.iter()
.filter_map(|b| b.secret_key(&self.secp).ok().clone())
.filter_map(|b| b.secret_key(&self.secp).ok())
.collect::<Vec<SecretKey>>();
pos_keys.extend(keys);
let keys = blind_sum
.negative_blinding_factors
.iter()
.filter_map(|b| b.secret_key(&self.secp).ok().clone())
.filter_map(|b| b.secret_key(&self.secp).ok())
.collect::<Vec<SecretKey>>();
neg_keys.extend(keys);
@@ -265,7 +265,7 @@ mod test {
// adding secret keys 1 and 2 to give secret key 3
let mut skey3 = skey1.clone();
let _ = skey3.add_assign(&keychain.secp, &skey2).unwrap();
skey3.add_assign(&keychain.secp, &skey2).unwrap();
// create commitments for secret keys 1, 2 and 3
// all committing to the value 0 (which is what we do for tx_kernels)
@@ -276,7 +276,7 @@ mod test {
// now sum commitments for keys 1 and 2
let sum = keychain
.secp
.commit_sum(vec![commit_1.clone(), commit_2.clone()], vec![])
.commit_sum(vec![commit_1, commit_2], vec![])
.unwrap();
// confirm the commitment for key 3 matches the sum of the commitments 1 and 2
+3 -3
View File
@@ -112,7 +112,7 @@ pub fn to_entropy(mnemonic: &str) -> Result<Vec<u8>, Error> {
}
/// Converts entropy to a mnemonic
pub fn from_entropy(entropy: &Vec<u8>) -> Result<String, Error> {
pub fn from_entropy(entropy: &[u8]) -> Result<String, Error> {
let sizes: [usize; 5] = [16, 20, 24, 28, 32];
let length = entropy.len();
if !sizes.contains(&length) {
@@ -124,7 +124,7 @@ pub fn from_entropy(entropy: &Vec<u8>) -> Result<String, Error> {
let mut hash = [0; 32];
let mut sha2sum = Sha256::default();
sha2sum.input(&entropy.clone());
sha2sum.input(entropy);
hash.copy_from_slice(sha2sum.result().as_slice());
let checksum = (hash[0] >> 8 - checksum_bits) & mask;
@@ -149,7 +149,7 @@ pub fn from_entropy(entropy: &Vec<u8>) -> Result<String, Error> {
let words: Vec<String> = indexes.iter().map(|x| WORDS[*x as usize].clone()).collect();
let mnemonic = words.join(" ");
Ok(mnemonic.to_owned())
Ok(mnemonic)
}
/// Converts a nemonic and a passphrase into a seed
+5 -5
View File
@@ -163,7 +163,7 @@ impl Identifier {
let mut p = ExtKeychainPath::from_identifier(&self);
if p.depth > 0 {
p.path[p.depth as usize - 1] = ChildNumber::from(0);
p.depth = p.depth - 1;
p.depth -= 1;
}
Identifier::from_path(&p)
}
@@ -176,7 +176,7 @@ impl Identifier {
}
pub fn to_bytes(&self) -> [u8; IDENTIFIER_SIZE] {
self.0.clone()
self.0
}
pub fn from_pubkey(secp: &Secp256k1, pubkey: &PublicKey) -> Identifier {
@@ -308,7 +308,7 @@ impl BlindingFactor {
// and secp lib checks this
Ok(secp::key::ZERO_KEY)
} else {
secp::key::SecretKey::from_slice(secp, &self.0).map_err(|e| Error::Secp(e))
secp::key::SecretKey::from_slice(secp, &self.0).map_err(Error::Secp)
}
}
@@ -579,7 +579,7 @@ mod test {
// split a key, sum the split keys and confirm the sum matches the original key
let mut skey_sum = split.blind_1.secret_key(&secp).unwrap();
let skey_2 = split.blind_2.secret_key(&secp).unwrap();
let _ = skey_sum.add_assign(&secp, &skey_2).unwrap();
skey_sum.add_assign(&secp, &skey_2).unwrap();
assert_eq!(skey_in, skey_sum);
}
@@ -592,7 +592,7 @@ mod test {
let skey_zero = ZERO_KEY;
let mut skey_out = skey_in.clone();
let _ = skey_out.add_assign(&secp, &skey_zero).unwrap();
skey_out.add_assign(&secp, &skey_zero).unwrap();
assert_eq!(skey_in, skey_out);
}
+1 -1
View File
@@ -123,7 +123,7 @@ impl ViewKey {
{
let (secret_key, chain_code) = self.ckd_pub_tweak(secp, hasher, i)?;
let mut public_key = self.public_key.clone();
let mut public_key = self.public_key;
public_key.add_exp_assign(secp, &secret_key)?;
let switch_public_key = match &self.switch_public_key {