Просмотр исходного кода

Encrypt/decrypt fully working now.

Lee Morgan 11 месяцев назад
Родитель
Сommit
7fb9416071

+ 1 - 1
src/models/account.rs

@@ -95,7 +95,7 @@ impl Account {
         ResponseAccount {
             id: self.id.to_string(),
             data: self.data.clone(),
-            iv: self.data.clone(),
+            iv: self.iv.clone(),
             created_at: self.created_at.timestamp_millis()
         }
     }

+ 8 - 6
src/views/js/EncryptionHandler.js

@@ -104,8 +104,8 @@ export default class EncryptionHandler {
 
     async encrypt(data, iv){
         const json = JSON.stringify(data);
-        const buff = new TextEncoder().encode(data);
-        const buffIv = new TextEncoder().encode(iv);
+        const buff = new TextEncoder().encode(json);
+        const buffIv = this.stringToBuffer(iv);
 
         const encrypted = await crypto.subtle.encrypt(
             {
@@ -116,10 +116,11 @@ export default class EncryptionHandler {
             buff
         );
 
-        return btoa(String.fromCharCode(...new Uint8Array(encrypted)));
+        let response = btoa(String.fromCharCode(...new Uint8Array(encrypted)));
+        return response;
     }
 
-    async decrypt(data, key, ivString){
+    async decrypt(data, ivString){
         const encrypted = this.stringToBuffer(data);
         const iv = this.stringToBuffer(ivString);
 
@@ -128,11 +129,12 @@ export default class EncryptionHandler {
                 name: "AES-GCM",
                 iv,
             },
-            key,
+            this._key,
             encrypted
         );
 
         const json = new TextDecoder().decode(decrypted);
-        return JSON.parse(json);
+        let response = JSON.parse(json);
+        return response;
     }
 }

+ 2 - 6
src/views/js/data/Account.js

@@ -1,17 +1,13 @@
 import EncryptionHandler from "../EncryptionHandler.js";
 
 export default class Account{
-    constructor(parent, id, data){
-        this._parent = parent;
+    constructor(id, data){
         this._id = id;
+        this._name = data.name;
         this._balance = data.balance;
         this._income = data.income;
         this._bills = data.bills;
         this._allowances = data.allowances;
         this._transactions = [];
     }
-
-    static async decryptAndCreate(parent, id, data){
-        
-    }
 }

+ 11 - 0
src/views/js/data/User.js

@@ -1,4 +1,5 @@
 import EncryptionHandler from "../EncryptionHandler.js";
+import Account from "./Account.js";
 
 export default class User{
     constructor(id, name, email, accounts = []){
@@ -20,4 +21,14 @@ export default class User{
             encryption_salt: EncryptionHandler.generateSalt()
         };
     }
+
+    addAccount(account){
+        this._accounts.push(account);
+    }
+
+    async decryptAndAddAccount(account){
+        const data = await encryptionHandler.decrypt(account.data, account.iv);
+
+        this._accounts.push(new Account(account.id, data));
+    }
 }

+ 11 - 4
src/views/js/index.js

@@ -36,14 +36,21 @@ fetch("/api/user", {
             response.id,
             response.name,
             response.email,
-            response.accounts
         );
 
-
-        return EncryptionHandler.getKeyFromStorage();
+        return Promise.all([EncryptionHandler.getKeyFromStorage(), response.accounts]);
     })
-    .then((key)=>{
+    .then(([key, accounts])=>{
         window.encryptionHandler = new EncryptionHandler(key);
+
+        let promises = [];
+        for(let i = 0; i < accounts.length; i++){
+            promises.push(user.decryptAndAddAccount(accounts[i]));
+        }
+
+        return Promise.all(promises);
+    })
+    .then((response)=>{
         currentPage = new Home();
     })
     .catch((err)=>{

+ 30 - 1
src/views/js/pages/CreateAccount.js

@@ -1,5 +1,7 @@
 import Page from "./Page.js";
 import Elem from "../Elem.js";
+import Notifier from "../Notifier.js";
+import Account from "../data/Account.js";
 
 export default class CreateAccount extends Page{
     constructor(){
@@ -13,10 +15,37 @@ export default class CreateAccount extends Page{
 
         const data = {
             name: this.container.querySelector(".name").value,
-            balance: this.container.querySelector(".balance").value
+            balance: this.container.querySelector(".balance").value,
+            income: [],
+            bills: [],
+            allowances: []
         };
         const iv = encryptionHandler.generateIv();
         const encryptedData = await encryptionHandler.encrypt(data, iv);
+
+        fetch("/api/account", {
+            method: "POST",
+            headers: {"Content-Type": "application/json"},
+            body: JSON.stringify({
+                data: encryptedData,
+                iv: iv
+            })
+        })
+            .then(r=>r.json())
+            .then((response)=>{
+                if(response.error) throw response;
+
+                let account = new Account(response.id, data);
+                user.addAccount(account);
+                changePage("home");
+            })
+            .catch((err)=>{
+                if(err.error){
+                    new Notifier("error", err.error.message);
+                }else{
+                    new Notifier("error", "Something went wrong, try refreshing the page");
+                }
+            });
     }
 
     render(){