@@ -603,27 +603,11 @@ impl Wallet {
603603 /// This panics when the caller requests for an address of derivation index greater than the
604604 /// [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) max index.
605605 pub fn peek_address ( & self , keychain : KeychainKind , mut index : u32 ) -> Result < AddressInfo , KeychainNotInKeyRingError > {
606- // let keychain = self.map_keychain(keychain);
607- // TODO: This will not work for KeychainKind::Other and is just working as a hack for Default and Change
608-
609- // if let KeychainKind::Other(ref name): = keychain {
610- // self.keychains()
611- // .find(|kc| kc.1.descriptor_id() == name.clone())
612- // } else {
613- // self.keychains()
614- // .find(|kc| kc.0 == keychain)
615- // }
616-
617- // let keychain = if keychain.0 == KeychainKind::Other {
618- // self.keychains()
619- // .find(|kc| kc.1.descriptor_id() == keychain.1.expect("should have a value"))
620- // .ok_or(KeychainNotInKeyRingError::KeychainNotFound)?
621- // } else {
622- // self.keychains()
623- // .find(|kc| kc.0 == keychain.0)
624- // .ok_or(KeychainNotInKeyRingError::KeychainNotFound)?
625- // };
626-
606+ // TODO #266: Add test for when the keychain provided is not part of the key_ring
607+ if !self . indexed_graph . index . keychains ( ) . any ( |( k, _) | k == keychain) {
608+ return Err ( KeychainNotInKeyRingError :: KeychainNotFound ( keychain) ) ;
609+ }
610+
627611 let mut spk_iter = self
628612 . indexed_graph
629613 . index
@@ -668,24 +652,28 @@ impl Wallet {
668652 /// println!("Next address: {}", next_address.address);
669653 /// # Ok::<(), anyhow::Error>(())
670654 /// ```
671- // pub fn reveal_next_address(&mut self, keychain: KeychainKind) -> AddressInfo {
672- // let keychain = self.map_keychain(keychain);
673- // let index = &mut self.indexed_graph.index;
674- // let stage = &mut self.stage;
675- //
676- // let ((index, spk), index_changeset) = index
677- // .reveal_next_spk(keychain)
678- // .expect("keychain must exist");
679- //
680- // stage.merge(index_changeset.into());
681- //
682- // AddressInfo {
683- // index,
684- // address: Address::from_script(spk.as_script(), self.network)
685- // .expect("must have address form"),
686- // keychain,
687- // }
688- // }
655+ pub fn reveal_next_address ( & mut self , keychain : KeychainKind ) -> Result < AddressInfo , KeychainNotInKeyRingError > {
656+ // TODO #266: Add test for when the keychain provided is not part of the key_ring
657+ if !self . indexed_graph . index . keychains ( ) . any ( |( k, _) | k == keychain) {
658+ return Err ( KeychainNotInKeyRingError :: KeychainNotFound ( keychain) ) ;
659+ }
660+
661+ let index = & mut self . indexed_graph . index ;
662+ let stage = & mut self . stage ;
663+
664+ let ( ( index, spk) , index_changeset) = index
665+ . reveal_next_spk ( keychain)
666+ . expect ( "keychain must exist" ) ;
667+
668+ stage. merge ( index_changeset. into ( ) ) ;
669+
670+ Ok ( AddressInfo {
671+ index,
672+ address : Address :: from_script ( spk. as_script ( ) , self . network )
673+ . expect ( "must have address form" ) ,
674+ keychain,
675+ } )
676+ }
689677
690678 /// Reveal addresses up to and including the target `index` and return an iterator
691679 /// of newly revealed addresses.
@@ -725,30 +713,40 @@ impl Wallet {
725713 ///
726714 /// **WARNING**: To avoid address reuse you must persist the changes resulting from one or more
727715 /// calls to this method before closing the wallet. See [`Wallet::reveal_next_address`].
728- // pub fn next_unused_address(&mut self, keychain: KeychainKind) -> AddressInfo {
729- // let keychain = self.map_keychain(keychain);
730- // let index = &mut self.indexed_graph.index;
731- //
732- // let ((index, spk), index_changeset) = index
733- // .next_unused_spk(keychain)
734- // .expect("keychain must exist");
735- //
736- // self.stage
737- // .merge(indexed_tx_graph::ChangeSet::from(index_changeset).into());
738- //
739- // AddressInfo {
740- // index,
741- // address: Address::from_script(spk.as_script(), self.network)
742- // .expect("must have address form"),
743- // keychain,
744- // }
745- // }
716+ pub fn next_unused_address ( & mut self , keychain : KeychainKind ) -> Result < AddressInfo , KeychainNotInKeyRingError > {
717+ // TODO #266: Add test for when the keychain provided is not part of the key_ring
718+ if !self . indexed_graph . index . keychains ( ) . any ( |( k, _) | k == keychain) {
719+ return Err ( KeychainNotInKeyRingError :: KeychainNotFound ( keychain) ) ;
720+ }
721+
722+ let index = & mut self . indexed_graph . index ;
723+
724+ let ( ( index, spk) , index_changeset) = index
725+ . next_unused_spk ( keychain)
726+ . expect ( "keychain must exist" ) ;
727+
728+ self . stage
729+ . merge ( indexed_tx_graph:: ChangeSet :: from ( index_changeset) . into ( ) ) ;
730+
731+ Ok ( AddressInfo {
732+ index,
733+ address : Address :: from_script ( spk. as_script ( ) , self . network )
734+ . expect ( "must have address form" ) ,
735+ keychain,
736+ } )
737+ }
746738
747739 /// Marks an address used of the given `keychain` at `index`.
748740 ///
749741 /// Returns whether the given index was present and then removed from the unused set.
750- pub fn mark_used ( & mut self , keychain : KeychainKind , index : u32 ) -> bool {
751- self . indexed_graph . index . mark_used ( keychain, index)
742+ pub fn mark_used ( & mut self , keychain : KeychainKind , index : u32 ) -> Result < bool , KeychainNotInKeyRingError > {
743+ // TODO #266: Add test for when the keychain provided is not part of the key_ring
744+ if !self . indexed_graph . index . keychains ( ) . any ( |( k, _) | k == keychain) {
745+ return Err ( KeychainNotInKeyRingError :: KeychainNotFound ( keychain) ) ;
746+ }
747+
748+ self . indexed_graph . index . mark_used ( keychain, index) ;
749+ Ok ( true )
752750 }
753751
754752 /// Undoes the effect of [`mark_used`] and returns whether the `index` was inserted
@@ -759,8 +757,13 @@ impl Wallet {
759757 /// derived spk.
760758 ///
761759 /// [`mark_used`]: Self::mark_used
762- pub fn unmark_used ( & mut self , keychain : KeychainKind , index : u32 ) -> bool {
763- self . indexed_graph . index . unmark_used ( keychain, index)
760+ pub fn unmark_used ( & mut self , keychain : KeychainKind , index : u32 ) -> Result < bool , KeychainNotInKeyRingError > {
761+ // TODO #266: Add test for when the keychain provided is not part of the key_ring
762+ if !self . indexed_graph . index . keychains ( ) . any ( |( k, _) | k == keychain) {
763+ return Err ( KeychainNotInKeyRingError :: KeychainNotFound ( keychain) ) ;
764+ }
765+ self . indexed_graph . index . unmark_used ( keychain, index) ;
766+ Ok ( true )
764767 }
765768
766769 /// List addresses that are revealed but unused.
0 commit comments