Ejemplo n.º 1
0
 def from_json(json_str: str) -> MixerDescription:
     json_dict = json.loads(json_str)
     mixer = InstanceDescription.from_json_dict(json_dict["mixer"])
     token_dict = json_dict.get("token", None)
     token = InstanceDescription.from_json_dict(token_dict) \
         if token_dict else None
     return MixerDescription(mixer, token)
Ejemplo n.º 2
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}'")
Ejemplo n.º 3
0
    def from_json_dict(json_dict: Dict[str, Any]) -> MixerDescription:
        zeth_mixer = json_dict["zeth_mixer"]

        mixer = InstanceDescription.from_json_dict(json_dict)
        token_dict = cast(Optional[Dict[str, Any]],
                          zeth_mixer.get("token", None))
        token = InstanceDescription.from_json_dict(token_dict) \
            if token_dict else None
        permitted_dispatcher = \
            cast(Optional[str], zeth_mixer.get("permitted_dispatcher", None))
        vk_hash = cast(Optional[str], zeth_mixer.get("vk_hash", None))
        return MixerDescription(mixer, token, permitted_dispatcher, vk_hash)
Ejemplo n.º 4
0
 def get_dispatcher_contract(self) -> Any:
     """
     Load (and cache) the dispatcher contract instance.
     """
     if not self._dispatcher_contract:
         with open(self.instance_file, "r") as instance_f:
             instance_dict = json.load(instance_f)
             instance = InstanceDescription.from_json_dict(instance_dict)
         self._dispatcher_contract = DispatcherContract(
             self.get_web3(), instance, self.get_wrapper_snark())
     return self._dispatcher_contract
Ejemplo n.º 5
0
def load_contract_address(contract_addr: str) -> str:
    """
    Parse a string as either an eth address, or a contract instance file.
    """
    if contract_addr.startswith("0x"):
        return Web3.toChecksumAddress(contract_addr)
    if exists(contract_addr):
        with open(contract_addr, "r") as instance_f:
            instance = InstanceDescription.from_json_dict(
                json.load(instance_f))
        return Web3.toChecksumAddress(instance.address)
    raise ClickException(
        f"failed to parse as address or instance file: {contract_addr}")
Ejemplo n.º 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)
Ejemplo n.º 7
0
 def deploy(
     web3: Any, zksnark: IZKSnarkProvider, pp: PairingParameters,
     vk: IVerificationKey, eth_addr: str, eth_private_key: Optional[bytes]
 ) -> Tuple[DispatcherContract, InstanceDescription]:
     """
     Deploy the contract, returning an instance of this wrapper, and a
     description (which can be saved to a file to later instantiate).
     """
     vk_evm = zksnark.verification_key_to_contract_parameters(vk, pp)
     instance_desc = InstanceDescription.deploy(
         web3, DISPATCHER_SOURCE_FILE, "ZecaleDispatcher", eth_addr,
         eth_private_key, DISPATCHER_DEPLOY_GAS,
         {"allow_paths": CONTRACTS_DIR}, [vk_evm])
     return DispatcherContract(web3, instance_desc, zksnark), instance_desc
Ejemplo n.º 8
0
def submit_batch(
        ctx: Context,
        batch_file: str,
        application_instance_file: str,
        wait: bool) -> None:
    """
    Submit an aggregated transaction ("batch") to a zecale dispatcher contract
    instance.
    """

    cmd_ctx: CommandContext = ctx.obj
    aggregator_config = cmd_ctx.get_aggregator_configuration()
    wrapper_snark = aggregator_config.wrapper_snark
    pp = aggregator_config.wrapper_pairing_parameters

    # Load the batch
    aggregated_tx = load_aggregated_transaction(wrapper_snark, batch_file)

    # Load the application instance address
    with open(application_instance_file, "r") as app_instance_f:
        app_instance = InstanceDescription.from_json_dict(
            json.load(app_instance_f))

    # Open the dispatcher client and submit the batch to it
    eth_addr, eth_private_key = cmd_ctx.get_eth_key_and_address()
    dispatcher_contract = cmd_ctx.get_dispatcher_contract()
    tx_id = dispatcher_contract.process_batch(
        pp, aggregated_tx, app_instance.address, eth_addr, eth_private_key)
    print(tx_id.hex())

    if wait:
        tx_receipt = cmd_ctx.get_web3().eth.waitForTransactionReceipt(
            tx_id, 10000)
        gas_used = tx_receipt.gasUsed
        status = tx_receipt.status
        print(f"(gasUsed={gas_used}, status={status})")
        if status != 1:
            raise ClickException("transaction failed")

        # This is kept for convenience during contract development. Can be
        # removed once the contract code is stable.
        dispatcher_contract.dump_logs(tx_receipt)
Ejemplo n.º 9
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
Ejemplo n.º 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")
Ejemplo n.º 11
0
 def __init__(self, web3: Any, instance_desc: InstanceDescription,
              zksnark: IZKSnarkProvider):
     self.web3 = web3
     self.instance = instance_desc.instantiate(web3)
     self.zksnark = zksnark
Ejemplo n.º 12
0
def get_erc20_instance_description(token_address: str) -> InstanceDescription:
    return InstanceDescription(token_address, get_erc20_abi())