Skip to content

Commit

Permalink
Fix docstring and missing tests for KeyPairSecretsHelper.mnemonic_bip…
Browse files Browse the repository at this point in the history
…39_to_seed
  • Loading branch information
Tim Churchard committed Jun 4, 2021
1 parent ff3e2b8 commit 6cea37f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
23 changes: 20 additions & 3 deletions iotics/lib/identity/crypto/key_pair_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,26 @@ class KeyPairSecretsHelper:

@staticmethod
def mnemonic_bip39_to_seed(mnemonic: str, lang: str = 'english') -> bytes:
"""mnemonic_bip39_to_seed: Take mnemonic string and return seed string hex"""
men = Mnemonic(lang)
return men.to_entropy(mnemonic)
"""
Take mnemonic string and return seed bytes
:param mnemonic: mnemonic string
:param lang: language
:return: seed bytes
:raises:
IdentityValidationError: if invalid seed
IdentityValidationError: if invalid lang
IdentityDependencyError: if incompatible Mnemonic dependency
"""
try:
men = Mnemonic(lang)
return men.to_entropy(mnemonic)
except ConfigurationError as err:
raise IdentityDependencyError(f'Dependency Mnemonic Internal Error: {err}') from err
except (LookupError, ValueError) as err:
raise IdentityValidationError(f'{err}') from err
except OSError as err:
raise IdentityValidationError(f'Invalid language for mnemonic: {err}') from err

@staticmethod
def seed_bip39_to_mnemonic(seed: bytes, lang: str = 'english') -> str:
Expand Down
2 changes: 1 addition & 1 deletion iotics/lib/identity/crypto/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class KeysHelper:
@staticmethod
def get_private_ECDSA(private_expo: str) -> ec.EllipticCurvePrivateKey:
"""
Get private key (ECDSA) from master and purpose
Get private key (ECDSA) from private exponent
:param private_expo: private exponent as hex string
:return: private ECDSA key
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/iotics/lib/identity/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ def valid_bip39_seed():
return b'd2397e8b83cf4a7073a26c1a1cdb6b65'


@pytest.fixture
def valid_bip39_mnemonic_english():
# Note: This matches the d239...6b65 bip39_seed from above
return 'goddess muscle soft human fatal country this hockey great perfect evidence gather industry rack silver ' + \
'small cousin another flee silver casino country sugar purse'


@pytest.fixture
def valid_bip39_mnemonic_spanish():
# Note: This matches the d239...6b65 bip39_seed from above
return 'glaciar mojar rueda hueso exponer chupar tanque hijo grano olvido ensayo gaita inmune percha retrato ' + \
'rojo cielo alivio fiel retrato brusco chupar sirena peine'


@pytest.fixture
def valid_key_pair_secrets(valid_bip39_seed):
return KeyPairSecrets.build(valid_bip39_seed, 'iotics/0/something/user')
Expand Down
36 changes: 28 additions & 8 deletions tests/unit/iotics/lib/identity/crypto/test_key_pair_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
from iotics.lib.identity.error import IdentityValidationError


def test_can_convert_seed_to_mnemonic(valid_bip39_seed):
def test_can_convert_seed_to_mnemonic(valid_bip39_seed, valid_bip39_mnemonic_english):
mnemonic = KeyPairSecretsHelper.seed_bip39_to_mnemonic(valid_bip39_seed)
assert mnemonic == ('goddess muscle soft human fatal country this hockey great perfect evidence '
'gather industry rack silver small cousin another flee silver casino country '
'sugar purse')
assert mnemonic == valid_bip39_mnemonic_english


def test_can_convert_seed_to_mnemonic_with_spanish(valid_bip39_seed):
def test_can_convert_seed_to_mnemonic_with_spanish(valid_bip39_seed, valid_bip39_mnemonic_spanish):
mnemonic = KeyPairSecretsHelper.seed_bip39_to_mnemonic(valid_bip39_seed, lang='spanish')
assert mnemonic == ('glaciar mojar rueda hueso exponer chupar tanque hijo grano olvido ensayo '
'gaita inmune percha retrato rojo cielo alivio fiel retrato brusco chupar '
'sirena peine')
assert mnemonic == valid_bip39_mnemonic_spanish


@pytest.mark.parametrize('invalid_seed,error_val', (('a' * 16, 'Invalid seed format'),
Expand All @@ -35,6 +31,30 @@ def test_convert_seed_to_mnemonic_raises_validation_error_if_invalid_language(va
with pytest.raises(IdentityValidationError) as err_wrp:
KeyPairSecretsHelper.seed_bip39_to_mnemonic(valid_bip39_seed, lang='invalid_lang')
assert 'Invalid language for mnemonic:' in str(err_wrp.value)
assert isinstance(err_wrp.value.__cause__, FileNotFoundError)


def test_can_convert_mnemonic_to_seed(valid_bip39_mnemonic_english, valid_bip39_seed):
seed = KeyPairSecretsHelper.mnemonic_bip39_to_seed(valid_bip39_mnemonic_english, lang='english')
assert seed == valid_bip39_seed


def test_can_convert_mnemonic_to_seed_with_spanish(valid_bip39_mnemonic_spanish, valid_bip39_seed):
seed = KeyPairSecretsHelper.mnemonic_bip39_to_seed(valid_bip39_mnemonic_spanish, lang='spanish')
assert seed == valid_bip39_seed


@pytest.mark.parametrize('invalid_mnemonic', ('flee' * 10,
'flee' * 32))
def test_convert_mnemonic_to_seed_raises_validation_error_if_invalid_seed(invalid_mnemonic):
with pytest.raises(IdentityValidationError):
KeyPairSecretsHelper.mnemonic_bip39_to_seed(invalid_mnemonic)


def test_convert_mnemonic_to_seed_raises_validation_error_if_invalid_language(valid_bip39_mnemonic_english):
with pytest.raises(IdentityValidationError) as err_wrp:
KeyPairSecretsHelper.mnemonic_bip39_to_seed(valid_bip39_mnemonic_english, lang='invalid_lang')
assert isinstance(err_wrp.value.__cause__, FileNotFoundError)


def test_validate_bip39_seed_should_not_raise_if_valid_seed(valid_bip39_seed):
Expand Down

0 comments on commit 6cea37f

Please sign in to comment.