def test_ursula_and_local_keystore_signer_integration(
        click_runner, tmp_path, staking_providers, application_economics,
        mocker, mock_funded_account_password_keystore, testerchain):
    config_root_path = tmp_path
    ursula_config_path = config_root_path / UrsulaConfiguration.generate_filename(
    )
    worker_account, password, mock_keystore_path = mock_funded_account_password_keystore

    #
    # Operator Steps
    #

    # Good signer...
    mock_signer_uri = f'keystore:{mock_keystore_path}'
    pre_config_signer = KeystoreSigner.from_signer_uri(uri=mock_signer_uri,
                                                       testnet=True)
    assert worker_account.address in pre_config_signer.accounts

    deploy_port = select_test_port()

    init_args = (
        'ursula',
        'init',
        '--network',
        TEMPORARY_DOMAIN,
        '--payment-network',
        TEMPORARY_DOMAIN,
        '--operator-address',
        worker_account.address,
        '--config-root',
        str(config_root_path.absolute()),
        '--eth-provider',
        TEST_ETH_PROVIDER_URI,
        '--payment-provider',
        TEST_POLYGON_PROVIDER_URI,
        '--rest-host',
        MOCK_IP_ADDRESS,
        '--rest-port',
        deploy_port,

        # The bit we are testing for here
        '--signer',
        mock_signer_uri)

    cli_env = {
        NUCYPHER_ENVVAR_KEYSTORE_PASSWORD: password,
        NUCYPHER_ENVVAR_OPERATOR_ETH_PASSWORD: password,
    }
    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 catch_exceptions=False,
                                 env=cli_env)
    assert result.exit_code == 0, result.stdout

    # Inspect the configuration file for the signer URI
    with open(ursula_config_path, 'r') as config_file:
        raw_config_data = config_file.read()
        config_data = json.loads(raw_config_data)
        assert config_data['signer_uri'] == mock_signer_uri,\
            "Keystore URI was not correctly included in configuration file"

    # Recreate a configuration with the signer URI preserved
    ursula_config = UrsulaConfiguration.from_configuration_file(
        ursula_config_path)
    assert ursula_config.signer_uri == mock_signer_uri

    # Mock decryption of web3 client keystore
    mocker.patch.object(Account, 'decrypt', return_value=worker_account.key)
    ursula_config.keystore.unlock(password=password)

    # Produce an Ursula with a Keystore signer correctly derived from the signer URI, and don't do anything else!
    ursula = ursula_config.produce()
    ursula.signer.unlock_account(account=worker_account.address,
                                 password=password)

    try:
        # Verify the keystore path is still preserved
        assert isinstance(ursula.signer, KeystoreSigner)
        assert isinstance(ursula.signer.path, Path), "Use Path"
        assert ursula.signer.path.absolute() == mock_keystore_path.absolute()

        # Show that we can produce the exact same signer as pre-config...
        assert pre_config_signer.path == ursula.signer.path

        # ...and that transactions are signed by the keystore signer
        txhash = ursula.confirm_address()
        receipt = testerchain.wait_for_receipt(txhash)
        transaction_data = testerchain.client.w3.eth.getTransaction(
            receipt['transactionHash'])
        assert transaction_data['from'] == worker_account.address
    finally:
        ursula.stop()
def test_ursula_init_with_local_keystore_signer(click_runner, custom_filepath,
                                                custom_config_filepath,
                                                agency_local_registry,
                                                worker_account, mocker,
                                                testerchain):

    # Good signer...
    pre_config_signer = KeystoreSigner.from_signer_uri(uri=MOCK_SIGNER_URI)
    assert worker_account.address in pre_config_signer.accounts

    init_args = (
        'ursula',
        'init',
        '--network',
        TEMPORARY_DOMAIN,
        '--worker-address',
        worker_account.address,
        '--config-root',
        custom_filepath,
        '--provider',
        TEST_PROVIDER_URI,
        '--registry-filepath',
        agency_local_registry.filepath,
        '--rest-host',
        MOCK_IP_ADDRESS,
        '--rest-port',
        MOCK_URSULA_STARTING_PORT,

        # The bit were' testing here
        '--signer',
        MOCK_SIGNER_URI)

    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 catch_exceptions=False,
                                 env=CLI_ENV)
    assert result.exit_code == 0, result.stdout

    # Inspect the configuration file for the signer URI
    with open(custom_config_filepath, 'r') as config_file:
        raw_config_data = config_file.read()
        config_data = json.loads(raw_config_data)
        assert config_data['signer_uri'] == MOCK_SIGNER_URI,\
            "Keystore URI was not correctly included in configuration file"

    # Recreate a configuration with the signer URI preserved
    ursula_config = UrsulaConfiguration.from_configuration_file(
        custom_config_filepath)
    assert ursula_config.signer_uri == MOCK_SIGNER_URI

    # Mock decryption of web3 client keyring
    mocker.patch.object(Account,
                        'decrypt',
                        return_value=worker_account.privateKey)
    ursula_config.attach_keyring(checksum_address=worker_account.address)
    ursula_config.keyring.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)

    # Produce an ursula with a Keystore signer correctly derived from the signer URI, and dont do anything else!
    mocker.patch.object(StakeList, 'refresh', autospec=True)
    ursula = ursula_config.produce(
        client_password=INSECURE_DEVELOPMENT_PASSWORD, block_until_ready=False)

    # Verify the keystore path is still preserved
    assert isinstance(ursula.signer, KeystoreSigner)
    assert ursula.signer.path == MOCK_KEYSTORE_PATH

    # Show that we can produce the exact same signer as pre-config...
    assert pre_config_signer.path == ursula.signer.path

    # ...and that transactions are signed by the keytore signer
    receipt = ursula.confirm_activity()
    transaction_data = testerchain.client.w3.eth.getTransaction(
        receipt['transactionHash'])
    assert transaction_data['from'] == worker_account.address
Esempio n. 3
0
def test_ursula_and_local_keystore_signer_integration(
        click_runner, tmp_path, manual_staker, stake_value, token_economics,
        mocker, mock_funded_account_password_keystore, testerchain):
    config_root_path = tmp_path
    ursula_config_path = config_root_path / UrsulaConfiguration.generate_filename(
    )
    stakeholder_config_path = config_root_path / StakeHolderConfiguration.generate_filename(
    )
    worker_account, password, mock_keystore_path = mock_funded_account_password_keystore

    #
    # Stakeholder Steps
    #

    init_args = ('stake', 'init-stakeholder', '--config-root',
                 config_root_path, '--provider', TEST_PROVIDER_URI,
                 '--network', TEMPORARY_DOMAIN)
    click_runner.invoke(nucypher_cli, init_args, catch_exceptions=False)

    stake_args = ('stake', 'create', '--config-file', stakeholder_config_path,
                  '--staking-address', manual_staker, '--value',
                  stake_value.to_tokens(), '--lock-periods',
                  token_economics.minimum_locked_periods, '--force')
    # TODO: Is This test is writing to the default system directory and ignoring updates to the passed filepath?
    user_input = f'0\n{password}\nY\n'
    click_runner.invoke(nucypher_cli,
                        stake_args,
                        input=user_input,
                        catch_exceptions=False)

    init_args = ('stake', 'bond-worker', '--config-file',
                 stakeholder_config_path, '--staking-address', manual_staker,
                 '--worker-address', worker_account.address, '--force')
    user_input = password
    click_runner.invoke(nucypher_cli,
                        init_args,
                        input=user_input,
                        catch_exceptions=False)

    #
    # Worker Steps
    #

    # Good signer...
    mock_signer_uri = f'keystore:{mock_keystore_path}'
    pre_config_signer = KeystoreSigner.from_signer_uri(uri=mock_signer_uri,
                                                       testnet=True)
    assert worker_account.address in pre_config_signer.accounts

    deploy_port = select_test_port()

    init_args = (
        'ursula',
        'init',
        '--network',
        TEMPORARY_DOMAIN,
        '--worker-address',
        worker_account.address,
        '--config-root',
        config_root_path,
        '--provider',
        TEST_PROVIDER_URI,
        '--rest-host',
        MOCK_IP_ADDRESS,
        '--rest-port',
        deploy_port,

        # The bit we are testing for here
        '--signer',
        mock_signer_uri)

    cli_env = {
        NUCYPHER_ENVVAR_KEYRING_PASSWORD: password,
        NUCYPHER_ENVVAR_WORKER_ETH_PASSWORD: password,
    }
    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 catch_exceptions=False,
                                 env=cli_env)
    assert result.exit_code == 0, result.stdout

    # Inspect the configuration file for the signer URI
    with open(ursula_config_path, 'r') as config_file:
        raw_config_data = config_file.read()
        config_data = json.loads(raw_config_data)
        assert config_data['signer_uri'] == mock_signer_uri,\
            "Keystore URI was not correctly included in configuration file"

    # Recreate a configuration with the signer URI preserved
    ursula_config = UrsulaConfiguration.from_configuration_file(
        ursula_config_path)
    assert ursula_config.signer_uri == mock_signer_uri

    # Mock decryption of web3 client keyring
    mocker.patch.object(Account,
                        'decrypt',
                        return_value=worker_account.privateKey)
    ursula_config.attach_keyring(checksum_address=worker_account.address)
    ursula_config.keyring.unlock(password=password)

    # Produce an Ursula with a Keystore signer correctly derived from the signer URI, and don't do anything else!
    mocker.patch.object(StakeList, 'refresh', autospec=True)
    ursula = ursula_config.produce(client_password=password,
                                   commit_now=False,
                                   block_until_ready=False)

    try:
        # Verify the keystore path is still preserved
        assert isinstance(ursula.signer, KeystoreSigner)
        assert isinstance(ursula.signer.path, str), "Use str"
        assert ursula.signer.path == str(mock_keystore_path)

        # Show that we can produce the exact same signer as pre-config...
        assert pre_config_signer.path == ursula.signer.path

        # ...and that transactions are signed by the keystore signer
        txhash = ursula.commit_to_next_period()
        receipt = testerchain.wait_for_receipt(txhash)
        transaction_data = testerchain.client.w3.eth.getTransaction(
            receipt['transactionHash'])
        assert transaction_data['from'] == worker_account.address
    finally:
        ursula.stop()
def test_ursula_init_with_local_keystore_signer(
        click_runner, temp_dir_path, mocker, mock_testerchain,
        mock_account_password_keystore):
    custom_filepath = temp_dir_path
    custom_config_filepath = temp_dir_path / UrsulaConfiguration.generate_filename(
    )
    worker_account, password, mock_keystore_path = mock_account_password_keystore
    mock_signer_uri = f'keystore://{mock_keystore_path}'

    # Good signer...
    pre_config_signer = KeystoreSigner.from_signer_uri(uri=mock_signer_uri,
                                                       testnet=True)

    deploy_port = select_test_port()

    init_args = (
        'ursula',
        'init',

        # Layer 1
        '--network',
        TEMPORARY_DOMAIN,
        '--eth-provider',
        mock_testerchain.eth_provider_uri,

        # Layer 2
        '--payment-network',
        TEMPORARY_DOMAIN,
        '--payment-provider',
        mock_testerchain.eth_provider_uri,
        '--rest-host',
        MOCK_IP_ADDRESS,
        '--rest-port',
        deploy_port,
        '--operator-address',
        worker_account.address,
        '--config-root',
        str(custom_filepath.absolute()),

        # The bit we are testing here
        '--signer',
        mock_signer_uri)

    cli_env = {
        NUCYPHER_ENVVAR_KEYSTORE_PASSWORD: password,
        NUCYPHER_ENVVAR_OPERATOR_ETH_PASSWORD: password,
    }
    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 catch_exceptions=False,
                                 env=cli_env)
    assert result.exit_code == 0, result.stdout

    # Inspect the configuration file for the signer URI
    with open(custom_config_filepath, 'r') as config_file:
        raw_config_data = config_file.read()
        config_data = json.loads(raw_config_data)
        assert config_data['signer_uri'] == mock_signer_uri,\
            "Keystore URI was not correctly included in configuration file"

    # Recreate a configuration with the signer URI preserved
    ursula_config = UrsulaConfiguration.from_configuration_file(
        custom_config_filepath, config_root=custom_filepath)
    assert ursula_config.signer_uri == mock_signer_uri

    # Mock decryption of web3 client keystore
    mocker.patch.object(Account, 'decrypt', return_value=worker_account.key)
    ursula_config.keystore.unlock(password=password)

    # Produce an ursula with a Keystore signer correctly derived from the signer URI, and don't do anything else!
    ursula = ursula_config.produce()
    ursula.signer.unlock_account(account=worker_account.address,
                                 password=password)

    # Verify the keystore path is still preserved
    assert isinstance(ursula.signer, KeystoreSigner)
    assert isinstance(ursula.signer.path, Path), "Use pathlib.Path"
    assert ursula.signer.path.absolute() == mock_keystore_path.absolute()

    # Show that we can produce the exact same signer as pre-config...
    assert pre_config_signer.path == ursula.signer.path
    ursula.stop()
Esempio n. 5
0
def test_ursula_init_with_local_keystore_signer(click_runner, tmp_path, mocker,
                                                mock_testerchain,
                                                mock_account_password_keystore,
                                                test_registry_source_manager):
    custom_filepath = tmp_path
    custom_config_filepath = tmp_path / UrsulaConfiguration.generate_filename()
    worker_account, password, mock_keystore_path = mock_account_password_keystore
    mock_signer_uri = f'keystore:{mock_keystore_path}'

    # Good signer...
    pre_config_signer = KeystoreSigner.from_signer_uri(uri=mock_signer_uri)

    init_args = (
        'ursula',
        'init',
        '--network',
        TEMPORARY_DOMAIN,
        '--worker-address',
        worker_account.address,
        '--config-root',
        custom_filepath,
        '--provider',
        TEST_PROVIDER_URI,
        '--rest-host',
        MOCK_IP_ADDRESS,
        '--rest-port',
        MOCK_URSULA_STARTING_PORT,

        # The bit we are testing here
        '--signer',
        mock_signer_uri)

    cli_env = {
        NUCYPHER_ENVVAR_KEYRING_PASSWORD: password,
        NUCYPHER_ENVVAR_WORKER_ETH_PASSWORD: password,
    }
    result = click_runner.invoke(nucypher_cli,
                                 init_args,
                                 catch_exceptions=False,
                                 env=cli_env)
    assert result.exit_code == 0, result.stdout

    # Inspect the configuration file for the signer URI
    with open(custom_config_filepath, 'r') as config_file:
        raw_config_data = config_file.read()
        config_data = json.loads(raw_config_data)
        assert config_data['signer_uri'] == mock_signer_uri,\
            "Keystore URI was not correctly included in configuration file"

    # Recreate a configuration with the signer URI preserved
    ursula_config = UrsulaConfiguration.from_configuration_file(
        custom_config_filepath)
    assert ursula_config.signer_uri == mock_signer_uri

    # Mock decryption of web3 client keyring
    mocker.patch.object(Account,
                        'decrypt',
                        return_value=worker_account.privateKey)
    ursula_config.attach_keyring(checksum_address=worker_account.address)
    ursula_config.keyring.unlock(password=password)

    # Produce an ursula with a Keystore signer correctly derived from the signer URI, and dont do anything else!
    mocker.patch.object(StakeList, 'refresh', autospec=True)
    ursula = ursula_config.produce(client_password=password,
                                   block_until_ready=False)

    # Verify the keystore path is still preserved
    assert isinstance(ursula.signer, KeystoreSigner)
    assert isinstance(ursula.signer.path, str), "Use str"
    assert ursula.signer.path == str(mock_keystore_path)

    # Show that we can produce the exact same signer as pre-config...
    assert pre_config_signer.path == ursula.signer.path