Skip to content

Commit f7c1adc

Browse files
committed
Modify load key functions considering the dynamic read function added in auto-utils
1 parent 1d9cf2f commit f7c1adc

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

Diff for: packages/auto-id/src/helper.ts

-8
This file was deleted.

Diff for: packages/auto-id/src/keyManagement.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { save } from '@autonomys/auto-utils'
1+
import { read, save } from '@autonomys/auto-utils'
22
import { KeyObject, createPrivateKey, createPublicKey, generateKeyPairSync } from 'crypto'
3-
import { promises as fs } from 'fs'
4-
import { tryParseJson } from './helper'
53

64
/**
75
* Generates an RSA key pair.
@@ -167,9 +165,7 @@ export function pemToPrivateKey(pemData: string, password?: string): KeyObject {
167165
*/
168166
export async function loadPrivateKey(filePath: string, password?: string): Promise<KeyObject> {
169167
try {
170-
let keyData = await fs.readFile(filePath, {encoding: 'utf-8'})
171-
// Check if keyData is JSON-encoded and parse it
172-
keyData = tryParseJson(keyData, keyData) // Fallback to original data if not JSON
168+
const keyData = await read(filePath)
173169
const privateKey = pemToPrivateKey(keyData, password)
174170
return privateKey;
175171
} catch (error: any) {
@@ -215,10 +211,8 @@ export function pemToPublicKey(pemData: string): KeyObject {
215211
*/
216212
export async function loadPublicKey(filePath: string): Promise<KeyObject> {
217213
try {
218-
let keyData = await fs.readFile(filePath, { encoding: 'utf8' })
219-
// Check if keyData is JSON-encoded and parse it
220-
keyData = tryParseJson(keyData, keyData); // Fallback to original data if not JSON
221-
const publicKey = pemToPublicKey(keyData);
214+
const keyData = await read(filePath)
215+
const publicKey = pemToPublicKey(keyData)
222216
return publicKey
223217
} catch (error: any) {
224218
throw new Error(`Failed to load public key: ${error.message}`)

Diff for: packages/auto-utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './api'
22
export * from './network'
3+
export * from './read'
34
export * from './save'

Diff for: packages/auto-utils/src/read.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const read = async (key: string) => {
2+
// detect if we are in the browser or in node
3+
if (typeof window !== 'undefined') return readFromLocalStorage(key)
4+
else return readFromFileSystem(key)
5+
}
6+
7+
export const readFromLocalStorage = async (key: string) => {
8+
if (typeof window !== 'undefined') {
9+
// read from local storage
10+
const value = localStorage.getItem(key)
11+
try {
12+
return value ? JSON.parse(value) : null
13+
} catch (error) {
14+
throw new Error('Failed to parse data from localStorage: ' + error)
15+
}
16+
} else throw new Error('This function can only be used in the browser')
17+
}
18+
19+
export const readFromFileSystem = async (key: string) => {
20+
if (typeof window === 'undefined') {
21+
// read from file system
22+
const fs = await import('fs/promises')
23+
try {
24+
const data = await fs.readFile(key, { encoding: 'utf-8' })
25+
return JSON.parse(data);
26+
} catch (error) {
27+
throw new Error('Failed to read or parse file: ' + error)
28+
}
29+
} else throw new Error('This function can only be used in node')
30+
}

Diff for: packages/auto-utils/src/save.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export const saveOnFileSystem = async (key: string, value: any) => {
1919
const data = typeof value === 'string' ? value : JSON.stringify(value);
2020
await fs.writeFile(key, JSON.stringify(data))
2121
} else throw new Error('This function can only be used in node')
22-
}
22+
}

0 commit comments

Comments
 (0)