lib.rs 4.1 KB

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