From aadf119f4ee9ae8010d7834194467d38d160a4bb Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Fri, 26 Apr 2024 11:15:30 +0200 Subject: [PATCH] Add Bitcoin segwit and taproot tests in ragger --- test/python/apps/bitcoin.py | 2 + test/python/apps/cal.py | 4 +- test/python/test_bitcoin.py | 155 ++++++++++++++++++++++++++---------- 3 files changed, 117 insertions(+), 44 deletions(-) diff --git a/test/python/apps/bitcoin.py b/test/python/apps/bitcoin.py index 32cbef94..7c742381 100644 --- a/test/python/apps/bitcoin.py +++ b/test/python/apps/bitcoin.py @@ -15,6 +15,8 @@ BTC_CONF = create_currency_config("BTC", "Bitcoin") BTC_PACKED_DERIVATION_PATH = bitcoin_pack_derivation_path(BtcDerivationPathFormat.BECH32, "m/84'/0'/0'/0/0") +BTC_SEGWIT_PACKED_DERIVATION_PATH = bitcoin_pack_derivation_path(BtcDerivationPathFormat.BECH32, "m/84'/0'/0'/0/10") +BTC_TAPROOT_PACKED_DERIVATION_PATH = bitcoin_pack_derivation_path(BtcDerivationPathFormat.BECH32M, "m/86'/0'/0'/0/6") CHAIN = Chain.MAIN diff --git a/test/python/apps/cal.py b/test/python/apps/cal.py index 2ad1c807..1e709713 100644 --- a/test/python/apps/cal.py +++ b/test/python/apps/cal.py @@ -12,7 +12,7 @@ from .ethereum import DAI_PACKED_DERIVATION_PATH, DAI_CONF from .litecoin import LTC_PACKED_DERIVATION_PATH, LTC_CONF -from .bitcoin import BTC_PACKED_DERIVATION_PATH, BTC_CONF +from .bitcoin import BTC_PACKED_DERIVATION_PATH, BTC_CONF, BTC_SEGWIT_PACKED_DERIVATION_PATH, BTC_TAPROOT_PACKED_DERIVATION_PATH from .stellar import XLM_PACKED_DERIVATION_PATH, XLM_CONF from .solana_utils import SOL_PACKED_DERIVATION_PATH, SOL_CONF from .xrp import XRP_PACKED_DERIVATION_PATH, XRP_CONF @@ -37,6 +37,8 @@ def get_conf_for_ticker(self, overload_signer: Optional[SigningAuthority]=None) ETC_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="ETC", conf=ETC_CONF, packed_derivation_path=ETC_PACKED_DERIVATION_PATH) ETH_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="ETH", conf=ETH_CONF, packed_derivation_path=ETH_PACKED_DERIVATION_PATH) BTC_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="BTC", conf=BTC_CONF, packed_derivation_path=BTC_PACKED_DERIVATION_PATH) +BTC_SEGWIT_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="BTC", conf=BTC_CONF, packed_derivation_path=BTC_SEGWIT_PACKED_DERIVATION_PATH) +BTC_TAPROOT_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="BTC", conf=BTC_CONF, packed_derivation_path=BTC_TAPROOT_PACKED_DERIVATION_PATH) LTC_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="LTC", conf=LTC_CONF, packed_derivation_path=LTC_PACKED_DERIVATION_PATH) XLM_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="XLM", conf=XLM_CONF, packed_derivation_path=XLM_PACKED_DERIVATION_PATH) SOL_CURRENCY_CONFIGURATION = CurrencyConfiguration(ticker="SOL", conf=SOL_CONF, packed_derivation_path=SOL_PACKED_DERIVATION_PATH) diff --git a/test/python/test_bitcoin.py b/test/python/test_bitcoin.py index 111cc01b..ef10168f 100644 --- a/test/python/test_bitcoin.py +++ b/test/python/test_bitcoin.py @@ -5,39 +5,10 @@ from .apps import cal as cal from ledger_bitcoin import WalletPolicy -in_wallet = WalletPolicy( - "", - "wpkh(@0/**)", - [ - "[f5acc2fd/84'/0'/0']xpub6DUYn4moKgHkK2d7bXX3mHTPb6XQwRVFRMdZ6ZwLS5u3nonGVpJiFeZiQkHutwdFqxKP75jex8gvVm7ed4euYeDtMnoiF1Cz1z4CeBJYWin" - ], -) - -out_wallet = WalletPolicy( - "", - "wpkh(@0/**)", - [ - "xpub6CatWdiZiodmYVtWLtEQsAg1H9ooS1bmsJUBwQ83FE1Fyk386FWcyicJgEZv3quZSJKA5dh5Lo2PbubMGxCfZtRthV6ST2qquL9w3HSzcUn" - ], -) - -out_wallet_2 = WalletPolicy( - "", - "wpkh(@0/**)", - [ - "xpub6D7atwj3ewAGT347tUzTNzfTGos1rCFVX4v8gViXiM2R1QHvox1LhEf6NtCeNsCwpppFUoQuS6mHUwfTveA5tEEwn2LqZHfVBEz5qvYmYhf" - ], -) - # ExchangeTestRunner implementation for Bitcoin -class BitcoinTests(ExchangeTestRunner): - - currency_configuration = cal.BTC_CURRENCY_CONFIGURATION - valid_destination_1 = BitcoinClient.get_address_from_wallet(out_wallet) +class BitcoinTestsCommon(ExchangeTestRunner): valid_destination_memo_1 = "" - valid_destination_2 = BitcoinClient.get_address_from_wallet(out_wallet_2) valid_destination_memo_2 = "0" - valid_refund = BitcoinClient.get_address_from_wallet(in_wallet) valid_refund_memo = "" valid_send_amount_1 = 20900000 valid_send_amount_2 = 446739662 @@ -50,25 +21,123 @@ class BitcoinTests(ExchangeTestRunner): signature_refusal_error_code = BitcoinErrors.SW_SWAP_CHECKING_FAIL def perform_final_tx(self, destination, send_amount, fees, memo): - if destination == BitcoinClient.get_address_from_wallet(out_wallet): - BitcoinClient(self.backend).send_simple_sign_tx(in_wallet=in_wallet, - fees=fees, - destination=out_wallet, - send_amount=send_amount) - - elif destination == BitcoinClient.get_address_from_wallet(out_wallet_2): - BitcoinClient(self.backend).send_simple_sign_tx(in_wallet=in_wallet, - fees=fees, - destination=out_wallet_2, - send_amount=send_amount) + if destination == BitcoinClient.get_address_from_wallet(self.out_wallet): + BitcoinClient(self.backend).send_simple_sign_tx(in_wallet=self.in_wallet, + fees=fees, + destination=self.out_wallet, + send_amount=send_amount) + + elif destination == BitcoinClient.get_address_from_wallet(self.out_wallet_2): + BitcoinClient(self.backend).send_simple_sign_tx(in_wallet=self.in_wallet, + fees=fees, + destination=self.out_wallet_2, + send_amount=send_amount) # TODO : assert signature validity -# Use a class to reuse the same Speculos instance -class TestsBitcoin: +class BitcoinTests(BitcoinTestsCommon): + currency_configuration = cal.BTC_CURRENCY_CONFIGURATION + in_wallet = WalletPolicy( + "", + "wpkh(@0/**)", + [ + "[f5acc2fd/84'/0'/0']xpub6DUYn4moKgHkK2d7bXX3mHTPb6XQwRVFRMdZ6ZwLS5u3nonGVpJiFeZiQkHutwdFqxKP75jex8gvVm7ed4euYeDtMnoiF1Cz1z4CeBJYWin" + ], + ) + + out_wallet = WalletPolicy( + "", + "wpkh(@0/**)", + [ + "xpub6CatWdiZiodmYVtWLtEQsAg1H9ooS1bmsJUBwQ83FE1Fyk386FWcyicJgEZv3quZSJKA5dh5Lo2PbubMGxCfZtRthV6ST2qquL9w3HSzcUn" + ], + ) + out_wallet_2 = WalletPolicy( + "", + "wpkh(@0/**)", + [ + "xpub6D7atwj3ewAGT347tUzTNzfTGos1rCFVX4v8gViXiM2R1QHvox1LhEf6NtCeNsCwpppFUoQuS6mHUwfTveA5tEEwn2LqZHfVBEz5qvYmYhf" + ], + ) + valid_destination_1 = BitcoinClient.get_address_from_wallet(out_wallet) + valid_destination_2 = BitcoinClient.get_address_from_wallet(out_wallet_2) + valid_refund = BitcoinClient.get_address_from_wallet(in_wallet) + +class TestsBitcoin: @pytest.mark.parametrize('test_to_run', ALL_TESTS_EXCEPT_MEMO) def test_bitcoin(self, backend, exchange_navigation_helper, test_to_run): BitcoinTests(backend, exchange_navigation_helper).run_test(test_to_run) + +# class BitcoinTestsSegwit(BitcoinTestsCommon): +# currency_configuration = cal.BTC_SEGWIT_CURRENCY_CONFIGURATION +# in_wallet = WalletPolicy( +# "", +# "sh(wpkh(@0/**))", +# [ +# "[f5acc2fd/84'/0'/0']xpub6DUYn4moKgHkK2d7bXX3mHTPb6XQwRVFRMdZ6ZwLS5u3nonGVpJiFeZiQkHutwdFqxKP75jex8gvVm7ed4euYeDtMnoiF1Cz1z4CeBJYWin" +# ], +# ) + +# out_wallet = WalletPolicy( +# "", +# "sh(wpkh(@0/**))", +# [ +# "ypub6Ynvx7RLNYgWzFGM8aeU43hFNjTh7u5Grrup7Ryu2nKZ1Y8FWKaJZXiUrkJSnMmGVNBoVH1DNDtQ32tR4YFDRSpSUXjjvsiMnCvoPHVWXJP" +# ], +# ) + +# out_wallet_2 = WalletPolicy( +# "", +# "sh(wpkh(@0/**))", +# [ +# "xpub6D7atwj3ewAGT347tUzTNzfTGos1rCFVX4v8gViXiM2R1QHvox1LhEf6NtCeNsCwpppFUoQuS6mHUwfTveA5tEEwn2LqZHfVBEz5qvYmYhf" +# ], +# ) +# valid_destination_1 = BitcoinClient.get_address_from_wallet(out_wallet) +# valid_destination_2 = BitcoinClient.get_address_from_wallet(out_wallet_2) +# valid_refund = BitcoinClient.get_address_from_wallet(in_wallet) + + +# class TestsBitcoinSegwit: +# @pytest.mark.parametrize('test_to_run', ALL_TESTS_EXCEPT_MEMO) +# def test_bitcoin_segwit(self, backend, exchange_navigation_helper, test_to_run): +# BitcoinTestsSegwit(backend, exchange_navigation_helper).run_test(test_to_run) + + +class BitcoinTestsTaproot(BitcoinTestsCommon): + currency_configuration = cal.BTC_TAPROOT_CURRENCY_CONFIGURATION + in_wallet = WalletPolicy( + "", + "tr(@0/**)", + [ + "[f5acc2fd/86'/0'/0']xpub6DUYn4moKgHkK2d7bXX3mHTPb6XQwRVFRMdZ6ZwLS5u3nonGVpJiFeZiQkHutwdFqxKP75jex8gvVm7ed4euYeDtMnoiF1Cz1z4CeBJYWin" + ], + ) + + out_wallet = WalletPolicy( + "", + "tr(@0/**)", + [ + "[f5acc2fd/86'/1'/0']tpubDDKYE6BREvDsSWMazgHoyQWiJwYaDDYPbCFjYxN3HFXJP5fokeiK4hwK5tTLBNEDBwrDXn8cQ4v9b2xdW62Xr5yxoQdMu1v6c7UDXYVH27U" + ], + ) + + out_wallet_2 = WalletPolicy( + "", + "tr(@0/**)", + [ + "xpub6D7atwj3ewAGT347tUzTNzfTGos1rCFVX4v8gViXiM2R1QHvox1LhEf6NtCeNsCwpppFUoQuS6mHUwfTveA5tEEwn2LqZHfVBEz5qvYmYhf" + ], + ) + valid_refund = BitcoinClient.get_address_from_wallet(in_wallet) + valid_destination_1 = BitcoinClient.get_address_from_wallet(out_wallet) + valid_destination_2 = BitcoinClient.get_address_from_wallet(out_wallet_2) + +class TestsBitcoinTaproot: + @pytest.mark.parametrize('test_to_run', ALL_TESTS_EXCEPT_MEMO) + def test_bitcoin_taproot(self, backend, exchange_navigation_helper, test_to_run): + BitcoinTestsTaproot(backend, exchange_navigation_helper).run_test(test_to_run) +