Exemplo n.º 1
0
def test_create_new_account_with_text_password(tmpdir):
    data_dir = str(tmpdir.mkdir("data-dir"))

    assert not get_accounts(data_dir)

    account_0 = create_new_account(data_dir, b'some-text-password')
    account_1 = create_new_account(data_dir, b'some-text-password')

    accounts = get_accounts(data_dir)
    assert (account_0, account_1) == accounts
def local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()
    chain = project.get_chain('local')

    # create a new account
    create_new_account(chain.geth.data_dir, b'a-test-password')

    return chain
def test_create_new_account_with_text_password(tmpdir):
    data_dir = str(tmpdir.mkdir("data-dir"))

    assert not get_accounts(data_dir)

    account_0 = create_new_account(data_dir, b'some-text-password')
    account_1 = create_new_account(data_dir, b'some-text-password')

    accounts = get_accounts(data_dir)
    assert sorted((account_0, account_1)) == sorted(tuple(set(accounts)))
Exemplo n.º 4
0
def local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()
    chain = project.get_chain('local')

    # create a new account
    create_new_account(chain.geth.data_dir, b'a-test-password')

    return chain
def test_create_new_account_with_file_based_password(tmpdir):
    pw_file_path = str(tmpdir.mkdir("data-dir").join('geth_password_file'))

    with open(pw_file_path, 'w') as pw_file:
        pw_file.write("some-text-password-in-a-file")

    data_dir = os.path.dirname(pw_file_path)

    assert not get_accounts(data_dir)

    account_0 = create_new_account(data_dir, pw_file_path)
    account_1 = create_new_account(data_dir, pw_file_path)

    accounts = get_accounts(data_dir)
    assert sorted((account_0, account_1)) == sorted(tuple(set(accounts)))
Exemplo n.º 6
0
def test_create_new_account_with_file_based_password(tmpdir):
    pw_file_path = str(tmpdir.mkdir("data-dir").join('geth_password_file'))

    with open(pw_file_path, 'w') as pw_file:
        pw_file.write("some-text-password-in-a-file")

    data_dir = os.path.dirname(pw_file_path)

    assert not get_accounts(data_dir)

    account_0 = create_new_account(data_dir, pw_file_path)
    account_1 = create_new_account(data_dir, pw_file_path)

    accounts = get_accounts(data_dir)
    assert (account_0, account_1) == accounts
Exemplo n.º 7
0
 def ensure_account_exists(self, password: str) -> str:
     accounts = get_accounts(**self.geth_kwargs)
     if not accounts:
         account = create_new_account(password=password.encode(), **self.geth_kwargs)
     else:
         account = accounts[0]  # etherbase by default
     checksum_address = to_checksum_address(account.decode())
     return checksum_address
Exemplo n.º 8
0
def test_select_account_helper_with_accounts(project_dir, account_index):
    project = Project()
    chain = project.get_chain('temp')

    # create 3 new accounts
    create_new_account(chain.geth.data_dir, b'a-test-password')


    @click.command()
    def wrapper():
        account = select_account(chain)
        print("~~{0}~~".format(account))

    with chain:
        account = chain.web3.eth.accounts[account_index]

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="{0}\n".format(account))

    assert result.exit_code == 0
    expected = "~~{0}~~".format(account)
    assert expected in result.output
Exemplo n.º 9
0
def new_local_chain(project_dir, chain_name):
    chains_path = os.path.join(project_dir, 'chains')
    ensure_path_exists(chains_path)
    chain_dir = os.path.join(chains_path, chain_name)
    data_dir = os.path.join(chain_dir, 'chain_data')
    overrides = {
        'data_dir': data_dir,
        'ws_port': '8546',
        'rpc_port': '8545',
        'port': '30303'
    }
    geth_kwargs = construct_test_chain_kwargs(**overrides)
    password = geth_kwargs.pop('password')
    data_dir = geth_kwargs.pop('data_dir')
    account = create_new_account(data_dir, password, **geth_kwargs)
    account = account.decode('ascii')

    genesis = GENESIS_BLOCK % (
        account,
        account,
    )
    genesis_path = os.path.join(chain_dir, 'genesis.json')
    with open(genesis_path, 'w+') as f:
        f.write(genesis)

    password_path = os.path.join(chain_dir, 'password')
    shutil.copyfile(password, password_path)

    ipc_path = geth_kwargs['ipc_path']
    init = INIT_COMMAND.format(data_dir=data_dir,
                               ipc_path=ipc_path,
                               genesis_path=genesis_path)
    init_geth_path = os.path.join(chain_dir, 'init_chain.sh')
    with open(init_geth_path, 'w+') as f:
        f.write(SHEBANG)
        f.write('\n')
        f.write(init)
    chmod_plus_x(init_geth_path)

    run = RUN_COMMAND.format(data_dir=data_dir,
                             ipc_path=ipc_path,
                             password_path=password_path,
                             account=account)
    run_geth_path = os.path.join(chain_dir, 'run_chain.sh')
    with open(run_geth_path, 'w+') as f:
        f.write(SHEBANG)
        f.write('\n')
        f.write(run)
    chmod_plus_x(run_geth_path)
Exemplo n.º 10
0
def test_with_locked_account(project):
    temp_chain = project.get_chain('temp')

    account = force_text(
        create_new_account(temp_chain.geth.data_dir, b'a-test-password'))

    with temp_chain:
        web3 = temp_chain.web3

        assert account in web3.eth.accounts
        assert is_account_locked(web3, account) is True

        assert web3.personal.unlockAccount(account, 'a-test-password')

        assert is_account_locked(web3, account) is False
Exemplo n.º 11
0
def test_with_locked_account(project):
    temp_chain = project.get_chain('temp')

    account = force_text(
        create_new_account(temp_chain.geth.data_dir, b'a-test-password')
    )

    with temp_chain:
        web3 = temp_chain.web3

        assert account in web3.eth.accounts
        assert is_account_locked(web3, account) is True

        assert web3.personal.unlockAccount(account, 'a-test-password')

        assert is_account_locked(web3, account) is False
Exemplo n.º 12
0
def target_account(web3: Web3) -> str:
    """Create a new Ethereum account on a running Geth node.

    The account can be used as a withdrawal target for tests.

    :return: 0x address of the account
    """

    # We store keystore files in the current working directory
    # of the test run
    data_dir = os.getcwd()

    # Use the default password "this-is-not-a-secure-password"
    # as supplied in geth/default_blockchain_password file.
    # The supplied password must be bytes, not string,
    # as we only want ASCII characters and do not want to
    # deal encoding problems with passwords
    account = create_new_account(data_dir, DEFAULT_PASSWORD_PATH)
    return account
Exemplo n.º 13
0
def target_account(web3: Web3) -> str:
    """Create a new Ethereum account on a running Geth node.

    The account can be used as a withdrawal target for tests.

    :return: 0x address of the account
    """

    # We store keystore files in the current working directory
    # of the test run
    data_dir = os.getcwd()

    # Use the default password "this-is-not-a-secure-password"
    # as supplied in geth/default_blockchain_password file.
    # The supplied password must be bytes, not string,
    # as we only want ASCII characters and do not want to
    # deal encoding problems with passwords
    account = create_new_account(data_dir, DEFAULT_PASSWORD_PATH)
    return account
def test_chain_web3_is_preconfigured_with_default_from(project_dir,
                                                       write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()

    chain = project.get_chain('local')

    default_account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password'))

    project.config.set('chain:local', 'default_account', default_account)
    project.write_config()

    with chain:
        web3 = chain.web3

        assert len(web3.eth.accounts) == 2
        assert web3.eth.defaultAccount == default_account
        assert web3.eth.coinbase != default_account
Exemplo n.º 15
0
def test_request_account_unlock_with_bad_password(project_dir):
    project = Project()
    chain = project.get_chain('temp')

    # create 3 new accounts
    account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password'))

    @click.command()
    def wrapper():
        request_account_unlock(chain, account, None)

    with chain:
        assert is_account_locked(chain.web3, account)

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="bad-password\n")

        assert result.exit_code != 0
        assert is_account_locked(chain.web3, account)
def test_request_account_unlock_with_bad_password(project_dir):
    project = Project()
    chain = project.get_chain('temp')

    # create 3 new accounts
    account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password')
    )

    @click.command()
    def wrapper():
        request_account_unlock(chain, account, None)

    with chain:
        assert is_account_locked(chain.web3, account)

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="bad-password\n")

        assert result.exit_code != 0
        assert is_account_locked(chain.web3, account)
def test_chain_web3_is_preconfigured_with_default_from(project_dir,
                                                       write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()

    chain = project.get_chain('local')

    default_account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password')
    )

    project.config.set('chain:local', 'default_account', default_account)
    project.write_config()

    with chain:
        web3 = chain.web3

        assert len(web3.eth.accounts) == 2
        assert web3.eth.defaultAccount == default_account
        assert web3.eth.coinbase != default_account
Exemplo n.º 18
0
def test_initializing_local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', "[chain:local_a]")
    project = Project()

    runner = CliRunner()

    chain = project.get_chain('local_a')

    deploy_from = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password')
    )

    with chain:
        chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30)
        funding_txn_hash = chain.web3.eth.sendTransaction({
            'from': chain.web3.eth.coinbase,
            'to': deploy_from,
            'value': int(chain.web3.toWei(10, 'ether')),
        })
        chain.wait.for_receipt(funding_txn_hash, timeout=60)

    result = runner.invoke(
        main,
        ['chain', 'init'],
        input=((
            "local_a\n"          # choose chain.
            "{0}\n"              # pick deploy account.
            "Y\n"                # set account as default
            "a-test-password\n"  # unlock account
            "".format(deploy_from)
        ))
    )

    assert result.exit_code == 0, result.output + str(result.exception)

    updated_project = Project()
    assert 'registrar' in updated_project.config.chains['local_a']
    assert 'deploy_from' in updated_project.config.chains['local_a']
Exemplo n.º 19
0
def test_initializing_local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', "[chain:local_a]")
    project = Project()

    runner = CliRunner()

    chain = project.get_chain('local_a')

    deploy_from = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password'))

    with chain:
        chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30)
        funding_txn_hash = chain.web3.eth.sendTransaction({
            'from':
            chain.web3.eth.coinbase,
            'to':
            deploy_from,
            'value':
            int(chain.web3.toWei(10, 'ether')),
        })
        chain.wait.for_receipt(funding_txn_hash, timeout=60)

    result = runner.invoke(
        main,
        ['chain', 'init'],
        input=((
            "local_a\n"  # choose chain.
            "{0}\n"  # pick deploy account.
            "Y\n"  # set account as default
            "a-test-password\n"  # unlock account
            "".format(deploy_from))))

    assert result.exit_code == 0, result.output + str(result.exception)

    updated_project = Project()
    assert 'registrar' in updated_project.config.chains['local_a']
    assert 'deploy_from' in updated_project.config.chains['local_a']
Exemplo n.º 20
0
def test_select_account_helper_with_accounts(project_dir, account_index):
    project = Project()
    chain = project.get_chain('temp')

    # create 3 new accounts
    create_new_account(chain.geth.data_dir, b'a-test-password')
    create_new_account(chain.geth.data_dir, b'a-test-password')
    create_new_account(chain.geth.data_dir, b'a-test-password')


    @click.command()
    def wrapper():
        account = select_account(chain)
        print("~~{0}~~".format(account))

    with chain:
        account = chain.web3.eth.accounts[account_index]

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="{0}\n".format(account))

    assert result.exit_code == 0
    expected = "~~{0}~~".format(account)
    assert expected in result.output
Exemplo n.º 21
0
def second_account(local_chain):
    # create a new account
    account = create_new_account(local_chain.geth.data_dir, b'a-test-password')
    return force_text(account)
Exemplo n.º 22
0
for i in range(number):

    #name and password
    name = "Hospital" + str(i + 1)
    account_file_path = "./blockchains/" + name
    password = "******"

    #create password file
    password_file_path = "./blockchains/password.txt"
    file = open(password_file_path, "w")
    file.write(password)
    file.close()

    #create account
    account = create_new_account(account_file_path,
                                 "./blockchains/password.txt")
    account = str(account)[4:44]
    print("Created account:", account)
    accounts.append(account)

#save addresses to a file
file = open("./blockchains/account_addresses.txt", "w")
writeString = ""
for i in accounts:
    writeString += i + "\n"
file.write(writeString)
file.close()

#make a dictionary for alloc portion of genesis.json
account_balance = {}
for i in accounts: