Skip to content

Commit

Permalink
intial sdk update
Browse files Browse the repository at this point in the history
  • Loading branch information
dshuffma-ibm committed Mar 17, 2017
1 parent 5fb1ad2 commit 878dbc9
Show file tree
Hide file tree
Showing 15 changed files with 927 additions and 426 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ certificate.pem
/chaincode/Dockerfile
chaincode.exe
app_state*
/utils/fc_wrangler/kvs
154 changes: 43 additions & 111 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ var url = require('url');
var fs = require('fs');
var cors = require('cors');
var async = require('async');
var ws = require('ws'); //websocket module
var block_delay = 10000;
var ws = require('ws'); //websocket module

// --- Set Our Things --- //
var HFC = require('fabric-client');
var Orderer = require('fabric-client/lib/Orderer.js');
var User = require('fabric-client/lib/User.js');
var CaService = require('fabric-ca-client/lib/FabricCAClientImpl.js');
var block_delay = 10000;
var logger = { //overwrite console to work with info/warn/debug
log: console.log,
info: console.log,
error: console.error,
warn: console.log,
debug: console.log
};
var fcw = require('./utils/fc_wrangler/index.js')(logger);

var more_entropy = randStr(32);
var ws_server = require('./utils/websocket_server_side.js')(null, null, null);
var helper = require(__dirname + '/utils/helper.js')(process.env.creds_filename, console);
var helper = require(__dirname + '/utils/helper.js')(process.env.creds_filename, logger);
var host = 'localhost';
var port = helper.getMarblesPort();
var wss = {};
Expand Down Expand Up @@ -128,13 +132,9 @@ else console.log('Running using Developer settings');
// ==================================
// Set up the blockchain sdk
// ==================================
var chain = null;
var network_id = helper.getNetworkId();
var uuid = network_id;
var enrollObj = null;
var marbles_lib = null;



// -------------------------------------------------------------------
// Life Starts Here!
// -------------------------------------------------------------------
Expand All @@ -149,14 +149,13 @@ try{
console.log('\n\nDetected that we have launched successfully before');
console.log('Welcome back - Initiating start up\n\n');
process.env.app_first_setup = 'no';
enroll_admin(helper.getUsers(0).enrollId, helper.getUsers(0).enrollSecret, helper.getCasUrl(0), function(e){
enroll_admin(function(e){
if(e == null){
setup_marbles_lib();
}
});
}
else wait_to_init();

}
catch(e){
wait_to_init();
Expand All @@ -170,24 +169,18 @@ function wait_to_init(){
}
// -------------------------------------------------------------------


//setup marbles library and check if cc is deployed
function setup_marbles_lib(){
console.log('Setup Marbles Lib...');

try{
chain.addOrderer(new Orderer(helper.getOrderersUrl(0)));
}
catch(e){
//may error with duplicate orderer, thats ok
}
var opts = {
channel_id: helper.getChannelId(),
chaincode_id: helper.getChaincodeId(),
event_url: helper.getEventUrl(0)
event_url: helper.getPeerEventUrl(0),
chaincode_version: helper.getChaincodeVersion(),
};
marbles_lib = require('./utils/marbles_cc_lib.js')(chain, opts, console);
ws_server.setup(chain, marbles_lib, wss.broadcast, null);
marbles_lib = require('./utils/marbles_cc_lib.js')(enrollObj, opts, console);
ws_server.setup(enrollObj, marbles_lib, wss.broadcast, null);

console.log('Checking if chaincode is already deployed or not');
var options = {
Expand All @@ -212,82 +205,27 @@ function setup_marbles_lib(){
});
}

//enroll admin
function enroll_admin(id, secret, ca_url, cb){
try {
var client = new HFC();
chain = client.newChain('mychain' + file_safe_name(process.env.marble_company) + '-' + uuid);
}
catch (e) {
//it might error about 1 chain per network, but that's not a problem just continue
}

// Make Cert kvs
HFC.newDefaultKeyValueStore({
path: path.join(__dirname, './keyValStore-' + file_safe_name(process.env.marble_company) + '-' + uuid)
}).then(function(store){
client.setStateStore(store);
console.log('! using id', id, 'secret', secret);
return getSubmitter(id, secret, ca_url, client); //do most of the work here
}).then(function(submitter){

// --- Success --- //
console.log('Successfully enrolled ' + id);
//chain = submitter; //push var to higher scope
broadcast_state('enrolled');
setTimeout(function(){
if(cb) cb();
}, block_delay);

}).catch(

// --- Failure --- //
function(err) {
console.log('Failed to enroll ' + id, err.stack ? err.stack : err);
broadcast_state('failed_enroll');
if(cb) cb(err);
}
);
}

// Get Submitter - ripped this function off from helper.js in fabric-client
function getSubmitter(enroll_id, enroll_secret, ca_url, client) {
var member;
return client.getUserContext(enroll_id)
.then((user) => {
if (user && user.isEnrolled()) {
console.log('Successfully loaded admin from persistence');
return user;
} else {

// Need to enroll it with CA server
var ca_client = new CaService(ca_url);
return ca_client.enroll({
enrollmentID: enroll_id,
enrollmentSecret: enroll_secret

// Store Certs
}).then((enrollment) => {
console.log('Successfully enrolled admin \'' + enroll_id + '\'');
member = new User(enroll_id, client);
return member.setEnrollment(enrollment.key, enrollment.certificate);

// Save Submitter Enrollment
}).then(() => {
return client.setUserContext(member);

// Return Submitter Enrollment
}).then(() => {
return member;

// Send Errors to Callback
}).catch((err) => {
console.log('Failed to enroll and persist admin. Error: ' + err.stack ? err.stack : err);
throw new Error('Failed to enrolled admin');
});
}
//enroll an admin with the CA for this peer/channel
function enroll_admin(cb){
var user = helper.getUser(0);
var options = {
channel_id: helper.getChannelId(),
uuid: helper.getNetworkId() + '-' + helper.getChannelId(),
ca_url: helper.getCasUrl(0),
orderer_url: helper.getOrderersUrl(0),
enroll_id: user.enrollId,
enroll_secret: user.enrollSecret,
msp_id: helper.getPeersMspId(0)
};
fcw.enroll(options, function(errCode, obj){
if (errCode != null) {
console.error('could not enroll');
if(cb) cb(errCode);
} else{
enrollObj = obj;
if(cb) cb(null);
}
);
});
}

//random integer
Expand Down Expand Up @@ -467,7 +405,7 @@ function setupWebSocket(){
//enroll admin
if(data.configure === 'enrollment'){
helper.write(data); //write new config data to file
enroll_admin(helper.getUsers(0).enrollId, helper.getUsers(0).enrollSecret, helper.getCasUrl(0), function(e){
enroll_admin(function(e){
if(e == null){
setup_marbles_lib();
}
Expand All @@ -481,24 +419,18 @@ function setupWebSocket(){
}

//deploy chaincode
else if(data.configure === 'deploy_chaincode'){
/*else if(data.configure === 'deploy_chaincode'){
helper.write(data); //write new config data to file
try{
chain.addOrderer(new Orderer(helper.getOrderersUrl(0)));
}
catch(e){
//may error with duplicate orderer, thats ok
}
var opts = {
channel_id: helper.getChannelId(),
chaincode_id: helper.getChaincodeId(),
event_url: helper.getEventUrl(0)
event_url: helper.getPeerEventUrl(0)
};
var temp_marbles_lib = require('./utils/marbles_cc_lib.js')(chain, opts, null);
temp_marbles_lib.deploy_chaincode(chain, null, function(){
var temp_marbles_lib = require('./utils/marbles_cc_lib.js')(enrollObj.chain, opts, null);
temp_marbles_lib.deploy_chaincode(enrollObj.chain, null, function(){
setup_marbles_lib();
});
}
}*/

//register marble owners
else if(data.configure === 'register'){
Expand Down
2 changes: 1 addition & 1 deletion busters_js.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"public/js/singlejshash":"9cee6fcacd7ecf3865903b828dcb0e31"}
{"public/js/singlejshash":"b021d592d23ae831b6e754e4d912fe72"}
86 changes: 37 additions & 49 deletions config/mycreds.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,39 @@
{
"credentials": {
"network_id": "NETWORK_ID",
"peers": [
{
"grpc_host": "PEER1_HOST",
"grpc_port": "PEER1_PORT",
"event_host": "PEER1_EVENT_HOST",
"event_port:": "PEER1_EVENT_PORT",
"type": "peer",
"network_id": "NETWORK_ID",
"id": "peer1"
}
],
"cas": [
{
"id": "NETWORK_ID-ca",
"host": "COP_HOST",
"port": "COP_PORT",
"type": "ca",
"network_id": "NETWORK_ID"
}
],
"orderers": [
{
"host": "ORDERER_HOST",
"port": "ORDERER_PORT",
"type": "orderer",
"network_id": "NETWORK_ID",
"id": "orderer-01"
}
],
"users": [
{
"enrollId": "ADMIN",
"enrollSecret": "ADMINPW"
}
],
"cert": "https://blockchain-certs.mybluemix.net/us.blockchain.ibm.com.cert",
"marbles": {
"company": "COMPANY",
"chaincode_id": "CHAINCODE_ID",
"usernames": [
"USER1",
"USER2",
"USER3"
],
"port": "MARBLES_PORT"
}
}
"credentials": {
"network_id": "asdf",
"peers": [
{
"discovery": "grpc://varadvm2.rtp.raleigh.ibm.com:7051",
"event": "grpc://varadvm2.rtp.raleigh.ibm.com:7053",
"msp_id": "Org1MSP"
}
],
"cas": [
{
"api": "http://varadvm2.rtp.raleigh.ibm.com:7054"
}
],
"orderers": [
{
"discovery": "grpc://varadvm2.rtp.raleigh.ibm.com:7050"
}
],
"users": [
{
"enrollId": "admin",
"enrollSecret": "adminpw"
}
],
"marbles": {
"company": "eMarbles",
"chaincode_id": "marbles",
"chaincode_version": "v0",
"channel_id": "mychannel",
"usernames": [
"cliff",
"cody"
],
"port": 3002
}
}
}
26 changes: 3 additions & 23 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ gulp.task('server', function(a, b) {
return;
});


// ---------------- Watch for Changes Tasks ---------------- //
gulp.task('watch-sass', ['build-sass'], function () {
gulp.watch(path.join(__dirname, '/scss/*.scss'), ['build-sass']);
Expand All @@ -51,38 +50,19 @@ gulp.task('watch-js', ['build-js-hash'], function () {

gulp.task('watch-server', function () {
gulp.watch(path.join(__dirname, '/routes/**/*.js'), ['server']);
gulp.watch([path.join(__dirname, '/utils/fc_wrangler/*.js')], ['server']);
gulp.watch([path.join(__dirname, '/utils/*.js')], ['server']);
gulp.watch(path.join(__dirname, '/app.js'), ['server']);
});


// ---------------- Gulp Tasks ---------------- //
gulp.task('default', ['watch-sass', 'watch-js', 'watch-server', 'server']); //run with command `gulp`
gulp.task('marbles', ['start_marbles', 'default']); //run with command `gulp marbles` [THIS ONE!]
gulp.task('united_marbles', ['start_mtc1', 'default']); //run with command `gulp united_marbles`
gulp.task('marble_market', ['start_mtc2', 'default']); //run with command `gulp marble_market`
gulp.task('emarbles', ['start_mtc3', 'default']); //run with command `gulp emarbles`
gulp.task('marbles', ['start_marbles', 'watch-sass', 'watch-js', 'watch-server', 'server']); //run with command `gulp marbles` [THIS ONE!]


//generic marbles
gulp.task('start_marbles', function () {
env['creds_filename'] = 'mycreds.json';
console.log('\n[International Marbles Trading Consortium]\n');
});

// IMTC Member 1
gulp.task('start_mtc1', function () {
console.log('\n[International Marbles Trading Consortium] - Member "United Marbles"\n');
env['creds_filename'] = 'creds_united_marbles.json';
});

// IMTC Member 2
gulp.task('start_mtc2', function () {
console.log('\n[International Marbles Trading Consortium] - Member "Marble Market"\n');
env['creds_filename'] = 'creds_marble_market.json';
});

// IMTC Member 3
gulp.task('start_mtc3', function () {
console.log('\n[International Marbles Trading Consortium] - Member "eMarbles"\n');
env['creds_filename'] = 'creds_emarbles.json';
});
Loading

0 comments on commit 878dbc9

Please sign in to comment.