Skip to content

Commit 2d4adbd

Browse files
committed
fix!(chain): Make spk revelation return owned spks
1 parent 910a5b5 commit 2d4adbd

File tree

7 files changed

+23
-25
lines changed

7 files changed

+23
-25
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
370370
/// Return the script that exists under the given `keychain`'s `index`.
371371
///
372372
/// This calls [`SpkTxOutIndex::spk_at_index`] internally.
373-
pub fn spk_at_index(&self, keychain: K, index: u32) -> Option<&Script> {
373+
pub fn spk_at_index(&self, keychain: K, index: u32) -> Option<ScriptBuf> {
374374
let descriptor_id = *self.keychains_to_descriptor_ids.get(&keychain)?;
375375
self.inner.spk_at_index(&(descriptor_id, index))
376376
}
@@ -630,7 +630,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
630630
pub fn revealed_spks(
631631
&self,
632632
range: impl RangeBounds<K>,
633-
) -> impl DoubleEndedIterator<Item = (&K, u32, &Script)> + Clone {
633+
) -> impl DoubleEndedIterator<Item = (&K, u32, ScriptBuf)> + Clone {
634634
self.keychains_to_descriptor_ids
635635
.range(range)
636636
.flat_map(|(_, descriptor_id)| {
@@ -648,7 +648,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
648648
self.keychain_of_desc_id(descriptor_id)
649649
.expect("must have keychain"),
650650
*i,
651-
spk.as_script(),
651+
spk.as_script().to_owned(),
652652
)
653653
})
654654
})
@@ -658,13 +658,13 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
658658
pub fn revealed_keychain_spks<'a>(
659659
&'a self,
660660
keychain: &'a K,
661-
) -> impl DoubleEndedIterator<Item = (u32, &Script)> + 'a {
661+
) -> impl DoubleEndedIterator<Item = (u32, ScriptBuf)> + 'a {
662662
self.revealed_spks(keychain..=keychain)
663663
.map(|(_, i, spk)| (i, spk))
664664
}
665665

666666
/// Iterate over revealed, but unused, spks of all keychains.
667-
pub fn unused_spks(&self) -> impl DoubleEndedIterator<Item = (K, u32, &Script)> + Clone {
667+
pub fn unused_spks(&self) -> impl DoubleEndedIterator<Item = (K, u32, ScriptBuf)> + Clone + '_ {
668668
self.keychains_to_descriptor_ids
669669
.keys()
670670
.flat_map(|keychain| {
@@ -675,10 +675,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
675675

676676
/// Iterate over revealed, but unused, spks of the given `keychain`.
677677
/// Returns an empty iterator if the provided keychain doesn't exist.
678-
pub fn unused_keychain_spks(
679-
&self,
678+
pub fn unused_keychain_spks<'a>(
679+
&'a self,
680680
keychain: &K,
681-
) -> impl DoubleEndedIterator<Item = (u32, &Script)> + Clone {
681+
) -> impl DoubleEndedIterator<Item = (u32, ScriptBuf)> + Clone + 'a {
682682
let desc_id = self
683683
.keychains_to_descriptor_ids
684684
.get(keychain)
@@ -909,8 +909,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
909909
let spk = self
910910
.inner
911911
.spk_at_index(&(descriptor_id, next_index))
912-
.expect("script must already be stored")
913-
.to_owned();
912+
.expect("script must already be stored");
914913
(next_index, spk)
915914
});
916915
debug_assert_eq!(new_spks.next(), None, "must only reveal one spk");
@@ -931,10 +930,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
931930
/// has used all scripts up to the derivation bounds, then the last derived script pubkey will
932931
/// be returned.
933932
pub fn next_unused_spk(&mut self, keychain: &K) -> (Option<(u32, ScriptBuf)>, ChangeSet<K>) {
934-
let next_unused_spk = self
935-
.unused_keychain_spks(keychain)
936-
.next()
937-
.map(|(i, spk)| (i, spk.to_owned()));
933+
let next_unused_spk = self.unused_keychain_spks(keychain).next();
938934
if next_unused_spk.is_some() {
939935
(next_unused_spk, Default::default())
940936
} else {

crates/chain/src/spk_client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,10 @@ impl SyncRequest {
195195
index: &crate::keychain::KeychainTxOutIndex<K>,
196196
spk_range: impl RangeBounds<K>,
197197
) -> Self {
198-
use alloc::borrow::ToOwned;
199198
self.chain_spks(
200199
index
201200
.revealed_spks(spk_range)
202-
.map(|(_, _, spk)| spk.to_owned())
201+
.map(|(_, _, spk)| spk)
203202
.collect::<Vec<_>>(),
204203
)
205204
}

crates/chain/src/spk_txout_index.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ impl<I: Clone + Ord> SpkTxOutIndex<I> {
174174
/// Returns the script that has been inserted at the `index`.
175175
///
176176
/// If that index hasn't been inserted yet, it will return `None`.
177-
pub fn spk_at_index(&self, index: &I) -> Option<&Script> {
178-
self.spks.get(index).map(|s| s.as_script())
177+
pub fn spk_at_index(&self, index: &I) -> Option<ScriptBuf> {
178+
self.spks.get(index).cloned()
179179
}
180180

181181
/// The script pubkeys that are being tracked by the index.
@@ -215,7 +215,10 @@ impl<I: Clone + Ord> SpkTxOutIndex<I> {
215215
/// let unused_change_spks =
216216
/// txout_index.unused_spks((change_index, u32::MIN)..(change_index, u32::MAX));
217217
/// ```
218-
pub fn unused_spks<R>(&self, range: R) -> impl DoubleEndedIterator<Item = (&I, &Script)> + Clone
218+
pub fn unused_spks<R>(
219+
&self,
220+
range: R,
221+
) -> impl DoubleEndedIterator<Item = (&I, ScriptBuf)> + Clone
219222
where
220223
R: RangeBounds<I>,
221224
{

crates/wallet/src/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl Wallet {
849849
.unused_keychain_spks(&keychain)
850850
.map(move |(index, spk)| AddressInfo {
851851
index,
852-
address: Address::from_script(spk, self.network).expect("must have address form"),
852+
address: Address::from_script(&spk, self.network).expect("must have address form"),
853853
keychain,
854854
})
855855
}

example-crates/example_cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ where
493493
false => Keychain::External,
494494
};
495495
for (spk_i, spk) in index.revealed_keychain_spks(&target_keychain) {
496-
let address = Address::from_script(spk, network)
496+
let address = Address::from_script(&spk, network)
497497
.expect("should always be able to derive address");
498498
println!(
499499
"{:?} {} used:{}",

example-crates/example_electrum/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn main() -> anyhow::Result<()> {
227227
let all_spks = graph
228228
.index
229229
.revealed_spks(..)
230-
.map(|(k, i, spk)| (k.to_owned(), i, spk.to_owned()))
230+
.map(|(k, i, spk)| (k.to_owned(), i, spk))
231231
.collect::<Vec<_>>();
232232
request = request.chain_spks(all_spks.into_iter().map(|(k, spk_i, spk)| {
233233
eprint!("Scanning {}: {}", k, spk_i);
@@ -238,7 +238,7 @@ fn main() -> anyhow::Result<()> {
238238
let unused_spks = graph
239239
.index
240240
.unused_spks()
241-
.map(|(k, i, spk)| (k, i, spk.to_owned()))
241+
.map(|(k, i, spk)| (k, i, spk))
242242
.collect::<Vec<_>>();
243243
request =
244244
request.chain_spks(unused_spks.into_iter().map(move |(k, spk_i, spk)| {

example-crates/example_esplora/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn main() -> anyhow::Result<()> {
245245
let all_spks = graph
246246
.index
247247
.revealed_spks(..)
248-
.map(|(k, i, spk)| (k.to_owned(), i, spk.to_owned()))
248+
.map(|(k, i, spk)| (k.to_owned(), i, spk))
249249
.collect::<Vec<_>>();
250250
request = request.chain_spks(all_spks.into_iter().map(|(k, i, spk)| {
251251
eprint!("scanning {}:{}", k, i);
@@ -258,7 +258,7 @@ fn main() -> anyhow::Result<()> {
258258
let unused_spks = graph
259259
.index
260260
.unused_spks()
261-
.map(|(k, i, spk)| (k, i, spk.to_owned()))
261+
.map(|(k, i, spk)| (k, i, spk))
262262
.collect::<Vec<_>>();
263263
request =
264264
request.chain_spks(unused_spks.into_iter().map(move |(k, i, spk)| {

0 commit comments

Comments
 (0)