mirror of
https://github.com/haexhub/haex-hub.git
synced 2025-12-17 06:30:50 +01:00
encryption of sqlite working
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
//import Database from '@tauri-apps/plugin-sql';
|
||||
import { drizzle, SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
||||
import { drizzle, SqliteRemoteDatabase } from "drizzle-orm/sqlite-proxy";
|
||||
//import Database from "tauri-plugin-sql-api";
|
||||
import * as schema from '@/../src-tauri/database/schemas/vault';
|
||||
import * as schema from "@/../src-tauri/database/schemas/vault";
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { count } from 'drizzle-orm';
|
||||
import { platform } from '@tauri-apps/plugin-os';
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { count } from "drizzle-orm";
|
||||
import { platform } from "@tauri-apps/plugin-os";
|
||||
|
||||
interface IVault {
|
||||
//database: Database;
|
||||
@ -18,24 +18,19 @@ interface IOpenVaults {
|
||||
[vaultPath: string]: IVault;
|
||||
}
|
||||
|
||||
export const useVaultStore = defineStore('vaultStore', () => {
|
||||
export const useVaultStore = defineStore("vaultStore", () => {
|
||||
const currentVaultId = computed<string | undefined>({
|
||||
get: () =>
|
||||
getSingleRouteParam(useRouter().currentRoute.value.params.vaultId),
|
||||
get: () => getSingleRouteParam(useRouter().currentRoute.value.params.vaultId),
|
||||
set: (newVaultId) => {
|
||||
useRouter().currentRoute.value.params.vaultId = newVaultId ?? '';
|
||||
useRouter().currentRoute.value.params.vaultId = newVaultId ?? "";
|
||||
},
|
||||
});
|
||||
|
||||
const read_only = computed<boolean>({
|
||||
get: () => {
|
||||
console.log(
|
||||
'query showSidebar',
|
||||
useRouter().currentRoute.value.query.readonly
|
||||
);
|
||||
console.log("query showSidebar", useRouter().currentRoute.value.query.readonly);
|
||||
return JSON.parse(
|
||||
getSingleRouteParam(useRouter().currentRoute.value.query.readonly) ||
|
||||
'false'
|
||||
getSingleRouteParam(useRouter().currentRoute.value.query.readonly) || "false"
|
||||
);
|
||||
},
|
||||
set: (readonly) => {
|
||||
@ -53,51 +48,32 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
|
||||
const currentVault = ref<IVault | undefined>();
|
||||
|
||||
/* computed(() => {
|
||||
console.log('compute currentVault', currentVaultId.value, openVaults.value);
|
||||
return openVaults.value?.[currentVaultId.value ?? ''];
|
||||
}); */
|
||||
|
||||
watch(
|
||||
currentVaultId,
|
||||
async () => {
|
||||
/* if (!openVaults.value?.[currentVaultId.value ?? '']) {
|
||||
console.log(
|
||||
'no vaultId',
|
||||
currentVault.value,
|
||||
openVaults.value?.[currentVaultId.value ?? '']
|
||||
);
|
||||
return await navigateTo(useLocalePath()({ name: 'vaultOpen' }));
|
||||
} else */
|
||||
currentVault.value = openVaults.value?.[currentVaultId.value ?? ''];
|
||||
currentVault.value = openVaults.value?.[currentVaultId.value ?? ""];
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
const openAsync = async ({
|
||||
path = '',
|
||||
password,
|
||||
}: {
|
||||
path: string;
|
||||
password: string;
|
||||
}) => {
|
||||
//const sqlitePath = path?.startsWith('sqlite:') ? path : `sqlite:${path}`;
|
||||
const openAsync = async ({ path = "", password }: { path: string; password: string }) => {
|
||||
const sqlitePath = path?.startsWith("sqlite:") ? path : `sqlite:${path}`;
|
||||
|
||||
console.log('try to open db', path, password);
|
||||
console.log("try to open db", path, password);
|
||||
|
||||
const result = await invoke<string>('open_encrypted_database', {
|
||||
const result = await invoke<string>("open_encrypted_database", {
|
||||
path,
|
||||
key: password,
|
||||
});
|
||||
|
||||
console.log('open vault from store', result);
|
||||
if (!(await testDatabaseReadAsync())) throw new Error('Passwort falsch');
|
||||
console.log("open vault from store", result);
|
||||
if (!(await testDatabaseReadAsync())) throw new Error("Passwort falsch");
|
||||
//const db = await Database.load(sqlitePath);
|
||||
|
||||
const vaultId = await getVaultIdAsync(path);
|
||||
const seperator = platform() === 'windows' ? '\\' : '/';
|
||||
const seperator = platform() === "windows" ? "\\" : "/";
|
||||
const fileName = path.split(seperator).pop();
|
||||
console.log('opened db fileName', fileName, vaultId);
|
||||
console.log("opened db fileName", fileName, vaultId);
|
||||
|
||||
openVaults.value = {
|
||||
...openVaults.value,
|
||||
@ -113,14 +89,14 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
|
||||
// If the query is a SELECT, use the select method
|
||||
if (isSelectQuery(sql)) {
|
||||
rows = await invoke('db_select', { sql, params }).catch((e) => {
|
||||
console.error('SQL Error:', e);
|
||||
rows = await invoke("db_select", { sql, params }).catch((e) => {
|
||||
console.error("SQL Error:", e);
|
||||
return [];
|
||||
});
|
||||
} else {
|
||||
// Otherwise, use the execute method
|
||||
rows = await invoke('db_execute', { sql, params }).catch((e) => {
|
||||
console.error('SQL Error:', e);
|
||||
rows = await invoke("db_execute", { sql, params }).catch((e) => {
|
||||
console.error("SQL Error:", e);
|
||||
return [];
|
||||
});
|
||||
return { rows: [] };
|
||||
@ -131,7 +107,7 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
});
|
||||
|
||||
// If the method is "all", return all rows
|
||||
results = method === 'all' ? rows : rows[0];
|
||||
results = method === "all" ? rows : rows[0];
|
||||
|
||||
return { rows: results };
|
||||
},
|
||||
@ -147,11 +123,18 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
return vaultId;
|
||||
};
|
||||
|
||||
const createTable = () => {
|
||||
console.log("ddd", schema.testTable.getSQL().queryChunks);
|
||||
|
||||
schema.testTable.getSQL().queryChunks.forEach((chunk) => {
|
||||
const res = currentVault.value?.drizzle.run(chunk);
|
||||
console.log("create table", res);
|
||||
});
|
||||
};
|
||||
|
||||
const testDatabaseReadAsync = async () => {
|
||||
try {
|
||||
currentVault.value?.drizzle
|
||||
.select({ count: count() })
|
||||
.from(schema.haexExtensions);
|
||||
currentVault.value?.drizzle.select({ count: count() }).from(schema.haexExtensions);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
@ -159,19 +142,13 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
};
|
||||
|
||||
const refreshDatabaseAsync = async () => {
|
||||
console.log('refreshDatabaseAsync');
|
||||
console.log("refreshDatabaseAsync");
|
||||
/* if (!currentVault.value?.database.close) {
|
||||
return navigateTo(useLocaleRoute()({ name: 'vaultOpen' }));
|
||||
} */
|
||||
};
|
||||
|
||||
const createAsync = async ({
|
||||
path,
|
||||
password,
|
||||
}: {
|
||||
path: string;
|
||||
password: string;
|
||||
}) => {
|
||||
const createAsync = async ({ path, password }: { path: string; password: string }) => {
|
||||
/* const existDb = await exists('default.db', {
|
||||
baseDir: BaseDirectory.Resource,
|
||||
}); */
|
||||
@ -179,12 +156,12 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
/* const existDb = await resolveResource('resources/default.db');
|
||||
if (!existDb) throw new Error('Keine Datenbank da');
|
||||
await copyFile(existDb, path); */
|
||||
const result = await invoke('create_encrypted_database', {
|
||||
const result = await invoke("create_encrypted_database", {
|
||||
path,
|
||||
key: password,
|
||||
});
|
||||
console.log('create_encrypted_database', result);
|
||||
return 'aaaaa'; //await openAsync({ path, password });
|
||||
console.log("create_encrypted_database", result);
|
||||
return await openAsync({ path, password });
|
||||
};
|
||||
|
||||
const closeAsync = async () => {
|
||||
@ -209,6 +186,7 @@ export const useVaultStore = defineStore('vaultStore', () => {
|
||||
openVaults,
|
||||
refreshDatabaseAsync,
|
||||
read_only,
|
||||
createTable,
|
||||
};
|
||||
});
|
||||
|
||||
@ -216,12 +194,10 @@ const getVaultIdAsync = async (path: string) => {
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(path);
|
||||
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
||||
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
|
||||
const hashHex = hashArray
|
||||
.map((b) => b.toString(16).padStart(2, '0'))
|
||||
.join(''); // convert bytes to hex string
|
||||
console.log('vaultId', hashHex);
|
||||
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); // convert bytes to hex string
|
||||
console.log("vaultId", hashHex);
|
||||
return hashHex;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user