1+ DROP VIEW IF EXISTS key_group_info_view;
2+ DROP VIEW IF EXISTS genesis_info_view;
3+
4+ -- This view is used to fetch the base asset information from disk based on
5+ -- the raw key of the batch that will ultimately create this set of assets.
6+ -- To do so, we'll need to traverse a few tables to join the set of assets
7+ -- with the genesis points, then with the batches that reference this
8+ -- points, to the internal key that reference the batch, then restricted
9+ -- for internal keys that match our main batch key.
10+ CREATE VIEW genesis_info_view AS
11+ SELECT
12+ gen_asset_id, asset_id, asset_tag, assets_meta .meta_data_hash meta_hash,
13+ output_index, asset_type, genesis_points .prev_out prev_out,
14+ chain_txns .txid anchor_txid, block_height
15+ FROM genesis_assets
16+ -- We do a LEFT JOIN here, as not every asset has a set of
17+ -- metadata that matches the asset.
18+ LEFT JOIN assets_meta
19+ ON genesis_assets .meta_data_id = assets_meta .meta_id
20+ JOIN genesis_points
21+ ON genesis_assets .genesis_point_id = genesis_points .genesis_id
22+ LEFT JOIN chain_txns
23+ ON genesis_points .anchor_tx_id = chain_txns .txn_id ;
24+
25+ -- This view is used to perform a series of joins that allow us to extract
26+ -- the group key information, as well as the group sigs for the series of
27+ -- assets we care about. We obtain only the assets found in the batch
28+ -- above, with the WHERE query at the bottom.
29+ CREATE VIEW key_group_info_view AS
30+ SELECT
31+ witness_id, gen_asset_id, witness_stack, tapscript_root,
32+ tweaked_group_key, raw_key, key_index, key_family,
33+ substr(tweaked_group_key, 2 ) AS x_only_group_key
34+ FROM asset_group_witnesses wit
35+ JOIN asset_groups groups
36+ ON wit .group_key_id = groups .group_id
37+ JOIN internal_keys keys
38+ ON keys .key_id = groups .internal_key_id
39+ WHERE wit .gen_asset_id IN (SELECT gen_asset_id FROM genesis_info_view);
0 commit comments