mirror of
https://github.com/SeedSigner/seedsigner.git
synced 2026-10-05 15:08:25 +00:00
109 lines
5.0 KiB
Python
109 lines
5.0 KiB
Python
import random
|
|
|
|
from embit import bip39
|
|
from seedsigner.helpers import mnemonic_generation
|
|
from seedsigner.models.settings_definition import SettingsConstants
|
|
|
|
|
|
|
|
def test_dice_rolls():
|
|
""" Given random dice rolls, the resulting mnemonic should be valid. """
|
|
dice_rolls = ""
|
|
for i in range(0, 99):
|
|
# Do not need truly rigorous random for this test
|
|
dice_rolls += str(random.randint(0, 5))
|
|
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
assert len(mnemonic) == 24
|
|
assert bip39.mnemonic_is_valid(" ".join(mnemonic))
|
|
|
|
dice_rolls = ""
|
|
for i in range(0, 50):
|
|
# Do not need truly rigorous random for this test
|
|
dice_rolls += str(random.randint(0, 5))
|
|
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
assert len(mnemonic) == 12
|
|
assert bip39.mnemonic_is_valid(" ".join(mnemonic))
|
|
|
|
|
|
|
|
def test_calculate_checksum():
|
|
""" Given an 11-word or 23-word mnemonic, the calculated checksum should yield a
|
|
valid complete mnemonic.
|
|
"""
|
|
# Test mnemonics from https://iancoleman.io/bip39/
|
|
partial_mnemonic = "crawl focus rescue cable view pledge rather dinner cousin unfair day"
|
|
mnemonic = mnemonic_generation.calculate_checksum(partial_mnemonic.split(" "), wordlist_language_code=SettingsConstants.WORDLIST_LANGUAGE__ENGLISH)
|
|
assert bip39.mnemonic_is_valid(" ".join(mnemonic))
|
|
|
|
partial_mnemonic = "bubble father debate ankle injury fence mesh evolve section wet coyote violin pyramid flower rent arrow round clutch myth safe base skin mobile"
|
|
mnemonic = mnemonic_generation.calculate_checksum(partial_mnemonic.split(" "), wordlist_language_code=SettingsConstants.WORDLIST_LANGUAGE__ENGLISH)
|
|
assert bip39.mnemonic_is_valid(" ".join(mnemonic))
|
|
|
|
|
|
|
|
def test_verify_against_coldcard_sample():
|
|
""" https://coldcard.com/docs/verifying-dice-roll-math """
|
|
dice_rolls = "123456"
|
|
expected = "mirror reject rookie talk pudding throw happy era myth already payment own sentence push head sting video explain letter bomb casual hotel rather garment"
|
|
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|
|
|
|
|
|
|
|
def test_known_dice_rolls():
|
|
""" Given 99 known dice rolls, the resulting mnemonic should be valid and match the expected. """
|
|
dice_rolls = "522222222222222222222222222222222222222222222555555555555555555555555555555555555555555555555555555"
|
|
expected = "resource timber firm banner horror pupil frozen main pear direct pioneer broken grid core insane begin sister pony end debate task silk empty curious"
|
|
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|
|
|
|
dice_rolls = "222222222222222222222222222222222222222222222555555555555555555555555555555555555555555555555555555"
|
|
expected = "garden uphold level clog sword globe armor issue two cute scorpion improve verb artwork blind tail raw butter combine move produce foil feature wave"
|
|
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|
|
|
|
dice_rolls = "222222222222222222222222222222222222222222222555555555555555555555555555555555555555555555555555556"
|
|
expected = "lizard broken love tired depend eyebrow excess lonely advance father various cram ignore panic feed plunge miss regret boring unique galaxy fan detail fly"
|
|
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|
|
|
|
|
|
|
|
def test_50_dice_rolls():
|
|
""" 50 dice roll input should yield the same 12-word mnemonic as iancoleman.io/bip39 """
|
|
# Check "Show entropy details", paste in dice_rolls sequence, click "Hex", select "Mnemonic Length" as "12 Words"
|
|
dice_rolls = "12345612345612345612345612345612345612345612345612"
|
|
expected = "unveil nice picture region tragic fault cream strike tourist control recipe tourist"
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|
|
|
|
dice_rolls = "11111111111111111111111111111111111111111111111111"
|
|
expected = "diet glad hat rural panther lawsuit act drop gallery urge where fit"
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|
|
|
|
dice_rolls = "66666666666666666666666666666666666666666666666666"
|
|
expected = "senior morning song proud recycle toy search apple trigger lend vibrant arrest"
|
|
mnemonic = mnemonic_generation.generate_mnemonic_from_dice(dice_rolls)
|
|
actual = " ".join(mnemonic)
|
|
assert bip39.mnemonic_is_valid(actual)
|
|
assert actual == expected
|