Coverage for tests/utils/multisig_sample_code_tester.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1import logging 

2 

3from monero import ( 

4 MoneroWallet, MoneroUtils, 

5 MoneroMultisigInitResult, MoneroMultisigInfo 

6) 

7 

8from .wallet_utils import WalletUtils 

9from .test_utils import TestUtils 

10 

11logger: logging.Logger = logging.getLogger("MultisigSampleCodeTester") 

12 

13 

14class MultisigSampleCodeTester: 

15 """Create and test a multisig wallet with M/N configuration.""" 

16 

17 m: int 

18 """Multisig threshold.""" 

19 n: int 

20 """Multisig participants.""" 

21 wallets: list[MoneroWallet] 

22 """Participants test wallets.""" 

23 

24 def __init__(self, m: int, participants: list[MoneroWallet]) -> None: 

25 """Initialize a new create-multisig-wallet tester. 

26 

27 :param int m: multisig threshold. 

28 :param list[MoneroWallet] participants: participant wallets. 

29 """ 

30 n: int = len(participants) 

31 assert m <= n, "Threshold must be less or equal to number of participants" 

32 self.m = m 

33 self.n = n 

34 self.wallets = participants 

35 self._disposed = False 

36 

37 def make_multisig_wallets(self) -> list[str]: 

38 """Prepare and make each participant wallet multisig. 

39 

40 :returns list[str]: the multisig hex produced by each participant wallet. 

41 """ 

42 # prepare and collect multisig hex from each participant 

43 prepared_multisig_hexes: list[str] = [] 

44 

45 for wallet in self.wallets: 

46 prepared_multisig_hexes.append(wallet.prepare_multisig()) 

47 

48 # make each wallet multisig and collect results 

49 num_wallets: int = len(self.wallets) 

50 made_multisig_hexes: list[str] = [] 

51 for i in range(num_wallets): 

52 # collect prepared multisig hexes from wallet's peers 

53 peer_multisig_hexes: list[str] = [] 

54 for j in range(num_wallets): 

55 if j != i: 

56 peer_multisig_hexes.append(prepared_multisig_hexes[j]) 

57 

58 # make wallet multisig and collect result hex 

59 multisig_hex: str = self.wallets[i].make_multisig(peer_multisig_hexes, self.m, TestUtils.WALLET_PASSWORD) 

60 made_multisig_hexes.append(multisig_hex) 

61 

62 return made_multisig_hexes.copy() 

63 

64 def test(self) -> None: 

65 """Test multisig wallet creation.""" 

66 

67 # exchange multisig keys N - M + 1 times 

68 multisig_hexes: list[str] = self.make_multisig_wallets() 

69 for i in range(self.n - self.m + 1): 

70 # exchange multisig keys among participants and collect results for next round if applicable 

71 result_multisig_hexes: list[str] = [] 

72 for wallet in self.wallets: 

73 # import the multisig hex of other participants and collect results 

74 result: MoneroMultisigInitResult = wallet.exchange_multisig_keys(multisig_hexes, TestUtils.WALLET_PASSWORD) 

75 logger.debug(f"Round {i + 1}, multisig init result: {result.serialize()}") 

76 assert result.multisig_hex is not None 

77 result_multisig_hexes.append(result.multisig_hex) 

78 

79 # use resulting multisig hex for next round of exchange if applicable 

80 multisig_hexes = result_multisig_hexes 

81 

82 # wallets are now multisig 

83 for wallet in self.wallets: 

84 primary_address: str = wallet.get_address(0, 0) 

85 # TODO: replace with MoneroWallet.get_network_type() when all methods defined in interface 

86 MoneroUtils.validate_address(primary_address, TestUtils.NETWORK_TYPE) 

87 info: MoneroMultisigInfo = wallet.get_multisig_info() 

88 WalletUtils.test_multisig_info(info, self.m, self.n) 

89 wallet.close(True)