lib.rs 4.7 KB

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