Skip to content

Commit

Permalink
Unify the methods to save and load session keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviatkov committed May 29, 2023
1 parent 04b47ef commit e5eab2d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 54 deletions.
4 changes: 2 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {getRandomBytesSync} from "ethereum-cryptography/random";

async function example(): Promise<void> {
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
Expand Down
45 changes: 7 additions & 38 deletions src/key-pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<void> {
await storage.save(StorageKeys.SESSION_KEY, this.privateKey);
public async save(): Promise<void> {
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<void> {
await this.saveToStorage(KeyPair.localStorage);
}

/**
* Save the generated key to the json file in the local file system
*/
public async saveToFile(): Promise<void> {
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<KeyPair | null> {
const privateKey = await storage.get(StorageKeys.SESSION_KEY);
public static async load(): Promise<KeyPair | null> {
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<KeyPair | null> {
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<KeyPair | null> {
return KeyPair.loadFromStorage(KeyPair.fileStorage);
}
}
17 changes: 4 additions & 13 deletions src/openfort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,13 @@ export default class Openfort {
return this._keyPair;
}

public async loadSessionKeyFromFile(): Promise<KeyPair | null> {
this._keyPair = await KeyPair.loadFromFile();
public async loadSessionKey(): Promise<KeyPair | null> {
this._keyPair = await KeyPair.load();
return this._keyPair;
}

public async loadSessionKeyFromLocalStorage(): Promise<KeyPair | null> {
this._keyPair = await KeyPair.loadFromLocalStorage();
return this._keyPair;
}

public async saveSessionKeyToFile(): Promise<void> {
return this.keyPair.saveToFile();
}

public async saveSessionKeyToLocalStorage(): Promise<void> {
return this.keyPair.saveToLocalStorage();
public async saveSessionKey(): Promise<void> {
return this.keyPair.save();
}

public signMessage(message: Bytes | string): string {
Expand Down
6 changes: 5 additions & 1 deletion src/storage/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit e5eab2d

Please sign in to comment.