Exemplo n.º 1
0
def test_import_custom_keystore(tmpdir):

    # Too short - 32 bytes is required
    custom_secret = b'tooshort'
    with pytest.raises(ValueError, match=f'Entropy bytes bust be exactly {SecretKey.serialized_size()}.'):
        _keystore = Keystore.import_secure(key_material=custom_secret,
                                           password=INSECURE_DEVELOPMENT_PASSWORD,
                                           keystore_dir=tmpdir)

    # Too short - 32 bytes is required
    custom_secret = b'thisisabunchofbytesthatisabittoolong'
    with pytest.raises(ValueError, match=f'Entropy bytes bust be exactly {SecretKey.serialized_size()}.'):
        _keystore = Keystore.import_secure(key_material=custom_secret,
                                           password=INSECURE_DEVELOPMENT_PASSWORD,
                                           keystore_dir=tmpdir)

    # Import private key
    custom_secret = os.urandom(SecretKey.serialized_size())  # insecure but works
    keystore = Keystore.import_secure(key_material=custom_secret,
                                      password=INSECURE_DEVELOPMENT_PASSWORD,
                                      keystore_dir=tmpdir)
    keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
    assert keystore._Keystore__secret == custom_secret
    keystore.lock()

    path = keystore.keystore_path
    del keystore

    # Restore custom secret from encrypted keystore file
    keystore = Keystore(keystore_path=path)
    keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
    assert keystore._Keystore__secret == custom_secret
Exemplo n.º 2
0
def test_decrypt_keystore(tmpdir, mocker):

    # Setup
    spy = mocker.spy(Mnemonic, 'generate')

    # Decrypt post-generation
    keystore = Keystore.generate(INSECURE_DEVELOPMENT_PASSWORD, keystore_dir=tmpdir)
    keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
    mnemonic = Mnemonic(_MNEMONIC_LANGUAGE)
    words = spy.spy_return
    secret = bytes(mnemonic.to_entropy(words))
    assert keystore._Keystore__secret == secret

    # Decrypt from keystore file
    keystore_path = keystore.keystore_path
    del words
    del keystore
    keystore = Keystore(keystore_path=keystore_path)
    keystore.unlock(INSECURE_DEVELOPMENT_PASSWORD)
    assert keystore._Keystore__secret == secret