Skip to content

Commit d482ad8

Browse files
committed
fix/identical_to_bitcoin_cores_signature
fixed issue for identical to Bitcoin Core's signature
1 parent db367d8 commit d482ad8

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

bitcoinutils/keys.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ def sign_taproot_input(
307307
tapleaf_scripts: Optional[Script | list[Script] | list[list[Script]]] | bytes = None,
308308
sighash: int = TAPROOT_SIGHASH_ALL,
309309
tweak: bool = True,
310+
rand_aux: bytes = None,
310311
):
311312
# get the digest from the transaction object and sign
312313
# note that when signing a tapleaf we typically won't use tweaked
@@ -324,7 +325,7 @@ def sign_taproot_input(
324325
tx_digest = tx.get_transaction_taproot_digest(
325326
txin_index, utxo_scripts, amounts, 0, sighash=sighash
326327
)
327-
return self._sign_taproot_input(tx_digest, sighash, tapleaf_scripts, tweak)
328+
return self._sign_taproot_input(tx_digest, sighash, tapleaf_scripts, tweak, rand_aux)
328329

329330
def _sign_input(self, tx_digest: bytes, sighash: int = SIGHASH_ALL) -> str:
330331
"""Signs a transaction input with the private key
@@ -441,6 +442,7 @@ def _sign_taproot_input(
441442
sighash: int = SIGHASH_ALL,
442443
scripts: Optional[Script | list[Script] | list[list[Script]]] = None,
443444
tweak: bool = True,
445+
rand_aux: bytes = None,
444446
) -> str:
445447
"""Signs a taproot transaction input with the private key
446448
@@ -470,7 +472,8 @@ def _sign_taproot_input(
470472
# it is the hash of the tx_digest and private key
471473
# TODO not identical to Bitcoin Core's signature, rand_aux
472474
# needs to change if we want identical signatures!
473-
rand_aux = hashlib.sha256(tx_digest + byte_key).digest()
475+
if rand_aux is None:
476+
rand_aux = hashlib.sha256(tx_digest + byte_key).digest()
474477

475478
# use BIP-340 python's reference implementation for signing
476479
sig = schnorr_sign(tx_digest, byte_key, rand_aux)
@@ -534,7 +537,7 @@ def __init__(self, hex_str: str = None, message: str = None, signature: bytes =
534537
----------
535538
hex_str : str, optional
536539
the public key in hex string
537-
540+
538541
In case of generating public key from message and signature:-
539542
message : str, optional
540543
The original message that was signed
@@ -611,7 +614,7 @@ def __init__(self, hex_str: str = None, message: str = None, signature: bytes =
611614
elif message or signature:
612615
if not message:
613616
raise ValueError("Empty message provided for public key recovery.")
614-
617+
615618
if(len(signature) != 65):
616619
raise ValueError("Invalid signature length, must be exactly 65 bytes")
617620

@@ -620,9 +623,9 @@ def __init__(self, hex_str: str = None, message: str = None, signature: bytes =
620623
recovery_id = signature[0] - 31
621624
if not (0 <= recovery_id <= 3): # A valid recovery ID is between 0 and 3
622625
raise ValueError(f"Invalid recovery ID: expected 31-34, got {signature[0]}")
623-
626+
624627
signature = signature[1:] #Remove recovery id from signature
625-
628+
626629
# All bitcoin signatures include the magic prefix. It is just a string
627630
# added to the message to distinguish Bitcoin-specific messages.
628631
message_magic = add_magic_prefix(message)
@@ -687,7 +690,7 @@ def to_taproot_hex(
687690
tweak_int = calculate_tweak(self, scripts)
688691

689692
# keep x-only coordinate
690-
tweak_and_odd = tweak_taproot_pubkey(self.key.to_string(), tweak_int)
693+
tweak_and_odd = tweak_taproot_pubkey(self.key.to_string(), tweak_int)
691694
pubkey = tweak_and_odd[0][:32]
692695
is_odd = tweak_and_odd[1]
693696

0 commit comments

Comments
 (0)