Skip to content

Commit

Permalink
Modify load key functions considering the dynamic read function added…
Browse files Browse the repository at this point in the history
… in auto-utils
  • Loading branch information
abhi3700 committed Jun 5, 2024
1 parent 1d9cf2f commit f7c1adc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
8 changes: 0 additions & 8 deletions packages/auto-id/src/helper.ts

This file was deleted.

14 changes: 4 additions & 10 deletions packages/auto-id/src/keyManagement.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -167,9 +165,7 @@ export function pemToPrivateKey(pemData: string, password?: string): KeyObject {
*/
export async function loadPrivateKey(filePath: string, password?: string): Promise<KeyObject> {
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) {
Expand Down Expand Up @@ -215,10 +211,8 @@ export function pemToPublicKey(pemData: string): KeyObject {
*/
export async function loadPublicKey(filePath: string): Promise<KeyObject> {
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}`)
Expand Down
1 change: 1 addition & 0 deletions packages/auto-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './api'
export * from './network'
export * from './read'
export * from './save'
30 changes: 30 additions & 0 deletions packages/auto-utils/src/read.ts
Original file line number Diff line number Diff line change
@@ -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')
}
2 changes: 1 addition & 1 deletion packages/auto-utils/src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}

0 comments on commit f7c1adc

Please sign in to comment.