Skip to content

Commit

Permalink
refactor Idx trait type system to make Keychain working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 19, 2023
1 parent d475e99 commit e3bac0a
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 87 deletions.
4 changes: 2 additions & 2 deletions descriptors/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::Range;
use std::collections::BTreeSet;
use std::{iter, vec};

use bpstd::{
Expand Down Expand Up @@ -140,7 +140,7 @@ pub enum StdDescr<S: DeriveSet = XpubDerivable> {
}

impl<S: DeriveSet> Derive<DerivedScript> for StdDescr<S> {
fn keychains(&self) -> Range<u8> {
fn keychains(&self) -> &BTreeSet<Keychain> {

Check warning on line 143 in descriptors/src/descriptor.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/descriptor.rs#L143

Added line #L143 was not covered by tests
match self {
StdDescr::Wpkh(d) => d.keychains(),
StdDescr::TrKey(d) => d.keychains(),

Check warning on line 146 in descriptors/src/descriptor.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/descriptor.rs#L145-L146

Added lines #L145 - L146 were not covered by tests
Expand Down
4 changes: 2 additions & 2 deletions descriptors/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use bpstd::{Address, AddressError, AddressNetwork, DeriveScripts, Idx, NormalIndex};
use bpstd::{Address, AddressError, AddressNetwork, DeriveScripts, Idx, Keychain, NormalIndex};

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct AddressFactory<D: DeriveScripts> {
pub descriptor: D,
pub network: AddressNetwork,
pub keychain: u8,
pub keychain: Keychain,
pub unused_tip: NormalIndex,
}

Expand Down
4 changes: 2 additions & 2 deletions descriptors/src/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeSet;
use std::iter;
use std::ops::Range;

use bpstd::{
CompressedPk, Derive, DeriveCompr, DerivedScript, KeyOrigin, Keychain, NormalIndex,
Expand All @@ -42,7 +42,7 @@ impl<K: DeriveCompr> Wpkh<K> {

impl<K: DeriveCompr> Derive<DerivedScript> for Wpkh<K> {
#[inline]
fn keychains(&self) -> Range<u8> { self.0.keychains() }
fn keychains(&self) -> &BTreeSet<Keychain> { self.0.keychains() }

Check warning on line 45 in descriptors/src/segwit.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/segwit.rs#L45

Added line #L45 was not covered by tests

fn derive(
&self,
Expand Down
4 changes: 2 additions & 2 deletions descriptors/src/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeSet;
use std::iter;
use std::ops::Range;

use bpstd::{
CompressedPk, Derive, DeriveXOnly, DerivedScript, InternalPk, KeyOrigin, Keychain, NormalIndex,
Expand All @@ -42,7 +42,7 @@ impl<K: DeriveXOnly> TrKey<K> {

impl<K: DeriveXOnly> Derive<DerivedScript> for TrKey<K> {
#[inline]
fn keychains(&self) -> Range<u8> { self.0.keychains() }
fn keychains(&self) -> &BTreeSet<Keychain> { self.0.keychains() }

Check warning on line 45 in descriptors/src/taproot.rs

View check run for this annotation

Codecov / codecov/patch

descriptors/src/taproot.rs#L45

Added line #L45 was not covered by tests

fn derive(
&self,
Expand Down
34 changes: 25 additions & 9 deletions std/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
// limitations under the License.

use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::num::ParseIntError;
use std::ops::Range;
use std::str::FromStr;

use bc::{
Expand All @@ -33,7 +33,7 @@ use indexmap::IndexMap;

use crate::address::AddressError;
use crate::{
Address, AddressNetwork, AddressParseError, ControlBlockFactory, DerivationIndex, Idx,
Address, AddressNetwork, AddressParseError, ControlBlockFactory, DerivationIndex, Idx, IdxBase,
IndexParseError, NormalIndex, TapTree, XpubDerivable, XpubSpec,
};

Expand All @@ -57,6 +57,22 @@ impl From<Keychain> for DerivationIndex {
fn from(keychain: Keychain) -> Self { DerivationIndex::Normal(keychain.into()) }

Check warning on line 57 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L57

Added line #L57 was not covered by tests
}

impl Keychain {
pub const OUTER: Self = Keychain(0);
pub const INNER: Self = Keychain(1);
}

impl IdxBase for Keychain {
#[inline]
fn is_hardened(&self) -> bool { false }

Check warning on line 67 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L67

Added line #L67 was not covered by tests

#[inline]
fn child_number(&self) -> u32 { self.0 as u32 }

Check warning on line 70 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L70

Added line #L70 was not covered by tests

#[inline]
fn index(&self) -> u32 { self.0 as u32 }

Check warning on line 73 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L73

Added line #L73 was not covered by tests
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -230,7 +246,7 @@ impl PartialOrd for DerivedAddr {
}

impl DerivedAddr {
pub fn new(addr: Address, keychain: u8, index: NormalIndex) -> Self {
pub fn new(addr: Address, keychain: Keychain, index: NormalIndex) -> Self {

Check warning on line 249 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L249

Added line #L249 was not covered by tests
DerivedAddr {
addr,
terminal: Terminal::new(keychain, index),
Expand Down Expand Up @@ -265,7 +281,7 @@ impl FromStr for DerivedAddr {
}

pub trait Derive<D> {
fn keychains(&self) -> Range<u8>;
fn keychains(&self) -> &BTreeSet<Keychain>;

fn derive(&self, keychain: impl Into<Keychain>, index: impl Into<NormalIndex>) -> D;

Expand Down Expand Up @@ -306,7 +322,7 @@ pub trait DeriveScripts: Derive<DerivedScript> {
fn derive_address(
&self,
network: AddressNetwork,
keychain: u8,
keychain: impl Into<Keychain>,

Check warning on line 325 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L325

Added line #L325 was not covered by tests
index: impl Into<NormalIndex>,
) -> Result<Address, AddressError> {
let spk = self.derive(keychain, index).to_script_pubkey();
Expand All @@ -316,7 +332,7 @@ pub trait DeriveScripts: Derive<DerivedScript> {
fn derive_address_batch(
&self,
network: AddressNetwork,
keychain: u8,
keychain: impl Into<Keychain>,

Check warning on line 335 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L335

Added line #L335 was not covered by tests
from: impl Into<NormalIndex>,
max_count: u8,
) -> Result<Vec<Address>, AddressError> {
Expand All @@ -343,7 +359,7 @@ impl DeriveKey<XOnlyPk> for XpubDerivable {

impl Derive<LegacyPk> for XpubDerivable {
#[inline]
fn keychains(&self) -> Range<u8> { 0..self.keychains.count() }
fn keychains(&self) -> &BTreeSet<Keychain> { self.keychains.as_ref() }

Check warning on line 362 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L362

Added line #L362 was not covered by tests

fn derive(&self, keychain: impl Into<Keychain>, index: impl Into<NormalIndex>) -> LegacyPk {
self.xpub().derive_pub([keychain.into().into(), index.into()]).to_legacy_pub()

Check warning on line 365 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L364-L365

Added lines #L364 - L365 were not covered by tests
Expand All @@ -352,7 +368,7 @@ impl Derive<LegacyPk> for XpubDerivable {

impl Derive<CompressedPk> for XpubDerivable {
#[inline]
fn keychains(&self) -> Range<u8> { 0..self.keychains.count() }
fn keychains(&self) -> &BTreeSet<Keychain> { self.keychains.as_ref() }

Check warning on line 371 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L371

Added line #L371 was not covered by tests

fn derive(&self, keychain: impl Into<Keychain>, index: impl Into<NormalIndex>) -> CompressedPk {
self.xpub().derive_pub([keychain.into().into(), index.into()]).to_compr_pub()

Check warning on line 374 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L373-L374

Added lines #L373 - L374 were not covered by tests
Expand All @@ -361,7 +377,7 @@ impl Derive<CompressedPk> for XpubDerivable {

impl Derive<XOnlyPk> for XpubDerivable {
#[inline]
fn keychains(&self) -> Range<u8> { 0..self.keychains.count() }
fn keychains(&self) -> &BTreeSet<Keychain> { self.keychains.as_ref() }

Check warning on line 380 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L380

Added line #L380 was not covered by tests

fn derive(&self, keychain: impl Into<Keychain>, index: impl Into<NormalIndex>) -> XOnlyPk {
self.xpub().derive_pub([keychain.into().into(), index.into()]).to_xonly_pub()

Check warning on line 383 in std/src/derive.rs

View check run for this annotation

Codecov / codecov/patch

std/src/derive.rs#L382-L383

Added lines #L382 - L383 were not covered by tests
Expand Down
Loading

0 comments on commit e3bac0a

Please sign in to comment.