-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new folder peer-gateway containing new connector Wallet Facade an…
…d Wallet Facade Factory (#1227) Signed-off-by: fraVlaca <[email protected]>
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
packages/caliper-fabric/lib/connector-versions/peer-gateway/WalletFacade.js
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,83 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const IWalletFacade = require('../../identity-management/IWalletFacade'); | ||
const ExportedIdentity = require('../../identity-management/ExportedIdentity'); | ||
|
||
/** | ||
* a Facade for the Peer Gateway Wallet implementation | ||
*/ | ||
class WalletFacade extends IWalletFacade { | ||
|
||
/** | ||
*/ | ||
constructor() { | ||
super(); | ||
this.wallet = new Map(); | ||
} | ||
|
||
/** | ||
* Import an identity | ||
* | ||
* @param {string} mspId The mspId that owns the identity | ||
* @param {string} identityName The name of the identity | ||
* @param {string} certificate The identity certificate | ||
* @param {string} privateKey The identity private key | ||
*/ | ||
async import(mspId, identityName, certificate, privateKey) { | ||
const exists = await this.wallet.get(identityName); | ||
|
||
if (exists) { | ||
throw new Error(`${identityName} already exists in the wallet`); | ||
} | ||
|
||
const identity = { | ||
credentials: { | ||
certificate, | ||
privateKey | ||
}, | ||
mspId, | ||
}; | ||
await this.wallet.set(identityName, identity); | ||
} | ||
|
||
/** | ||
* Export an identity | ||
* | ||
* @param {string} identityName The identity to export | ||
* @returns {Promise<ExportedIdentity>} The exported identity or null if it doesn't exist | ||
* @async | ||
*/ | ||
async export(identityName) { | ||
const exported = await this.wallet.get(identityName); | ||
if (exported) { | ||
return new ExportedIdentity(exported.mspId, exported.credentials.certificate, exported.credentials.privateKey); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Get all the identity names in the wallet | ||
* | ||
* @returns {Promise<[string]>} all the identity names in the wallet | ||
* @async | ||
*/ | ||
async getAllIdentityNames() { | ||
return Array.from(this.wallet.keys()); | ||
} | ||
} | ||
|
||
module.exports = WalletFacade; |
35 changes: 35 additions & 0 deletions
35
packages/caliper-fabric/lib/connector-versions/peer-gateway/WalletFacadeFactory.js
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,35 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const IWalletFacadeFactory = require('../../identity-management/IWalletFacadeFactory'); | ||
const WalletFacade = require('./WalletFacade'); | ||
|
||
/** | ||
* Factory for a Peer Gateway Wallet Facade | ||
*/ | ||
class WalletFacadeFactory extends IWalletFacadeFactory { | ||
|
||
/** | ||
* create a Peer Gateway Wallet Facade | ||
* | ||
* @param {string} [walletPath] optional path to a file system wallet | ||
*/ | ||
async create() { | ||
return new WalletFacade(); | ||
} | ||
} | ||
|
||
module.exports = WalletFacadeFactory; |
66 changes: 66 additions & 0 deletions
66
packages/caliper-fabric/test/connector-versions/peer-gateway/WalletFacade.js
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,66 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const chai = require('chai'); | ||
const chaiAsPromised = require('chai-as-promised'); | ||
chai.use(chaiAsPromised); | ||
const should = chai.should(); | ||
|
||
|
||
const WalletFacadeFactory = require('../../../lib/connector-versions/peer-gateway/WalletFacadeFactory'); | ||
const WalletFacade = require('../../../lib/connector-versions/peer-gateway/WalletFacade'); | ||
|
||
describe('When testing a Peer Gateway Wallet Facade Implementation', () => { | ||
|
||
it('A Wallet Facade Factory should create a wallet facade', async () => { | ||
const walletFacade = await new WalletFacadeFactory().create(); | ||
walletFacade.should.be.instanceOf(WalletFacade); | ||
const walletFacade2 = await new WalletFacadeFactory().create('dsgdsfdsjfdk'); | ||
walletFacade2.should.be.instanceOf(WalletFacade); | ||
}); | ||
|
||
it('A wallet facade should be able to import and export identities', async () => { | ||
const walletFacade = await new WalletFacadeFactory().create(); | ||
await walletFacade.import('mspid', 'label', 'cert', 'key'); | ||
const exported = await walletFacade.export('label'); | ||
exported.should.deep.equal({mspid: 'mspid', certificate: 'cert', privateKey: 'key'}); | ||
}); | ||
|
||
it('A wallet facade should return null on export if the identity does not exist', async () => { | ||
const walletFacade = await new WalletFacadeFactory().create(); | ||
const exported = await walletFacade.export('label'); | ||
should.equal(exported, null); | ||
}); | ||
|
||
it('A wallet facade should throw an error if an identity already exists', async () => { | ||
const walletFacade = await new WalletFacadeFactory().create(); | ||
await walletFacade.import('mspid', 'label', 'cert', 'key'); | ||
await walletFacade.import('mspid', 'label', 'cert', 'key').should.be.rejectedWith(/already exists/); | ||
}); | ||
|
||
it('A wallet facade should get all identity names it has', async () => { | ||
const walletFacade = await new WalletFacadeFactory().create(); | ||
await walletFacade.import('mspid', 'label', 'cert', 'key'); | ||
await walletFacade.import('mspid', 'bart', 'cert', 'key'); | ||
await walletFacade.import('mspid', 'lisa', 'cert', 'key'); | ||
(await walletFacade.getAllIdentityNames()).should.deep.equal(['label', 'bart', 'lisa']); | ||
}); | ||
|
||
it('A wallet facade should throw an error when calling the getWallet() since it is an abstract not implemented method', async () => { | ||
const walletFacade = await new WalletFacadeFactory().create(); | ||
(() => {walletFacade.getWallet();}).should.throw('Abstract method called'); | ||
}); | ||
}); |