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
use dioxus::prelude::*;
use crate::components::modal::Modal;
use crate::platform::file_dialog::{FileFilter, pick_file, pick_save_path};
use crate::state::app_state::AppState;
/// Key export/import dialog for E2E encryption keys.
#[component]
pub fn KeyExportDialog(on_close: EventHandler<()>) -> Element {
let state = use_context::<Signal<AppState>>();
let mut passphrase = use_signal(|| String::new());
let mut confirm_passphrase = use_signal(|| String::new());
let mut import_passphrase = use_signal(|| String::new());
let mut status = use_signal(|| Option::<String>::None);
let mut is_working = use_signal(|| false);
let mut mode = use_signal(|| "export".to_string());
rsx! {
Modal {
title: "E2E Key Export / Import".to_string(),
on_close: move |_| on_close.call(()),
div {
class: "key-export",
// Mode tabs
div {
class: "key-export__tabs",
button {
class: if *mode.read() == "export" { "key-export__tab key-export__tab--active" } else { "key-export__tab" },
onclick: move |_| mode.set("export".to_string()),
"Export Keys"
}
button {
class: if *mode.read() == "import" { "key-export__tab key-export__tab--active" } else { "key-export__tab" },
onclick: move |_| mode.set("import".to_string()),
"Import Keys"
}
}
if let Some(ref msg) = *status.read() {
div {
class: "key-export__status",
"{msg}"
}
}
if *mode.read() == "export" {
div {
class: "key-export__section",
p {
class: "key-export__info",
"Export your E2E encryption room keys so you can import them on another device or client. The exported file will be encrypted with the passphrase you provide."
}
div {
class: "key-export__field",
label { "Passphrase" }
input {
r#type: "password",
placeholder: "Enter a passphrase to protect the export",
value: "{passphrase}",
oninput: move |evt| passphrase.set(evt.value()),
}
}
div {
class: "key-export__field",
label { "Confirm Passphrase" }
input {
r#type: "password",
placeholder: "Confirm passphrase",
value: "{confirm_passphrase}",
oninput: move |evt| confirm_passphrase.set(evt.value()),
}
}
button {
class: "btn btn--primary",
disabled: *is_working.read() || passphrase.read().is_empty() || *passphrase.read() != *confirm_passphrase.read(),
onclick: move |_| {
let pp = passphrase.read().clone();
is_working.set(true);
status.set(Some("Exporting keys...".to_string()));
spawn(async move {
let client = { state.read().client.clone() };
if let Some(client) = client {
// Pick save location first
match pick_save_path("Save E2E key export", "element-keys.txt").await {
Ok(Some(path)) => {
let encryption = client.encryption();
match encryption.export_room_keys(path, &pp, |_| true).await {
Ok(()) => {
status.set(Some("Keys exported successfully!".to_string()));
}
Err(e) => {
status.set(Some(format!("Export failed: {e}")));
}
}
}
Ok(None) => {
status.set(None);
}
Err(err) => {
status.set(Some(err));
}
}
}
is_working.set(false);
});
},
if *is_working.read() { "Exporting..." } else { "Export" }
}
}
}
if *mode.read() == "import" {
div {
class: "key-export__section",
p {
class: "key-export__info",
"Import E2E encryption room keys from a previously exported file. You'll need the passphrase used during export."
}
div {
class: "key-export__field",
label { "Passphrase" }
input {
r#type: "password",
placeholder: "Enter the export passphrase",
value: "{import_passphrase}",
oninput: move |evt| import_passphrase.set(evt.value()),
}
}
button {
class: "btn btn--primary",
disabled: *is_working.read() || import_passphrase.read().is_empty(),
onclick: move |_| {
is_working.set(true);
status.set(Some("Select a key file...".to_string()));
spawn(async move {
match pick_file(
"Select E2E key file",
&[FileFilter {
name: "Key files",
extensions: &["txt", "json"],
}],
)
.await
{
Ok(Some(handle)) => {
let content = String::from_utf8_lossy(&handle.bytes);
status.set(Some(format!("Read {} bytes. Import processing requires SDK key import API.", content.len())));
}
Ok(None) => {
status.set(None);
}
Err(err) => {
status.set(Some(err));
}
}
is_working.set(false);
});
},
if *is_working.read() { "Importing..." } else { "Select File & Import" }
}
}
}
}
}
}
}