def fund_eth_address(eth_network: Optional[str], eth_addr: Optional[str],
                     source_addr: Optional[str], amount: int) -> None:
    """
    Fund an address. If no source address is given, the first hosted account on
    the RPC host is used.
    """
    eth_addr = load_eth_address(eth_addr)
    network = get_eth_network(eth_network)
    web3 = open_web3_from_network(network)

    if not source_addr:
        # Use the first hosted address.
        source_addr = web3.eth.accounts[0]  # pylint: disable=no-member

        if network.name == "autonity-helloworld":
            # The Autonity helloworld network supplies hosted accounts, secured
            # with the password 'test'. Attempt to unlock it.
            # pylint: disable=import-outside-toplevel, no-member
            from web3.middleware import geth_poa_middleware  # type: ignore
            web3.middleware_stack.inject(geth_poa_middleware, layer=0)
            web3.personal.unlockAccount(source_addr, "test")

    print(f"eth_addr = {eth_addr}")
    print(f"source_addr = {source_addr}")
    print(f"amount = {amount}")

    web3.eth.sendTransaction({  # pylint: disable=no-member
        "from": source_addr,
        "to": eth_addr,
        "value": EtherValue(amount).wei
    })
Exemple #2
0
def token_approve(
        ctx: Context,
        value: str,
        eth_addr: str,
        eth_private_key: str,
        wait: bool,
        instance_file: str) -> None:
    """
    Approve the mixer to spend some amount of ERC20/223 tokens
    """
    approve_value = EtherValue(value)
    eth_addr = load_eth_address(eth_addr)
    eth_private_key_data = load_eth_private_key(eth_private_key)
    web3 = open_web3_from_network(get_eth_network(ctx.obj["eth_network"]))
    mixer_desc = load_mixer_description(instance_file)
    if not mixer_desc.token:
        raise ClickException("no token for mixer {mixer_desc.mixer.address}")

    token_instance = mixer_desc.token.instantiate(web3)
    approve_call = token_instance.functions.approve(
        mixer_desc.mixer.address,
        approve_value.wei)
    tx_hash = send_contract_call(
        web3, approve_call, eth_addr, eth_private_key_data)

    if wait:
        web3.eth.waitForTransactionReceipt(tx_hash)  # pylint: disable=no-member
    else:
        print(tx_hash.hex())
Exemple #3
0
def deploy(ctx: Context, verification_key_hash: str,
           dispatcher_instance_file: str, instance_file: str) -> None:
    """
    Deploy the contract for a dummy application.
    """

    eth_network = ctx.obj["eth_network"]

    # Load the dispatcher instance
    with open(dispatcher_instance_file, "r") as dispatcher_instance_f:
        dispatcher_desc = InstanceDescription.from_json_dict(
            json.load(dispatcher_instance_f))

    # Verification key hash as an array of evm words.
    verification_key_hash_evm = list(
        hex_to_uint256_list(verification_key_hash))
    print(f"verification_key_hash_evm = {verification_key_hash_evm}")

    web3 = open_web3_from_network(eth_network)
    eth_address = load_eth_address(ctx.obj["eth_addr"])
    eth_private_key_data = load_eth_private_key(ctx.obj["eth_private_key"])
    instance_desc = InstanceDescription.deploy(
        web3, DUMMY_APP_CONTRACT_FILE, "DummyApplication", eth_address,
        eth_private_key_data, DUMMY_APP_CONTRACT_DEPLOY_GAS,
        {"allow_paths": CONTRACTS_DIR},
        [dispatcher_desc.address, verification_key_hash_evm])

    with open(instance_file, "w") as instance_file_f:
        json.dump(instance_desc.to_json_dict(), instance_file_f)

    print(f"Instance file written to '{instance_file}'")
 def get_web3(self) -> Any:
     """
     Create and cache web3 connection.
     """
     if not self._web3:
         self._web3 = open_web3_from_network(
             get_eth_network(self.eth_network))
     return self._web3
Exemple #5
0
def eth_get_balance(ctx: Any, eth_addr: Optional[str], wei: bool) -> None:
    """
    Command to get the balance of specific addresses. Support multiple queries
    per invocation (outputs one per line), for efficiency.
    """
    eth_addr = load_eth_address(eth_addr)
    web3 = open_web3_from_network(get_eth_network(ctx.obj["eth_network"]))
    balance_wei = web3.eth.getBalance(eth_addr)  # pylint: disable=no-member
    if wei:
        print(balance_wei)
    else:
        print(EtherValue(balance_wei, "wei").ether())
Exemple #6
0
 def setUpClass() -> None:
     web3: Any = open_web3_from_network(get_eth_network(None))
     contracts_dir = get_contracts_dir()
     contract_instance_desc = InstanceDescription.deploy(
         web3,
         join(contracts_dir, "TestGroth16BLS12_377.sol"),
         "TestGroth16BLS12_377",
         web3.eth.accounts[0],  # pylint: disable=no-member
         None,
         500000,
         {"allow_paths": contracts_dir})
     global CONTRACT_INSTANCE  # pylint: disable=global-statement
     CONTRACT_INSTANCE = contract_instance_desc.instantiate(web3)
Exemple #7
0
def main() -> int:
    web3: Any = open_web3_from_network(get_eth_network(None))
    contracts_dir = get_contracts_dir()
    contract_instance_desc = InstanceDescription.deploy(
        web3,
        join(contracts_dir, "MiMC_test.sol"),
        "MiMC_test",
        web3.eth.accounts[0],  # pylint: disable=no-member
        None,
        500000,
        {"allow_paths": contracts_dir})
    contract_instance = contract_instance_desc.instantiate(web3)

    test_mimc7(contract_instance)
    test_mimc31(contract_instance)

    print("========================================")
    print("==              PASSED                ==")
    print("========================================")
    return 0
def deploy_test_token(eth_network: Optional[str], eth_addr: Optional[str],
                      eth_private_key: Optional[str], mint_amount: int,
                      recipient_address: str) -> None:
    """
    Deploy a simple ERC20 token for testing, and mint some for a specific
    address. Print the token address.
    """
    eth_addr = load_eth_address(eth_addr)
    eth_private_key_data = load_eth_private_key(eth_private_key)
    recipient_address = load_eth_address(recipient_address)
    web3 = open_web3_from_network(get_eth_network(eth_network))
    token_instance = deploy_token(
        web3, eth_addr, eth_private_key_data, 4000000) \
        # pylint: disable=no-member

    mint_tx_hash = mint_token(web3, token_instance, recipient_address,
                              eth_addr, eth_private_key_data,
                              EtherValue(mint_amount, 'ether'))
    web3.eth.waitForTransactionReceipt(mint_tx_hash)
    print(token_instance.address)
Exemple #9
0
def eth_send(
        ctx: Context,
        dest_addr: str,
        eth_private_key: Optional[str],
        eth_addr: Optional[str],
        amount: str) -> None:
    """
    Send Ether from the local eth-addr to a destination address.
    """
    dest_addr = load_eth_address(dest_addr)
    eth_private_key_data = load_eth_private_key(eth_private_key)
    eth_addr = load_eth_address(eth_addr)
    eth_network = get_eth_network(ctx.obj["eth_network"])
    web3 = open_web3_from_network(eth_network)

    if eth_private_key_data is None:
        raise ClickException("hosted accounts are not supported")

    print(f"eth_addr = {eth_addr}")
    print(f"dest_addr = {dest_addr}")
    print(f"amount = {amount}")

    # pylint: disable=no-member
    send_tx_desc = {
        "from": eth_addr,
        "to": dest_addr,
        "value": EtherValue(amount).wei,
        "gasPrice": web3.eth.gasPrice,
        "nonce": web3.eth.getTransactionCount(eth_addr)
    }
    send_tx_desc["gas"] = web3.eth.estimateGas(send_tx_desc)

    signed_tx = web3.eth.account.signTransaction(
        send_tx_desc, eth_private_key_data)

    tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    # pylint: enable=no-member

    print(tx_hash.hex())
Exemple #10
0
def get(ctx: Context, scalar: int, instance_file: str,
        check: Optional[int]) -> None:
    """
    Query the deployed contract to find the value stored for a given scalar.
    (These values are the parameters submitted along side the proof for the
    given scalar.)
    """

    eth_network = ctx.obj["eth_network"]

    # Load the contract instance
    with open(instance_file, "r") as instance_f:
        instance_desc = InstanceDescription.from_json_dict(
            json.load(instance_f))

    # Instantiate
    web3 = open_web3_from_network(eth_network)
    app_contract = instance_desc.instantiate(web3)
    result: int = app_contract.functions.get(scalar).call()
    print(f"{scalar}: {result}")

    if (check is not None) and (result != check):
        raise ClickException("state check failed")