Find a key in the tree
Public entry point for searching the tree. Returns comprehensive information about the key including whether it exists, its value, and the Merkle path siblings required for proof generation.
env - The Soroban environmentkey - Key to search for in the treeReturns Ok(FindResult) containing whether the key was found, siblings
along the path, and collision information for non-membership proofs,
or an error if database operations fail.
Error::KeyNotFound - Database operations failed or invalid node
structurefn find_key(
env: soroban_sdk::Env,
key: soroban_sdk::U256,
) -> Result
Get the current root of the tree
Returns the root hash of the Sparse Merkle tree. Returns zero if the tree is empty or hasn't been initialized yet.
env - The Soroban environmentReturns the current root hash as a U256 value, or zero if empty
fn get_root(env: soroban_sdk::Env) -> Result
Delete a key from the tree
Removes a leaf from the Sparse Merkle tree, handling both sparse branches (single child) and mixed branches (two populated children). When a leaf is deleted, its sibling may be promoted to replace the parent node, collapsing the tree structure. Requires admin authorization.
env - The Soroban environmentkey - Key to delete from the treeReturns Ok(()) on success, emitting a LeafDeletedEvent with the new
root.
Error::KeyNotFound - Key does not exist in the tree or database
operations failedfn delete_leaf(
env: soroban_sdk::Env,
key: soroban_sdk::U256,
) -> Result<(), soroban_sdk::Error>
Insert a new key-value pair into the tree
Adds a new leaf to the Sparse Merkle tree, building any missing intermediate nodes. Handles collision cases where a new key shares a path prefix with an existing leaf by extending the tree depth. Requires admin authorization.
env - The Soroban environmentkey - Key to insertvalue - Value to associate with the keyReturns Ok(()) on success, emitting a LeafInsertedEvent with the new
root.
Error::KeyAlreadyExists - Key already exists in the treeError::KeyNotFound - Database operations failedfn insert_leaf(
env: soroban_sdk::Env,
key: soroban_sdk::U256,
value: soroban_sdk::U256,
) -> Result<(), soroban_sdk::Error>
Update the admin address
Transfers administrative control to a new address. Requires authorization from the current admin.
env - The Soroban environmentnew_admin - New address that will have permission to modify the treefn update_admin(env: soroban_sdk::Env, new_admin: soroban_sdk::Address)
Constructor: initialize the contract with an admin address and an empty tree
Sets up the contract with the specified admin and initializes an empty Sparse Merkle Tree with root = 0. This function can only be called once.
env - The Soroban environmentadmin - Address that will have permission to modify the treeReturns Ok(()) on success
fn __constructor(
env: soroban_sdk::Env,
admin: soroban_sdk::Address,
) -> Result<(), soroban_sdk::Error>
Verify non-membership proof for a key
Verifies that a key is NOT in the tree by checking the provided Merkle proof. The proof includes siblings along the path and collision information (not_found_key/value at the leaf). Reconstructs the root from the proof and compares it with the stored root.
env - The Soroban environmentkey - Key to verify is not in the treesiblings - Sibling hashes along the path from root to leafnot_found_key - Key at the collision point (or queried key if empty
path)not_found_value - Value at the collision point (or zero if empty
path)Returns Ok(true) if non-membership is verified, Ok(false) if the key
actually exists in the tree.
fn verify_non_membership(
env: soroban_sdk::Env,
key: soroban_sdk::U256,
siblings: soroban_sdk::Vec,
not_found_key: soroban_sdk::U256,
not_found_value: soroban_sdk::U256,
) -> Result
fn approve(
env: soroban_sdk::Env,
from: soroban_sdk::Address,
spender: soroban_sdk::Address,
amount: i128,
)
fn balance(env: soroban_sdk::Env, id: soroban_sdk::Address) -> i128
fn transfer(
env: soroban_sdk::Env,
from: soroban_sdk::Address,
to: soroban_sdk::Address,
amount: i128,
)
fn allowance(
env: soroban_sdk::Env,
from: soroban_sdk::Address,
spender: soroban_sdk::Address,
) -> i128
fn transfer_from(
env: soroban_sdk::Env,
from: soroban_sdk::Address,
to: soroban_sdk::Address,
amount: i128,
)