Skip to content

Commit

Permalink
[FABN-862] Implemented CouchDB wallet storage
Browse files Browse the repository at this point in the history
    - Implemented CouchDB wallet using Nano. Unit tests in mocha at 100% and integration test
    - Specified eslint to ignore root test directory

Change-Id: I1ec9d5333dfff75ca73f66beeb0c1fd5233d81c4
Signed-off-by: Liam Grace <liamgrace.896@gmail.com>
  • Loading branch information
liam-grace committed Oct 10, 2018
1 parent f32c3b6 commit 28f48d3
Show file tree
Hide file tree
Showing 10 changed files with 472 additions and 9 deletions.
4 changes: 1 addition & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
#
# SPDX-License-Identifier: Apache-2.0
#
./test
coverage
node_modules
config
.nyc_output
1 change: 0 additions & 1 deletion fabric-ca-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"tag": "unstable",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "nyc mocha --exclude 'test/data/**/*.js' --recursive -t 10000"
},
"repository": {
Expand Down
1 change: 0 additions & 1 deletion fabric-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"tag": "unstable",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "nyc mocha --exclude 'test/data/**/*.js' --recursive -t 10000"
},
"repository": {
Expand Down
1 change: 0 additions & 1 deletion fabric-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"email": "fabric@lists.hyperledger.org"
},
"scripts": {
"lint": "eslint .",
"test": "nyc mocha --exclude 'test/data/**/*.js' --recursive -t 10000"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions fabric-network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ module.exports.Wallet = require('./lib/api/wallet');
module.exports.InMemoryWallet = require('./lib/impl/wallet/inmemorywallet');
module.exports.X509WalletMixin = require('./lib/impl/wallet/x509walletmixin');
module.exports.FileSystemWallet = require('./lib/impl/wallet/filesystemwallet');
module.exports.CouchDBWallet = require('./lib/impl/wallet/couchdbwallet');
module.exports.DefaultEventHandlerStrategies = require('fabric-network/lib/impl/event/defaulteventhandlerstrategies');
153 changes: 153 additions & 0 deletions fabric-network/lib/impl/wallet/couchdbwallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* Copyright 2018 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';

const Client = require('fabric-client');
const BaseWallet = require('./basewallet');
const CouchDBVStore = require('fabric-client/lib/impl/CouchDBKeyValueStore');
const logger = require('../../logger').getLogger('CouchDBWallet');
const Nano = require('nano');
/**
* This class defines an implementation of an Identity wallet that persists
* to a Couch DB database
*
* @class
* @extends {BaseWallet}
*/
class CouchDBWallet extends BaseWallet {

/**
* Creates an instance of the CouchDBWallet
* @param {Object} options contains required property <code>url</code> and other Nano options
* @param {WalletMixin} mixin
* @memberof CouchDBWallet
*/
constructor(options, mixin) {
const method = 'constructor';
super(mixin);
logger.debug('in CouchDBWallet %s', method);
if (!options) {
throw new Error('No options given');
}
if (!options.url) {
throw new Error('No url given');
}
this.options = options;
this.couch = Nano(options.url);
this.dbOptions = {};
Object.assign(this.dbOptions, this.options);
}

_createOptions() {
const dbOptions = {};
Object.assign(dbOptions, this.options);
dbOptions.name = 'wallet';
return dbOptions;
}

/**
* @inheritdoc
*/
async getStateStore(label) {
const method = 'getStateStore';
logger.debug('in %s, label = %s', method, label);
const store = new CouchDBWalletKeyValueStore(this._createOptions());
return store;
}

/**
* @inheritdoc
*/
async getCryptoSuite(label) {
const method = 'getCryptoSuite';
logger.debug('in %s, label = %s', method, label);
const cryptoSuite = Client.newCryptoSuite();
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore(CouchDBWalletKeyValueStore, this._createOptions()));
return cryptoSuite;
}

/**
* @inheritdoc
*/
async delete(label) {
const method = 'delete';
logger.debug('in %s, label = %s', method, label);
label = this.normalizeLabel(label);
const kvs = await this.getStateStore(this.options.name);
return kvs.delete(label);
}

/**
* @inheritdoc
*/
async exists(label) {
const method = 'exists';
logger.debug('in %s, label = %s', method, label);
label = this.normalizeLabel(label);
const kvs = await this.getStateStore(this.options.name);
return kvs.exists(label);
}

/**
* @inheritdoc
*/
async getAllLabels() {
const method = 'getAllLabels';
logger.debug('in %s', method);
const kvs = await this.getStateStore(this.options.name);
return kvs.getAllLabels();
}
}

class CouchDBWalletKeyValueStore extends CouchDBVStore {
constructor(options) {
super(options);
}

async delete(key) {
const self = this;
return new Promise((resolve) => {
self._database.destroy(key, (err) => {
if (err) {
return resolve(false);
}
return resolve(true);
});
});
}

async exists(key) {
const self = this;
return new Promise((resolve, reject) => {
self._database.get(key, (err) => {
if (err) {
if (err.error === 'not_found') {
return resolve(false);
} else {
return reject(err);
}
} else {
return resolve(true);
}
});
});
}

async getAllLabels() {
const self = this;
return new Promise((resolve, reject) => {
self._database.list((err, list) => {
if (err) {
return reject(err);
}
return resolve(list);
});
});
}
}

module.exports = CouchDBWallet;
module.exports.CouchDBWalletKeyValueStore = CouchDBWalletKeyValueStore;
4 changes: 2 additions & 2 deletions fabric-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"email": "fabric@lists.hyperledger.org"
},
"scripts": {
"lint": "eslint .",
"test": "nyc mocha --exclude 'test/data/**/*.js' --recursive -t 10000"
"test": "nyc mocha --exclude 'test/data/**/*.js' --recursive -t 10000"
},
"types": "./types/index.d.ts",
"dependencies": {
Expand All @@ -30,6 +29,7 @@
"devDependencies": {
"mocha": "^5.2.0",
"nyc": "^12.0.2",
"proxyquire": "^2.1.0",
"rewire": "^4.0.1",
"sinon": "^5.0.7"
},
Expand Down
Loading

0 comments on commit 28f48d3

Please sign in to comment.