]> Untitled Git - bdk/commitdiff
Fix path calculation, fix test
authorDaniela Brozzoni <brozzoni@live.it>
Sat, 22 Feb 2020 19:52:30 +0000 (11:52 -0800)
committerAlekos Filini <alekos.filini@gmail.com>
Tue, 7 Apr 2020 09:17:48 +0000 (11:17 +0200)
src/database/keyvalue.rs
src/descriptor/extended_key.rs
src/descriptor/mod.rs

index 80fdfb31dd47426b61525c694ac48da3e91ddcf3..fb285322ef7ad07a42192417c5149a96d0981eef 100644 (file)
@@ -646,7 +646,7 @@ mod test {
         assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), None);
 
         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();
         assert_eq!(res, 0);
 
@@ -654,7 +654,7 @@ mod test {
             tree.get_last_index(ScriptType::External).unwrap(),
             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...
index 9e6fc10aa2028c9fc46874639f98d19be218362a..71347f161c8b8848d2b45fe9c1e108f2d04f3179 100644 (file)
@@ -59,6 +59,14 @@ impl DescriptorExtendedKey {
             let path_as_vec: Vec<ChildNumber> = path.clone().into();
             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();
         final_path.extend_from_slice(&our_path);
         let other_path: Vec<ChildNumber> = self.final_index.as_path(index).into();
@@ -67,6 +75,7 @@ impl DescriptorExtendedKey {
         final_path.into()
     }
 
+
     pub fn derive<C: secp256k1::Verification + secp256k1::Signing>(
         &self,
         ctx: &secp256k1::Secp256k1<C>,
@@ -81,10 +90,10 @@ impl DescriptorExtendedKey {
         index: u32,
     ) -> Result<ExtendedPubKey, super::Error> {
         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))
         } else {
-            Ok(self.pubkey.derive_pub(ctx, &self.full_path(index))?)
+            Ok(self.pubkey.derive_pub(ctx, &self.path_with_index(index))?)
         }
     }
 
index bca3ddd78e71405adf99f6a4f410f5bf437059b2..ac241b53a92c1118d5c92e26764bec0d91e32ffd 100644 (file)
@@ -479,7 +479,7 @@ mod test {
                 .to_string(),
             "mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx"
         );
-        assert_eq!(desc.get_secret_keys().len(), 1);
+        assert_eq!(desc.get_secret_keys().into_iter().collect::<Vec<_>>().len(), 1);
     }
 
     #[test]
@@ -503,7 +503,7 @@ mod test {
                 .to_string(),
             "mqwpxxvfv3QbM8PU8uBx2jaNt9btQqvQNx"
         );
-        assert_eq!(desc.get_secret_keys().len(), 0);
+        assert_eq!(desc.get_secret_keys().into_iter().collect::<Vec<_>>().len(), 0);
     }
 
     #[test]
@@ -527,7 +527,7 @@ mod test {
                 .to_string(),
             "mhtuS1QaEV4HPcK4bWk4Wvpd64SUjiC5Zt"
         );
-        assert_eq!(desc.get_xprv().len(), 0);
+        assert_eq!(desc.get_xprv().into_iter().collect::<Vec<_>>().len(), 0);
     }
 
     #[test]