spark_rust/wallet/internal_handlers/traits/
create_tree.rs

1// std
2// use std::cell::RefCell;
3
4use std::sync::Arc;
5
6// crates
7use crate::error::SparkSdkError;
8use crate::signer::traits::SparkSigner;
9
10// crates::common_types
11
12use crate::common_types::types::Transaction;
13use crate::common_types::types::TxOut;
14use crate::common_types::types::Txid;
15
16use parking_lot::RwLock;
17// external spark crates
18
19use spark_protos::spark::AddressNode as AddressNodeProto;
20use spark_protos::spark::AddressRequestNode as AddressRequestNodeProto;
21use spark_protos::spark::CreationNode;
22use spark_protos::spark::FinalizeNodeSignaturesResponse;
23use spark_protos::spark::TreeNode as TreeNodeProto;
24use tonic::async_trait;
25
26/// A node in the deposit address tree.
27#[derive(Debug, Clone)]
28pub struct DepositAddressTree {
29    /// The address of the node
30    pub address: Option<String>,
31
32    /// The public key of the node
33    pub verification_key: Option<Vec<u8>>,
34
35    /// The signing public key of the node
36    pub signing_public_key: Vec<u8>,
37
38    /// The children of the node
39    pub children: Vec<Arc<RwLock<DepositAddressTree>>>, // RwLock is fine here since this tree is cannot be manipulated by other threads during the creation. Leaves are written atomically in the end of the flow.
40}
41
42#[allow(dead_code)]
43pub(crate) struct CreateDepositAddressBinaryTreeSdkResponse {
44    pub(crate) tree: Vec<Arc<RwLock<DepositAddressTree>>>,
45}
46
47#[allow(dead_code)]
48pub(crate) struct CreateAddressRequestNodeFromTreeNodesSdkResponse {
49    pub(crate) address_request_nodes: Vec<AddressRequestNodeProto>,
50}
51
52#[allow(dead_code)]
53pub(crate) struct GenerateDepositAddressForTreeSdkResponse {
54    pub(crate) tree: Arc<RwLock<DepositAddressTree>>,
55}
56
57#[allow(dead_code)]
58pub(crate) struct BuildCreationNodesFromTreeSdkResponse {
59    pub(crate) creation_nodes: CreationNode, // stands for the root creation node, where it already has children in the children field
60}
61
62#[allow(dead_code)]
63pub(crate) struct FinalizeTreeCreationSdkResponse {
64    pub(crate) finalize_tree_response: FinalizeNodeSignaturesResponse,
65    #[allow(dead_code)]
66    pub(crate) signing_public_keys: Vec<Vec<u8>>,
67}
68
69#[async_trait]
70#[allow(dead_code)]
71pub(crate) trait CreateTreeInternalHandlers<S: SparkSigner + Send + Sync> {
72    fn create_deposit_address_binary_tree(
73        &self,
74        split_level: u32,
75        target_pubkey: &Vec<u8>,
76    ) -> Result<CreateDepositAddressBinaryTreeSdkResponse, SparkSdkError>;
77
78    fn create_address_request_node_from_tree_nodes(
79        &self,
80        tree_nodes: &Vec<Arc<RwLock<DepositAddressTree>>>,
81    ) -> Result<CreateAddressRequestNodeFromTreeNodesSdkResponse, SparkSdkError>;
82
83    fn apply_address_nodes_to_tree(
84        &self,
85        tree: &mut Vec<Arc<RwLock<DepositAddressTree>>>,
86        address_nodes: Vec<AddressNodeProto>,
87    ) -> Result<(), SparkSdkError>;
88
89    async fn generate_deposit_address_for_tree(
90        &self,
91        parent_tx: Option<Transaction>,
92        parent_node: Option<Arc<RwLock<TreeNodeProto>>>,
93        vout: u32,
94        parent_public_key: Vec<u8>,
95        split_level: u32,
96    ) -> Result<GenerateDepositAddressForTreeSdkResponse, SparkSdkError>;
97
98    fn build_creation_nodes_from_tree(
99        &self,
100        parent_txid: Txid,
101        txout_: &TxOut,
102        vout: u32,
103        root: Arc<RwLock<DepositAddressTree>>,
104    ) -> Result<BuildCreationNodesFromTreeSdkResponse, SparkSdkError>;
105
106    async fn finalize_tree_creation(
107        &self,
108        parent_tx: Option<Transaction>,
109        parent_node: Option<Arc<RwLock<TreeNodeProto>>>,
110        vout: u32,
111        root: Arc<RwLock<DepositAddressTree>>,
112    ) -> Result<FinalizeTreeCreationSdkResponse, SparkSdkError>;
113}