-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
395 lines (356 loc) · 11.9 KB
/
popup.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
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
import { ethers } from "./ethers.min.js";
import Contract from './contracts/SecureVault.json' assert { type: "json" };
// Initialize butotn with users's prefered color
let changeColor = document.getElementById("changeColor");
let app = document.getElementById("app")
const CONTRACT_ADDRESS = '0x6872967283CB2e20C6aB554EAdf8Db31A819320f'
const RPC_URL = 'https://rpc-mumbai.maticvigil.com'
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const contract = new ethers.Contract(CONTRACT_ADDRESS, Contract, provider);
const historyPage = ["home"];
const account = null;
chrome.storage.sync.get("account", function (result) {
if (!result.account) {
var account = ethers.Wallet.createRandom()
chrome.storage.sync.set({
"account": {
"privateKey": account.privateKey,
"address": account.address
}
}, function () { })
}
account = result.account;
})
chrome.storage.local.get("creditentals", function (result) {
if (result.creditentals) {
var creditentals = result.creditentals;
if (Object.keys(creditentals).length > 1) {
console.log(creditentals)
historyPage.push('add_details')
render()
return
}
}
render()
})
async function getCreditentials({ url, account }) {
if (account) {
var privateKey = account.privateKey;
let wallet = new ethers.Wallet(privateKey, provider);
let contractWithSigner = contract.connect(wallet);
var data = await contractWithSigner.getAll()
console.log(data)
var parsedData = [];
for (const element in data) {
const elementData = data[element];
console.log(elementData, account)
var decryptedPass = decryptText({
account: account,
encryptHex: elementData.password
})
parsedData.push({ ...elementData, password: decryptedPass })
}
return parsedData
}
return [];
}
function getAESInstance(account) {
var instance = new aesjs.ModeOfOperation.ctr(aesjs.utils.utf8.toBytes(account.privateKey.substring(0, 16)), new aesjs.Counter(0));
console.log(instance)
return instance
}
function encryptText({ account, text }) {
console.log(account)
if (account) {
var inBytes = aesjs.utils.utf8.toBytes(text);
console.log('bytes : ', inBytes);
var encryptedBytes = getAESInstance(account).encrypt(inBytes);
console.log('encrypted bytes : ', encryptedBytes);
var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
console.log('encrypted hex : ', encryptedHex);
return encryptedHex;
}
return '';
}
function decryptText({ account, encryptHex }) {
console.log(account, encryptHex)
var encryptedBytes = aesjs.utils.hex.toBytes(encryptHex);
var decryptedBytes = getAESInstance(account).decrypt(encryptedBytes);
console.log('decrypted bytes : ', decryptedBytes);
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
console.log('decrypted text : ', decryptedText);
return decryptedText;
}
async function setCreditentials({ url, username, password }) {
console.log(url, username, password)
chrome.storage.sync.get("account", async function (result) {
if (result) {
var encrpytPass = encryptText({
account: result.account,
text: password
})
console.log(encrpytPass)
var account = result.account;
var privateKey = account.privateKey;
let wallet = new ethers.Wallet(privateKey, provider);
let contractWithSigner = contract.connect(wallet);
contractWithSigner.set(url, username, encrpytPass).catch(console.log)
chrome.storage.local.set({ 'creditentals': null }, function () {
historyPage.pop()
render()
});
}
});
}
function updateBalance() {
chrome.storage.sync.get("account", function (result) {
if (result) {
var account = result.account;
var privateKey = account.privateKey;
let wallet = new ethers.Wallet(privateKey, provider);
wallet.getBalance().then((balance) => {
var coins = document.getElementsByClassName("balance-text")[0];
var data = (BigNumber(balance).toString())
console.log(data)
coins.innerText = ethers.utils.formatEther(balance).substring(0, 5) + " MATIC"
})
}
});
}
function loading() {
var loading = document.createElement("div");
loading.classList.add("loading");
loading.innerHTML = "<div>Loading...</div>";
app.appendChild(loading);
}
async function home() {
loading()
chrome.storage.sync.get("account", async function (result) {
if (result) {
let passwords = await getCreditentials({
account: result.account,
})
console.log(passwords)
updateBalance()
let coins = 0
function navbar() {
let navbar = document.createElement("div");
navbar.classList.add("navbar");
let back = document.createElement("div");
back.innerHTML = "<img src='icons/back.svg' style='object-fit: cover;height:10px;width: 20px;'/>Back";
back.style.opacity = '0';
let title = document.createElement("div");
title.innerText = coins + " MATIC";
title.classList.add('balance-text');
let addFunds = document.createElement("div");
addFunds.innerText = "Add Funds";
addFunds.classList.add("navbar-button");
addFunds.addEventListener("click", () => {
// navigate to add funds page
historyPage.push("add_fund")
render()
});
navbar.append(back, title, addFunds);
app.appendChild(navbar);
}
function content() {
let content = document.createElement('div');
content.classList.add("content");
if (passwords.length == 0) {
let text = document.createElement("h2")
text.classList.add("heading")
text.innerText = "No Passwords"
let button = document.createElement("div")
button.classList.add("primary-button")
button.innerText = " + "
button.addEventListener("click", () => {
// navigate to add password page
historyPage.push("add_details")
render()
})
content.append(text, button)
}
passwords.forEach(element => {
createPasswordTile(content, element)
});
// createPasswords(content)
app.appendChild(content);
}
function action() {
let action = document.createElement('div');
action.classList.add("action");
if (passwords.length != 0) {
let button = document.createElement("div");
button.classList.add("action-button");
button.innerText = "Add More";
button.addEventListener("click", () => {
// navigate to add password page
historyPage.push("add_details")
render()
})
action.appendChild(button);
}
app.appendChild(action);
}
app.innerHTML = "";
navbar()
content()
action()
}
});
}
function add_fund_view() {
function navbar() {
let navbar = document.createElement("div");
navbar.classList.add("navbar");
let back = document.createElement("span");
back.innerHTML = "<img src='icons/back.svg' style='object-fit: cover;height:10px;width: 20px;'/>Back";
back.addEventListener("click", () => {
historyPage.pop()
render()
});
let title = document.createElement("div");
var addFunds = document.createElement("div");
navbar.append(back, title, addFunds);
app.appendChild(navbar);
}
function content() {
let content = document.createElement('div');
content.classList.add("content");
let logo = document.createElement("img")
logo.src = "https://cryptologos.cc/logos/polygon-matic-logo.svg?v=023"
logo.classList.add("logo")
let text = document.createElement("h1")
text.classList.add("heading")
text.innerText = "Add Some MATIC to"
let _account = document.createElement("div")
_account.classList.add("account")
console.log(account)
chrome.storage.sync.get("account", function (result) {
if (result) {
_account.innerText = result.account.address.substring(0, 10) + "..." + result.account.address.substring(result.account.address.length - 4,);
_account.addEventListener("click", () => {
// copy to clipboard
navigator.clipboard.writeText(result.account.address)
})
}
});
let button = document.createElement("div")
button.classList.add("action-button")
button.innerText = "Add Funds"
button.addEventListener("click", () => {
// open metamask
window.open("https://wallet.matic.network/")
})
content.append(logo, text, _account, button)
app.appendChild(content);
}
app.innerHTML = "";
navbar()
content()
}
async function autoFillField() {
chrome.storage.local.get("creditentals", function (result) {
if (result.creditentals) {
let creditentals = result.creditentals;
let username = document.getElementById("uname")
let password = document.getElementById("pass")
console.log(creditentals)
username.value = creditentals['0']
password.value = creditentals['1']
}
});
}
function add_details() {
updateBalance()
autoFillField()
function navbar() {
let navbar = document.createElement("div");
navbar.classList.add("navbar");
let back = document.createElement("div");
back.innerHTML = "<img src='icons/back.svg' style='object-fit: cover;height:10px;width: 20px;'/>Back";
back.addEventListener("click", () => {
historyPage.pop()
render()
});
let title = document.createElement("div");
title.innerText = "0 MATIC";
title.classList.add('balance-text');
let addFunds = document.createElement("div");
navbar.append(back, title, addFunds);
app.appendChild(navbar);
}
function content() {
let content = document.createElement('form');
content.classList.add("content");
let userinput = document.createElement("input")
userinput.classList.add("user-input")
userinput.id = "uname"
userinput.type = "text"
userinput.placeholder = "Enter Email"
let passinput = document.createElement("input")
passinput.classList.add("user-input")
passinput.id = "pass"
passinput.type = "password"
passinput.placeholder = "Enter Password"
let button = document.createElement("div")
button.classList.add("action-button")
button.innerText = "Add"
button.addEventListener("click", () => {
let username = document.getElementById("uname")
let password = document.getElementById("pass")
//get url from current tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let url = tabs[0].url;
setCreditentials({
url: url,
username: username.value,
password: password.value
})
});
})
content.append(userinput, passinput, button)
app.appendChild(content);
}
navbar()
content()
}
function render() {
app.innerHTML = "";
switch (historyPage[historyPage.length - 1]) {
case "home":
home()
break;
case "add_fund":
add_fund_view()
break;
case "add_details":
add_details()
break;
default:
break;
}
}
function createPasswordTile(content, data) {
let tile = document.createElement("div");
tile.classList.add("tile");
let title = document.createElement("div");
title.classList.add("tile-title");
title.innerText = data.username;
let button = document.createElement("div");
button.classList.add("tile-button");
button.innerText = "Next";
button.addEventListener("click", () => {
// navigate to add password page
chrome.tabs.query({ 'active': true, 'lastFocusedWindow': true }, function (tabs) {
// request content_script to retrieve title element innerHTML from current tab
chrome.tabs.sendMessage(tabs[0].id, {
username: data.username,
password: data.password,
}, null, function (obj) {
});
});
});
tile.append(title, button);
content.appendChild(tile);
}