Skip to content

Commit

Permalink
Merge pull request #28 from BP-WG/derivable
Browse files Browse the repository at this point in the history
Add XpubDerivable constructors
  • Loading branch information
dr-orlovsky committed Jun 8, 2024
2 parents e649b0a + 14349c1 commit 8177aab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
18 changes: 18 additions & 0 deletions derive/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ pub enum DerivationParseError {
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct DerivationSeg<I: IdxBase = NormalIndex>(Confined<BTreeSet<I>, 1, 8>);

impl<I: IdxBase> From<&'static [I]> for DerivationSeg<I> {
fn from(indexes: &'static [I]) -> Self {
Self(Confined::from_iter_unsafe(indexes.iter().copied()))
}
}

impl<I: IdxBase> From<[I; 2]> for DerivationSeg<I> {
fn from(indexes: [I; 2]) -> Self { Self(Confined::from_iter_unsafe(indexes)) }
}

impl<I: IdxBase> From<[I; 3]> for DerivationSeg<I> {
fn from(indexes: [I; 3]) -> Self { Self(Confined::from_iter_unsafe(indexes)) }
}

impl<I: IdxBase> From<[I; 4]> for DerivationSeg<I> {
fn from(indexes: [I; 4]) -> Self { Self(Confined::from_iter_unsafe(indexes)) }
}

impl<I: IdxBase> DerivationSeg<I> {
pub fn new(index: I) -> Self { DerivationSeg(confined_bset![index]) }

Expand Down
40 changes: 39 additions & 1 deletion derive/src/xpub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::borrow::Borrow;
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;

use amplify::{hex, ByteArray, Bytes20, Bytes32, Bytes4, Wrapper};
use amplify::{confinement, hex, ByteArray, Bytes20, Bytes32, Bytes4, Wrapper};
use bc::secp256k1::SECP256K1;
use bc::{secp256k1, CompressedPk, InvalidPubkey, LegacyPk, XOnlyPk};
use bitcoin_hashes::{hash160, sha512, Hash, HashEngine, Hmac, HmacEngine};
Expand Down Expand Up @@ -495,7 +495,45 @@ pub struct XpubDerivable {
pub(crate) keychains: DerivationSeg<Keychain>,
}

impl From<XpubSpec> for XpubDerivable {
fn from(spec: XpubSpec) -> Self {
XpubDerivable {
spec,
variant: None,
keychains: DerivationSeg::from([Keychain::INNER, Keychain::OUTER]),
}
}
}

impl XpubDerivable {
pub fn new_standard(xpub: Xpub, origin: XpubOrigin) -> Self {
XpubDerivable {
spec: XpubSpec::new(xpub, origin),
variant: None,
keychains: DerivationSeg::from([Keychain::INNER, Keychain::OUTER]),
}
}

pub fn new_custom(xpub: Xpub, origin: XpubOrigin, keychains: &'static [Keychain]) -> Self {
XpubDerivable {
spec: XpubSpec::new(xpub, origin),
variant: None,
keychains: DerivationSeg::from(keychains),
}
}

pub fn try_custom(
xpub: Xpub,
origin: XpubOrigin,
keychains: impl IntoIterator<Item = Keychain>,
) -> Result<Self, confinement::Error> {
Ok(XpubDerivable {
spec: XpubSpec::new(xpub, origin),
variant: None,
keychains: DerivationSeg::with(keychains)?,
})
}

pub fn xpub(&self) -> Xpub { self.spec.xpub }

pub fn origin(&self) -> &XpubOrigin { &self.spec.origin }
Expand Down

0 comments on commit 8177aab

Please sign in to comment.