Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/embit/psbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,22 +977,25 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int:
continue

# get all possible derivations with matching fingerprint
bip32_derivations = set()
bip32_derivations = OrderedDict() # OrderedDict to keep order
if fingerprint:
# if taproot derivations are present add them
for pub in inp.taproot_bip32_derivations:
(_leafs, derivation) = inp.taproot_bip32_derivations[pub]
if derivation.fingerprint == fingerprint:
bip32_derivations.add((pub, derivation))
# Add only if not already present
if (pub, derivation) not in bip32_derivations:
bip32_derivations[(pub, derivation)] = True

# segwit and legacy derivations
for pub in inp.bip32_derivations:
derivation = inp.bip32_derivations[pub]
if derivation.fingerprint == fingerprint:
bip32_derivations.add((pub, derivation))
if (pub, derivation) not in bip32_derivations:
bip32_derivations[(pub, derivation)] = True

# get derived keys for signing
derived_keypairs = set() # (prv, pub)
derived_keypairs = OrderedDict() # (prv, pub)
for pub, derivation in bip32_derivations:
der = derivation.derivation
# descriptor key has origin derivation that we take into account
Expand All @@ -1008,7 +1011,9 @@ def sign_with(self, root, sighash=SIGHASH.DEFAULT) -> int:

if hdkey.xonly() != pub.xonly():
raise PSBTError("Derivation path doesn't look right")
derived_keypairs.add((hdkey.key, pub))
# Insert into derived_keypairs if not present
if (hdkey.key, pub) not in derived_keypairs:
derived_keypairs[(hdkey.key, pub)] = True

# sign with taproot key
if inp.is_taproot:
Expand Down
16 changes: 11 additions & 5 deletions src/embit/psbtview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Makes sense to run gc.collect() after processing of each scope to free memory.
"""
# TODO: refactor, a lot of code is duplicated here from transaction.py
from collections import OrderedDict
import hashlib
from . import compact
from . import ec
Expand Down Expand Up @@ -742,22 +743,25 @@ def sign_input(
return 0

# get all possible derivations with matching fingerprint
bip32_derivations = set()
bip32_derivations = OrderedDict()
if fingerprint:
# if taproot derivations are present add them
for pub in inp.taproot_bip32_derivations:
(_leafs, derivation) = inp.taproot_bip32_derivations[pub]
if derivation.fingerprint == fingerprint:
bip32_derivations.add((pub, derivation))
# Add only if not already present
if (pub, derivation) not in bip32_derivations:
bip32_derivations[(pub, derivation)] = True

# segwit and legacy derivations
for pub in inp.bip32_derivations:
derivation = inp.bip32_derivations[pub]
if derivation.fingerprint == fingerprint:
bip32_derivations.add((pub, derivation))
if (pub, derivation) not in bip32_derivations:
bip32_derivations[(pub, derivation)] = True

# get derived keys for signing
derived_keypairs = set() # (prv, pub)
derived_keypairs = OrderedDict() # (prv, pub)
for pub, derivation in bip32_derivations:
der = derivation.derivation
# descriptor key has origin derivation that we take into account
Expand All @@ -773,7 +777,9 @@ def sign_input(

if hdkey.xonly() != pub.xonly():
raise PSBTError("Derivation path doesn't look right")
derived_keypairs.add((hdkey.key, pub))
# Insert into derived_keypairs if not present
if (hdkey.key, pub) not in derived_keypairs:
derived_keypairs[(hdkey.key, pub)] = True

counter = 0
# sign with taproot key
Expand Down