Skip to content

Commit

Permalink
to_public method for descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
stepansnigirev committed May 11, 2022
1 parent 405fbd1 commit 1007c44
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/embit/descriptor/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__(self, key, origin=None, derivation=None, taproot=False):
self.origin = origin
self.key = key
self.taproot = taproot
if not hasattr(key, "derive") and derivation is not None:
if not hasattr(key, "derive") and derivation:
raise ArgumentError("Key %s doesn't support derivation" % key)
self.allowed_derivation = derivation

Expand Down Expand Up @@ -335,6 +335,14 @@ def is_private(self):
self.is_extended and self.key.is_private
)

def to_public(self):
if not self.is_private:
return self
if isinstance(self.key, ec.PrivateKey):
return type(self)(self.key.get_public_key(), self.origin, self.allowed_derivation, self.taproot)
else:
return type(self)(self.key.to_public(), self.origin, self.allowed_derivation, self.taproot)

@property
def private_key(self):
if not self.is_private:
Expand Down
16 changes: 16 additions & 0 deletions src/embit/descriptor/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ def derive(self, idx, branch_index=None):
None, self.sh, self.wsh, self.key.derive(idx, branch_index), self.wpkh, self.taproot
)

def to_public(self):
if self.miniscript:
return type(self)(
self.miniscript.to_public(),
self.sh,
self.wsh,
None,
self.wpkh,
self.taproot,
)
else:
return type(self)(
None, self.sh, self.wsh, self.key.to_public(), self.wpkh, self.taproot
)


def owns(self, psbt_scope):
"""Checks if psbt input or output belongs to this descriptor"""
# we can't check if we don't know script_pubkey
Expand Down
7 changes: 7 additions & 0 deletions src/embit/descriptor/miniscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def derive(self, idx, branch_index=None):
]
return type(self)(*args)

def to_public(self):
args = [
arg.to_public() if hasattr(arg, "to_public") else arg
for arg in self.args
]
return type(self)(*args)

def branch(self, branch_index):
args = [
arg.branch(branch_index) if hasattr(arg, "branch") else arg
Expand Down

0 comments on commit 1007c44

Please sign in to comment.