|
1 | 1 | from unittest import TestCase
|
2 | 2 |
|
3 |
| -from buidl.mnemonic import secure_mnemonic |
| 3 | +from buidl.mnemonic import ( |
| 4 | + secure_mnemonic, |
| 5 | + dice_rolls_to_mnemonic, |
| 6 | + InvalidBIP39Length, |
| 7 | +) |
4 | 8 | from buidl.hd import HDPrivateKey
|
5 | 9 |
|
6 | 10 |
|
@@ -45,3 +49,128 @@ def test_secure_mnemonic_extra_entropy(self):
|
45 | 49 | secure_mnemonic(extra_entropy="not an int")
|
46 | 50 | with self.assertRaises(ValueError):
|
47 | 51 | secure_mnemonic(extra_entropy=-1)
|
| 52 | + |
| 53 | + def test_dice_to_mnemonic(self): |
| 54 | + tests = [ # dice_rolls, num_words, allow_low_entropy, expected_mnemonic, expected_exception |
| 55 | + [ |
| 56 | + "", |
| 57 | + 24, |
| 58 | + True, |
| 59 | + "together mail awful cradle scrub apart hip leader silk slice unusual embark kit can muscle nature nation gown century cram resource citizen throw produce", |
| 60 | + None, |
| 61 | + ], |
| 62 | + [ |
| 63 | + "123456", |
| 64 | + 24, |
| 65 | + True, |
| 66 | + "mirror reject rookie talk pudding throw happy era myth already payment own sentence push head sting video explain letter bomb casual hotel rather garment", |
| 67 | + None, |
| 68 | + ], |
| 69 | + [ |
| 70 | + "123456123456123456123456123456123456123456123456123456123456123456123456123456123456123456123456123456", |
| 71 | + 24, |
| 72 | + False, |
| 73 | + "more matter caught bind tip twin indicate visa rifle angle defense lizard stock cave cradle injury always mule photo horse range opinion affair garlic", |
| 74 | + None, |
| 75 | + ], |
| 76 | + [ |
| 77 | + "523365252662366", |
| 78 | + 24, |
| 79 | + True, |
| 80 | + "dilemma rural physical exhaust divorce escape nut umbrella lawn midnight prosper prevent employ caught mercy student arctic umbrella feed super mad magic crawl fiscal", |
| 81 | + None, |
| 82 | + ], |
| 83 | + [ |
| 84 | + "", |
| 85 | + 12, |
| 86 | + True, |
| 87 | + "together mail awful cradle scrub apart hip leader silk slice unusual embark", |
| 88 | + None, |
| 89 | + ], |
| 90 | + [ |
| 91 | + "123456", |
| 92 | + 12, |
| 93 | + True, |
| 94 | + "mirror reject rookie talk pudding throw happy era myth already payment owner", |
| 95 | + None, |
| 96 | + ], |
| 97 | + [ |
| 98 | + "12345612345612345612345612345612345612345612345612", |
| 99 | + 12, |
| 100 | + False, |
| 101 | + "unveil nice picture region tragic fault cream strike tourist control recipe tourist", |
| 102 | + None, |
| 103 | + ], |
| 104 | + [ |
| 105 | + "12345612345612345612345612345612345612345612345612345612345612", |
| 106 | + 15, |
| 107 | + False, |
| 108 | + "end spider topple cliff tomorrow process dismiss produce athlete film monster team vacant ill silk", |
| 109 | + None, |
| 110 | + ], |
| 111 | + [ |
| 112 | + "123456123456123456123456123456123456123456123456123456123456123456123456123", |
| 113 | + 18, |
| 114 | + False, |
| 115 | + "melt churn alley retreat flip once enough gather project prosper cannon nasty furnace isolate cost laundry lottery slice", |
| 116 | + None, |
| 117 | + ], |
| 118 | + [ |
| 119 | + "123456123456123456123456123456123456123456123456123456123456123456123456123456123456123", |
| 120 | + 21, |
| 121 | + False, |
| 122 | + "start insane amazing fall kite punch owner refuse bone trigger spirit luggage slide sound reopen broom remember nose limb swallow kitten", |
| 123 | + None, |
| 124 | + ], |
| 125 | + [ # low entropy not allowed |
| 126 | + "123456", |
| 127 | + 24, |
| 128 | + False, |
| 129 | + "", |
| 130 | + ValueError( |
| 131 | + "Received 6 rolls but need at least 100 rolls (for 256 bits of entropy)" |
| 132 | + ), |
| 133 | + ], |
| 134 | + [ # Invalid num_words |
| 135 | + "123456", |
| 136 | + 23, |
| 137 | + False, |
| 138 | + "", |
| 139 | + InvalidBIP39Length( |
| 140 | + "23 words requested (must be 12, 15, 18, 21, or 24 words)" |
| 141 | + ), |
| 142 | + ], |
| 143 | + [ # non-string dice rolls |
| 144 | + b"123456", |
| 145 | + 24, |
| 146 | + False, |
| 147 | + "", |
| 148 | + ValueError("Dice rolls must be provided as a string"), |
| 149 | + ], |
| 150 | + [ # string containing non-dice values |
| 151 | + "1234567", |
| 152 | + 24, |
| 153 | + False, |
| 154 | + "", |
| 155 | + ValueError("Dice roll string contained invalid dice numbers"), |
| 156 | + ], |
| 157 | + ] |
| 158 | + |
| 159 | + for ( |
| 160 | + dice_rolls, |
| 161 | + num_words, |
| 162 | + allow_low_entropy, |
| 163 | + expected_mnemonic, |
| 164 | + expected_exception, |
| 165 | + ) in tests: |
| 166 | + if expected_exception is None: |
| 167 | + received_mnemonic = dice_rolls_to_mnemonic( |
| 168 | + dice_rolls, num_words, allow_low_entropy |
| 169 | + ) |
| 170 | + self.assertEqual(received_mnemonic, expected_mnemonic) |
| 171 | + else: |
| 172 | + with self.assertRaises(type(expected_exception)) as exception_context: |
| 173 | + dice_rolls_to_mnemonic(dice_rolls, num_words, allow_low_entropy) |
| 174 | + self.assertEqual( |
| 175 | + str(exception_context.exception), str(expected_exception) |
| 176 | + ) |
0 commit comments