lib.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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::create_dir_all, sync::Arc};
  19. use smol::lock::RwLock;
  20. use url::Url;
  21. use darkfi::{system::ExecutorPtr, util::path::expand_path, Error, Result};
  22. /// Error codes
  23. pub mod error;
  24. use error::{WalletDbError, WalletDbResult};
  25. /// darkfid JSON-RPC related methods
  26. pub mod rpc;
  27. use rpc::DarkfidRpcClient;
  28. /// Payment methods
  29. pub mod transfer;
  30. /// Swap methods
  31. pub mod swap;
  32. /// Token methods
  33. pub mod token;
  34. /// CLI utility functions
  35. pub mod cli_util;
  36. /// Drk interactive shell
  37. pub mod interactive;
  38. /// Wallet functionality related to Money
  39. pub mod money;
  40. /// Wallet functionality related to Dao
  41. pub mod dao;
  42. /// Wallet functionality related to Deployooor
  43. pub mod deploy;
  44. /// Wallet functionality related to transactions history
  45. pub mod txs_history;
  46. /// Wallet functionality related to scanned blocks
  47. pub mod scanned_blocks;
  48. /// Wallet database operations handler
  49. pub mod walletdb;
  50. use walletdb::{WalletDb, WalletPtr};
  51. /// Blockchain cache database operations handler
  52. pub mod cache;
  53. use cache::Cache;
  54. /// Atomic pointer to a `Drk` structure.
  55. pub type DrkPtr = Arc<RwLock<Drk>>;
  56. /// CLI-util structure
  57. pub struct Drk {
  58. /// Blockchain cache database operations handler
  59. pub cache: Cache,
  60. /// Wallet database operations handler
  61. pub wallet: WalletPtr,
  62. /// JSON-RPC client to execute requests to darkfid daemon
  63. pub rpc_client: Option<RwLock<DarkfidRpcClient>>,
  64. /// Flag indicating if fun stuff are enabled
  65. pub fun: bool,
  66. }
  67. impl Drk {
  68. pub async fn new(
  69. cache_path: String,
  70. wallet_path: String,
  71. wallet_pass: String,
  72. endpoint: Option<Url>,
  73. ex: &ExecutorPtr,
  74. fun: bool,
  75. ) -> Result<Self> {
  76. // Initialize blockchain cache database
  77. let db_path = expand_path(&cache_path)?;
  78. let sled_db = sled_overlay::sled::open(&db_path)?;
  79. let Ok(cache) = Cache::new(&sled_db) else {
  80. return Err(Error::DatabaseError(format!("{}", WalletDbError::InitializationFailed)));
  81. };
  82. // Initialize wallet
  83. let wallet_path = expand_path(&wallet_path)?;
  84. if !wallet_path.exists() {
  85. if let Some(parent) = wallet_path.parent() {
  86. create_dir_all(parent)?;
  87. }
  88. }
  89. let Ok(wallet) = WalletDb::new(Some(wallet_path), Some(&wallet_pass)) else {
  90. return Err(Error::DatabaseError(format!("{}", WalletDbError::InitializationFailed)));
  91. };
  92. // Initialize rpc client
  93. let rpc_client = if let Some(endpoint) = endpoint {
  94. Some(RwLock::new(DarkfidRpcClient::new(endpoint, ex.clone()).await))
  95. } else {
  96. None
  97. };
  98. Ok(Self { cache, wallet, rpc_client, fun })
  99. }
  100. pub fn into_ptr(self) -> DrkPtr {
  101. Arc::new(RwLock::new(self))
  102. }
  103. /// Initialize wallet with tables for `Drk`.
  104. pub async fn initialize_wallet(&self) -> WalletDbResult<()> {
  105. // Initialize wallet schema
  106. self.wallet.exec_batch_sql(include_str!("../wallet.sql"))?;
  107. Ok(())
  108. }
  109. /// Auxiliary function to completely reset wallet state.
  110. pub fn reset(&self, output: &mut Vec<String>) -> WalletDbResult<()> {
  111. output.push(String::from("Resetting full wallet state"));
  112. self.reset_scanned_blocks(output)?;
  113. self.reset_money_tree(output)?;
  114. self.reset_money_smt(output)?;
  115. self.reset_money_coins(output)?;
  116. self.reset_mint_authorities(output)?;
  117. self.reset_dao_trees(output)?;
  118. self.reset_daos(output)?;
  119. self.reset_dao_proposals(output)?;
  120. self.reset_dao_votes(output)?;
  121. self.reset_deploy_authorities(output)?;
  122. self.reset_deploy_history(output)?;
  123. self.reset_tx_history(output)?;
  124. output.push(String::from("Successfully reset full wallet state"));
  125. Ok(())
  126. }
  127. }