Fix path calculation, fix test

This commit is contained in:
Daniela Brozzoni 2020-02-22 11:52:30 -08:00 committed by Alekos Filini
parent 2a7c7d5272
commit 914719ccf4
No known key found for this signature in database
GPG Key ID: 5E8AFC3034FDFA4F
3 changed files with 16 additions and 7 deletions

View File

@ -646,7 +646,7 @@ mod test {
assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), None); assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), None);
let res = tree.increment_last_index(ScriptType::External).unwrap(); let res = tree.increment_last_index(ScriptType::External).unwrap();
assert_eq!(res, 1337); assert_eq!(res, 1338);
let res = tree.increment_last_index(ScriptType::Internal).unwrap(); let res = tree.increment_last_index(ScriptType::Internal).unwrap();
assert_eq!(res, 0); assert_eq!(res, 0);
@ -654,7 +654,7 @@ mod test {
tree.get_last_index(ScriptType::External).unwrap(), tree.get_last_index(ScriptType::External).unwrap(),
Some(1338) Some(1338)
); );
assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), Some(1)); assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), Some(0));
} }
// TODO: more tests... // TODO: more tests...

View File

@ -59,6 +59,14 @@ impl DescriptorExtendedKey {
let path_as_vec: Vec<ChildNumber> = path.clone().into(); let path_as_vec: Vec<ChildNumber> = path.clone().into();
final_path.extend_from_slice(&path_as_vec); final_path.extend_from_slice(&path_as_vec);
} }
let our_path: Vec<ChildNumber> = self.path_with_index(index).into();
final_path.extend_from_slice(&our_path);
final_path.into()
}
pub fn path_with_index(&self, index: u32) -> DerivationPath {
let mut final_path: Vec<ChildNumber> = Vec::new();
let our_path: Vec<ChildNumber> = self.path.clone().into(); let our_path: Vec<ChildNumber> = self.path.clone().into();
final_path.extend_from_slice(&our_path); final_path.extend_from_slice(&our_path);
let other_path: Vec<ChildNumber> = self.final_index.as_path(index).into(); let other_path: Vec<ChildNumber> = self.final_index.as_path(index).into();
@ -67,6 +75,7 @@ impl DescriptorExtendedKey {
final_path.into() final_path.into()
} }
pub fn derive<C: secp256k1::Verification + secp256k1::Signing>( pub fn derive<C: secp256k1::Verification + secp256k1::Signing>(
&self, &self,
ctx: &secp256k1::Secp256k1<C>, ctx: &secp256k1::Secp256k1<C>,
@ -81,10 +90,10 @@ impl DescriptorExtendedKey {
index: u32, index: u32,
) -> Result<ExtendedPubKey, super::Error> { ) -> Result<ExtendedPubKey, super::Error> {
if let Some(xprv) = self.secret { if let Some(xprv) = self.secret {
let derive_priv = xprv.derive_priv(ctx, &self.full_path(index))?; let derive_priv = xprv.derive_priv(ctx, &self.path_with_index(index))?;
Ok(ExtendedPubKey::from_private(ctx, &derive_priv)) Ok(ExtendedPubKey::from_private(ctx, &derive_priv))
} else { } else {
Ok(self.pubkey.derive_pub(ctx, &self.full_path(index))?) Ok(self.pubkey.derive_pub(ctx, &self.path_with_index(index))?)
} }
} }

View File

@ -479,7 +479,7 @@ mod test {
.to_string(), .to_string(),
"mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx" "mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx"
); );
assert_eq!(desc.get_secret_keys().len(), 1); assert_eq!(desc.get_secret_keys().into_iter().collect::<Vec<_>>().len(), 1);
} }
#[test] #[test]
@ -503,7 +503,7 @@ mod test {
.to_string(), .to_string(),
"mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx" "mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx"
); );
assert_eq!(desc.get_secret_keys().len(), 0); assert_eq!(desc.get_secret_keys().into_iter().collect::<Vec<_>>().len(), 0);
} }
#[test] #[test]
@ -527,7 +527,7 @@ mod test {
.to_string(), .to_string(),
"mhtuS1QaEV4HPcK4bWk4Wvpd64SUjiC5Zt" "mhtuS1QaEV4HPcK4bWk4Wvpd64SUjiC5Zt"
); );
assert_eq!(desc.get_xprv().len(), 0); assert_eq!(desc.get_xprv().into_iter().collect::<Vec<_>>().len(), 0);
} }
#[test] #[test]