bwk 0.0.8

Bitcoin wallet kit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::{
    collections::BTreeMap,
    fs::{self, File},
    io::{Read, Write},
    path::PathBuf,
    str::FromStr,
};

use miniscript::{
    bitcoin::{self, ScriptBuf},
    Descriptor, DescriptorPublicKey,
};
use serde::{Deserialize, Serialize};

use crate::{descriptor::ScriptType, signer::HotSigner};

const CONFIG_FILENAME: &str = "config.json";

/// Returns the data directory path based on the operating system.
///
/// On Linux, it returns the path to the `.qoinstr` directory in the user's home directory.
/// On other operating systems, it returns the path to the `Qoinstr` directory in the user's config directory.
/// The directory is created if it does not exist.
/// NOTE: do NOT use on mobile
pub fn datadir(dir_name: &str) -> PathBuf {
    #[cfg(target_os = "linux")]
    let dir = {
        let mut dir = dirs::home_dir().unwrap();
        dir.push(dir_name);
        dir
    };

    #[cfg(not(target_os = "linux"))]
    let dir = {
        let mut dir = dirs::config_dir().unwrap();
        dir.push("Qoinstr");
        dir
    };

    maybe_create_dir(&dir);

    dir
}

/// Creates a directory if it does not exist.
pub fn maybe_create_dir(dir: &PathBuf) {
    if !dir.exists() {
        #[cfg(unix)]
        {
            use std::fs::DirBuilder;
            use std::os::unix::fs::DirBuilderExt;

            let mut builder = DirBuilder::new();
            builder.mode(0o700).recursive(true).create(dir).unwrap();
        }

        #[cfg(not(unix))]
        std::fs::create_dir_all(dir).unwrap();
    }
}

/// Represents the configuration settings for the application.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
    #[serde(skip)]
    data_dir: PathBuf,
    #[serde(skip)]
    dir_name: &'static str,
    pub account: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub electrum_url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub electrum_port: Option<u16>,
    pub network: bitcoin::Network,
    pub look_ahead: u32,
    pub mnemonic: Option<String>,
    pub descriptor: Descriptor<DescriptorPublicKey>,
    pub persist: bool,
}

impl Config {
    /// Creates a new `Config` instance with the specified descriptor.
    ///
    /// # Arguments
    ///
    /// * `mnemonic` - A string representing the mnemonic words.
    /// * `account` - A string representing the account name.
    /// * `network` - the bitcoin network for this config.
    ///
    /// # Returns
    ///
    /// A `Config` instance initialized with the provided descriptor.
    pub fn new(
        mnemonic: Option<String>,
        account: String,
        network: bitcoin::Network,
        script: ScriptType,
        data_dir: PathBuf,
        dir_name: &'static str,
        persist: bool,
    ) -> Option<Config> {
        let descriptor = match script {
            ScriptType::Segwit(_) | ScriptType::Taproot(_) => {
                if let Some(mnemo) = &mnemonic {
                    let signer = HotSigner::new_from_mnemonics(network, mnemo).unwrap();
                    script.to_descriptor(network, |d| signer.xpub(&d)).unwrap()
                } else {
                    return None;
                }
            }
            ScriptType::Descriptor(descriptor) => descriptor,
        };
        Some(Config {
            data_dir,
            dir_name,
            account,
            electrum_url: None,
            electrum_port: None,
            network,
            look_ahead: 20,
            mnemonic,
            descriptor,
            persist,
        })
    }
    /// Allow to disable persistance of data, useful for tests
    pub fn enable_persist(mut self, persist: bool) -> Self {
        self.persist = persist;
        self
    }

    /// Returns the Electrum URL as a string.
    pub fn electrum_url(&self) -> String {
        self.electrum_url.clone().unwrap_or_default()
    }
    /// Returns the Electrum port as a string.
    pub fn electrum_port(&self) -> String {
        self.electrum_port
            .map(|v| format!("{v}"))
            .unwrap_or_default()
    }
    /// Returns the look-ahead value as a string.
    pub fn look_ahead(&self) -> String {
        self.look_ahead.to_string()
    }
    /// Returns the network as a `Network` instance.
    pub fn network(&self) -> bitcoin::Network {
        self.network
    }
    /// Sets the Electrum URL.
    pub fn set_electrum_url(&mut self, url: String) {
        self.electrum_url = Some(url);
    }
    /// Sets the Electrum port from a string.
    pub fn set_electrum_port(&mut self, port: String) {
        self.electrum_port = port.parse::<u16>().ok();
    }
    /// Sets the look-ahead value from a string.
    pub fn set_look_ahead(&mut self, look_ahead: String) {
        if let Ok(la) = look_ahead.parse::<u32>() {
            self.look_ahead = la;
        }
    }
    /// Sets the network.
    pub fn set_network(&mut self, network: bitcoin::Network) {
        self.network = network;
    }
    /// Sets the mnemonic.
    pub fn set_mnemonic(&mut self, mnemonic: String) {
        self.mnemonic = Some(mnemonic);
    }
    /// Sets the account name.
    pub fn set_account(&mut self, name: String) {
        self.account = name;
    }
    /// Saves the configuration to a file.
    pub fn to_file(&self) {
        let mut path = Self::path(self.data_dir.clone(), self.dir_name, self.account.clone());
        maybe_create_dir(&path);
        path.push(CONFIG_FILENAME);

        log::warn!("Config::to_file() {:?}", path);

        let mut file = File::create(path).unwrap();
        let content = serde_json::to_string_pretty(&self).unwrap();
        file.write_all(content.as_bytes()).unwrap();
    }

    pub fn dir_name(&self) -> &'static str {
        self.dir_name
    }

    pub fn data_dir(&self) -> PathBuf {
        self.data_dir.clone()
    }

    /// Lists all configuration directories in the data directory.
    ///
    /// # Returns
    ///
    /// A vector of strings representing the account names of the configurations
    /// found in the data directory.
    /// Lists all configuration directories in the data directory.
    pub fn list_configs(data_dir: PathBuf, dir_name: &'static str) -> Vec<String> {
        let mut path = data_dir.clone();
        path.push(dir_name);
        let mut out = vec![];
        if let Ok(folders) = fs::read_dir(path) {
            folders.for_each(|account| {
                if let Ok(entry) = account {
                    if let Ok(md) = entry.metadata() {
                        if md.is_dir() {
                            let acc_name = entry.file_name().to_str().unwrap().to_string();
                            let path = Self::path(data_dir.clone(), dir_name, acc_name.clone());
                            let parsed = Self::from_file(path);
                            if !parsed.account.is_empty() {
                                out.push(acc_name);
                            }
                        };
                    }
                }
            });
        }

        out
    }

    /// Checks if a configuration file exists for the given account.
    ///
    /// # Arguments
    ///
    /// * `account` - A string representing the account name.
    ///
    /// # Returns
    ///
    /// A boolean value indicating whether the configuration file exists.
    /// Checks if a configuration file exists for the given account.
    pub fn config_exists(data_dir: PathBuf, dir_name: &'static str, account: String) -> bool {
        let mut path = Self::path(data_dir, dir_name, account.clone());
        path.push(CONFIG_FILENAME);
        path.exists()
    }
    /// Returns the path to the configuration directory for the specified account.
    ///
    /// # Arguments
    ///
    /// * `account` - A string representing the account name.
    ///
    /// # Returns
    ///
    /// A `PathBuf` representing the path to the configuration directory.
    pub fn path(data_dir: PathBuf, dir_name: &'static str, account: String) -> PathBuf {
        let mut dir = data_dir;
        dir.push(dir_name);
        dir.push(account);
        dir
    }

    /// Creates a `Config` instance from a configuration file.
    pub fn from_file(mut path: PathBuf) -> Self {
        path.push(CONFIG_FILENAME);

        let mut file = File::open(path).unwrap();
        let mut content = String::new();
        let _ = file.read_to_string(&mut content);
        let conf: Config = serde_json::from_str(&content).unwrap();
        conf
    }

    /// Returns the path to the transactions file for the current account.
    pub fn transactions_path(&self) -> PathBuf {
        let mut path = Self::path(self.data_dir.clone(), self.dir_name, self.account.clone());
        path.push("transactions.json");
        path
    }

    /// Returns the path to the statuses file for the current account.
    pub fn statuses_path(&self) -> PathBuf {
        let mut path = Self::path(self.data_dir.clone(), self.dir_name, self.account.clone());
        path.push("statuses.json");
        path
    }

    /// Returns the path to the tip file for the current account.
    pub fn tip_path(&self) -> PathBuf {
        let mut path = Self::path(self.data_dir.clone(), self.dir_name, self.account.clone());
        path.push("tip.json");
        path
    }

    /// Returns the path to the labels file for the current account.
    pub fn labels_path(&self) -> PathBuf {
        let mut path = Self::path(self.data_dir.clone(), self.dir_name, self.account.clone());
        path.push("labels.json");
        path
    }

    /// Persists the tip information to a file for the current account.
    ///
    /// # Arguments
    ///
    /// * `receive` - The amount to receive.
    /// * `change` - The amount of change.
    pub fn persist_tip(&self, receive: u32, change: u32) {
        if !self.persist {
            return;
        }
        let file = File::create(self.tip_path());
        match file {
            Ok(mut file) => {
                let tip = Tip { receive, change };
                let content = serde_json::to_string_pretty(&tip).expect("cannot fail");
                let _ = file.write(content.as_bytes());
            }
            Err(e) => {
                log::error!("Config::persist_tip() fail to open file: {e}");
            }
        }
    }

    /// Retrieves the tip information from the tip file for the current account.
    ///
    /// # Returns
    ///
    /// A `Tip` instance containing the tip information.
    pub fn tip_from_file(&self) -> Tip {
        if let Ok(mut file) = File::open(self.tip_path()) {
            let mut content = String::new();
            let _ = file.read_to_string(&mut content);
            serde_json::from_str(&content).unwrap_or_default()
        } else {
            Default::default()
        }
    }

    /// Persists the statuses information to a file for the current account.
    ///
    /// # Arguments
    ///
    /// * `statuses` - A reference to a `BTreeMap` containing the statuses information.
    pub fn persist_statuses(&self, statuses: &BTreeMap<ScriptBuf, (Option<String>, u32, u32)>) {
        if !self.persist {
            return;
        }
        let file = File::create(self.statuses_path());
        match file {
            Ok(mut file) => {
                let content = serde_json::to_string_pretty(statuses).expect("cannot fail");
                let _ = file.write(content.as_bytes());
            }
            Err(e) => {
                log::error!("Config::statuses() fail to open file: {e}");
            }
        }
    }

    /// Retrieves the statuses information from the statuses file for the current account.
    ///
    /// # Returns
    ///
    /// A `BTreeMap` containing the statuses information.
    pub fn statuses_from_file(&self) -> BTreeMap<ScriptBuf, (Option<String>, u32, u32)> {
        if let Ok(mut file) = File::open(self.statuses_path()) {
            let mut content = String::new();
            let _ = file.read_to_string(&mut content);
            serde_json::from_str(&content).unwrap_or_default()
        } else {
            Default::default()
        }
    }
}

/// Represents the tip information for the current account.
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Tip {
    pub receive: u32,
    pub change: u32,
}

/// Checks if the provided descriptor string is valid.
///
/// # Arguments
///
/// * `descriptor` - A string representing the descriptor to validate.
pub fn is_descriptor_valid(descriptor: String) -> bool {
    Descriptor::<DescriptorPublicKey>::from_str(&descriptor).is_ok()
}

#[cfg(test)]
pub mod tests {
    use miniscript::bitcoin::bip32::ChildNumber;

    use super::*;

    #[test]
    fn test_persist() {
        let temp = temp_dir::TempDir::new().unwrap();
        let path = temp.child("storage");
        let mnemonic = bip39::Mnemonic::generate(12).unwrap();
        let dir_name = "wallet";
        let cfg = Config::new(
            Some(mnemonic.to_string()),
            "my_account".to_string(),
            bitcoin::Network::Regtest,
            ScriptType::Segwit(ChildNumber::from_hardened_idx(0).unwrap()),
            path.clone(),
            dir_name,
            true,
        )
        .unwrap();
        cfg.to_file();
        let mut path = path.to_path_buf();
        path.push(dir_name);
        path.push("my_account");
        let cfg2 = Config::from_file(path);
        assert_eq!(cfg.account, cfg2.account);
        assert_eq!(cfg2.account, "my_account")
    }
}