diff --git a/packages/auto-id/src/helper.ts b/packages/auto-id/src/helper.ts deleted file mode 100644 index 73df1873..00000000 --- a/packages/auto-id/src/helper.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Helper function to safely parse JSON with a fallback -export function tryParseJson(jsonString: string, fallback: string): string { - try { - return JSON.parse(jsonString); - } catch (e) { - return fallback; // Return the original string if it's not JSON - } -} \ No newline at end of file diff --git a/packages/auto-id/src/keyManagement.ts b/packages/auto-id/src/keyManagement.ts index 8e394d9c..391f2618 100644 --- a/packages/auto-id/src/keyManagement.ts +++ b/packages/auto-id/src/keyManagement.ts @@ -1,7 +1,5 @@ -import { save } from '@autonomys/auto-utils' +import { read, save } from '@autonomys/auto-utils' import { KeyObject, createPrivateKey, createPublicKey, generateKeyPairSync } from 'crypto' -import { promises as fs } from 'fs' -import { tryParseJson } from './helper' /** * Generates an RSA key pair. @@ -167,9 +165,7 @@ export function pemToPrivateKey(pemData: string, password?: string): KeyObject { */ export async function loadPrivateKey(filePath: string, password?: string): Promise { try { - let keyData = await fs.readFile(filePath, {encoding: 'utf-8'}) - // Check if keyData is JSON-encoded and parse it - keyData = tryParseJson(keyData, keyData) // Fallback to original data if not JSON + const keyData = await read(filePath) const privateKey = pemToPrivateKey(keyData, password) return privateKey; } catch (error: any) { @@ -215,10 +211,8 @@ export function pemToPublicKey(pemData: string): KeyObject { */ export async function loadPublicKey(filePath: string): Promise { try { - let keyData = await fs.readFile(filePath, { encoding: 'utf8' }) - // Check if keyData is JSON-encoded and parse it - keyData = tryParseJson(keyData, keyData); // Fallback to original data if not JSON - const publicKey = pemToPublicKey(keyData); + const keyData = await read(filePath) + const publicKey = pemToPublicKey(keyData) return publicKey } catch (error: any) { throw new Error(`Failed to load public key: ${error.message}`) diff --git a/packages/auto-utils/src/index.ts b/packages/auto-utils/src/index.ts index 5f9ea5e0..d38eecfc 100644 --- a/packages/auto-utils/src/index.ts +++ b/packages/auto-utils/src/index.ts @@ -1,3 +1,4 @@ export * from './api' export * from './network' +export * from './read' export * from './save' diff --git a/packages/auto-utils/src/read.ts b/packages/auto-utils/src/read.ts new file mode 100644 index 00000000..fd5a7e1e --- /dev/null +++ b/packages/auto-utils/src/read.ts @@ -0,0 +1,30 @@ +export const read = async (key: string) => { + // detect if we are in the browser or in node + if (typeof window !== 'undefined') return readFromLocalStorage(key) + else return readFromFileSystem(key) +} + +export const readFromLocalStorage = async (key: string) => { + if (typeof window !== 'undefined') { + // read from local storage + const value = localStorage.getItem(key) + try { + return value ? JSON.parse(value) : null + } catch (error) { + throw new Error('Failed to parse data from localStorage: ' + error) + } + } else throw new Error('This function can only be used in the browser') +} + +export const readFromFileSystem = async (key: string) => { + if (typeof window === 'undefined') { + // read from file system + const fs = await import('fs/promises') + try { + const data = await fs.readFile(key, { encoding: 'utf-8' }) + return JSON.parse(data); + } catch (error) { + throw new Error('Failed to read or parse file: ' + error) + } + } else throw new Error('This function can only be used in node') +} \ No newline at end of file diff --git a/packages/auto-utils/src/save.ts b/packages/auto-utils/src/save.ts index 48b454cc..5fa575a6 100644 --- a/packages/auto-utils/src/save.ts +++ b/packages/auto-utils/src/save.ts @@ -19,4 +19,4 @@ export const saveOnFileSystem = async (key: string, value: any) => { const data = typeof value === 'string' ? value : JSON.stringify(value); await fs.writeFile(key, JSON.stringify(data)) } else throw new Error('This function can only be used in node') -} +} \ No newline at end of file