Skip to content

Commit

Permalink
Add ragger navigation to musig sign_psbt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspider committed May 31, 2024
1 parent a086ae7 commit 2e85fa8
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions tests/test_sign_psbt_musig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from hashlib import sha256
import hmac
from typing import Optional


from ledger_bitcoin.client_base import Client, MusigPartialSignature, MusigPubNonce
Expand All @@ -20,19 +21,43 @@
tests_root: Path = Path(__file__).parent


# TODO: move elsewhere, or generalize
def sign_psbt_instruction_approve_musig(model: Firmware) -> Instructions:
instructions = Instructions(model)

if model.name.startswith("nano"):
instructions.new_request("Continue", save_screenshot=False)
instructions.new_request(
"Continue", save_screenshot=False)
instructions.same_request(
"Continue", save_screenshot=False)
instructions.same_request("Sign", save_screenshot=False)
else:
instructions.confirm_wallet(save_screenshot=False)
instructions.navigate_end_of_flow(save_screenshot=False)
instructions.warning_accept(save_screenshot=False)
instructions.same_request_confirm_transaction(save_screenshot=False)
return instructions


# for now, we assume that there's a single internal musig placeholder, with a single
class LedgerMusig2Cosigner(PsbtMusig2Cosigner):
"""
Implements a PsbtMusig2Cosigner that uses a BitcoinClient
"""

def __init__(self, client: Client, wallet_policy: WalletPolicy, wallet_hmac: bytes) -> None:
def __init__(self, client: Client, wallet_policy: WalletPolicy, wallet_hmac: bytes, *, navigator: Optional[Navigator] = None,
testname: str = "", instructions: Instructions = None) -> None:
super().__init__()

self.client = client
self.wallet_policy = wallet_policy
self.wallet_hmac = wallet_hmac

self.navigator = navigator
self.testname = testname
self.instructions = instructions

self.fingerprint = client.get_master_fingerprint()

desc_tmpl = TrDescriptorTemplate.from_string(
Expand Down Expand Up @@ -61,7 +86,8 @@ def get_participant_pubkey(self) -> bip0327.Point:

def generate_public_nonces(self, psbt: PSBT) -> None:
print("PSBT before nonce generation:", psbt.serialize())
res = self.client.sign_psbt(psbt, self.wallet_policy, self.wallet_hmac)
res = self.client.sign_psbt(
psbt, self.wallet_policy, self.wallet_hmac, navigator=self.navigator, testname=self.testname, instructions=self.instructions)
print("Pubnonces:", res)
for (input_index, yielded) in res:
if isinstance(yielded, MusigPubNonce):
Expand All @@ -80,7 +106,8 @@ def generate_public_nonces(self, psbt: PSBT) -> None:

def generate_partial_signatures(self, psbt: PSBT) -> None:
print("PSBT before partial signature generation:", psbt.serialize())
res = self.client.sign_psbt(psbt, self.wallet_policy, self.wallet_hmac)
res = self.client.sign_psbt(
psbt, self.wallet_policy, self.wallet_hmac, navigator=self.navigator, testname=self.testname, instructions=self.instructions)
print("Ledger result of second round:", res)
for (input_index, yielded) in res:
if isinstance(yielded, MusigPartialSignature):
Expand All @@ -99,7 +126,7 @@ def generate_partial_signatures(self, psbt: PSBT) -> None:
raise ValueError("Expected partial signatures, got a pubnonce")


def test_sign_psbt_musig2_keypath(client: RaggerClient, speculos_globals: SpeculosGlobals):
def test_sign_psbt_musig2_keypath(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
cosigner_1_xpub = "[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT"

cosigner_2_xpriv = "tprv8gFWbQBTLFhbX3EK3cS7LmenwE3JjXbD9kN9yXfq7LcBm81RSf8vPGPqGPjZSeX41LX9ZN14St3z8YxW48aq5Yhr9pQZVAyuBthfi6quTCf"
Expand All @@ -122,13 +149,14 @@ def test_sign_psbt_musig2_keypath(client: RaggerClient, speculos_globals: Specul
"a3aeecb6c236b4a7e72c95fa138250d449b97a75c573f8ab612356279ff64046")
]

signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac)
signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac,
navigator=navigator, instructions=sign_psbt_instruction_approve_musig(firmware), testname=test_name)
signer_2 = HotMusig2Cosigner(wallet_policy, cosigner_2_xpriv)

run_musig2_test(wallet_policy, psbt, [signer_1, signer_2], sighashes)


def test_sign_psbt_musig2_scriptpath(client: RaggerClient, speculos_globals: SpeculosGlobals):
def test_sign_psbt_musig2_scriptpath(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str, speculos_globals: SpeculosGlobals):
cosigner_1_xpub = "[f5acc2fd/44'/1'/0']tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT"

cosigner_2_xpriv = "tprv8gFWbQBTLFhbX3EK3cS7LmenwE3JjXbD9kN9yXfq7LcBm81RSf8vPGPqGPjZSeX41LX9ZN14St3z8YxW48aq5Yhr9pQZVAyuBthfi6quTCf"
Expand Down Expand Up @@ -156,7 +184,8 @@ def test_sign_psbt_musig2_scriptpath(client: RaggerClient, speculos_globals: Spe
"28f86cd95c144ed4a877701ae7166867e8805b654c43d9f44da45d7b0070c313")
]

signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac)
signer_1 = LedgerMusig2Cosigner(client, wallet_policy, wallet_hmac,
navigator=navigator, instructions=sign_psbt_instruction_approve_musig(firmware), testname=test_name)
signer_2 = HotMusig2Cosigner(wallet_policy, cosigner_2_xpriv)

run_musig2_test(wallet_policy, psbt, [signer_1, signer_2], sighashes)

0 comments on commit 2e85fa8

Please sign in to comment.