示例#1
0
    def unlock_wallet(self):
        account_name = self.cfg.account
        active_key = ""
        memo_key = ""

        log.info(f"Try to found encrypted keys of account {account_name}")

        enc_keys = get_wallet_keys(account_name)

        if not enc_keys:
            log.info(f"{account_name} is new account. Let's add and encrypt keys\n")
            memo_key = getpass(f"Please Enter {account_name}'s active private key\n")
            active_key = getpass(
                f"Ok.\nPlease Enter {account_name}'s memo active key\n"
            )
            password = getpass(
                "Ok\nNow enter password to encrypt keys\n"
                "Warning! Currently there is NO WAY TO RECOVER your password!!! Please be careful!\n"
            )
            password_confirm = getpass("Repeat the password\n")
            if password == password_confirm:
                save_wallet_keys(
                    account_name,
                    encrypt(active_key, password),
                    encrypt(memo_key, password),
                )
                log.info(
                    f"Successfully encrypted and stored in file config/.{account_name}.keys"
                )
                del password_confirm
        else:
            while not (active_key and memo_key):
                password = getpass(
                    f"Account {account_name} found. Enter password to decrypt keys\n"
                )
                try:
                    active_key = decrypt(enc_keys["active"], password)
                    memo_key = decrypt(enc_keys["memo"], password)
                    log.info(
                        f"Successfully decrypted {account_name} keys:\n"
                        f"active: {active_key[:3] + '...' + active_key[-3:]}\n"
                        f"memo: {memo_key[:3] + '...' + memo_key[-3:]}\n"
                    )
                except DecryptionError:
                    log.warning("Wrong password!")
                except Exception as ex:
                    log.exception(ex)

        self.cfg.keys = [active_key, memo_key]
        del password
def test_secret_file_write_and_read():
    account_name = "test_acc"
    active_key = "active_key_1"
    memo_key = "memo_key_2"
    test_password = "******"
    enc_key_1 = encrypt(active_key, test_password)
    enc_key_2 = encrypt(memo_key, test_password)

    saved = save_wallet_keys(account_name, enc_key_1, enc_key_2)

    assert saved

    enc_keys = get_wallet_keys(account_name)

    for key_type, key in enc_keys.items():
        if key_type == "active":
            assert active_key == decrypt(key, test_password)
        elif key_type == "memo":
            assert memo_key == decrypt(key, test_password)
        else:
            raise

    os.remove(f"{project_root_dir}/.{account_name}.keys")
def test_crypto_types():
    start_string = "some_secret_string"
    test_password = "******"
    encrypted_string = encrypt(start_string, test_password)
    final_string = decrypt(encrypted_string, test_password)
    assert type(encrypted_string) is type(final_string)
def test_crypto_wrong_pass():
    start_string = "some_secret_string"
    test_password = "******"
    encrypted_string = encrypt(start_string, test_password)
    with pytest.raises(DecryptionError):
        decrypt(encrypted_string, "wrongpassword")
def test_crypto_success():
    start_string = "some_secret_string"
    test_password = "******"
    encrypted_string = encrypt(start_string, test_password)
    final_string = decrypt(encrypted_string, test_password)
    assert start_string == final_string