-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ramananda Panda
committed
Feb 23, 2019
1 parent
36ba8c2
commit 4155021
Showing
153 changed files
with
9,257 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright 2017 IBM All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var helper = require('./helper.js'); | ||
var logger = helper.getLogger('Create-Channel'); | ||
//Attempt to send a request to the orderer with the sendTransaction method | ||
var createChannel = async function(channelName, channelConfigPath, username, orgName) { | ||
logger.debug('\n====== Creating Channel \'' + channelName + '\' ======\n'); | ||
try { | ||
// first setup the client for this org | ||
var client = await helper.getClientForOrg(orgName); | ||
logger.debug('Successfully got the fabric client for the organization "%s"', orgName); | ||
|
||
// read in the envelope for the channel config raw bytes | ||
var envelope = fs.readFileSync(path.join(__dirname, channelConfigPath)); | ||
// extract the channel config bytes from the envelope to be signed | ||
var channelConfig = client.extractChannelConfig(envelope); | ||
|
||
//Acting as a client in the given organization provided with "orgName" param | ||
// sign the channel config bytes as "endorsement", this is required by | ||
// the orderer's channel creation policy | ||
// this will use the admin identity assigned to the client when the connection profile was loaded | ||
let signature = client.signChannelConfig(channelConfig); | ||
|
||
let request = { | ||
config: channelConfig, | ||
signatures: [signature], | ||
name: channelName, | ||
txId: client.newTransactionID(true) // get an admin based transactionID | ||
}; | ||
|
||
// send to orderer | ||
const result = await client.createChannel(request) | ||
logger.debug(' result ::%j', result); | ||
if (result) { | ||
if (result.status === 'SUCCESS') { | ||
logger.debug('Successfully created the channel.'); | ||
const response = { | ||
success: true, | ||
message: 'Channel \'' + channelName + '\' created Successfully' | ||
}; | ||
return response; | ||
} else { | ||
logger.error('Failed to create the channel. status:' + result.status + ' reason:' + result.info); | ||
const response = { | ||
success: false, | ||
message: 'Channel \'' + channelName + '\' failed to create status:' + result.status + ' reason:' + result.info | ||
}; | ||
return response; | ||
} | ||
} else { | ||
logger.error('\n!!!!!!!!! Failed to create the channel \'' + channelName + | ||
'\' !!!!!!!!!\n\n'); | ||
const response = { | ||
success: false, | ||
message: 'Failed to create the channel \'' + channelName + '\'', | ||
}; | ||
return response; | ||
} | ||
} catch (err) { | ||
logger.error('Failed to initialize the channel: ' + err.stack ? err.stack : err); | ||
throw new Error('Failed to initialize the channel: ' + err.toString()); | ||
} | ||
}; | ||
|
||
exports.createChannel = createChannel; |
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,120 @@ | ||
/** | ||
* Copyright 2017 IBM All Rights Reserved. | ||
* | ||
* 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'; | ||
var log4js = require('log4js'); | ||
var logger = log4js.getLogger('Helper'); | ||
logger.setLevel('DEBUG'); | ||
|
||
var path = require('path'); | ||
var util = require('util'); | ||
|
||
var hfc = require('fabric-client'); | ||
hfc.setLogger(logger); | ||
|
||
async function getClientForOrg (userorg, username) { | ||
logger.debug('getClientForOrg - ****** START %s %s', userorg, username) | ||
// get a fabric client loaded with a connection profile for this org | ||
let config = '-connection-profile-path'; | ||
|
||
// build a client context and load it with a connection profile | ||
// lets only load the network settings and save the client for later | ||
let client = hfc.loadFromConfig(hfc.getConfigSetting('network'+config)); | ||
|
||
// This will load a connection profile over the top of the current one one | ||
// since the first one did not have a client section and the following one does | ||
// nothing will actually be replaced. | ||
// This will also set an admin identity because the organization defined in the | ||
// client section has one defined | ||
client.loadFromConfig(hfc.getConfigSetting(userorg+config)); | ||
|
||
// this will create both the state store and the crypto store based | ||
// on the settings in the client section of the connection profile | ||
await client.initCredentialStores(); | ||
|
||
// The getUserContext call tries to get the user from persistence. | ||
// If the user has been saved to persistence then that means the user has | ||
// been registered and enrolled. If the user is found in persistence | ||
// the call will then assign the user to the client object. | ||
if(username) { | ||
let user = await client.getUserContext(username, true); | ||
if(!user) { | ||
throw new Error(util.format('User was not found :', username)); | ||
} else { | ||
logger.debug('User %s was found to be registered and enrolled', username); | ||
} | ||
} | ||
logger.debug('getClientForOrg - ****** END %s %s \n\n', userorg, username) | ||
|
||
return client; | ||
} | ||
|
||
var getRegisteredUser = async function(username, userOrg, isJson) { | ||
try { | ||
var client = await getClientForOrg(userOrg); | ||
logger.debug('Successfully initialized the credential stores'); | ||
// client can now act as an agent for organization Org1 | ||
// first check to see if the user is already enrolled | ||
var user = await client.getUserContext(username, true); | ||
if (user && user.isEnrolled()) { | ||
logger.info('Successfully loaded member from persistence'); | ||
} else { | ||
// user was not enrolled, so we will need an admin user object to register | ||
logger.info('User %s was not enrolled, so we will need an admin user object to register',username); | ||
var admins = hfc.getConfigSetting('admins'); | ||
let adminUserObj = await client.setUserContext({username: admins[0].username, password: admins[0].secret}); | ||
let caClient = client.getCertificateAuthority(); | ||
let secret = await caClient.register({ | ||
enrollmentID: username, | ||
affiliation: userOrg.toLowerCase() + '.department1' | ||
}, adminUserObj); | ||
logger.debug('Successfully got the secret for user %s',username); | ||
user = await client.setUserContext({username:username, password:secret}); | ||
logger.debug('Successfully enrolled username %s and setUserContext on the client object', username); | ||
} | ||
if(user && user.isEnrolled) { | ||
if (isJson && isJson === true) { | ||
var response = { | ||
success: true, | ||
secret: user._enrollmentSecret, | ||
message: username + ' enrolled Successfully', | ||
}; | ||
return response; | ||
} | ||
} else { | ||
throw new Error('User was not enrolled '); | ||
} | ||
} catch(error) { | ||
logger.error('Failed to get registered user: %s with error: %s', username, error.toString()); | ||
return 'failed '+error.toString(); | ||
} | ||
|
||
}; | ||
|
||
|
||
var setupChaincodeDeploy = function() { | ||
process.env.GOPATH = path.join(__dirname, hfc.getConfigSetting('CC_SRC_PATH')); | ||
}; | ||
|
||
var getLogger = function(moduleName) { | ||
var logger = log4js.getLogger(moduleName); | ||
logger.setLevel('DEBUG'); | ||
return logger; | ||
}; | ||
|
||
exports.getClientForOrg = getClientForOrg; | ||
exports.getLogger = getLogger; | ||
exports.setupChaincodeDeploy = setupChaincodeDeploy; | ||
exports.getRegisteredUser = getRegisteredUser; |
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,86 @@ | ||
/** | ||
* Copyright 2017 IBM All Rights Reserved. | ||
* | ||
* 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'; | ||
var util = require('util'); | ||
var helper = require('./helper.js'); | ||
var logger = helper.getLogger('install-chaincode'); | ||
|
||
var installChaincode = async function(peers, chaincodeName, chaincodePath, | ||
chaincodeVersion, chaincodeType, username, org_name) { | ||
logger.debug('\n\n============ Install chaincode on organizations ============\n'); | ||
helper.setupChaincodeDeploy(); | ||
let error_message = null; | ||
try { | ||
logger.info('Calling peers in organization "%s" to join the channel', org_name); | ||
|
||
// first setup the client for this org | ||
var client = await helper.getClientForOrg(org_name, username); | ||
logger.debug('Successfully got the fabric client for the organization "%s"', org_name); | ||
|
||
var request = { | ||
targets: peers, | ||
chaincodePath: chaincodePath, | ||
chaincodeId: chaincodeName, | ||
chaincodeVersion: chaincodeVersion, | ||
chaincodeType: chaincodeType | ||
}; | ||
let results = await client.installChaincode(request); | ||
// the returned object has both the endorsement results | ||
// and the actual proposal, the proposal will be needed | ||
// later when we send a transaction to the orederer | ||
var proposalResponses = results[0]; | ||
var proposal = results[1]; | ||
|
||
// lets have a look at the responses to see if they are | ||
// all good, if good they will also include signatures | ||
// required to be committed | ||
for (const i in proposalResponses) { | ||
if (proposalResponses[i] instanceof Error) { | ||
error_message = util.format('install proposal resulted in an error :: %s', proposalResponses[i].toString()); | ||
logger.error(error_message); | ||
} else if (proposalResponses[i].response && proposalResponses[i].response.status === 200) { | ||
logger.info('install proposal was good'); | ||
} else { | ||
all_good = false; | ||
error_message = util.format('install proposal was bad for an unknown reason %j', proposalResponses[i]); | ||
logger.error(error_message); | ||
} | ||
} | ||
} catch(error) { | ||
logger.error('Failed to install due to error: ' + error.stack ? error.stack : error); | ||
error_message = error.toString(); | ||
} | ||
|
||
if (!error_message) { | ||
let message = util.format('Successfully installed chaincode'); | ||
logger.info(message); | ||
// build a response to send back to the REST caller | ||
const response = { | ||
success: true, | ||
message: message | ||
}; | ||
return response; | ||
} else { | ||
let message = util.format('Failed to install due to:%s',error_message); | ||
logger.error(message); | ||
const response = { | ||
success: false, | ||
message: message | ||
}; | ||
return response; | ||
} | ||
}; | ||
exports.installChaincode = installChaincode; |
Oops, something went wrong.