-
Notifications
You must be signed in to change notification settings - Fork 8
/
keys_controller.js
115 lines (101 loc) 路 4.15 KB
/
keys_controller.js
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
import { Controller } from "stimulus";
import { generateMyKeys } from "../model/crypto";
import { download, copy, showPass } from "../model/index";
export default class extends Controller {
static targets = ["emailParams", "passphraseParams", "curveParams", "privateKey", "publicKey", "initialState", "generateButton", "error"];
async generate(e) {
// Initial display
e.preventDefault();
this.initialStateTarget.classList.add("d-none");
this.emailParamsTarget.classList.remove("border-danger");
this.passphraseParamsTarget.classList.remove("border-danger");
this.curveParamsTarget.classList.remove("border-danger");
this.errorTarget.classList.add("d-none");
// Validation form
if (this.emailParamsTarget.value == "") {
this.emailParamsTarget.classList.add("border-danger");
}
if (this.passphraseParamsTarget.value == "") {
this.passphraseParamsTarget.classList.add("border-danger");
}
if (this.curveParamsTarget.value == "") {
this.curveParamsTarget.classList.add("border-danger");
}
if (this.emailParamsTarget.value == "" || this.passphraseParamsTarget.value == "" || this.curveParamsTarget.value == "") {
return
}
// UX button
this.generateButtonTarget.disabled = true;
this.generateButtonTarget.getElementsByClassName("material-icons")[0].classList.add("d-none");
this.generateButtonTarget.getElementsByClassName("spinner-border")[0].classList.remove("d-none");
// Params
const emailParams = this.emailParamsTarget.value;
const passphraseParams = this.passphraseParamsTarget.value;
const curveParams = this.curveParamsTarget.value;
const key = await generateMyKeys(emailParams, passphraseParams, curveParams).catch((err) => { console.error(err); });
if (key) {
this.privateKeyTarget.innerText = key.privateKey;
this.publicKeyTarget.innerText = key.publicKey;
this.initialStateTarget.classList.remove("d-none");
this.errorTarget.classList.add("d-none");
$([document.documentElement, document.body]).animate({
scrollTop: $(this.initialStateTarget).offset().top
}, 1000);
} else {
this.errorTarget.classList.remove("d-none");
$([document.documentElement, document.body]).animate({
scrollTop: 0
}, 1000);
}
// Go back to initial UX button
this.generateButtonTarget.disabled = false;
this.generateButtonTarget.getElementsByClassName("material-icons")[0].classList.remove("d-none");
this.generateButtonTarget.getElementsByClassName("spinner-border")[0].classList.add("d-none");
}
showPassphrase(e) {
let el = this.passphraseParamsTarget;
showPass(el);
}
copyToClipboard(e) {
let el = e.target;
let text;
if (el.classList.contains("public-key")) {
text = this.publicKeyTarget.innerText;
} else if (el.classList.contains("private-key")) {
text = this.privateKeyTarget.innerText;
} else {
this.errorTarget.classList.remove("d-none");
$([document.documentElement, document.body]).animate({
scrollTop: 0
}, 1000);
return
}
copy(text, el);
}
downloadKey(e) {
e.preventDefault();
let type = e.currentTarget.dataset.type;
let format = e.currentTarget.dataset.format;
if (type == "public") {
const text = this.publicKeyTarget.innerText;
if (format == "asc") {
download(text, "text/asc", "aliceandbob.io - Public Key.asc");
} else {
download(text, "text/txt", "aliceandbob.io - Public Key.txt");
}
} else if (type == "private") {
const text = this.privateKeyTarget.innerText;
if (format == "asc") {
download(text, "text/asc", "aliceandbob.io - Private Key.asc");
} else {
download(text, "text/txt", "aliceandbob.io - Private Key.txt");
}
} else {
download("Let's hope you didn't have any bad intention by doing so ;)", "txt", "Well tried");
this.errorTarget.classList.remove("d-none");
$([document.documentElement, document.body]).animate({
scrollTop: 0
}, 1000);
}
}
}