SELECT
convert_from(x.leaf_blob , 'UTF8')::json -> 'public' ->> 'id' AS vault_id,
encode(decode(convert_from(x.leaf_blob , 'UTF8')::json -> 'internal' -> 'keyMaterial' ->> 'seedKeyHandle', 'base64'), 'hex') AS seed_key_handle,
encode(decode(convert_from(x.leaf_blob , 'UTF8')::json -> 'internal' -> 'keyMaterial' ->> 'vaultKeyHandle', 'base64'), 'hex') AS vault_key_handle
FROM
secure_key_value_store.node x
WHERE
collection_key = 'vaults'
AND is_leaf
Help me explain this query
Assistant
Certainly! Here’s a detailed explanation of what this SQL query is doing:
Query Breakdown:
sql
SELECT
convert_from(x.leaf_blob , 'UTF8')::json -> 'public' ->> 'id' AS vault_id,
encode(decode(convert_from(x.leaf_blob , 'UTF8')::json -> 'internal' -> 'keyMaterial' ->> 'seedKeyHandle', 'base64'), 'hex') AS seed_key_handle,
encode(decode(convert_from(x.leaf_blob , 'UTF8')::json -> 'internal' -> 'keyMaterial' ->> 'vaultKeyHandle', 'base64'), 'hex') AS vault_key_handle
FROM
secure_key_value_store.node x
WHERE
collection_key = 'vaults'
AND is_leaf
Explanation:
Table and Alias:
The data is being selected from the table secure_key_value_store.node, given the alias x.
Filtering (WHERE clause):
collection_key = 'vaults' — Only rows where the collection_key column equals the string 'vaults' are considered. This probably means these rows contain vault-related data.
is_leaf — This is assumed to be a boolean column; only rows where is_leaf is true are selected. This likely means these rows contain actual data (leaf nodes in a data tree or key-value store).
Transforming and extracting data in SELECT:
Each row contains a column leaf_blob, which presumably stores some serialized JSON data, but it's stored in binary or bytea format (or something similar). The query:
convert_from(x.leaf_blob , 'UTF8'):
Converts leaf_blob from its binary encoding into a UTF-8 string.
::json:
Casts the UTF-8 string into a JSON object so it can be navigated.
Extracting the vault id:
sql
convert_from(x.leaf_blob , 'UTF8')::json -> 'public' ->> 'id' AS vault_id
This extracts the JSON object's "public" key, which is another JSON object, then extracts the string value under the "id" key. This is selected as vault_id.
Extracts from the JSON the string value under: "internal" -> "keyMaterial" -> "seedKeyHandle".
This string value is base64-encoded.
decode(..., 'base64') converts the base64 string into binary data.
encode(..., 'hex') then converts the binary data into a hexadecimal string.
Result assigned as seed_key_handle.
Extracting and decoding vaultKeyHandle:
Similar to seed_key_handle, for:
"internal" -> "keyMaterial" -> "vaultKeyHandle"
This base64 string is decoded and re-encoded as hex, resulting in vault_key_handle.
Summary:
The query fetches rows representing vault data from the secure_key_value_store.node table. Each row stores a JSON blob (leaf_blob) representing vault information. The query:
Converts the blob to JSON,
Extracts the vault ID string from the "public" section,
Extracts two cryptographic handles (seedKeyHandle and vaultKeyHandle) under "internal" -> "keyMaterial", which are originally base64-encoded, decodes them, and converts them into hex-encoded strings for easier readability or processing.
Use case:
This query is useful when you want to get the vault's unique identifier along with its associated cryptographic handles in a human-readable hex format, by interpreting the JSON structure stored inside a binary column.
If you need any further explanation or details about specific parts, feel free to ask!