diff --git a/example/index.ts b/example/index.ts index 14af47d..d38a667 100644 --- a/example/index.ts +++ b/example/index.ts @@ -3,10 +3,10 @@ import {getRandomBytesSync} from "ethereum-cryptography/random"; async function example(): Promise { const openfort = new Openfort("pk_test_a6508438-48d2-4af9-a557-51b638800a14", "http://localhost:3002"); - if (!(await openfort.loadSessionKeyFromFile())) { + if (!(await openfort.loadSessionKey())) { openfort.createSessionKey(); // TODO: call server to authenticate user and register session - await openfort.saveSessionKeyToFile(); + await openfort.saveSessionKey(); } // TODO: replace the message diff --git a/src/key-pair.ts b/src/key-pair.ts index deebe3a..315adbb 100644 --- a/src/key-pair.ts +++ b/src/key-pair.ts @@ -8,8 +8,7 @@ import {arrayify, Bytes, BytesLike, joinSignature} from "@ethersproject/bytes"; import {hashMessage} from "@ethersproject/hash"; export class KeyPair extends SigningKey { - private static readonly localStorage = new LocalStorage(); - private static readonly fileStorage = new FileStorage(); + private static readonly storage = LocalStorage.isAvailable ? new LocalStorage() : new FileStorage(); /** * Initialize keypair based on the private key, if it is provided or generate a brand new keypair. @@ -28,47 +27,17 @@ export class KeyPair extends SigningKey { } /** - * Save to the storage provided in the parameter of the function - * @param storage The implementation of BaseStorage interface to store and load + * Save to the storage initialized as a static property of the KeyPair class */ - public async saveToStorage(storage: BaseStorage): Promise { - await storage.save(StorageKeys.SESSION_KEY, this.privateKey); + public async save(): Promise { + await KeyPair.storage.save(StorageKeys.SESSION_KEY, this.privateKey); } /** - * Save the generated key to the local storage + * Load private key from the storage and generate keypair based on it. */ - public async saveToLocalStorage(): Promise { - await this.saveToStorage(KeyPair.localStorage); - } - - /** - * Save the generated key to the json file in the local file system - */ - public async saveToFile(): Promise { - await this.saveToStorage(KeyPair.fileStorage); - } - - /** - * Load private key from the json file in the local file system and generate keypair based on it. - * @param storage The implementation of BaseStorage interface to store and load - */ - public static async loadFromStorage(storage: BaseStorage): Promise { - const privateKey = await storage.get(StorageKeys.SESSION_KEY); + public static async load(): Promise { + const privateKey = await KeyPair.storage.get(StorageKeys.SESSION_KEY); return privateKey ? new KeyPair(arrayify(privateKey)) : null; } - - /** - * Load private key from the local storage and generate keypair based on it. - */ - public static async loadFromLocalStorage(): Promise { - return KeyPair.loadFromStorage(KeyPair.localStorage); - } - - /** - * Load private key from the json file in the local file system and generate keypair based on it. - */ - public static async loadFromFile(): Promise { - return KeyPair.loadFromStorage(KeyPair.fileStorage); - } } diff --git a/src/openfort.ts b/src/openfort.ts index c569cad..df280bc 100644 --- a/src/openfort.ts +++ b/src/openfort.ts @@ -40,22 +40,13 @@ export default class Openfort { return this._keyPair; } - public async loadSessionKeyFromFile(): Promise { - this._keyPair = await KeyPair.loadFromFile(); + public async loadSessionKey(): Promise { + this._keyPair = await KeyPair.load(); return this._keyPair; } - public async loadSessionKeyFromLocalStorage(): Promise { - this._keyPair = await KeyPair.loadFromLocalStorage(); - return this._keyPair; - } - - public async saveSessionKeyToFile(): Promise { - return this.keyPair.saveToFile(); - } - - public async saveSessionKeyToLocalStorage(): Promise { - return this.keyPair.saveToLocalStorage(); + public async saveSessionKey(): Promise { + return this.keyPair.save(); } public signMessage(message: Bytes | string): string { diff --git a/src/storage/local-storage.ts b/src/storage/local-storage.ts index a143154..186a2cd 100644 --- a/src/storage/local-storage.ts +++ b/src/storage/local-storage.ts @@ -7,12 +7,16 @@ export class LocalStorage implements BaseStorage { public constructor(private readonly name?: string) {} + public static get isAvailable(): boolean { + return "localStorage" in global && !!global.localStorage; + } + private formatKey(key: StorageKeys): string { return [LocalStorage._prefix, this.name, key].filter((n) => n).join(LocalStorage._separator); } private static get localStorage(): LocalStorageInterface { - if ("localStorage" in global && global.localStorage) { + if (LocalStorage.isAvailable) { return global.localStorage as LocalStorageInterface; } throw Error("Local storage is not available in the current context");