Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spks iterators should have their own iterator type #904

Closed
evanlinjin opened this issue Mar 17, 2023 · 3 comments
Closed

Spks iterators should have their own iterator type #904

evanlinjin opened this issue Mar 17, 2023 · 3 comments
Assignees
Labels
new feature New feature or request
Milestone

Comments

@evanlinjin
Copy link
Member

Something like this because with the current method using range_descriptor_spks the returned iterator will not be able quickly call skip or nth on the iterator. It will do all the intermediate derivations.

use bitcoin::{
    secp256k1::{Secp256k1, VerifyOnly},
    Script,
};
use miniscript::{Descriptor, DescriptorPublicKey};

/// An iterator over a descriptor's script pubkeys.
///
// TODO: put this into miniscript
#[derive(Clone, Debug)]
pub struct SpkIter {
    descriptor: Descriptor<DescriptorPublicKey>,
    index: usize,
    secp: Secp256k1<VerifyOnly>,
    end: usize,
}

impl SpkIter {
    /// Creates a new script pubkey iterator starting at 0 from a descriptor
    pub fn new(descriptor: Descriptor<DescriptorPublicKey>) -> Self {
        let secp = Secp256k1::verification_only();
        let end = if descriptor.has_wildcard() {
            // Because we only iterate over non-hardened indexes there are 2^31 values
            (1 << 31) - 1
        } else {
            0
        };

        Self {
            descriptor,
            index: 0,
            secp,
            end,
        }
    }
}

impl Iterator for SpkIter {
    type Item = (u32, Script);

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.index = self.index.saturating_add(n);
        self.next()
    }

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.index;
        if index > self.end {
            return None;
        }

        let script = self
            .descriptor
            .at_derivation_index(self.index as u32)
            .derived_descriptor(&self.secp)
            .expect("the descritpor cannot need hardened derivation")
            .script_pubkey();

        self.index += 1;

        Some((index as u32, script))
    }
}

Original ticket: LLFourn/bdk_core_staging#190

@evanlinjin evanlinjin added new feature New feature or request BDK 1.0.0 labels Mar 17, 2023
@evanlinjin evanlinjin added this to the 1.0.0 milestone Mar 17, 2023
@evanlinjin
Copy link
Member Author

@LagginTimes is currently working on this. I can't seem to assign him to the GitHub ticket for some reason.

@notmandatory
Copy link
Member

@LagginTimes is currently working on this. I can't seem to assign him to the GitHub ticket for some reason.

It's something with GitHub where we can only assign issues to people who are part of the organization. I think your comment here is sufficient.

@notmandatory notmandatory added this to BDK May 4, 2023
@notmandatory notmandatory removed this from the 1.0.0 milestone May 4, 2023
@github-project-automation github-project-automation bot moved this to Done in BDK May 7, 2023
@evanlinjin
Copy link
Member Author

#927 completed this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants