Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
 def start(self):
     super(TestApp, self).start()
     log.debug('adding test accounts')
     # high balance account
     self.services.accounts.add_account(Account.new('', tester.keys[0]), store=False)
     # low balance account
     self.services.accounts.add_account(Account.new('', tester.keys[1]), store=False)
     # locked account
     locked_account = Account.new('', tester.keys[2])
     locked_account.lock()
     self.services.accounts.add_account(locked_account, store=False)
     assert set(acct.address for acct in self.services.accounts) == set(tester.accounts[:3])
Beispiel #5
0
 def start(self):
     super(TestApp, self).start()
     log.debug("adding test accounts")
     # high balance account
     self.services.accounts.add_account(Account.new("", tester.keys[0]), store=False)
     # low balance account
     self.services.accounts.add_account(Account.new("", tester.keys[1]), store=False)
     # locked account
     locked_account = Account.new("", tester.keys[2])
     locked_account.lock()
     self.services.accounts.add_account(locked_account, store=False)
     assert set(acct.address for acct in self.services.accounts) == set(tester.accounts[:3])
Beispiel #6
0
 def start(self):
     super(EdgeChainApp, self).start()
     log.debug('adding test accounts')
     self.services.accounts.add_account(Account.new('ADMIN',
                                                    tester.keys[0]),
                                        store=False)
     self.services.accounts.add_account(Account.new('MES', tester.keys[1]),
                                        store=False)
     self.services.accounts.add_account(Account.new('REQUESTER1',
                                                    tester.keys[2]),
                                        store=False)
     self.services.accounts.add_account(Account.new('REQUESTER2',
                                                    tester.keys[3]),
                                        store=False)
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
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
Beispiel #9
0
 def start(self):
     super(TestApp, self).start()
     log.debug('adding test accounts')
     # high balance account
     self.services.accounts.add_account(Account.new('', tester.keys[0]), store=False)
     # low balance account
     self.services.accounts.add_account(Account.new('', tester.keys[1]), store=False)
     # locked account
     locked_account = Account.new('', tester.keys[2])
     locked_account.lock()
     self.services.accounts.add_account(locked_account, store=False)
     self.privkey = None
     assert set(acct.address for acct in self.services.accounts) == set(tester.accounts[:3])
     test_transport = TestTransport(call_func=self.rpc_request)
     self.client = JSONRPCClient(transport=test_transport)
Beispiel #10
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
Beispiel #11
0
def call(keyfile, password):
    print("------------------------------------------------------------------")
    print("Trying {}".format(password))
    print("------------------------------------------------------------------")

    try:
        Account.load(keyfile, password=password)
    except ValueError as e:
        if str(e) == 'MAC mismatch. Password incorrect?':
            return False
        # else raise, as we got unexpected error
        raise e

    print("Found password: {}".format(password))
    return True
Beispiel #12
0
def rundummy(ctx, num_validators, node_num, seed):

    # reduce key derivation iterations
    PBKDF2_CONSTANTS['c'] = 100

    config = ctx.obj['config']

    # create bootstrap node priv_key and enode
    bootstrap_node_privkey = mk_privkey('%d:udp:%d' % (seed, 0))
    bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey)
    assert len(bootstrap_node_pubkey) == 64,  len(bootstrap_node_pubkey)
    base_port = 29870
    host = b'0.0.0.0'

    bootstrap_node = host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey)
    config['discovery']['bootstrap_nodes'] = [bootstrap_node]

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))

    # set ports based on node
    config['discovery']['listen_port'] = base_port + node_num
    config['p2p']['listen_port'] = base_port + node_num
    config['p2p']['min_peers'] = 2
    config['jsonrpc']['listen_port'] += node_num

    _start_app(account, config)
Beispiel #13
0
 def player_roll_dice(self, bet_size_ether, chances, wallet_path,
                      wallet_password):
     """
     Work in progress:
     https://github.com/AndreMiras/EtherollApp/issues/1
     """
     roll_under = chances
     value_wei = w3.toWei(bet_size_ether, 'ether')
     gas = 310000
     gas_price = w3.toWei(4, 'gwei')
     # since Account.load is hanging while decrypting the password
     # we set password to None and use `w3.eth.account.decrypt` instead
     account = Account.load(wallet_path, password=None)
     from_address_normalized = checksum_encode(account.address)
     nonce = self.web3.eth.getTransactionCount(from_address_normalized)
     transaction = {
         # 'chainId': ChainID.ROPSTEN.value,
         'chainId': int(self.web3.net.version),
         'gas': gas,
         'gasPrice': gas_price,
         'nonce': nonce,
         'value': value_wei,
     }
     transaction = self.contract.functions.playerRollDice(
         roll_under).buildTransaction(transaction)
     encrypted_key = open(wallet_path).read()
     private_key = w3.eth.account.decrypt(encrypted_key, wallet_password)
     signed_tx = self.web3.eth.account.signTransaction(
         transaction, private_key)
     tx_hash = self.web3.eth.sendRawTransaction(signed_tx.rawTransaction)
     print("tx_hash:", tx_hash.hex())
     return tx_hash
Beispiel #14
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
Beispiel #15
0
 def create_account_helper(self, password):
     # reduces key derivation iterations to speed up creation
     PBKDF2_CONSTANTS['c'] = 1
     wallet_path = os.path.join(self.keystore_dir, 'wallet.json')
     account = Account.new(password, path=wallet_path)
     with open(account.path, 'w') as f:
         f.write(account.dump())
     return account
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 private_to_account(ctx, privatekey, password):
    # privatekey is provided in the console, so it's expected to be hexadecimal
    privatekey = safe_address_decode(privatekey)

    # cast the values to bytes because it is the expected type in the Crypto library
    password = bytes(password)
    privkey = bytes(privatekey)

    account = Account.new(password, key=privkey)
    print account.dump()
def test_store_private(app, account, password):
    s = app.services.accounts
    account.path = os.path.join(app.config["accounts"]["keystore_dir"], "account1")
    s.add_account(account, include_id=False, include_address=False)
    account_reloaded = Account.load(account.path)
    assert account_reloaded.address is None
    assert account_reloaded.uuid is None
    account_reloaded.unlock(password)
    assert account_reloaded.address == account.address
    assert account_reloaded.uuid is None
    account.path = None
def test_store_absolute(app, account):
    s = app.services.accounts
    tmpdir = tempfile.mkdtemp()
    account.path = os.path.join(tmpdir, "account1")
    assert os.path.isabs(account.path)
    s.add_account(account)
    assert os.path.exists(account.path)
    account_reloaded = Account.load(account.path)
    assert account_reloaded.address == account.address
    shutil.rmtree(tmpdir)
    account.path = None
def test_store_absolute(app, account):
    s = app.services.accounts
    tmpdir = tempfile.mkdtemp()
    account.path = os.path.join(tmpdir, 'account1')
    assert os.path.isabs(account.path)
    s.add_account(account)
    assert os.path.exists(account.path)
    account_reloaded = Account.load(account.path)
    assert account_reloaded.address == account.address
    shutil.rmtree(tmpdir)
    account.path = None
Beispiel #21
0
 def test_pyethapp(self):
     from pyethapp.accounts import Account
     from ethereum_utils import AccountUtils
     AccountUtils.patch_ethereum_tools_keys()
     password = "******"
     uuid = None
     account = Account.new(password, uuid=uuid)
     # restore iterations
     address = account.address.hex()
     self.assertIsNotNone(account)
     self.assertIsNotNone(address)
def test_store_private(app, account, password):
    s = app.services.accounts
    account.path = os.path.join(app.config['accounts']['keystore_dir'], 'account1')
    s.add_account(account, include_id=False, include_address=False)
    account_reloaded = Account.load(account.path)
    assert account_reloaded.address is None
    assert account_reloaded.uuid is None
    account_reloaded.unlock(password)
    assert account_reloaded.address == account.address
    assert account_reloaded.uuid is None
    account.path = None
 def new_account(self, password):
     """
     Creates an account on the disk and returns it.
     """
     # lazy loading
     from pyethapp.accounts import Account
     account = Account.new(password, uuid=None)
     account.path = os.path.join(
         self.app.services.accounts.keystore_dir,
         account.address.hex())
     self.app.services.accounts.add_account(account)
     return account
Beispiel #24
0
def start_app(config, accounts):

    # create app
    app = HPCApp(config)

    # development mode
    if False:
        gevent.get_hub().SYSTEM_ERROR = BaseException

    if config['test_privkeys']:
        # init accounts first, as we need (and set by copy) the coinbase early FIXME
        genesis_config = dict(alloc=dict())
        for privkey in config['test_privkeys']:
            assert len(privkey) == 32
            address = privtoaddr(privkey)
            account = Account.new(password='', key=privkey)
            accounts.append(account)
            # add to genesis alloc
            genesis_config['alloc'][address] = {'wei': config['test_privkeys_endowment']}

        if config['test_privkeys'] and config['eth'].get('genesis_hash'):
            del config['eth']['genesis_hash']

        konfig.update_config_from_genesis_json(config, genesis_config)

    # dump config
    pyethapp_app.dump_config(config)

    if AccountsService in services:
        AccountsService.register_with_app(app)

    # add account
    for account in accounts:
        app.services.accounts.add_account(account, store=False)

    if config['hdc']['validators']:
        assert app.services.accounts.coinbase in config['hdc']['validators']

    # register services
    for service in services:
        assert issubclass(service, BaseService)
        if service.name not in app.config['deactivated_services'] + [AccountsService.name]:
            assert service.name not in app.services
            service.register_with_app(app)
            assert hasattr(app.services, service.name)

    # start app
    log.info('starting')
    app.start()
    for cb in config['post_app_start_callbacks']:
        cb(app)
    return app
Beispiel #25
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(app, account):
    s = app.services.accounts
    account.path = os.path.join(app.config['accounts']['keystore_dir'], 'account1')
    s.add_account(account, include_id=True, include_address=True)
    assert os.path.exists(account.path)
    account_reloaded = Account.load(account.path)
    assert account_reloaded.uuid is not None
    assert account_reloaded.address is not None
    assert account_reloaded.uuid == account.uuid
    assert account_reloaded.address == account.address
    assert account_reloaded.privkey is None
    assert account_reloaded.path == account.path
    assert account.privkey is not None
    account.path = None
def test_store(app, account):
    s = app.services.accounts
    account.path = os.path.join(app.config["accounts"]["keystore_dir"], "account1")
    s.add_account(account, include_id=True, include_address=True)
    assert os.path.exists(account.path)
    account_reloaded = Account.load(account.path)
    assert account_reloaded.uuid is not None
    assert account_reloaded.address is not None
    assert account_reloaded.uuid == account.uuid
    assert account_reloaded.address == account.address
    assert account_reloaded.privkey is None
    assert account_reloaded.path == account.path
    assert account.privkey is not None
    account.path = None
Beispiel #28
0
 def test_pyethapp(self):
     from pyethapp.accounts import Account
     from ethereum.tools.keys import PBKDF2_CONSTANTS
     # backup iterations
     iterations_backup = PBKDF2_CONSTANTS['c']
     # speeds up the test
     PBKDF2_CONSTANTS['c'] = 100
     password = "******"
     uuid = None
     account = Account.new(password, uuid=uuid)
     # restore iterations
     PBKDF2_CONSTANTS['c'] = iterations_backup
     address = account.address.encode('hex')
     self.assertIsNotNone(account)
     self.assertIsNotNone(address)
Beispiel #29
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
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
Beispiel #31
0
def _configure_node_network(config, num_validators, node_num, seed):
    assert node_num < num_validators

    # reduce key derivation iterations
    PBKDF2_CONSTANTS["c"] = 100

    # create this node priv_key
    config["node"]["privkey_hex"] = mk_privkey("%d:udp:%d" % (seed, node_num)).encode("hex")

    # create validator addresses
    validators = [privtoaddr(mk_privkey("%d:account:%d" % (seed, i))) for i in range(num_validators)]
    config["hdc"]["validators"] = validators

    # create this node account
    account = Account.new(password="", key=mk_privkey("%d:account:%d" % (seed, node_num)))
    assert account.address in validators
    return config, account
Beispiel #32
0
 def new_account_helper(password, security_ratio=None):
     """
     Helper method for creating an account in memory.
     Returns the created account.
     security_ratio is a ratio of the default PBKDF2 iterations.
     Ranging from 1 to 100 means 100% of the iterations.
     """
     # TODO: perform validation on security_ratio (within allowed range)
     if security_ratio:
         default_iterations = PBKDF2_CONSTANTS["c"]
         new_iterations = int((default_iterations * security_ratio) / 100)
         PBKDF2_CONSTANTS["c"] = new_iterations
     uuid = None
     account = Account.new(password, uuid=uuid)
     # reverts to previous iterations
     if security_ratio:
         PBKDF2_CONSTANTS["c"] = default_iterations
     return account
Beispiel #33
0
def _configure_node_network(config, num_validators, node_num, seed):
    assert node_num < num_validators

    # reduce key derivation iterations
    PBKDF2_CONSTANTS['c'] = 100

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))
    assert account.address in validators
    return config, account
Beispiel #34
0
    def __init__(self, privkey, validators, simenv=None):
        self.config = copy.deepcopy(hdc_service.ChainService.default_config)
        self.config['db'] = dict(path='_db')
        self.config['data_dir'] = tempfile.mkdtemp()
        self.config['hdc']['validators'] = validators

        self.simenv = simenv
        self.services = self.Services()
        self.services.db = EphemDB()
        self.services.accounts = AccountsService(self)
        self.services.peermanager = PeerManagerMock(self)
        account = Account.new(password='', key=privkey)
        self.services.accounts.add_account(account, store=False)
        if simenv:
            self.services.chainservice = SimChainService(self, simenv=simenv)
        else:
            self.services.chainservice = hdc_service.ChainService(self)
        self.isactive = True
Beispiel #35
0
def _configure_node_network(config, num_validators, node_num, seed):
    assert node_num < num_validators

    # reduce key derivation iterations
    PBKDF2_CONSTANTS['c'] = 100

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))
    assert account.address in validators
    return config, account
Beispiel #36
0
    def __init__(self, privkey, validators, simenv=None):
        self.config = copy.deepcopy(hdc_service.ChainService.default_config)
        self.config["db"] = dict(path="_db")
        self.config["data_dir"] = tempfile.mkdtemp()
        self.config["hdc"]["validators"] = validators

        initial_alloc = dict((a, dict(wei=2 ** 200)) for a in validators)
        self.config["eth"]["block"]["GENESIS_INITIAL_ALLOC"] = initial_alloc

        self.simenv = simenv
        self.services = self.Services()
        self.services.db = EphemDB()
        self.services.accounts = AccountsService(self)
        self.services.peermanager = PeerManagerMock(self)
        account = Account.new(password="", key=privkey)
        self.services.accounts.add_account(account, store=False)
        if simenv:
            self.services.chainservice = SimChainService(self, simenv=simenv)
        else:
            self.services.chainservice = hdc_service.ChainService(self)
        self.isactive = True
Beispiel #37
0
    def __init__(self, privkey, validators, simenv=None):
        self.config = copy.deepcopy(hdc_service.ChainService.default_config)
        self.config['db'] = dict(path='_db')
        self.config['data_dir'] = tempfile.mkdtemp()
        self.config['hdc']['validators'] = validators

        initial_alloc = dict((a, dict(wei=2 ** 200)) for a in validators)
        self.config['eth']['block']['GENESIS_INITIAL_ALLOC'] = initial_alloc

        self.simenv = simenv
        self.services = self.Services()
        self.services.db = EphemDB()
        self.services.accounts = AccountsService(self)
        self.services.peermanager = PeerManagerMock(self)
        account = Account.new(password='', key=privkey)
        self.services.accounts.add_account(account, store=False)
        if simenv:
            self.services.chainservice = SimChainService(self, simenv=simenv)
        else:
            self.services.chainservice = hdc_service.ChainService(self)
        self.isactive = True
Beispiel #38
0
 def player_roll_dice(self,
                      bet_size_ether,
                      chances,
                      wallet_path,
                      wallet_password,
                      gas_price_gwei=DEFAULT_GAS_PRICE_GWEI):
     """
     Signs and broadcasts `playerRollDice` transaction.
     Returns transaction hash.
     """
     roll_under = chances
     # `w3.toWei` one has some issues on Android, see:
     # https://github.com/AndreMiras/EtherollApp/issues/77
     # value_wei = w3.toWei(bet_size_ether, 'ether')
     value_wei = int(bet_size_ether * 1e18)
     gas = 310000
     gas_price = w3.toWei(gas_price_gwei, 'gwei')
     # since Account.load is hanging while decrypting the password
     # we set password to None and use `w3.eth.account.decrypt` instead
     account = Account.load(wallet_path, password=None)
     from_address_normalized = checksum_encode(account.address)
     nonce = self.web3.eth.getTransactionCount(from_address_normalized)
     transaction = {
         'chainId': self.chain_id.value,
         'gas': gas,
         'gasPrice': gas_price,
         'nonce': nonce,
         'value': value_wei,
     }
     transaction = self.contract.functions.playerRollDice(
         roll_under).buildTransaction(transaction)
     private_key = AccountUtils.get_private_key(wallet_path,
                                                wallet_password)
     signed_tx = self.web3.eth.account.signTransaction(
         transaction, private_key)
     tx_hash = self.web3.eth.sendRawTransaction(signed_tx.rawTransaction)
     return tx_hash
Beispiel #39
0
def runlocal(ctx, num_validators, node_num, seed):

    assert node_num < num_validators

    # reduce key derivation iterations
    PBKDF2_CONSTANTS['c'] = 100

    config = ctx.obj['config']

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))
    assert account.address in validators

    config['p2p']['min_peers'] = 2

    _start_app(account, config, validators)
Beispiel #40
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
def account(privkey, password, uuid):
    return Account.new(password, privkey, uuid)
Beispiel #42
0
 def __init__(self, privkey):
     self.services = self.Services()
     self.services.db = EphemDB()
     self.services.accounts = AccountsService(self)
     account = Account.new(password='', key=privkey)
     self.services.accounts.add_account(account, store=False)
 def __init__(self, privkey):
     self.services = self.Services()
     self.services.db = EphemDB()
     self.services.accounts = AccountsService(self)
     account = Account.new(password='', key=privkey)
     self.services.accounts.add_account(account, store=False)
Beispiel #44
0
def create_hydrachain_cluster(private_keys, hydrachain_private_keys, p2p_base_port, base_datadir):
    """ Initializes a hydrachain network used for testing. """
    # pylint: disable=too-many-locals
    from hydrachain.app import services, start_app, HPCApp
    import pyethapp.config as konfig

    def privkey_to_uri(private_key):
        host = b'0.0.0.0'
        pubkey = privtopub(private_key)
        return host_port_pubkey_to_uri(host, p2p_base_port, pubkey)

    account_addresses = [
        privtoaddr(priv)
        for priv in private_keys
    ]

    alloc = {
        encode_hex(address): {
            'balance': DEFAULT_BALANCE,
        }
        for address in account_addresses
    }

    genesis = {
        'nonce': '0x00006d6f7264656e',
        'difficulty': '0x20000',
        'mixhash': '0x00000000000000000000000000000000000000647572616c65787365646c6578',
        'coinbase': '0x0000000000000000000000000000000000000000',
        'timestamp': '0x00',
        'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000',
        'extraData': '0x',
        'gasLimit': GAS_LIMIT_HEX,
        'alloc': alloc,
    }

    bootstrap_nodes = [
        privkey_to_uri(hydrachain_private_keys[0]),
    ]

    validators_addresses = [
        privtoaddr(private_key)
        for private_key in hydrachain_private_keys
    ]

    all_apps = []
    for number, private_key in enumerate(hydrachain_private_keys):
        config = konfig.get_default_config(services + [HPCApp])
        config = update_config_from_genesis_json(config, genesis)

        datadir = os.path.join(base_datadir, str(number))
        konfig.setup_data_dir(datadir)

        account = Account.new(
            password='',
            key=private_key,
        )

        config['data_dir'] = datadir
        config['hdc']['validators'] = validators_addresses
        config['node']['privkey_hex'] = encode_hex(private_key)
        config['jsonrpc']['listen_port'] += number
        config['client_version_string'] = 'NODE{}'.format(number)

        # setting to 0 so that the CALLCODE opcode works at the start of the
        # network
        config['eth']['block']['HOMESTEAD_FORK_BLKNUM'] = 0

        config['discovery']['bootstrap_nodes'] = bootstrap_nodes
        config['discovery']['listen_port'] = p2p_base_port + number

        config['p2p']['listen_port'] = p2p_base_port + number
        config['p2p']['min_peers'] = min(10, len(hydrachain_private_keys) - 1)
        config['p2p']['max_peers'] = len(hydrachain_private_keys) * 2

        # only one of the nodes should have the Console service running
        if number != 0 and Console.name not in config['deactivated_services']:
            config['deactivated_services'].append(Console.name)

        hydrachain_app = start_app(config, accounts=[account])
        all_apps.append(hydrachain_app)

    hydrachain_wait(private_keys, len(hydrachain_private_keys) - 1)

    return all_apps
Beispiel #45
0
def account_file():
    account = Account.new('', key="1" * 64)
    print account.dump()
def account(privkey, password, uuid):
    return Account.new(password, privkey, uuid)
Beispiel #47
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
Beispiel #48
0
def hydrachain_create_blockchain(private_keys, hydrachain_private_keys,
                                 p2p_base_port, base_datadir):
    """ Initializes a hydrachain network used for testing. """
    # pylint: disable=too-many-locals
    from hydrachain.app import services, start_app, HPCApp
    import pyethapp.config as konfig

    def privkey_to_uri(private_key):
        host = b'0.0.0.0'
        pubkey = privtopub(private_key)
        return host_port_pubkey_to_uri(host, p2p_base_port, pubkey)

    account_addresses = [
        privatekey_to_address(priv)
        for priv in private_keys
    ]

    alloc = {
        encode_hex(address): {
            'balance': DEFAULT_BALANCE_BIN,
        }
        for address in account_addresses
    }

    genesis = {
        'nonce': '0x00006d6f7264656e',
        'difficulty': '0x20000',
        'mixhash': '0x00000000000000000000000000000000000000647572616c65787365646c6578',
        'coinbase': '0x0000000000000000000000000000000000000000',
        'timestamp': '0x00',
        'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000',
        'extraData': '0x',
        'gasLimit': GAS_LIMIT_HEX,
        'alloc': alloc,
    }

    bootstrap_nodes = [
        privkey_to_uri(hydrachain_private_keys[0]),
    ]

    validators_addresses = [
        privatekey_to_address(private_key)
        for private_key in hydrachain_private_keys
    ]

    all_apps = []
    for number, private_key in enumerate(hydrachain_private_keys):
        config = konfig.get_default_config(services + [HPCApp])
        config = update_config_from_genesis_json(config, genesis)

        datadir = os.path.join(base_datadir, str(number))
        konfig.setup_data_dir(datadir)

        account = Account.new(
            password='',
            key=private_key,
        )

        config['data_dir'] = datadir
        config['hdc']['validators'] = validators_addresses
        config['node']['privkey_hex'] = encode_hex(private_key)
        config['jsonrpc']['listen_port'] += number
        config['client_version_string'] = 'NODE{}'.format(number)

        # setting to 0 so that the CALLCODE opcode works at the start of the
        # network
        config['eth']['block']['HOMESTEAD_FORK_BLKNUM'] = 0

        config['discovery']['bootstrap_nodes'] = bootstrap_nodes
        config['discovery']['listen_port'] = p2p_base_port + number

        config['p2p']['listen_port'] = p2p_base_port + number
        config['p2p']['min_peers'] = min(10, len(hydrachain_private_keys) - 1)
        config['p2p']['max_peers'] = len(hydrachain_private_keys) * 2

        # only one of the nodes should have the Console service running
        if number != 0 and Console.name not in config['deactivated_services']:
            config['deactivated_services'].append(Console.name)

        hydrachain_app = start_app(config, accounts=[account])
        all_apps.append(hydrachain_app)

    hydrachain_wait(private_keys, len(hydrachain_private_keys) - 1)

    return all_apps
Beispiel #49
0
def hydrachain_network(private_keys, base_port, base_datadir):
    """ Initializes a hydrachain network used for testing. """
    # pylint: disable=too-many-locals
    from hydrachain.app import services, start_app, HPCApp
    import pyethapp.config as konfig

    gevent.get_hub().SYSTEM_ERROR = BaseException
    PBKDF2_CONSTANTS['c'] = 100
    quantity = len(private_keys)

    def privkey_to_uri(private_key):
        host = b'0.0.0.0'
        pubkey = privtopub(private_key)
        return host_port_pubkey_to_uri(host, base_port, pubkey)

    addresses = [
        privtoaddr(priv)
        for priv in private_keys
    ]

    bootstrap_nodes = [
        privkey_to_uri(private_keys[0]),
    ]

    validator_keys = [
        mk_privkey('raidenvalidator:{}'.format(position))
        for position in range(quantity)
    ]

    validator_addresses = [
        privtoaddr(validator_keys[position])
        for position in range(quantity)
    ]

    alloc = {
        addr.encode('hex'): {
            'balance': '1606938044258990275541962092341162602522202993782792835301376',
        }
        for addr in addresses
    }

    genesis = {
        'nonce': '0x00006d6f7264656e',
        'difficulty': '0x20000',
        'mixhash': '0x00000000000000000000000000000000000000647572616c65787365646c6578',
        'coinbase': '0x0000000000000000000000000000000000000000',
        'timestamp': '0x00',
        'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000',
        'extraData': '0x',
        'gasLimit': '0x5FEFD8',
        'alloc': alloc,
    }

    all_apps = []
    for number in range(quantity):
        port = base_port + number

        config = konfig.get_default_config(services + [HPCApp])

        # del config['eth']['genesis_hash']
        config = update_config_from_genesis_json(config, genesis)

        datadir = os.path.join(base_datadir, str(number))
        konfig.setup_data_dir(datadir)

        account = Account.new(
            password='',
            key=validator_keys[number],
        )

        config['data_dir'] = datadir
        config['node']['privkey_hex'] = private_keys[number].encode('hex')
        config['hdc']['validators'] = validator_addresses
        config['jsonrpc']['listen_port'] += number
        config['client_version_string'] = 'NODE{}'.format(number)

        # setting to 0 so that the CALLCODE opcode works at the start of the
        # network
        config['eth']['block']['HOMESTEAD_FORK_BLKNUM'] = 0

        config['discovery']['bootstrap_nodes'] = bootstrap_nodes
        config['discovery']['listen_port'] = port

        config['p2p']['listen_port'] = port
        config['p2p']['min_peers'] = min(10, quantity - 1)
        config['p2p']['max_peers'] = quantity * 2

        # only one of the nodes should have the Console service running
        if number != 0:
            config['deactivated_services'].append(Console.name)

        hydrachain_app = start_app(config, accounts=[account])
        all_apps.append(hydrachain_app)

    return all_apps