diff --git a/tests/test_monero_wallet_full.py b/tests/test_monero_wallet_full.py index e56660a..3c56cf1 100644 --- a/tests/test_monero_wallet_full.py +++ b/tests/test_monero_wallet_full.py @@ -10,7 +10,8 @@ from utils import ( TestUtils as Utils, StringUtils, - AssertUtils, WalletUtils, WalletType + AssertUtils, WalletUtils, WalletType, + MultisigSampleCodeTester ) from test_monero_wallet_common import BaseTestMoneroWallet @@ -199,6 +200,15 @@ def test_close(self) -> None: # TODO monero-cpp add monero_wallet::is_closed() #assert wallet.is_closed() + # Supports multisig sample code + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + def test_multisig_sample(self) -> None: + self._test_multisig_sample(1, 2) + self._test_multisig_sample(1, 4) + self._test_multisig_sample(2, 2) + self._test_multisig_sample(2, 3) + self._test_multisig_sample(2, 4) + #endregion #region Disabled Tests @@ -224,3 +234,23 @@ def test_set_account_label(self, wallet: MoneroWallet) -> None: super().test_set_account_label(wallet) #endregion + + #region Utils + + def _test_multisig_sample(self, m: int, n: int) -> None: + """ + Test multisig sample code. + + :param int m: multisig threshold. + :param int n: number of participants. + """ + # create participant wallets + wallets: list[MoneroWalletFull] = [] + for i in range(n): + wallets.append(self._create_wallet(MoneroWalletConfig())) + logger.debug(f"Created multisig wallet participant {i + 1}") + + tester: MultisigSampleCodeTester = MultisigSampleCodeTester(m, wallets) + tester.test() + + #endregion diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index 35139d1..c9df6f8 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -27,6 +27,7 @@ from .view_only_and_offline_wallet_tester import ViewOnlyAndOfflineWalletTester from .wallet_notification_collector import WalletNotificationCollector from .submit_then_relay_tx_tester import SubmitThenRelayTxTester +from .multisig_sample_code_tester import MultisigSampleCodeTester __all__ = [ 'WalletUtils', @@ -57,5 +58,6 @@ 'WalletType', 'ViewOnlyAndOfflineWalletTester', 'WalletNotificationCollector', - 'SubmitThenRelayTxTester' + 'SubmitThenRelayTxTester', + 'MultisigSampleCodeTester' ] diff --git a/tests/utils/multisig_sample_code_tester.py b/tests/utils/multisig_sample_code_tester.py new file mode 100644 index 0000000..501aebc --- /dev/null +++ b/tests/utils/multisig_sample_code_tester.py @@ -0,0 +1,81 @@ + +from monero import ( + MoneroWalletFull, MoneroMultisigInitResult, + MoneroUtils, MoneroMultisigInfo +) + +from .test_utils import TestUtils + + +class MultisigSampleCodeTester: + """Create and test a multisig wallet with M/N configuration""" + + m: int + """Multisig threshold""" + n: int + """Multisig participants""" + wallets: list[MoneroWalletFull] + """Participants test wallets""" + + def __init__(self, m: int, participants: list[MoneroWalletFull]) -> None: + """ + Initialize a new create-multisig-wallet tester. + + :param int m: multisig threshold. + :param list[MoneroWalletFull]: participant wallets. + """ + n: int = len(participants) + assert m <= n, "Threshold must be less or equal to number of participants" + self.m = m + self.n = n + self.wallets = participants + + def test(self) -> None: + """ + Test multisig wallet creation. + """ + # prepare and collect multisig hex from each participant + prepared_multisig_hexes: list[str] = [] + + for wallet in self.wallets: + prepared_multisig_hexes.append(wallet.prepare_multisig()) + + # make each wallet multisig and collect results + num_wallets: int = len(self.wallets) + made_multisig_hexes: list[str] = [] + for i in range(num_wallets): + # collect prepared multisig hexes from wallet's peers + peer_multisig_hexes: list[str] = [] + for j in range(num_wallets): + if j != i: + peer_multisig_hexes.append(prepared_multisig_hexes[j]) + + # make wallet multisig and collect result hex + multisig_hex: str = self.wallets[i].make_multisig(peer_multisig_hexes, self.m, TestUtils.WALLET_PASSWORD) + made_multisig_hexes.append(multisig_hex) + + # exchange multisig keys N - M + 1 times + multisig_hexes: list[str] = made_multisig_hexes.copy() + for i in range(self.n - self.m + 1): + # exchange multisig keys among participants and collect results for next round if applicable + result_multisig_hexes: list[str] = [] + for wallet in self.wallets: + # import the multisig hex of other participants and collect results + result: MoneroMultisigInitResult = wallet.exchange_multisig_keys(multisig_hexes, TestUtils.WALLET_PASSWORD) + assert result.multisig_hex is not None + result_multisig_hexes.append(result.multisig_hex) + + # use resulting multisig hex for next round of exchange if applicable + multisig_hexes = result_multisig_hexes + + # wallets are now multisig + for wallet in self.wallets: + primary_address: str = wallet.get_address(0, 0) + # TODO: replace with MoneroWallet.get_network_type() when all methods defined in interface + MoneroUtils.validate_address(primary_address, TestUtils.NETWORK_TYPE) + info: MoneroMultisigInfo = wallet.get_multisig_info() + assert info.is_multisig is True + assert info.is_ready is True + assert self.m == info.threshold + assert self.n == info.num_participants + wallet.close(True)