예제 #1
0
def test_locked(keystore, uuid):
    account = Account(keystore)
    assert account.locked
    assert account.address.encode('hex') == keystore['address']
    assert account.privkey is None
    assert account.pubkey is None
    assert account.uuid == uuid
    keystore2 = keystore.copy()
    keystore2.pop('address')
    account = Account(keystore2)
    assert account.locked
    assert account.address is None
    assert account.privkey is None
    assert account.pubkey is None
    assert account.uuid == uuid
예제 #2
0
    def get_privkey(self, address, password=None):
        """Find the keystore file for an account, unlock it and get the private key

        :param str address: The Ethereum address for which to find the keyfile in the system
        :param str password: Mostly for testing purposes. A password can be provided
                             as the function argument here. If it's not then the
                             user is interactively queried for one.
        :return str: The private key associated with the address
        """

        if address.startswith('0x'):
            address = address[2:]

        address = address.lower()

        if not self.address_in_keystore(address):
            raise ValueError("Keystore file not found for %s" % address)

        with open(self.accounts[address]) as data_file:
            data = json.load(data_file)

        # Since file was found prompt for a password if not already given
        if password is None:
            password = getpass.getpass("Enter the password to unlock %s: " % address)
        acc = Account(data, password, self.accounts[address])
        return acc.privkey
예제 #3
0
def test_unlock(keystore, password, privkey, uuid):
    account = Account(keystore)
    assert account.locked
    account.unlock(password)
    assert not account.locked
    assert account.privkey == privkey
    assert account.address == privtoaddr(privkey)
예제 #4
0
def test_address(keystore, password, privkey):
    keystore_wo_address = keystore.copy()
    keystore_wo_address.pop('address')
    account = Account(keystore_wo_address)
    assert account.address is None
    account.unlock(password)
    account.lock()
    assert account.address == privtoaddr(privkey)
def test_store_dir(app, account):
    s = app.services.accounts
    uuid = account.uuid
    account.uuid = None
    paths = [os.path.join(app.config['accounts']['keystore_dir'], p) for p in [
        'some/sub/dir/account1',
        'some/sub/dir/account2',
        'account1',
    ]]

    for path in paths:
        new_account = Account(account.keystore, path=path)
        s.add_account(new_account)
    for path in paths:
        new_account = Account(account.keystore, path=path)
        with pytest.raises(IOError):
            s.add_account(new_account)

    account.uuid = uuid
def test_store_overwrite(app, account):
    s = app.services.accounts
    uuid = account.uuid
    account.uuid = None
    account.path = os.path.join(app.config['accounts']['keystore_dir'], 'account1')
    account2 = Account(account.keystore)
    account2.path = os.path.join(app.config['accounts']['keystore_dir'], 'account2')

    s.add_account(account, store=True)
    with pytest.raises(IOError):
        s.add_account(account, store=True)
    s.add_account(account2, store=True)
    account.uuid = uuid
    account.path = None
예제 #7
0
def test_account_sorting(app):
    keystore_dummy = {}
    paths = [
        '/absolute/path/b', '/absolute/path/c', '/absolute/path/letter/e',
        '/absolute/path/letter/d', '/letter/f', '/absolute/path/a', None
    ]
    paths_sorted = sorted(paths)

    s = app.services.accounts
    for path in paths:
        s.add_account(Account(keystore_dummy, path=path), store=False)

    assert [account.path for account in s.accounts] == paths_sorted
    assert [s.find(str(i)).path
            for i in xrange(1,
                            len(paths) + 1)] == paths_sorted
예제 #8
0
def test_account_sorting(app):
    keystore_dummy = {}
    paths = [
        '/absolute/path/b', '/absolute/path/c', '/absolute/path/letter/e',
        '/absolute/path/letter/d', '/letter/f', '/absolute/path/a', None
    ]

    # paths_sortable = [(x or "") for x in paths]
    min_value = MinType()
    paths_sorted = sorted(paths, key=lambda x: min_value if x is None else x)
    # paths_sorted = sorted(paths, key=lambda x: (x is None, x))
    s = app.services.accounts
    for path in paths:
        s.add_account(Account(keystore_dummy, path=path), store=False)

    assert [account.path for account in s.accounts] == paths_sorted
예제 #9
0
def test_unlock_wrong(keystore, password, privkey, uuid):
    account = Account(keystore)
    assert account.locked
    with pytest.raises(ValueError):
        account.unlock(password + '1234')
    assert account.locked
    with pytest.raises(ValueError):
        account.unlock('4321' + password)
    assert account.locked
    with pytest.raises(ValueError):
        account.unlock(password[:len(password) / 2])
    assert account.locked
    account.unlock(password)
    assert not account.locked
    account.unlock(password + 'asdf')
    assert not account.locked
    account.unlock(password + '1234')
    assert not account.locked