lib.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{fs, sync::Arc};
  19. use url::Url;
  20. use darkfi::{rpc::client::RpcClient, util::path::expand_path, Error, Result};
  21. /// Error codes
  22. pub mod error;
  23. use error::{WalletDbError, WalletDbResult};
  24. /// darkfid JSON-RPC related methods
  25. pub mod rpc;
  26. /// Payment methods
  27. pub mod transfer;
  28. /// Swap methods
  29. pub mod swap;
  30. /// Token methods
  31. pub mod token;
  32. /// CLI utility functions
  33. pub mod cli_util;
  34. /// Wallet functionality related to Money
  35. pub mod money;
  36. /// Wallet functionality related to Dao
  37. pub mod dao;
  38. /// Wallet functionality related to Deployooor
  39. pub mod deploy;
  40. /// Wallet functionality related to transactions history
  41. pub mod txs_history;
  42. /// Wallet functionality related to scanned blocks
  43. pub mod scanned_blocks;
  44. /// Wallet database operations handler
  45. pub mod walletdb;
  46. use walletdb::{WalletDb, WalletPtr};
  47. /// Blockchain cache database operations handler
  48. pub mod cache;
  49. use cache::Cache;
  50. /// CLI-util structure
  51. pub struct Drk {
  52. /// Blockchain cache database operations handler
  53. pub cache: Cache,
  54. /// Wallet database operations handler
  55. pub wallet: WalletPtr,
  56. /// JSON-RPC client to execute requests to darkfid daemon
  57. pub rpc_client: Option<RpcClient>,
  58. /// Flag indicating if fun stuff are enabled
  59. pub fun: bool,
  60. }
  61. impl Drk {
  62. pub async fn new(
  63. cache_path: String,
  64. wallet_path: String,
  65. wallet_pass: String,
  66. endpoint: Option<Url>,
  67. ex: Arc<smol::Executor<'static>>,
  68. fun: bool,
  69. ) -> Result<Self> {
  70. // Initialize blockchain cache database
  71. let db_path = expand_path(&cache_path)?;
  72. let sled_db = sled_overlay::sled::open(&db_path)?;
  73. let Ok(cache) = Cache::new(&sled_db) else {
  74. return Err(Error::DatabaseError(format!("{}", WalletDbError::InitializationFailed)));
  75. };
  76. // Initialize wallet
  77. let wallet_path = expand_path(&wallet_path)?;
  78. if !wallet_path.exists() {
  79. if let Some(parent) = wallet_path.parent() {
  80. fs::create_dir_all(parent)?;
  81. }
  82. }
  83. let Ok(wallet) = WalletDb::new(Some(wallet_path), Some(&wallet_pass)) else {
  84. return Err(Error::DatabaseError(format!("{}", WalletDbError::InitializationFailed)));
  85. };
  86. // Initialize rpc client
  87. let rpc_client = if let Some(endpoint) = endpoint {
  88. Some(RpcClient::new(endpoint, ex).await?)
  89. } else {
  90. None
  91. };
  92. Ok(Self { cache, wallet, rpc_client, fun })
  93. }
  94. /// Initialize wallet with tables for `Drk`.
  95. pub async fn initialize_wallet(&self) -> WalletDbResult<()> {
  96. // Initialize wallet schema
  97. self.wallet.exec_batch_sql(include_str!("../wallet.sql"))?;
  98. Ok(())
  99. }
  100. /// Auxiliary function to completely reset wallet state.
  101. pub async fn reset(&self) -> WalletDbResult<()> {
  102. println!("Resetting full wallet state");
  103. self.reset_scanned_blocks()?;
  104. self.reset_money_tree()?;
  105. self.reset_money_smt()?;
  106. self.reset_money_coins()?;
  107. self.reset_mint_authorities()?;
  108. self.reset_dao_trees().await?;
  109. self.reset_daos().await?;
  110. self.reset_dao_proposals().await?;
  111. self.reset_dao_votes()?;
  112. self.reset_tx_history()?;
  113. println!("Successfully reset full wallet state");
  114. Ok(())
  115. }
  116. /// Auxiliary function to reset `walletdb` inverse cache state.
  117. pub async fn reset_inverse_cache(&self) -> Result<()> {
  118. // Reset `walletdb` inverse cache
  119. if let Err(e) = self.wallet.clear_inverse_cache() {
  120. return Err(Error::DatabaseError(format!(
  121. "[reset_inverse_cache] Clearing wallet inverse cache failed: {e:?}"
  122. )))
  123. }
  124. Ok(())
  125. }
  126. }