Chromia
Start Building

15 / 19Module 2 – DappAdvanced

Dapp queries overview

This guide walks through the queries in the zkp-demo workspace. The workspace shows how to build a private token transfer system on Chromia with zero-knowledge proofs.

Queries

The queries fall into the following groups:

Transaction queries

rell/src/zkp_demo/operations.rell
// Check nullifier statusquery is_nullifier_spent(nullifier_hash: big_integer) {    return spent_nullifier @? { .nullifier_hash == nullifier_hash } != null;}// Get unspent commitmentsquery get_all_unspent_commitments() {    return unspent_commitment @* {} (.commitment_hash);}// Get transfer eventsquery get_private_transfer_events() {    return private_transfer_event @* {} ($.to_struct());}// Get test assetquery get_test_asset() {    return init.test_asset.to_struct();}

Shield/unshield queries

rell/src/zkp_demo/operations.rell
// Get shield logsquery get_shield_logs(): list<struct<shield_log>> {    return shield_log @* {} ($.to_struct());}// Get unspent shield logs by accountquery get_all_unspent_shield_logs_by_account_id(account_id: byte_array): list<struct<shield_log>> {    return (uc: zkp_demo.unspent_commitment, sl: shield_log) @* {         uc.commitment_hash == sl.commitment,         sl.account_id == account_id     } (sl.to_struct());}// Get unshield logsquery get_unshield_logs(): list<struct<unshield_log>> {    return unshield_log @* {} ($.to_struct());}

Address registry queries

rell/src/zkp_demo/operations.rell
// Get private address by account IDquery get_private_address_by_account_id(account_id: byte_array): big_integer? {    return private_address_registry @? { account_id } ( .private_address );}// Get encryption key by account IDquery get_public_encryption_key_by_account_id(account_id: byte_array): text? {    return private_address_registry @? { account_id } ( .public_encryption_key );}// Get both private address and public encryption key by account IDquery get_user_keys_by_account_id(account_id: byte_array): (private_address: big_integer?, public_encryption_key: text?) {    val user = private_address_registry @? { account_id };    return (        private_address = user?.private_address,        public_encryption_key = user?.public_encryption_key    );}// Get all registered usersquery get_all_registered_users() {    return private_address_registry @* {} ( $.to_struct() );}// Get EVM wallet address from account IDquery get_user_wallet_address(account_id: byte_array): byte_array? {    val account = ft4.accounts.account @? { .id == account_id };    if (account == null) return null;        var evm_wallet_address: byte_array? = null;    val auth_descriptor_signers = (        mad: ft4.accounts.main_auth_descriptor,        ads: ft4.accounts.auth_descriptor_signer    ) @* {        account == mad.account,        ads.account_auth_descriptor == mad.auth_descriptor    } ( ads );    for (auth_descriptor_signer in auth_descriptor_signers) {        if (auth_descriptor_signer.id.size() == 20) {            evm_wallet_address = auth_descriptor_signer.id;            break;        }    }    return evm_wallet_address;}

Integration with FT4

The ZKP demo builds on Chromia's FT4 framework in three areas:

  • Account authentication: relies on the authentication system that FT4 provides
  • Asset management: handles public tokens through FT4's asset functionality
  • Balance operations: works with FT4's balance management
Written for this site from Chromia’s public course. Code samples are the course’s own.