-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FABN-859 Add support for File System Wallet
The extends the network package to add a File System Based Wallet where identities are persisted to the file system Change-Id: I1cffeb0bb2e9b4c043ac86a234a424ea49e17055 Signed-off-by: Dave Kelsey <[email protected]>
- Loading branch information
Dave Kelsey
committed
Aug 22, 2018
1 parent
b2416b3
commit cb591c5
Showing
12 changed files
with
626 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/** | ||
* Copyright 2018 IBM All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
'use strict'; | ||
|
||
const Client = require('fabric-client'); | ||
const rimraf = require('rimraf'); | ||
const fs = require('fs-extra'); | ||
const Path = require('path'); | ||
const BaseWallet = require('./basewallet'); | ||
const FileKVS = require('fabric-client/lib/impl/FileKeyValueStore'); | ||
const logger = require('../../logger').getLogger('FileSystemWallet'); | ||
|
||
/** | ||
* This class defines an implementation of an Identity wallet that persists | ||
* to the file system. | ||
* | ||
* @class FileSystemWallet | ||
* @see See {@link BaseWallet} | ||
* @extends {BaseWallet} | ||
*/ | ||
class FileSystemWallet extends BaseWallet { | ||
|
||
/* | ||
* create a new File Key Value Store | ||
* | ||
* @static | ||
* @param {string} path the root path of the key value store | ||
* @returns {Promise} a promise that is resolved when a new File KVS instance is recreated. | ||
* @memberof FileSystemWallet | ||
* @private | ||
*/ | ||
static async _createFileKVS(path) { | ||
return await new FileKVS({path}); | ||
} | ||
|
||
/* | ||
* check to see if the label defines a directory in the wallet | ||
* | ||
* @static | ||
* @param {string} label | ||
* @returns {Promise} a promise that returns true if this is a valid directory, false otherwise | ||
* @memberof FileSystemWallet | ||
* @private | ||
*/ | ||
async _isDirectory(label) { | ||
const method = '_isDirectory'; | ||
let isDir; | ||
try { | ||
const stat = await fs.lstat(Path.join(this.path, label)); | ||
isDir = stat.isDirectory(); | ||
} catch(err) { | ||
isDir = false; | ||
} | ||
logger.debug('%s - return value: %s', method, isDir); | ||
return isDir; | ||
} | ||
|
||
/** | ||
* Creates an instance of FileSystemWallet. | ||
* @param {string} path The root path for this wallet on the file system | ||
* @param {WalletMixin} [mixin] optionally provide an alternative WalletMixin. Defaults to X509WalletMixin | ||
* @memberof FileSystemWallet | ||
*/ | ||
constructor(path, mixin) { | ||
if (!path) { | ||
throw new Error('No path for wallet has been provided'); | ||
} | ||
super(mixin); | ||
this.path = path; | ||
} | ||
|
||
/* | ||
* Get the partitioned path for the provided label | ||
* | ||
* @param {string} label | ||
* @returns {string} the partitioned path | ||
* @memberof FileSystemWallet | ||
* @private | ||
*/ | ||
_getPartitionedPath(label) { | ||
label = this.normalizeLabel(label); | ||
const partitionedPath = Path.join(this.path, label); | ||
return partitionedPath; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async getStateStore(label) { | ||
const partitionedPath = this._getPartitionedPath(label); | ||
return FileSystemWallet._createFileKVS(partitionedPath); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async getCryptoSuite(label) { | ||
const partitionedPath = this._getPartitionedPath(label); | ||
const cryptoSuite = Client.newCryptoSuite(); | ||
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({path: partitionedPath})); | ||
return cryptoSuite; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async getAllLabels() { | ||
let dirList; | ||
const labelList = []; | ||
try { | ||
dirList = await fs.readdir(this.path); | ||
} catch(err) { | ||
return []; | ||
} | ||
|
||
if (dirList && dirList.length > 0) { | ||
for (const label of dirList) { | ||
const reallyExists = await this.exists(label); | ||
if (reallyExists) { | ||
labelList.push(label); | ||
} | ||
} | ||
} | ||
return labelList; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async delete(label) { | ||
const method = 'delete'; | ||
const reallyExists = await this.exists(label); | ||
if (!reallyExists) { | ||
return false; | ||
} | ||
const partitionedPath = this._getPartitionedPath(label); | ||
const rmPromise = new Promise((resolve, reject) => { | ||
rimraf(partitionedPath, (err) => { | ||
if (err) { | ||
logger.debug('%s - error returned trying to rm rf \'%s\': %s', method, partitionedPath, err); | ||
reject(err); | ||
} | ||
resolve(true); | ||
}); | ||
}); | ||
return await rmPromise; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async exists(label) { | ||
const method = 'exists'; | ||
let exists = false; | ||
const isDir = await this._isDirectory(label); | ||
if (isDir) { | ||
exists = await fs.exists(Path.join(this._getPartitionedPath(label), label)); | ||
} | ||
logger.debug('%s - label: %s, isDir: %s, exists: %s', method, label, isDir, exists); | ||
return exists; | ||
} | ||
} | ||
|
||
module.exports = FileSystemWallet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.