From 80dc42bbe616c2654b5d1c8b80a4b84eabcbc155 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 3 Sep 2025 15:10:42 +0200 Subject: [PATCH] Replace blobs with buffers --- src/service.ts | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/service.ts b/src/service.ts index 08743a0..e188067 100644 --- a/src/service.ts +++ b/src/service.ts @@ -985,31 +985,31 @@ export class Service { } // Blob and data storage methods - async saveBlobToDb(hash: string, data: Blob) { + async saveBufferToDb(hash: string, data: Buffer) { const db = await Database.getInstance(); try { await db.addObject({ storeName: 'data', object: data, key: hash, - }); + }, true); } catch (e) { console.error(`Failed to save data to db: ${e}`); } } - async getBlobFromDb(hash: string): Promise { + async getBufferFromDb(hash: string): Promise { const db = await Database.getInstance(); try { - return await db.getObject('data', hash); + return await db.getObject('data', hash, true); } catch (e) { return null; } } - async saveDataToStorage(hash: string, data: Blob, ttl: number | null) { + async saveDataToStorage(hash: string, data: Buffer, ttl: number | null) { console.log('💾 Saving data to storage:', hash); - // TODO: Implement actual storage service + console.debug('TODO'); // const storages = [STORAGEURL]; // try { // await storeData(storages, hash, data, ttl); @@ -1050,6 +1050,10 @@ export class Service { return uint8Array; } + hexToBuffer(hexString: string): Buffer { + return Buffer.from(this.hexToUInt8Array(hexString)); + } + public async handleApiReturn(apiReturn: ApiReturn) { // Check for errors in the returned objects if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.error) { @@ -1114,9 +1118,9 @@ export class Service { if (updatedProcess.encrypted_data && Object.keys(updatedProcess.encrypted_data).length != 0) { for (const [hash, cipher] of Object.entries(updatedProcess.encrypted_data)) { - const blob = this.hexToBlob(cipher); + const buffer = this.hexToBuffer(cipher); try { - await this.saveBlobToDb(hash, blob); + await this.saveBufferToDb(hash, buffer); } catch (e) { console.error(e); } @@ -1137,9 +1141,9 @@ export class Service { if (apiReturn.push_to_storage && apiReturn.push_to_storage.length != 0) { for (const hash of apiReturn.push_to_storage) { - const blob = await this.getBlobFromDb(hash); - if (blob) { - await this.saveDataToStorage(hash, blob, null); + const buffer = await this.getBufferFromDb(hash); + if (buffer) { + await this.saveDataToStorage(hash, buffer, null); } else { console.error('Failed to get data from db'); } @@ -1240,11 +1244,10 @@ export class Service { } if (hash && key) { - const blob = await this.getBlobFromDb(hash); - if (blob) { + const buffer = await this.getBufferFromDb(hash); + if (buffer) { // Decrypt the data - const buf = await blob.arrayBuffer(); - const cipher = new Uint8Array(buf); + const cipher = new Uint8Array(buffer); const keyUIntArray = this.hexToUInt8Array(key);