spark_sdk/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
42pub(crate) struct CreateDepositAddressBinaryTreeSdkResponse {
43    pub(crate) tree: Vec<Arc<RwLock<DepositAddressTree>>>,
44}
45
46pub(crate) struct CreateAddressRequestNodeFromTreeNodesSdkResponse {
47    pub(crate) address_request_nodes: Vec<AddressRequestNodeProto>,
48}
49
50pub(crate) struct GenerateDepositAddressForTreeSdkResponse {
51    pub(crate) tree: Arc<RwLock<DepositAddressTree>>,
52}
53
54pub(crate) struct BuildCreationNodesFromTreeSdkResponse {
55    pub(crate) creation_nodes: CreationNode, // stands for the root creation node, where it already has children in the children field
56}
57
58pub(crate) struct FinalizeTreeCreationSdkResponse {
59    pub(crate) finalize_tree_response: FinalizeNodeSignaturesResponse,
60    pub(crate) signing_public_keys: Vec<Vec<u8>>,
61}
62
63#[async_trait]
64pub(crate) trait CreateTreeInternalHandlers<S: SparkSigner + Send + Sync> {
65    fn create_deposit_address_binary_tree(
66        &self,
67        split_level: u32,
68        target_pubkey: &Vec<u8>,
69    ) -> Result<CreateDepositAddressBinaryTreeSdkResponse, SparkSdkError>;
70
71    fn create_address_request_node_from_tree_nodes(
72        &self,
73        tree_nodes: &Vec<Arc<RwLock<DepositAddressTree>>>,
74    ) -> Result<CreateAddressRequestNodeFromTreeNodesSdkResponse, SparkSdkError>;
75
76    fn apply_address_nodes_to_tree(
77        &self,
78        tree: &mut Vec<Arc<RwLock<DepositAddressTree>>>,
79        address_nodes: Vec<AddressNodeProto>,
80    ) -> Result<(), SparkSdkError>;
81
82    async fn generate_deposit_address_for_tree(
83        &self,
84        parent_tx: Option<Transaction>,
85        parent_node: Option<Arc<RwLock<TreeNodeProto>>>,
86        vout: u32,
87        parent_public_key: Vec<u8>,
88        split_level: u32,
89    ) -> Result<GenerateDepositAddressForTreeSdkResponse, SparkSdkError>;
90
91    fn build_creation_nodes_from_tree(
92        &self,
93        parent_txid: Txid,
94        txout_: &TxOut,
95        vout: u32,
96        root: Arc<RwLock<DepositAddressTree>>,
97    ) -> Result<BuildCreationNodesFromTreeSdkResponse, SparkSdkError>;
98
99    async fn finalize_tree_creation(
100        &self,
101        parent_tx: Option<Transaction>,
102        parent_node: Option<Arc<RwLock<TreeNodeProto>>>,
103        vout: u32,
104        root: Arc<RwLock<DepositAddressTree>>,
105    ) -> Result<FinalizeTreeCreationSdkResponse, SparkSdkError>;
106}