Example #1
0
    async def ecRecover(self, message: str, signature: bytes):

        w3 = Web3()
        signable_message = encode_defunct(text=message)
        checksum_address = w3.hls.account.recover_message(signable_message,
                                                          signature=signature)
        return checksum_address
Example #2
0
def console(ipc_path: Path,
            use_ipython: bool=True,
            env: Dict[str, Any]=None,
            banner: str=DEFAULT_BANNER) -> Any:
    """
    Method that starts the chain, setups the helios CLI and register the
    cleanup function.
    """
    if env is None:
        env = {}

    # if ipc_path is not found, raise an exception with a useful message
    if not ipc_path.exists():
        raise FileNotFoundError(create_missing_ipc_error_message(ipc_path))

    # wait to import web3, because it's somewhat large, and not usually used
    from helios_web3 import HeliosWeb3 as Web3
    from web3 import IPCProvider
    #w3 = web3.Web3(web3.IPCProvider(ipc_path))
    w3 = Web3(IPCProvider(ipc_path))

    namespace = merge({'w3': w3}, env)

    if use_ipython:
        ipython_shell(namespace, banner)()
    else:
        python_shell(namespace, banner)()
Example #3
0
def _test_get_accounts_with_receivable_transactions():
    w3 = Web3(IPCProvider(ipc_path))
    INSTANCE_0 = get_primary_node_private_helios_key(0)

    new_account_address_hex_1 = w3.personal.newAccount(test_password)
    new_account_address_hex_2 = w3.personal.newAccount(test_password)

    tx = [{
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex_1,
        'value': 1
    }, {
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex_1,
        'value': 1
    }, {
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex_2,
        'value': 1
    }]

    print("Sending transactions")
    w3.personal.sendTransactions(tx, 'dev')

    time.sleep(0.5)

    print("Getting accounts over all time")
    accounts_with_receivable = w3.personal.getAccountsWithReceivableTransactions(
    )
    assert (new_account_address_hex_1 in accounts_with_receivable
            and new_account_address_hex_2 in accounts_with_receivable)

    print("Getting accounts over short time")
    accounts_with_receivable = w3.personal.getAccountsWithReceivableTransactions(
        int(time.time()) - 10)
    assert (new_account_address_hex_1 in accounts_with_receivable
            and new_account_address_hex_2 in accounts_with_receivable)

    print("Getting accounts over future time")
    accounts_with_receivable = w3.personal.getAccountsWithReceivableTransactions(
        int(time.time()) + 1)
    assert (accounts_with_receivable == [])

    print("Receiving transactions")
    w3.personal.receiveTransactions(new_account_address_hex_1, test_password)
    w3.personal.receiveTransactions(new_account_address_hex_2, test_password)

    time.sleep(0.5)

    print("Getting accounts over all time")
    accounts_with_receivable = w3.personal.getAccountsWithReceivableTransactions(
    )
    assert (new_account_address_hex_1 not in accounts_with_receivable
            and new_account_address_hex_2 not in accounts_with_receivable)

    print("Getting accounts over short time")
    accounts_with_receivable = w3.personal.getAccountsWithReceivableTransactions(
        int(time.time()) - 10)
    assert (new_account_address_hex_1 not in accounts_with_receivable
            and new_account_address_hex_2 not in accounts_with_receivable)
Example #4
0
    def _save_account(self, account, password):
        w3 = Web3()
        new_account_json_encrypted = w3.hls.account.encrypt(account.privateKey, password)
        keyfile_name = "HLS_account_{}".format(account.address)
        keyfile_path = self.rpc_context.keystore_dir / keyfile_name

        f = open(str(keyfile_path), "w")
        f.write(json.dumps(new_account_json_encrypted))
        f.close()
Example #5
0
    async def _unlock_account(self, wallet_address: bytes, password: str):
        normalized_wallet_address = to_normalized_address(wallet_address)
        print("Unlocking account {}".format(normalized_wallet_address))
        w3 = Web3()
        keystore = self._get_keystore_for_address(normalized_wallet_address)

        private_key = w3.hls.account.decrypt(keystore, password)
        account = w3.hls.account.privateKeyToAccount(private_key)
        return account
Example #6
0
    async def sign(self, message: str, wallet_address: bytes, password: str = None):
        # using EIP 191 https://github.com/ethereum/eth-account/blob/master/eth_account/messages.py
        normalized_wallet_address = to_normalized_address(wallet_address)
        account = self._get_unlocked_account_or_unlock_now(wallet_address, password)

        signable_message = encode_defunct(text = message)
        w3 = Web3()

        signed_message = w3.hls.account.sign_message(signable_message, account.privateKey)
        return signed_message['signature'].hex()
Example #7
0
def _test_signing_messages():
    w3 = Web3(IPCProvider(ipc_path))
    INSTANCE_0 = get_primary_node_private_helios_key(0)
    message = 'my_cool_message'
    signature = w3.personal.sign(message,
                                 INSTANCE_0.public_key.to_canonical_address(),
                                 'dev')

    recovered_address = w3.personal.ecRecover(message, signature)

    assert (recovered_address == INSTANCE_0.public_key.to_checksum_address())
Example #8
0
def _test_send_receive_transactions():
    w3 = Web3(IPCProvider(ipc_path))
    INSTANCE_0 = get_primary_node_private_helios_key(0)

    new_account_address_hex_1 = w3.personal.newAccount(test_password)
    new_account_address_hex_2 = w3.personal.newAccount(test_password)

    tx = [{
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex_1,
        'value': 1
    }, {
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex_1,
        'value': 1
    }, {
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex_2,
        'value': 1
    }]

    print("Sending transaction")
    tx_hashes = w3.personal.sendTransactions(tx, 'dev')

    #
    # Make sure the hashes exists
    #
    time.sleep(0.5)
    # If it doesnt exist, it will throw an exception
    print("Checking transaction exists")
    tx_from_node = w3.hls.getTransaction(tx_hashes[0])
    tx_from_node = w3.hls.getTransaction(tx_hashes[1])

    #
    # Receive the transaction
    #
    print("Receiving transactions")
    tx_hashes_1 = w3.personal.receiveTransactions(new_account_address_hex_1,
                                                  test_password)
    tx_hashes_2 = w3.personal.receiveTransactions(new_account_address_hex_2,
                                                  test_password)

    #
    # Make sure the hash exists
    #
    time.sleep(0.5)
    # If it doesnt exist, it will throw an exception
    print("Checking transactions exists")
    tx_from_node = w3.hls.getTransaction(tx_hashes_1[0])
    tx_from_node = w3.hls.getTransaction(tx_hashes_1[1])
    tx_from_node = w3.hls.getTransaction(tx_hashes_2[0])
Example #9
0
    def _save_account(self, account, password):
        if not self._account_address_cache_ready.is_set():
            raise BaseRPCError("Account cache is still building. Please wait and try again in a moment.")

        w3 = Web3()
        new_account_json_encrypted = w3.hls.account.encrypt(account.privateKey, password)
        keyfile_name = "HLS_account_{}".format(account.address)
        keyfile_path = self._rpc_context.keystore_dir / keyfile_name

        f = open(str(keyfile_path), "w")
        f.write(json.dumps(new_account_json_encrypted))
        f.close()

        self._account_address_cache.add(account.address)
def _test_DOS_with_many_transactions_on_rpc():

    # w3 = web3.Web3(web3.IPCProvider(ipc_path))
    w3 = Web3(IPCProvider(ipc_path))
    w3_instance_1 = Web3(IPCProvider(ipc_path))

    start_time = time.time()
    num_transactions = 0

    while True:
        for instance_num in range(10):
            to_account = w3.hls.account.create()
            print("Sending 100 tx from instance {}".format(instance_num))
            txs = []
            for i in range(100):
                txs.append({
                    'to': to_account.address,
                    'value': 1,
                })

                num_transactions += 1

            signed_block, header_dict, transactions = prepare_and_sign_block(
                w3, txs, get_primary_node_private_helios_key(instance_num))

            response = w3.hls.sendRawBlock(signed_block['rawBlock'])
            time.sleep(1.1)

        tx_per_second = (num_transactions / (time.time() - start_time))
        print("Transactions per second = {}".format(tx_per_second))
        gas_price_instance_0 = w3.hls.gasPrice
        print('gas_price_instance_0 {}'.format(gas_price_instance_0))

        gas_price_instance_1 = w3_instance_1.hls.gasPrice
        print('gas_price_instance_1 {}'.format(gas_price_instance_1))

        time.sleep(4)
Example #11
0
def _test_key_management():
    w3 = Web3(IPCProvider(ipc_path))
    #
    # Import a raw key
    #
    print("Creating new account")
    new_account = w3.hls.account.create()
    private_key = new_account.key
    w3.personal.importRawKey(private_key, test_password)

    #
    # List the account
    #
    print("Listing accounts")
    list_of_accounts = w3.personal.listAccounts()
    assert (new_account.address in list_of_accounts)

    #
    # Load the account
    #
    print("Unlocking account with incorrect password")
    with pytest.raises(ValueError):
        w3.personal.unlockAccount(new_account.address, 'incorrect')

    print("Unlocking account with correct password")
    w3.personal.unlockAccount(new_account.address, test_password)

    #
    # New Account
    #
    new_account_address_hex = w3.personal.newAccount(test_password)

    #
    # List the account
    #
    print("Listing accounts")
    list_of_accounts = w3.personal.listAccounts()
    assert (new_account_address_hex in list_of_accounts)

    print("Unlocking account with incorrect password")
    with pytest.raises(ValueError):
        w3.personal.unlockAccount(new_account_address_hex, 'incorrect')

    print("Unlocking account with correct password")
    w3.personal.unlockAccount(new_account_address_hex, test_password)
Example #12
0
def _test_simple_responses():
    w3 = Web3(IPCProvider(ipc_path))

    ping = w3.hls.ping
    assert (ping == True)

    block_number = w3.hls.blockNumber(
        instance(1).public_key.to_checksum_address())
    assert (block_number == 0)

    gas_price = w3.hls.gasPrice
    assert (gas_price == 1)

    gas_price = w3.hls.getGasPrice()
    assert (gas_price == 1)

    protocol_version = w3.hls.protocolVersion
    assert (protocol_version == '63')

    syncing = w3.hls.syncing
    assert (syncing == False)
Example #13
0
    async def newAccount(self, password: str):
        w3 = Web3()
        new_account = w3.hls.account.create()
        self._save_account(new_account, password)

        return to_checksum_address(new_account.address)
Example #14
0
    async def importRawKey(self, keydata: bytes, password: str):
        w3 = Web3()
        new_account = w3.hls.account.privateKeyToAccount(keydata)
        self._save_account(new_account, password)

        return to_checksum_address(new_account.address)
Example #15
0
def _test_send_receive_transaction_delayed_unlock():
    w3 = Web3(IPCProvider(ipc_path))
    INSTANCE_0 = get_primary_node_private_helios_key(0)

    new_account_address_hex = w3.personal.newAccount(test_password)

    tx = {
        'from': INSTANCE_0.public_key.to_checksum_address(),
        'to': new_account_address_hex,
        'value': 1
    }

    print("Unlocking account")
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 5)
    print("Sending transaction")
    tx_hash = w3.personal.sendTransaction(tx)

    #
    # Make sure the hash exists
    #
    time.sleep(0.5)
    # If it doesnt exist, it will throw an exception
    print("Checking transaction exists")
    tx_from_node = w3.hls.getTransaction(tx_hash)

    #
    # Unlock account for time that is too short
    #
    print("Unlocking account")
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 1)
    time.sleep(1)
    print("Sending transaction")
    with pytest.raises(ValueError):
        w3.personal.sendTransaction(tx)

    #
    # Unlock twice, and make sure it replaces the old time with the new one
    #

    print("Unlocking account")
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 300)
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 1)
    time.sleep(1)
    print("Sending transaction")
    with pytest.raises(ValueError):
        w3.personal.sendTransaction(tx)

    #
    # Unlock twice, and make sure it replaces the old time with the new one
    #

    print("Unlocking account")
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 2)
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 300)
    time.sleep(3)
    print("Sending transaction")
    tx_hash = w3.personal.sendTransaction(tx)

    #
    # Make sure the hash exists
    #
    time.sleep(0.5)
    # If it doesnt exist, it will throw an exception
    print("Checking transaction exists")
    tx_from_node = w3.hls.getTransaction(tx_hash)

    #
    # Unlock the account, then lock it before trying to send. Make sure it locked
    #

    print("Unlocking account")
    w3.personal.unlockAccount(INSTANCE_0.public_key.to_checksum_address(),
                              'dev', 300)
    w3.personal.lockAccount(INSTANCE_0.public_key.to_checksum_address())
    print("Sending transaction")
    with pytest.raises(ValueError):
        w3.personal.sendTransaction(tx)