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