Skip to content

Commit

Permalink
Merge pull request #10 from lorena-ssi/delete_unlock
Browse files Browse the repository at this point in the history
fix lock password checking; add delete
  • Loading branch information
celeduc authored Apr 21, 2020
2 parents 6e64662 + 731665f commit 2b8fb51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@lorena-ssi/wallet-lib",
"version": "1.0.12",
"description": "Lorena Posix Filesystem-based Wallet",
"version": "1.1.0",
"description": "Lorena Wallet",
"author": "Alex Puig <alex@caelumlabs.com>",
"license": "MIT",
"main": "index.js",
Expand Down Expand Up @@ -38,7 +38,6 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"fs-extra": "^9.0.0",
"husky": "^4.2.5",
"mocha": "^7.1.1",
"mocha-lcov-reporter": "^1.3.0",
Expand All @@ -49,7 +48,6 @@
"coverage": "nyc --reporter=text --reporter=html --reporter=lcov npm run mocha",
"coveralls": "nyc npm run mocha && nyc report --reporter=text-lcov | coveralls",
"coverage:serve": "npm run coverage && serve coverage",
"debug": "./pds 1",
"lint": "eslint ./src ./test",
"lint:fix": "eslint ./src ./test --fix",
"mocha": "mocha test --timeout 50000 --require esm",
Expand Down
29 changes: 27 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default class Wallet {
return false
})
return !result ? result : result.isDirectory()
/* istanbul ignore else */
} else if (this.opts.storage === 'mem') {
return !!this.storage[this.directoryPath]
}
Expand Down Expand Up @@ -87,6 +86,9 @@ export default class Wallet {
const infoDecrypted = await this.zenroom.decryptSymmetric(password, JSON.parse(Buffer.from(info, 'base64').toString()))
const data = await this.read('data')
const dataDecrypted = await this.zenroom.decryptSymmetric(password, JSON.parse(Buffer.from(data, 'base64').toString()))
if (!infoDecrypted || !dataDecrypted) {
return false
}
if (!onlyCheck) {
this.info = JSON.parse(infoDecrypted.message)
this.data = JSON.parse(dataDecrypted.message)
Expand All @@ -102,7 +104,7 @@ export default class Wallet {
/**
* Encrypt and save configuration.
*
* @param {string} password Password to encrypt configuration
* @param {string} password Password to decrypt configuration
*/
async lock (password) {
try {
Expand All @@ -127,6 +129,29 @@ export default class Wallet {
}
}

/**
* Delete the wallet.
*
* @returns {boolean} success
*/
async delete () {
try {
// if it exists, delete it
if (await this.exist()) {
if (this.opts.storage === 'fs') {
await fsPromises.rmdir(this.directoryPath, { recursive: true })
} else if (this.opts.storage === 'mem') {
this.storage = {}
}
}
// now it doesn't exist regardless
return true
} catch (e) {
/* istanbul ignore next */
return false
}
}

get (collection, where) {
let result = false
this.data[collection].filter((item, index) => {
Expand Down
44 changes: 25 additions & 19 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import Wallet from '..'
import { expect, assert } from 'chai'
import { describe, it } from 'mocha'
import fs from 'fs-extra'

const deleteWallet = async (w, path) => {
if (w.opts.storage === 'fs') {
fs.removeSync(path)
} else {
assert(w.opts.storage === 'mem')
w.storage = {}
}
}

describe('Wallet API', function () {
// run (almost) all tests for each supported storage type
Expand All @@ -22,7 +12,7 @@ describe('Wallet API', function () {
})

it('should delete the wallet (if any remaining from the last test) ' + storage, async () => {
await deleteWallet(w, w.directoryPath)
await w.delete()
})

it('should add to credentials collection ' + storage, () => {
Expand Down Expand Up @@ -71,12 +61,20 @@ describe('Wallet API', function () {
expect(w.data.credentials.length).to.equal(1)
})

it('should remove all coincidences ' + storage, async () => {
await w.lock('password0')
it('should lock ' + storage, async () => {
await w.lock('myPassword0')
})

it('should not lock with the wrong password ' + storage, async () => {
let result = await w.unlock('myPassword0')
expect(result).to.be.true
w.add('credentials', { name: 'test6', role: 'user' })
result = await w.lock('NotMyPasswordX')
expect(result).to.be.false
})

it('should NOT unlock wallet (because it does not exist) ' + storage, (done) => {
deleteWallet(w, w.directoryPath).then(() => {
w.delete().then(() => {
w.unlock('myPassword0').then((response) => {
assert(!response)
done()
Expand All @@ -85,11 +83,9 @@ describe('Wallet API', function () {
})

it('should lock new wallet (thus creating it) ' + storage, (done) => {
deleteWallet(w, w.directoryPath).then(() => {
w.lock('myPassword1').then((response) => {
assert(response)
done()
})
w.lock('myPassword1').then((response) => {
assert(response)
done()
})
})

Expand Down Expand Up @@ -120,6 +116,13 @@ describe('Wallet API', function () {
done()
})
})

it('should delete wallet ' + storage, (done) => {
w.delete().then((response) => {
assert(response)
done()
})
})
})

// Scenarios specific to filesystem
Expand All @@ -136,6 +139,9 @@ describe('Wallet API', function () {
})
.then((response) => {
expect(w2.data.credentials[0]).to.eql({ name: 'admintest', role: 'admin' })
return w2.delete()
})
.then(() => {
done()
})
})
Expand Down

0 comments on commit 2b8fb51

Please sign in to comment.