redgold 0.1.23

p2p database / compute engine & portfolio target models
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#![allow(dead_code)]

use std::fmt::format;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, Once};
use eframe::egui::widgets::TextEdit;
use eframe::egui::{Align, TextStyle};
use eframe::egui;
use itertools::Itertools;
use log::{error, info};
use redgold_schema::{EasyJson, error_info, RgResult};

use crate::util::sym_crypt;
// 0.8
// use crate::gui::image_load::TexMngr;
use crate::gui::{ClientApp, home, top_panel};
use crate::util;
use rand::Rng;
use rocket::form::validate::Contains;
// impl NetworkStatusInfo {
//     pub fn default_vec() -> Vec<Self> {
//         NetworkEnvironment::status_networks().iter().enumerate().map()
//     }
// }

pub trait PublicKeyStoredState {
    fn public_key(&self, xpub_name: String) -> Option<PublicKey>;
}

impl PublicKeyStoredState for LocalStoredState {
    fn public_key(&self, xpub_name: String) -> Option<PublicKey> {
        let pk = self.xpubs.iter().find(|x| x.name == xpub_name)
            .and_then(|g| XpubWrapper::new(g.xpub.clone()).public_at(0, 0).ok());
        pk
    }
}


// #[derive(Clone)]
// #[derive(Clone)]
pub struct LocalState {
    active_tab: Tab,
    session_salt: [u8; 32],
    session_password_hashed: Option<[u8; 32]>,
    session_locked: bool,
    // This is only used by the text box and should be cleared immediately
    password_entry: String,
    // This is only used by the text box and should be cleared immediately
    wallet_passphrase_entry: String,
    // wallet_words_entry: String,
    active_passphrase: Option<String>,
    password_visible: bool,
    show_mnemonic: bool,
    visible_mnemonic: Option<String>,
    // TODO: Encrypt these with session password
    // TODO: Allow multiple passphrase, i'm too lazy for now
    // stored_passphrases: HashMap<String, String>,
    stored_passphrase: Vec<u8>,
    // stored_mnemonics: Vec<String>,
    stored_private_key_hexes: Vec<String>,
    iv: [u8; 16],
    wallet_first_load_state: bool,
    pub node_config: NodeConfig,
    // pub runtime: Arc<Runtime>,
    pub home_state: HomeState,
    pub server_state: ServersState,
    pub current_time: i64,
    pub keygen_state: KeygenState,
    pub wallet_state: WalletState,
    pub qr_state: QrState,
    pub qr_show_state: QrShowState,
    pub identity_state: IdentityState,
    pub settings_state: SettingsState,
    pub address_state: AddressState,
    pub otp_state: OtpState,
    pub ds_env: DataStore,
    pub ds_env_secure: Option<DataStore>,
    pub local_stored_state: LocalStoredState,
    pub updates: Channel<StateUpdate>,
    pub keytab_state: KeyTabState,
}

impl LocalState {
    pub(crate) fn process_tab_change(&mut self, p0: Tab) {
        match p0 {
            Tab::Home => {}
            Tab::Keys => {
                init_state(&mut self.wallet_state)
            }
            Tab::Transact => {}
            Tab::Portfolio => {}
            Tab::Identity => {}
            Tab::Contacts => {}
            Tab::Address => {}
            Tab::Servers => {}
            Tab::Ratings => {}
            Tab::Settings => {
                self.settings_state.lss_serialized = self.local_stored_state.json_or();
            }
            _ => {

            }
        }
    }
}

impl LocalState {

    pub fn add_mnemonic(&mut self, name: String, mnemonic: String, persist_disk: bool) {
        self.updates.sender.send(StateUpdate {
            update: Box::new(
                move |lss: &mut LocalState| {
                    lss.upsert_mnemonic(StoredMnemonic {
                        name: name.clone(),
                        mnemonic: mnemonic.clone(),
                        persist_disk: Some(persist_disk),
                    });
                })
        }).unwrap();
    }

    pub fn secure_or(&self) -> DataStore {
        self.ds_env_secure.clone().unwrap_or(self.ds_env.clone())
    }
    pub fn send_update<F: FnMut(&mut LocalState) + Send + 'static>(updates: &Channel<StateUpdate>, p0: F) {
        updates.sender.send(StateUpdate{update: Box::new(p0)}).unwrap();
    }

    pub fn persist_local_state_store(&self) {
        let store = self.secure_or();
        let mut state = self.local_stored_state.clone();
        state.clear_sensitive();
        tokio::spawn(async move {
            store.config_store.update_stored_state(state).await
        });
    }
    pub fn add_named_xpubs(&mut self, overwrite_name: bool, new_named: Vec<NamedXpub>, prepend: bool) -> RgResult<()> {
        let new_names = new_named.iter().map(|x| x.name.clone())
            .collect_vec();
        let existing = self.local_stored_state.xpubs.clone();
        let mut filtered = existing.iter().filter(|x| {
            !new_names.contains(&x.name)
        }).map(|x| x.clone()).collect_vec();
        if filtered.len() != existing.len() && !overwrite_name {
            return Err(error_info("Xpub with name already exists"));
        }
        let mut new_named2 = new_named.clone();
        if !prepend {
            filtered.extend(new_named);
            self.local_stored_state.xpubs = filtered;
        } else {
            new_named2.extend(filtered);
            self.local_stored_state.xpubs = new_named2;
        }
        self.persist_local_state_store();
        Ok(())
    }
    pub fn upsert_identity(&mut self, new_named: Identity) -> () {
        let mut updated = self.local_stored_state.identities.iter().filter(|x| {
            x.name != new_named.name
        }).map(|x| x.clone()).collect_vec();
        updated.push(new_named);

        self.local_stored_state.identities = updated;
        self.persist_local_state_store();
    }


    pub fn upsert_mnemonic(&mut self, new_named: StoredMnemonic) -> () {
        let mut updated = self.local_stored_state.mnemonics.as_ref().unwrap_or(&vec![]).iter().filter(|x| {
            x.name != new_named.name
        }).map(|x| x.clone()).collect_vec();
        updated.push(new_named);
        self.local_stored_state.mnemonics = Some(updated);
        self.persist_local_state_store();
    }

    pub fn upsert_private_key(&mut self, new_named: StoredPrivateKey) -> () {
        let mut updated = self.local_stored_state.private_keys.as_ref().unwrap_or(&vec![]).iter().filter(|x| {
            x.name != new_named.name
        }).map(|x| x.clone()).collect_vec();
        updated.push(new_named);
        self.local_stored_state.private_keys = Some(updated);
        self.persist_local_state_store();
    }


    pub fn process_updates(&mut self) {
        match self.updates.recv_while() {
            Ok(updates) => {
                for mut update in updates {
                    (update.update)(self);
                }
            }
            Err(e) => { error!("Error receiving updates: {}", e.json_or()) }
        }
    }
}

#[allow(dead_code)]
impl LocalState {
    pub async fn from(node_config: NodeConfig) -> Result<LocalState, ErrorInfo> {
        let mut node_config = node_config.clone();
        node_config.load_balancer_url = "lb.redgold.io".to_string();
        let iv = sym_crypt::get_iv();
        let ds_env = node_config.data_store_all().await;
        let ds_env_secure = node_config.data_store_all_secure().await;
        let ds_or = ds_env_secure.clone().unwrap_or(ds_env.clone());
        info!("Starting local state with secure_or connection path {}", ds_or.ctx.connection_path.clone());
        let string = ds_or.ctx.connection_path.clone().replace("file:", "");
        info!("ds_or connection path {}", string);
        ds_or.run_migrations_fallback_delete(
            true,
            PathBuf::from(string)
        ).await.expect("migrations");
        // DataStore::run_migrations(&ds_or).await.expect("");
        let hot_mnemonic = node_config.secure_or().all().mnemonic().await.unwrap_or(node_config.mnemonic_words.clone());
        let mut local_stored_state = ds_or.config_store.get_stored_state().await?;

        let mut ss = crate::gui::tabs::server_tab::ServersState::default();

        ss.csv_edit_path = node_config.clone().secure_data_folder.unwrap_or(node_config.data_folder.clone())
            .all().servers_path().to_str().expect("").to_string();
        ss.genesis = node_config.opts.development_mode;
        let mut ls = LocalState {
            active_tab: Tab::Home,
            session_salt: random_bytes(),
            session_password_hashed: None,
            session_locked: false,
            password_entry: "".to_string(),
            wallet_passphrase_entry: "".to_string(),
            // wallet_words_entry: "".to_string(),
            active_passphrase: None,
            password_visible: false,
            show_mnemonic: false,
            visible_mnemonic: None,
            stored_passphrase: vec![],
            // stored_passphrases: HashMap::new(),
            // stored_mnemonics: vec![],
            stored_private_key_hexes: vec![],
            iv,
            wallet_first_load_state: true,
            node_config: node_config.clone(),
            // runtime,
            home_state: HomeState::from(),
            server_state: ss,
            current_time: util::current_time_millis_i64(),
            keygen_state: KeygenState::new(
                node_config.clone().executable_checksum.clone().unwrap_or("".to_string())
            ),
            wallet_state: WalletState::new(hot_mnemonic, local_stored_state.xpubs.first()),
            qr_state: Default::default(),
            qr_show_state: Default::default(),
            identity_state: IdentityState::new(),
            settings_state: SettingsState::new(local_stored_state.json_or(),
                                               node_config.data_folder.clone().path.parent().unwrap().to_str().unwrap().to_string(),
                                               node_config.secure_data_folder.clone().unwrap_or(node_config.data_folder.clone())
                                                   .path.parent().unwrap().to_str().unwrap().to_string()
            ),
            address_state: Default::default(),
            otp_state: Default::default(),
            ds_env,
            ds_env_secure,
            local_stored_state,
            updates: new_channel(),
            keytab_state: Default::default(),
        };

        let mut new_xpubs = vec![];

        if let Some(df) = node_config.secure_data_folder.as_ref() {
            if let Ok(m) = df.all().mnemonic().await {
                if let Ok(w) = WordsPass::new_validated(m.clone(), None) {
                    let key_name = "secure_df_all".to_string();
                    ls.wallet_state.selected_key_name = key_name.clone();
                    ls.add_mnemonic(key_name.clone(), m, false);
                    if let Ok(xpub) = w.named_xpub(key_name, true) {
                        new_xpubs.push(xpub);
                    }
                }
            }
        }

        if let Ok(m) = node_config.data_folder.all().mnemonic().await {
            if let Ok(w) = WordsPass::new_validated(m.clone(), None) {
                let key_name = "df_all".to_string();
                ls.add_mnemonic(key_name.clone(), m, false);
                if let Ok(xpub) = w.named_xpub(key_name, true) {
                    new_xpubs.push(xpub);
                }
            }
        }

        if let Ok(m) = std::env::var("REDGOLD_TEST_WORDS") {
            if let Ok(w) = WordsPass::new_validated(m.clone(), None) {
                let key_name = "test_words".to_string();
                ls.add_mnemonic(key_name.clone(), m, false);
                if let Ok(xpub) = w.named_xpub(&key_name, true) {
                    new_xpubs.push(xpub);
                }
                let dp_btc_faucet = "m/84'/0'/0'/0/0".to_string();
                if let Ok(xpub) = w.xpub_str(&dp_btc_faucet.as_account_path().expect("acc")) {
                    let mut named = NamedXpub::default();
                    let key_into = key_name.clone();
                    named.name = format!("{}_840", key_into);
                    named.xpub = xpub;
                    named.key_name_source = Some(key_into);
                    named.request_type = Some(XPubRequestType::Hot);
                    named.skip_persist = Some(true);
                    named.derivation_path = dp_btc_faucet.clone();
                    new_xpubs.push(named);
                }
            }
        }

        if !new_xpubs.is_empty() {
            let first_xpub = new_xpubs.get(0).unwrap().clone();
            ls.wallet_state.selected_xpub_name = first_xpub.name.clone();
            ls.add_named_xpubs(true, new_xpubs, true).expect("Adding xpubs");
        }

        // TODO: Add from environment

        Ok(ls)
    }

    fn encrypt(&self, str: String) -> Vec<u8> {
        return sym_crypt::encrypt(
            str.as_bytes(),
            &self.session_password_hashed.unwrap(),
            &self.iv,
        )
        .unwrap();
    }

    fn decrypt(&self, data: &[u8]) -> Vec<u8> {
        return sym_crypt::decrypt(data, &self.session_password_hashed.unwrap(), &self.iv).unwrap();
    }

    pub fn accept_passphrase(&mut self, pass: String) {
        let encrypted = self.encrypt(pass);
        self.stored_passphrase = encrypted;
    } // https://www.quora.com/Is-it-useful-to-multi-hash-like-10-000-times-a-password-for-an-anti-brute-force-encryption-algorithm-Do-different-challenges-exist

    fn hash_password(&mut self) -> [u8; 32] {
        let mut vec = self.password_entry.as_bytes().to_vec();
        vec.extend(self.session_salt.to_vec());
        return dhash_vec(&vec);
    }
    fn store_password(&mut self) {
        self.session_password_hashed = Some(self.hash_password());
    }
}

fn random_bytes() -> [u8; 32] {
    return rand::thread_rng().gen::<[u8; 32]>();
}

use strum::IntoEnumIterator; // 0.17.1
use strum_macros::EnumIter;
use redgold_schema::structs::{ErrorInfo, PublicKey};
use crate::node_config::NodeConfig; // 0.17.1



#[derive(Debug, EnumIter, Clone)]
#[repr(i32)]
pub enum Tab {
    Home,
    Keys,
    Transact,
    Portfolio,
    Identity,
    Contacts,
    Address,
    Servers,
    Ratings,
    Settings,
    OTP,
}

fn update_lock_screen(app: &mut ClientApp, ctx: &egui::Context) {
    let ClientApp { local_state, .. } = app;
    egui::CentralPanel::default().show(ctx, |ui| {
        let layout = egui::Layout::top_down(egui::Align::Center);
        ui.with_layout(layout, |ui| {
            ui.add_space(ctx.available_rect().max.y / 3f32);
            ui.heading("Enter session password");
            ui.add_space(20f32);

            let edit = TextEdit::singleline(&mut local_state.password_entry)
                .password(true)
                .lock_focus(true);
            ui.add(edit).request_focus();
            if ctx.input(|i| { i.key_pressed(egui::Key::Enter)}) {
                if local_state.session_locked {
                    if local_state.session_password_hashed.unwrap() == local_state.hash_password() {
                        local_state.session_locked = false;
                    } else {
                        panic!("Session password state error");
                    }
                } else {
                    local_state.store_password();
                }
                local_state.password_entry = "".to_string();
                ()
            };
            //ui.text_edit_singleline(texts);
        });
    });
}

use redgold_data::data_store::DataStore;
use redgold_keys::util::dhash_vec;
use redgold_keys::util::mnemonic_support::WordsPass;
use redgold_keys::xpub_wrapper::{ValidateDerivationPath, XpubWrapper};
use crate::core::internal_message::{Channel, new_channel};
use crate::gui::home::HomeState;
use redgold_schema::local_stored_state::{Identity, LocalStoredState, NamedXpub, StoredMnemonic, StoredPrivateKey, XPubRequestType};
use crate::gui::tabs::address_tab::AddressState;
use crate::gui::tabs::identity_tab::IdentityState;
use crate::gui::tabs::otp_tab::{otp_tab, OtpState};
use crate::gui::tabs::{server_tab};
use crate::gui::tabs::keys::keygen_subtab::KeygenState;
use crate::gui::tabs::keys::keys_tab::{keys_tab, KeyTabState};
use crate::gui::tabs::server_tab::{ServersState, ServerStatus};
use crate::gui::tabs::settings_tab::{settings_tab, SettingsState};
use crate::gui::tabs::transact::hot_wallet::init_state;
use crate::gui::tabs::transact::wallet_tab::{StateUpdate, wallet_screen, WalletState};
use crate::qr_window::{qr_show_window, qr_window, QrShowState, QrState};

static INIT: Once = Once::new();

// /// Setup function that is only run once, even if called multiple times.
// pub fn init_logger_once() {
//     INIT.call_once(|| {
//         init_logger();
//     });
// }

pub fn app_update(app: &mut ClientApp, ctx: &egui::Context, _frame: &mut eframe::Frame) {
    let ClientApp {
        logo,
        local_state,
    } = app;

    // TODO: Replace with config query and check.
    INIT.call_once(|| {
        ctx.set_pixels_per_point(2.5);
    });

    local_state.current_time = util::current_time_millis_i64();
    // Continuous mode
    ctx.request_repaint();

    local_state.process_updates();

    // let mut style: egui::Style = (*ctx.style()).clone();
    // style.visuals.widgets.
    //style.spacing.item_spacing = egui::vec2(10.0, 20.0);
    // ctx.set_style(style);
    // Examples of how to create different panels and windows.
    // Pick whichever suits you.
    // Tip: a good default choice is to just keep the `CentralPanel`.
    // For inspiration and more examples, go to https://emilk.github.io/egui

    // TODO: Change this to lock screen state transition, also enable it only based on a lock button
    // if local_state.session_password_hashed.is_none() || local_state.session_locked {
    //     update_lock_screen(app, ctx, frame);
    //     return;
    // }

    top_panel::render_top(ctx, local_state);

    let img = logo;
    // let texture_id = img.texture_id(ctx);

    let mut changed_tab: Option<Tab> = None;

    egui::SidePanel::left("side_panel")
        .resizable(false)
        .show(ctx, |ui| {
            // ui.horizontal(|ui| {
            //     ui.label("Write something: ");
            //     ui.text_edit_singleline(label);
            // });
            // ui.add(egui::Slider::new(value, 0.0..=10.0).text("value"));

            //https://github.com/emilk/egui/blob/master/egui_demo_lib/src/apps/http_app.rs
            // ui.image(TextureId::default())

            ui.set_max_width(54f32);
            // ui.set_max_width(104f32);

            ui.with_layout(
                egui::Layout::top_down_justified(egui::Align::default()),
                |ui| {
                    let scale = 2.0;
                    let size =
                        egui::Vec2::new((img.size()[0] as f32 / scale) as f32, (img.size()[1] as f32 / scale) as f32);
                    // ui.style_mut().spacing.window_padding.y += 20.0f32;
                    ui.add_space(10f32);
                    // ui.image(texture_id); //, size);
                    let image = egui::Image::new(egui::include_image!("../resources/svg_rg_2_crop.png"));
                    // image.load_for_size(ctx, size).expect("works");
                    ui.add(
                        image
                        // egui::Image::new("https://picsum.photos/seed/1.759706314/1024").rounding(10.0),
                    );

                    ui.style_mut().override_text_style = Some(TextStyle::Heading);

                    ui.style_mut().spacing.item_spacing.y = 5f32;
                    ui.add_space(10f32);
                    //
                    // if ui.button("Home").clicked() {
                    //     *tab = Tab::Home;
                    // }
                    for tab_i in Tab::iter() {
                        let tab_str = format!("{:?}", tab_i);
                        if ui.button(tab_str).clicked() {
                            local_state.active_tab = tab_i.clone();
                            local_state.process_tab_change(tab_i.clone());
                            changed_tab = Some(tab_i.clone());
                        }
                    }
                    //
                    // if ui.button("Wallet").clicked() {
                    //     *tab = Tab::Wallet;
                    // }
                    //
                    // if ui.button("Settings").clicked() {
                    //     *tab = Tab::Wallet;
                    // }
                },
            );

            // ui.with_layout(egui::Layout::bottom_up(egui::Align::Center), |ui| {
            //     ui.add(
            //         egui::Hyperlink::new("https://github.com/emilk/egui/").text("powered by egui"),
            //     );
            // });
        });

    // if ctx.input().key_pressed(egui::Key::Escape) {
    //     local_state.session_locked = true;
    // }
    let has_changed_tab = changed_tab.is_some();

    egui::CentralPanel::default().show(ctx, |ui| {
        // The central panel the region left after adding TopPanel's and SidePanel's
        match local_state.active_tab {
            Tab::Home => {
                home::home_screen(ui, ctx, local_state);
            }
            Tab::Keys => {

                keys_tab(ui, ctx, local_state, has_changed_tab);
            }
            Tab::Settings => {
                settings_tab(ui, ctx, local_state);
            }
            Tab::Ratings => {}
            Tab::Servers => {
                server_tab::servers_tab(ui, ctx, local_state);
            }
            Tab::Transact => {
                wallet_screen(ui, ctx, local_state, has_changed_tab);
            }
            Tab::Identity => {
                crate::gui::tabs::identity_tab::identity_tab(ui, ctx, local_state);
            }
            Tab::Address => {
                crate::gui::tabs::address_tab::address_tab(ui, ctx, local_state);
            },
            Tab::OTP => {
                otp_tab(ui, ctx, local_state);
            }
            _ => {}
        }
        // ui.hyperlink("https://github.com/emilk/egui_template");
        // ui.add(egui::github_link_file!(
        //     "https://github.com/emilk/egui_template/blob/master/",
        //     "Source code."
        // ));
        ui.with_layout(egui::Layout::top_down(Align::BOTTOM), |ui| {
            egui::warn_if_debug_build(ui)
        });
    });

    qr_window(ctx, local_state);
    qr_show_window(ctx, local_state);

    // sync local data to RDS -- apart from data associated with phrases
    // discuss extra features around confirmation process. p2p negotation, contacts table.
}