Skip to content

Commit

Permalink
Use data source to persist discovery certificates (hyperledger-archiv…
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Stone authored Apr 4, 2017
1 parent e759a85 commit 4f878a1
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 30 deletions.
6 changes: 6 additions & 0 deletions packages/composer-rest-server/common/models/wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"type": "Boolean",
"default": false,
"description": "Was this wallet created as the default wallet?"
},
"createdAsSystem": {
"type": "Boolean",
"default": false,
"description": "Was this wallet created as the system wallet?"
}
},
"validations": [],
Expand Down Expand Up @@ -73,6 +78,7 @@
"hidden": [
"userId",
"createdAsDefault",
"createdAsSystem",
"defaultIdentity"
]
}
1 change: 0 additions & 1 deletion packages/composer-rest-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"loopback-component-explorer": "^4.1.1",
"loopback-component-passport": "^3.2.0",
"loopback-connector-composer": "^0.5.7",
"passport-github2": "^0.1.10",
"passport-local": "^1.0.0",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^1.0.1",
Expand Down
87 changes: 67 additions & 20 deletions packages/composer-rest-server/server/boot/composer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const connector = require('loopback-connector-composer');
const LoopBackWallet = require('../../lib/loopbackwallet');

module.exports = function (app, callback) {

Expand All @@ -24,30 +25,76 @@ module.exports = function (app, callback) {
callback();
return Promise.resolve();
}
let dataSource;
return Promise.resolve()
.then(() => {

// If this isn't the memory connector, then we want to persist the enrollment certificate.
// This means that the Composer APIs will fall back to using the default filesystem wallet.
const isMemory = app.datasources.db.name === 'Memory';
if (isMemory) {
return;
}

// Find or create the system wallet.
let filter = {
where: {
createdAsSystem: true
}
};
let data = {
description: 'System wallet',
createdAsSystem: true
};
return app.models.Wallet.findOrCreate(filter, data)
.then((parts) => {

// Create a LoopBack wallet for the system wallet.
let wallet = parts[0];
composer.wallet = new LoopBackWallet(app, wallet);

// Ensure that the specified identity exists.
let filter = {
where: {
enrollmentID: composer.participantId
}
};
let data = {
walletId: wallet.id,
enrollmentID: composer.participantId,
enrollmentSecret: composer.participantPwd
};
return app.models.WalletIdentity.findOrCreate(filter, data);

// Create an instance of the LoopBack data source that uses the connector.
const connectorSettings = {
name: 'composer',
connector: connector,
connectionProfileName: composer.connectionProfileName,
businessNetworkIdentifier: composer.businessNetworkIdentifier,
participantId: composer.participantId,
participantPwd: composer.participantPwd,
namespaces: composer.namespaces,
fs: composer.fs
};
const dataSource = app.loopback.createDataSource('composer', connectorSettings);

return new Promise((resolve, reject) => {
});

})
.then(() => {

// Create an instance of the LoopBack data source that uses the connector.
const connectorSettings = {
name: 'composer',
connector: connector,
connectionProfileName: composer.connectionProfileName,
businessNetworkIdentifier: composer.businessNetworkIdentifier,
participantId: composer.participantId,
participantPwd: composer.participantPwd,
namespaces: composer.namespaces,
fs: composer.fs,
wallet: composer.wallet
};
dataSource = app.loopback.createDataSource('composer', connectorSettings);

// Discover the model definitions (types) from the connector.
// This will go and find all of the non-abstract types in the business network definition.
console.log('Discovering types from business network definition ...');
dataSource.discoverModelDefinitions({}, (error, modelDefinitions) => {
if (error) {
return reject(error);
}
resolve(modelDefinitions);
return new Promise((resolve, reject) => {
console.log('Discovering types from business network definition ...');
dataSource.discoverModelDefinitions({}, (error, modelDefinitions) => {
if (error) {
return reject(error);
}
resolve(modelDefinitions);
});
});

})
Expand Down
4 changes: 2 additions & 2 deletions packages/composer-rest-server/server/providers.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"usernameField": "username",
"passwordField": "password",
"authPath": "/auth/local",
"successRedirect": "/auth/account",
"failureRedirect": "/local"
"successRedirect": "/",
"failureRedirect": "/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const composerDiscovery = require('../../../server/boot/composer-discovery');
const fs = require('fs');
const loopback = require('loopback');
require('loopback-component-passport');
const LoopBackWallet = require('../../../lib/loopbackwallet');
const path = require('path');

require('chai').should();
Expand All @@ -32,13 +33,7 @@ const bfs_fs = BrowserFS.BFSRequire('fs');

describe('composer-discovery boot script', () => {

let composerConfig = {
connectionProfileName: 'defaultProfile',
businessNetworkIdentifier: 'bond-network',
participantId: 'admin',
participantPwd: 'adminpw',
fs: bfs_fs
};
let composerConfig;
let app;
let sandbox;

Expand All @@ -62,6 +57,13 @@ describe('composer-discovery boot script', () => {

beforeEach(() => {
sandbox = sinon.sandbox.create();
composerConfig = {
connectionProfileName: 'defaultProfile',
businessNetworkIdentifier: 'bond-network',
participantId: 'admin',
participantPwd: 'adminpw',
fs: bfs_fs
};
app = loopback();
return new Promise((resolve, reject) => {
boot(app, path.resolve(__dirname, '..', '..', '..', 'server'), (err) => {
Expand Down Expand Up @@ -144,4 +146,22 @@ describe('composer-discovery boot script', () => {
});
});

it('should discover the business network using a wallet to persist certificates', () => {
sinon.spy(app.loopback, 'createDataSource');
app.datasources.db.name = 'MongoDB';
const cb = sinon.stub();
return composerDiscovery(app, cb)
.then(() => {
return app.models.Wallet.findOne({ where: { createdAsSystem: true } });
})
.then((wallet) => {
wallet.should.exist;
return app.models.WalletIdentity.findOne({ where: { walletId: wallet.id, enrollmentID: 'admin', enrollmentSecret: 'adminpw' }});
})
.then((identity) => {
identity.should.exist;
app.loopback.createDataSource.args[0][1].wallet.should.be.an.instanceOf(LoopBackWallet);
});
});

});

0 comments on commit 4f878a1

Please sign in to comment.