Skip to content

Commit

Permalink
Merge pull request #15 from vault12/BETA-1198
Browse files Browse the repository at this point in the history
add multiple set get and remove methods to crypto storage
  • Loading branch information
lomchik committed May 12, 2022
2 parents 709b1fd + 666207a commit 9fc9d15
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glow.ts",
"version": "1.0.1",
"version": "1.0.2",
"displayName": "Glow.ts",
"description": "Client library for interacting with Zax Cryptographic Relay Servers",
"keywords": [
Expand Down
12 changes: 6 additions & 6 deletions src/crypto-storage/crypto-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ export class CryptoStorage {
const nonce = await this.nacl.crypto_secretbox_random_nonce();
const cipherText = await this.nacl.crypto_secretbox(encoded, nonce, this.storageKey);
// Save the cipher text and nonce
await this.driver.set(this.addPrefix(tag), Utils.toBase64(cipherText));
await this.driver.set(this.addNonceTag(tag), Utils.toBase64(nonce));
await this.driver.setMultiple({
[this.addPrefix(tag)]: Utils.toBase64(cipherText),
[this.addNonceTag(tag)]: Utils.toBase64(nonce)
});
return true;
}

Expand All @@ -88,8 +90,7 @@ export class CryptoStorage {
throw new Error('[CryptoStorage] Storage key is not set');
}
// Get cipher text and nonce from the storage
const data = await this.driver.get(this.addPrefix(tag));
const nonce = await this.driver.get(this.addNonceTag(tag));
const [data, nonce] = await this.driver.getMultiple([this.addPrefix(tag), this.addNonceTag(tag)]);
// Nothing to do without cipher text or nonce
if (!data || !nonce) {
return null;
Expand All @@ -109,8 +110,7 @@ export class CryptoStorage {
if (!this.driver) {
throw new Error('[CryptoStorage] Storage driver is not set');
}
await this.driver.remove(this.addPrefix(tag));
await this.driver.remove(this.addNonceTag(tag));
await this.driver.removeMultiple([this.addPrefix(tag), this.addNonceTag(tag)]);
return true;
}

Expand Down
33 changes: 25 additions & 8 deletions src/crypto-storage/in-memory-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@ import { StorageDriver } from './storage-driver.interface';
* temporary created in-memory storage for testing purposes
*/
export class InMemoryStorage implements StorageDriver {
private storage: {[key: string]: any} = {};
get(key: string) {
return Promise.resolve(this.storage[key]);

private storage: {[key: string]: string|null} = {};

async get(key: string) {
return (await this.getMultiple([key]))[0];
}

async getMultiple(keys: string[]) {
return keys.map(key => this.storage[key]);
}
set (key: string, value: any) {
this.storage[key] = value;
return Promise.resolve();

set(key: string, value: any) {
return this.setMultiple({[key]: value});
}

async setMultiple(values: { [key: string]: string | null; }){
for (const key in values) {
this.storage[key] = values[key];
}
}

remove(key: string) {
delete this.storage[key];
return Promise.resolve();
return this.removeMultiple([key]);
}

async removeMultiple(keys: string[]) {
keys.forEach(key => delete this.storage[key]);
}

reset() {
this.storage = {};
}
Expand Down
24 changes: 19 additions & 5 deletions src/crypto-storage/local-storage.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@ export class LocalStorageDriver implements StorageDriver {
}

async get(key: string): Promise<string | null> {
const item = localStorage.getItem(this.tag(key));
return item ? item : null;
return (await this.getMultiple([key]))[0];
}

async getMultiple(keys: string[]): Promise<(string | null)[]> {
return keys.map(key => localStorage.getItem(this.tag(key)) || null);
}

async set(key: string, value: string | null): Promise<void> {
if (value !== null) {
localStorage.setItem(this.tag(key), value);
return this.setMultiple({ [key]: value });
}

async setMultiple(values: { [key: string]: string | null; }): Promise<void> {
for(const key in values) {
const value = values[key];
if (value !== null) {
localStorage.setItem(this.tag(key), value);
}
}
}

async remove(key: string): Promise<void> {
localStorage.removeItem(this.tag(key));
this.removeMultiple([key]);
}

async removeMultiple(keys: string[]): Promise<void> {
keys.forEach(key => localStorage.removeItem(this.tag(key)));
}

private tag(key: string) {
Expand Down
3 changes: 3 additions & 0 deletions src/crypto-storage/storage-driver.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export interface StorageDriver {
get(key: string): Promise<string | null>;
getMultiple(keys: string[]): Promise<(string | null)[]>;
set(key: string, value: string | null): Promise<void>;
setMultiple(values: {[key: string]: string | null}): Promise<void>;
remove(key: string): Promise<void>;
removeMultiple(keys: string[]): Promise<void>;
}
15 changes: 8 additions & 7 deletions src/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ export class KeyRing {
static async new(id: string): Promise<KeyRing> {
const nacl = NaCl.getInstance();
const cryptoStorage = await CryptoStorage.new(id);
const commKey = await KeyRing.getCommKey(nacl, cryptoStorage);
const commKey = await KeyRing.getCommKeyOrCreate(nacl, cryptoStorage);
const keyRing = new KeyRing(nacl, cryptoStorage, commKey);

await cryptoStorage.save(KeyRing.commKeyTag, commKey.toString());
await keyRing.loadGuestKeys();
return keyRing;
}
Expand Down Expand Up @@ -163,13 +162,15 @@ export class KeyRing {
await this.storage.save(KeyRing.guestRegistryTag, Utils.toObject(this.guestKeys.entries()));
}

private static async getCommKey(nacl: NaClDriver, storage: CryptoStorage): Promise<Keys> {
const commKey = await storage.get(KeyRing.commKeyTag);
if (commKey && typeof commKey === 'string') {
return new Keys(commKey);
private static async getCommKeyOrCreate(nacl: NaClDriver, storage: CryptoStorage): Promise<Keys> {
const commKeyRecord = await storage.get(KeyRing.commKeyTag);
if (commKeyRecord && typeof commKeyRecord === 'string') {
return new Keys(commKeyRecord);
} else {
const keypair = await nacl.crypto_box_keypair();
return new Keys(keypair);
const commKey = new Keys(keypair);
await storage.save(KeyRing.commKeyTag, commKey.toString());
return commKey;
}
}

Expand Down

0 comments on commit 9fc9d15

Please sign in to comment.