Coverage for tests/comm/test_bip32.py: 100%

51 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23from unittest import TestCase 

24from comm.bip32 import BIP32Element, BIP32Path 

25 

26import logging 

27 

28logging.disable(logging.CRITICAL) 

29 

30 

31class TestBIP32Element(TestCase): 

32 def test_normal(self): 

33 element = BIP32Element("456") 

34 self.assertFalse(element.is_hardened) 

35 self.assertEqual(456, element.spec_index) 

36 self.assertEqual(456, element.index) 

37 self.assertEqual("456", str(element)) 

38 

39 def test_max_normal(self): 

40 element = BIP32Element("0") 

41 self.assertFalse(element.is_hardened) 

42 self.assertEqual(0, element.spec_index) 

43 self.assertEqual(0, element.index) 

44 self.assertEqual("0", str(element)) 

45 

46 def test_hardened(self): 

47 element = BIP32Element("789'") 

48 self.assertTrue(element.is_hardened) 

49 self.assertEqual(789, element.spec_index) 

50 self.assertEqual(2147484437, element.index) 

51 self.assertEqual("789'", str(element)) 

52 

53 def test_max_hardened(self): 

54 element = BIP32Element("0'") 

55 self.assertTrue(element.is_hardened) 

56 self.assertEqual(0, element.spec_index) 

57 self.assertEqual(2147483648, element.index) 

58 self.assertEqual("0'", str(element)) 

59 

60 def test_spec_invalid(self): 

61 for spec in [ 

62 "", 

63 "notanumber", 

64 "notanumber'", 

65 "'", 

66 "2147483648", 

67 "2147483648'", 

68 "-1", 

69 "-1'", 

70 ]: 

71 with self.assertRaises(ValueError): 

72 BIP32Element(spec) 

73 

74 

75class TestBIP32Path(TestCase): 

76 def test_paths(self): 

77 self.assertEqual("m/44'/137'/0'/0/0", str(BIP32Path("m/44'/137'/0'/0/0"))) 

78 self.assertEqual("m/44'/0'/0'/0/0", str(BIP32Path("m/44'/0'/0'/0/0"))) 

79 

80 def test_to_binary(self): 

81 self.assertEqual( 

82 "052c00008089000080000000800000000000000000", 

83 BIP32Path("m/44'/137'/0'/0/0").to_binary().hex(), 

84 ) 

85 self.assertEqual( 

86 "058000002c80000089800000000000000000000000", 

87 BIP32Path("m/44'/137'/0'/0/0").to_binary("big").hex(), 

88 ) 

89 self.assertEqual( 

90 "052c00008000000080000000800000000000000000", 

91 BIP32Path("m/44'/0'/0'/0/0").to_binary().hex(), 

92 ) 

93 self.assertEqual( 

94 "058000002c80000000800000000000000000000000", 

95 BIP32Path("m/44'/0'/0'/0/0").to_binary("big").hex(), 

96 ) 

97 

98 def test_spec_invalid(self): 

99 for spec in ["44/1/2/3/4", "m/", "m/44'", "m/44'/0'/0/0/0/1", "notevenaspec"]: 

100 with self.assertRaises(ValueError): 

101 BIP32Path(spec) 

102 

103 def test_equality(self): 

104 self.assertEqual(BIP32Path("m/44'/0'/0'/0/0"), BIP32Path("m/44'/0'/0'/0/0")) 

105 self.assertEqual(BIP32Path("m/44'/137'/0'/0/0"), BIP32Path("m/44'/137'/0'/0/0")) 

106 self.assertNotEqual(BIP32Path("m/44'/137'/0'/0/0'"), 

107 BIP32Path("m/44'/137'/0'/0/0")) 

108 self.assertNotEqual(BIP32Path("m/45'/137'/0'/0/0"), 

109 BIP32Path("m/44'/137'/0'/0/0"))