-
Notifications
You must be signed in to change notification settings - Fork 2
/
ninja.detailwallet.js
276 lines (260 loc) · 10.6 KB
/
ninja.detailwallet.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
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
(function (wallets, qrCode, privateKey, translator) {
var detail = wallets.detailwallet = {
isOpen: function () {
return (document.getElementById("detailwallet").className.indexOf("selected") != -1);
},
open: function () {
document.getElementById("detailarea").style.display = "block";
document.getElementById("detailprivkey").focus();
},
close: function () {
document.getElementById("detailarea").style.display = "none";
},
openCloseFaq: function (faqNum) {
// do close
if (document.getElementById("detaila" + faqNum).style.display == "block") {
document.getElementById("detaila" + faqNum).style.display = "none";
document.getElementById("detaile" + faqNum).setAttribute("class", "more");
}
// do open
else {
document.getElementById("detaila" + faqNum).style.display = "block";
document.getElementById("detaile" + faqNum).setAttribute("class", "less");
}
},
getKeyFromInput: function () {
var key = document.getElementById("detailprivkey").value.toString().replace(/^\s+|\s+$/g, ""); // trim white space
document.getElementById("detailprivkey").value = key;
return key;
},
checkAndShowMini: function (key) {
if (Bitcoin.ECKey.isMiniFormat(key)) {
// show Private Key Mini Format
document.getElementById("detailprivmini").innerHTML = key;
document.getElementById("detailmini").style.display = "block";
}
},
checkAndShowBase6: function (key) {
if (Bitcoin.ECKey.isBase6Format(key)) {
// show Private Key Base6 Format
document.getElementById("detailprivb6").innerHTML = key;
document.getElementById("detailb6").style.display = "block";
}
},
keyToECKeyWithBrain: function (key) {
var btcKey = new Bitcoin.ECKey(key);
if (btcKey.error != null) {
alert(translator.get("detailalertnotvalidprivatekey") + "\n" + btcKey.error);
}
else if (btcKey.priv == null) {
// enforce a minimum passphrase length
if (key.length >= wallets.brainwallet.minPassphraseLength) {
// Deterministic Wallet confirm box to ask if user wants to SHA256 the input to get a private key
var usePassphrase = confirm(translator.get("detailconfirmsha256"));
if (usePassphrase) {
var bytes = Crypto.SHA256(key, { asBytes: true });
btcKey = new Bitcoin.ECKey(bytes);
}
}
else {
alert(translator.get("detailalertnotvalidprivatekey"));
}
}
return btcKey;
},
decryptBip38: function () {
detail.clear();
var key = detail.getKeyFromInput();
if (key == "") {
return;
}
if (privateKey.isBIP38Format(key) == false) {
return;
}
document.getElementById("detailbip38toggle").style.display = "none";
var passphrase = document.getElementById("detailprivkeypassphrase").value.toString()
if (passphrase == "") {
alert(translator.get("bip38alertpassphraserequired"));
return;
}
document.getElementById("busyblock").className = "busy";
// show Private Key BIP38 Format
document.getElementById("detailprivbip38").innerHTML = key;
document.getElementById("detailbip38").style.display = "block";
qrCode.showQrCode({
"detailqrcodeprivatebip38": key
}, 4);
privateKey.BIP38EncryptedKeyToByteArrayAsync(key, passphrase, function (btcKeyOrError) {
document.getElementById("busyblock").className = "";
if (btcKeyOrError.message) {
alert(btcKeyOrError.message);
detail.clear();
} else {
detail.populateKeyDetails(new Bitcoin.ECKey(btcKeyOrError));
}
});
},
encryptBip38: function () {
detail.clear();
var key = detail.getKeyFromInput();
if (key == "") {
return;
}
if (privateKey.isBIP38Format(key)) {
return;
}
detail.checkAndShowMini(key);
detail.checkAndShowBase6(key);
var btcKey = detail.keyToECKeyWithBrain(key);
if (btcKey.priv == null) {
return;
}
var detailEncryptCheckbox = document.getElementById("detailbip38checkbox");
if (detailEncryptCheckbox.checked == true) {
document.getElementById("detailbip38commands").style.display = "block";
var passphrase = document.getElementById("detailprivkeypassphrase").value.toString()
if (passphrase == "") {
alert(translator.get("bip38alertpassphraserequired"));
return;
}
document.getElementById("busyblock").className = "busy";
privateKey.BIP38PrivateKeyToEncryptedKeyAsync(btcKey.getBitcoinWalletImportFormat(), passphrase, btcKey.compressed, function (encryptedKey) {
qrCode.showQrCode({
"detailqrcodeprivatebip38": encryptedKey
}, 4);
// show Private Key BIP38 Format
document.getElementById("detailprivbip38").innerHTML = encryptedKey;
document.getElementById("detailbip38").style.display = "block";
document.getElementById("busyblock").className = "";
});
detail.populateKeyDetails(btcKey);
}
},
viewDetails: function () {
detail.clear();
document.getElementById("detailbip38checkbox").checked = false;
var key = detail.getKeyFromInput();
if (key == "") {
return;
}
if (privateKey.isBIP38Format(key)) {
document.getElementById("detailbip38commands").style.display = "block";
document.getElementById("detailprivkeypassphrase").focus();
return;
}
document.getElementById("detailbip38commands").style.display = "none";
detail.checkAndShowMini(key);
detail.checkAndShowBase6(key);
var btcKey = detail.keyToECKeyWithBrain(key);
if(btcKey.priv == null){
return;
}
detail.populateKeyDetails(btcKey);
},
populateKeyDetails: function (btcKey) {
if (btcKey.priv != null) {
// get the original compression value and set it back later in this function
var originalCompression = btcKey.compressed;
btcKey.setCompressed(false);
document.getElementById("detailprivhex").innerHTML = btcKey.toString().toUpperCase();
document.getElementById("detailprivb64").innerHTML = btcKey.toString("base64");
var bitcoinAddress = btcKey.getBitcoinAddress();
var wif = btcKey.getBitcoinWalletImportFormat();
document.getElementById("detailpubkey").innerHTML = btcKey.getPubKeyHex();
document.getElementById("detailaddress").innerHTML = bitcoinAddress;
document.getElementById("detailprivwif").innerHTML = wif;
btcKey.setCompressed(true);
var bitcoinAddressComp = btcKey.getBitcoinAddress();
var wifComp = btcKey.getBitcoinWalletImportFormat();
document.getElementById("detailpubkeycomp").innerHTML = btcKey.getPubKeyHex();
document.getElementById("detailaddresscomp").innerHTML = bitcoinAddressComp;
document.getElementById("detailprivwifcomp").innerHTML = wifComp;
btcKey.setCompressed(originalCompression); // to satisfy the key pool
var pool1 = new Bitcoin.ECKey(wif); // to satisfy the key pool
var pool2 = new Bitcoin.ECKey(wifComp); // to satisfy the key pool
qrCode.showQrCode({
"detailqrcodepublic": bitcoinAddress,
"detailqrcodepubliccomp": bitcoinAddressComp,
"detailqrcodeprivate": wif,
"detailqrcodeprivatecomp": wifComp
}, 4);
}
},
clear: function () {
var key = detail.getKeyFromInput();
if (privateKey.isBIP38Format(key)) {
document.getElementById("detailbip38commands").style.display = "block";
document.getElementById("detailbip38toggle").style.display = "none";
document.getElementById("detailbip38decryptspan").style.display = "inline-block";
document.getElementById("detailbip38encryptspan").style.display = "none";
document.getElementById("detailbip38checkbox").checked = false;
}
else {
document.getElementById("detailbip38toggle").style.display = "block";
if (document.getElementById("detailbip38checkbox").checked) {
document.getElementById("detailbip38commands").style.display = "block";
document.getElementById("detailbip38decryptspan").style.display = "none";
document.getElementById("detailbip38encryptspan").style.display = "inline-block";
}
else {
document.getElementById("detailbip38commands").style.display = "none";
document.getElementById("detailbip38decryptspan").style.display = "inline-block";
document.getElementById("detailbip38encryptspan").style.display = "none";
}
}
document.getElementById("detailpubkey").innerHTML = "";
document.getElementById("detailpubkeycomp").innerHTML = "";
document.getElementById("detailaddress").innerHTML = "";
document.getElementById("detailaddresscomp").innerHTML = "";
document.getElementById("detailprivwif").innerHTML = "";
document.getElementById("detailprivwifcomp").innerHTML = "";
document.getElementById("detailprivhex").innerHTML = "";
document.getElementById("detailprivb64").innerHTML = "";
document.getElementById("detailprivb6").innerHTML = "";
document.getElementById("detailprivmini").innerHTML = "";
document.getElementById("detailprivbip38").innerHTML = "";
document.getElementById("detailqrcodepublic").innerHTML = "";
document.getElementById("detailqrcodepubliccomp").innerHTML = "";
document.getElementById("detailqrcodeprivate").innerHTML = "";
document.getElementById("detailqrcodeprivatecomp").innerHTML = "";
document.getElementById("detailb6").style.display = "none";
document.getElementById("detailmini").style.display = "none";
document.getElementById("detailbip38").style.display = "none";
},
enterOnPassphrase: function () {
var detailEncryptCheckbox = document.getElementById("detailbip38checkbox");
if (detailEncryptCheckbox.checked) {
detail.encryptBip38();
}
else {
detail.decryptBip38();
}
},
toggleEncrypt: function (element) {
// enable/disable passphrase textbox
var bip38CommandDisplay = document.getElementById("detailbip38commands").style.display;
var key = detail.getKeyFromInput();
if (element.checked == true) {
if (privateKey.isBIP38Format(key)) {
document.getElementById("detailbip38toggle").style.display = "none";
document.getElementById("detailbip38commands").style.display = "block";
document.getElementById("detailprivkeypassphrase").focus();
return;
}
else {
// show encrypt button
document.getElementById("detailbip38commands").style.display = "block";
document.getElementById("detailprivkeypassphrase").focus();
document.getElementById("detailbip38decryptspan").style.display = "none";
document.getElementById("detailbip38encryptspan").style.display = "inline-block";
}
}
else {
// show decrypt button
document.getElementById("detailbip38decryptspan").style.display = "inline-block";
document.getElementById("detailbip38encryptspan").style.display = "none";
document.getElementById("detailbip38commands").style.display = "none";
}
}
};
})(ninja.wallets, ninja.qrCode, ninja.privateKey, ninja.translator);