Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
*  Added new function getKeys()

* Changed loadPrivatekey and savePrivatekey methods to static functions

* Updated README.md and package.json version

* Updated localforage

* Added setConfig and getConfig
  • Loading branch information
Alex99y authored Oct 9, 2020
1 parent c861c77 commit 041ecc1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 85 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ const password = "secret-password";
const itemKey = "private_key";
const privateData = "private key information";
(async () => {
const appStorage = new AppStorage(obfuscatekey);
const data = new Uint8Array(Buffer.from(privateData));
await appStorage.savePrivatekeyToStorage(itemKey, password, data);
await AppStorage.savePrivatekeyToStorage(itemKey, password, data);
})().catch(e => {
console.log(e);
});
Expand All @@ -119,8 +118,7 @@ const password = "secret-password";
const itemKey = "private_key";

(async () => {
const appStorage = new AppStorage(obfuscatekey);
const data = await appStorage.loadPrivatekeyFromStorage(itemKey, password, data);
const data = await AppStorage.loadPrivatekeyFromStorage(itemKey, password, data);
console.log(Buffer.from(data).toString());
})().catch(e => {
console.log(e);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@randlabs/encrypted-local-storage",
"version": "1.0.0",
"version": "2.0.0",
"description": "",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -47,6 +47,6 @@
"typescript": "^3.9.3"
},
"dependencies": {
"localforage": "^1.7.4"
"localforage": "^1.9.0"
}
}
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export default class AppStorage {
}

// Private key storage
async savePrivatekeyToStorage(key: string, password: string, pk: Uint8Array): Promise<void> {
static async savePrivatekeyToStorage(key: string, password: string, pk: Uint8Array): Promise<void> {
const masterkey = await StrongCipher.generateMasterKey(password);
const encryptedPk = await StrongCipher.encrypt(toHexString(pk), masterkey);
await AppStorage.setItem(key, encryptedPk);
}

async loadPrivatekeyFromStorage(key: string, password: string): Promise<Uint8Array> {
static async loadPrivatekeyFromStorage(key: string, password: string): Promise<Uint8Array> {
const masterkey = await StrongCipher.generateMasterKey(password);
const encryptedPk = await AppStorage.getItem(key);
if (!encryptedPk) {
Expand Down Expand Up @@ -115,6 +115,10 @@ export default class AppStorage {
return hasItem;
}

static getKeys(): Promise<string[]> {
return Storage.getKeys();
}

static async resetStorage(): Promise<void> {
await Storage.clearStorage();
}
Expand Down
16 changes: 16 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ export default class Storage {

return ret !== null;
}

static getKeys(): Promise<string[]> {
return localforage.keys();
}

static setConfig(config: LocalForageOptions): void {
localforage.config(config);
}

static getConfig(): LocalForageOptions {
return localforage.config();
}

static setDriver(driver: string | string[]): void {
localforage.setDriver(driver);
}
}
55 changes: 13 additions & 42 deletions src/test/test-strong-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ describe("Strong Store Management", function() {
const secretItem = "Item to store";

before(function(done) {
AppStorage.createPassword(masterkey, password)
.then(() => {
done();
})
.catch((err) => {
done(err);
});
AppStorage.createPassword(masterkey, password).then(done).catch(done);
});

describe("Save string to the storage", function() {
Expand All @@ -23,31 +17,23 @@ describe("Strong Store Management", function() {
AppStorage.verifyPassword(masterkey, password)
.then((result) => {
done();
})
.catch((err) => {
done(err);
});
}).catch(done);
});

it("Should save the item", function(done) {
const storage = new AppStorage();
storage.savePrivatekeyToStorage(secretKey, password, new Uint8Array(Buffer.from(secretItem)))
AppStorage.savePrivatekeyToStorage(secretKey, password, new Uint8Array(Buffer.from(secretItem)))
.then(() => {
done();
})
.catch((err) => {
done(err);
});
}).catch(done);
});

});

describe("Load string from the storage with a invalid password", function() {

it("Should throw error by load item with a wrong password", function(done) {
const storage = new AppStorage();
storage.loadPrivatekeyFromStorage(secretKey, "wrong password")
.then((storedItem) => {
AppStorage.loadPrivatekeyFromStorage(secretKey, "wrong password")
.then(() => {
done("Invalid test, must fail");
})
.catch((err) => {
Expand All @@ -64,29 +50,20 @@ describe("Strong Store Management", function() {
AppStorage.verifyPassword(masterkey, password)
.then((result) => {
done();
})
.catch((err) => {
done(err);
});
}).catch(done);
});

it("Should load the item", function(done) {
const storage = new AppStorage();
storage.loadPrivatekeyFromStorage(secretKey, password)
AppStorage.loadPrivatekeyFromStorage(secretKey, password)
.then((storedItem) => {
const item = (Buffer.from(storedItem)).toString();
expect(item).to.be.equal(secretItem);
done();
})
.catch((err) => {
done(err);
});
}).catch(done);
});

after(function(done) {
AppStorage.removeItem(secretKey).then(() => {
done();
});
AppStorage.removeItem(secretKey).then(done).catch(done);
});

});
Expand All @@ -96,15 +73,11 @@ describe("Strong Store Management", function() {
AppStorage.verifyPassword(masterkey, password)
.then((result) => {
done();
})
.catch((err) => {
done(err);
});
}).catch(done)
});

it("Should load the item", function(done) {
const storage = new AppStorage();
storage.loadPrivatekeyFromStorage(secretKey, password)
AppStorage.loadPrivatekeyFromStorage(secretKey, password)
.then(() => {
done("Invalid test, must fail");
})
Expand All @@ -117,8 +90,6 @@ describe("Strong Store Management", function() {
});

after(function(done) {
AppStorage.resetStorage().then(() => {
done();
});
AppStorage.resetStorage().then(done).catch(done);
});
});
48 changes: 13 additions & 35 deletions src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,57 @@ describe("Test utils functions", function() {
const itemToStore = "item";

it("Should save a string", function(done) {
AppStorage.setItem(itemKey, itemToStore)
.then(() => {
done();
})
.catch((err) => {
done(err);
});
AppStorage.setItem(itemKey, itemToStore).catch(done).then(done);
});

it("Should load a string", function(done) {
AppStorage.getItem(itemKey)
AppStorage.getItem(itemKey).catch(done)
.then((storedItem) => {
expect(storedItem).to.be.equal(itemToStore);
done();
})
.catch((err) => {
done(err);
});
});

it("Should not load a string", function(done) {
AppStorage.getItem(itemKey + "1")
AppStorage.getItem(itemKey + "1").catch(done)
.then((storedItem) => {
expect(storedItem).to.be.equal(null);
done();
})
.catch((err) => {
done(err);
});
});

it("Should has an item", function(done) {
AppStorage.hasItem(itemKey)
AppStorage.hasItem(itemKey).catch(done)
.then((isStored) => {
expect(isStored).to.be.a("boolean");
expect(isStored).to.be.equal(true);
done();
})
.catch((err) => {
done(err);
});
});

it("Should not has an item", function(done) {
AppStorage.hasItem(itemKey + "1")
AppStorage.hasItem(itemKey + "1").catch(done)
.then((isStored) => {
expect(isStored).to.be.a("boolean");
expect(isStored).to.be.equal(false);
done();
})
.catch((err) => {
done(err);
});
});

it("Should remove an item", function(done) {
AppStorage.removeItem(itemKey)
.then(() => {
AppStorage.removeItem(itemKey).catch(done).then(done);
});

it("Should list all storage keys", function(done) {
AppStorage.getKeys().catch(done)
.then((keys) => {
expect(keys).to.be.an("array");
done();
})
.catch((err) => {
done(err);
});
});

it("Should reset the storage", function(done) {
AppStorage.resetStorage()
.then(() => {
done();
})
.catch((err) => {
done(err);
});
AppStorage.resetStorage().catch(done).then(done);
});

});

0 comments on commit 041ecc1

Please sign in to comment.