Wednesday, 28 February 2018

Checklist for setting up Hyperledger-Fabric block chain on Windows (local) and fixes - Sandeep Kanao

Checklist for setting up Hyperledger-Fabric block chain on Windows (local) and fixes - Sandeep Kanao

1.Docker
https://docs.docker.com/toolbox/toolbox_install_windows/
2. curl
https://curl.haxx.se/download.html

Add to  path
C:\curl\curl-7.58.0-win64-mingw\bin

lib
C:\curl\curl-7.58.0-win64-mingw\lib

3. GoLanguage
https://golang.org/dl/
https://golang.org/doc/install?download=go1.10.windows-amd64.msi

update path
PATH=%PATH%:C:/go/bin

update npm beofre installing windows build tools

4. npm install npm@5.6.0 -g

5. Node.js
https://nodejs.org/en/download/

6. npm install --global windows-build-tools (powershell admin)

7. npm install --global grpc (powershell admin)


8. git bash
https://git-scm.com/downloads (minggw64)

setx PATH=%PATH%;C:\Program Files\Docker Toolbox\

set
git config --global core.autocrlf false
git config --global core.longpaths true

open git and verify
git config --get core.autocrlf (should be false)
git config --get core.longpaths (should be true)

9. docker-machine create test (bash)

10 docker-machine.exe env test (bash)

11. eval $("C:\Program Files\Docker Toolbox\docker-machine.exe" env test)  --(bash-to-set all env vars)

12. Hyperledger
curl -sSL https://goo.gl/6wtTN5 | bash -s 1.1.0-alpha

13. cd c:/Users/kanao/fabric-samples/first-network
git clone -b master https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
git checkout v1.1.0-alpha

14 from bash
cd 
/c/Users/kanao/fabric-samples/first-network

 ./byfn.sh -m generate -c mychannel
./byfn.sh -m up -l node


**** To stop channel
./byfn.sh down



Check if anything fails


kill any stale or active containers
docker rm -f $(docker ps -aq)

2. Clear any cached networks
docker network prune

3. Remove chaincode image
docker rmi dev-peer0.org1.example.com-fabcar-1.0-

5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba

4. export FABRIC_START_TIMEOUT=10

5. npm install

6. ./startFabric.sh -- if error use powershell and issue --  restart-service *docker*

7. Node.js implementation
./startFabric.sh node

8. In a new cmd window -
docker logs -f ca.example.com

9. node enrollAdmin.js (bash)
*** if any errors, replace localhost in js file with ip 192.168.99.100 keep the port

10 error when running js
npm install --global windows-build-tools
then rerun "npm install"

11. Clear 
docker rm -f $(docker ps -aq)

---------------------------------------------
Install chaincode with new name or incremented version
Same name, but updated version:

peer chaincode install -n fabcar -v 2.0 -p github.com/fabcar
or same version, but new name:

peer chaincode install -n tickets -v 1.0 -p github.com/fabcar
Cleanup old chaincode container and install updated version of alternated chaincode over again.
You can use following command to remove old chaincode container image:

docker images | grep fabcar | awk '{print $2}' | docker rmi
--------------------------------------------

Restart again
update tags in docker-compose.yml
couchdb tags : https://hub.docker.com/r/hyperledger/fabric-couchdb/tags/

----------------------------------------------------------------------------


Patient data smart contract on Hyperledger-Fabric blockchain development - Sandeep Kanao

Patient data smart contract on Hyperledger-Fabric blockchain development (source code) - Sandeep Kanao


startFabric.sh



#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error
set -e

# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
starttime=$(date +%s)
LANGUAGE=${1:-"golang"}
CC_SRC_PATH=github.com/healthdata/go
if [ "$LANGUAGE" = "node" -o "$LANGUAGE" = "NODE" ]; then
 CC_SRC_PATH=/opt/gopath/src/github.com/healthdata/node
fi

# clean the keystore
rm -rf ./hfc-key-store

# launch network; create channel and join peer to channel
cd ../basic-network
./start.sh

# Now launch the CLI container in order to install, instantiate chaincode
# and prime the ledger with our 10 cars
docker-compose -f ./docker-compose.yml up -d cli

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n healthdata -v 1.1 -p "$CC_SRC_PATH" -l "$LANGUAGE"
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n healthdata -l "$LANGUAGE" -v 1.1 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
sleep 10
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n healthdata -c '{"function":"initLedger","Args":[""]}'

printf "\nTotal setup execution time : $(($(date +%s) - starttime)) secs ...\n\n\n"
printf "Start by installing required packages run 'npm install'\n"
printf "Then run 'node enrollAdmin.js', then 'node registerUser'\n\n"
printf "The 'node invoke.js' will fail until it has been updated with valid arguments\n"
printf "The 'node query.js' may be run at anytime once the user has been registered\n\n"





enrolladmin.js

'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
 * Enroll the admin user
 */

var Fabric_Client = require('fabric-client');
var Fabric_CA_Client = require('fabric-ca-client');

var path = require('path');
var util = require('util');
var os = require('os');

//
var fabric_client = new Fabric_Client();
var fabric_ca_client = null;
var admin_user = null;
var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
console.log(' Store path:'+store_path);

// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
Fabric_Client.newDefaultKeyValueStore({ path: store_path
}).then((state_store) => {
    // assign the store to the fabric client
    fabric_client.setStateStore(state_store);
    var crypto_suite = Fabric_Client.newCryptoSuite();
    // use the same location for the state store (where the users' certificate are kept)
    // and the crypto store (where the users' keys are kept)
    var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
    crypto_suite.setCryptoKeyStore(crypto_store);
    fabric_client.setCryptoSuite(crypto_suite);
    var tlsOptions = {
     trustedRoots: [],
     verify: false
    };
    // be sure to change the http to https when the CA is running TLS enabled
    fabric_ca_client = new Fabric_CA_Client('http://192.168.99.100:7054', tlsOptions , 'ca.example.com', crypto_suite);

    // first check to see if the admin is already enrolled
    return fabric_client.getUserContext('admin', true);
}).then((user_from_store) => {
    if (user_from_store && user_from_store.isEnrolled()) {
        console.log('Successfully loaded admin from persistence');
        admin_user = user_from_store;
        return null;
    } else {
        // need to enroll it with CA server
        return fabric_ca_client.enroll({
          enrollmentID: 'admin',
          enrollmentSecret: 'adminpw'
        }).then((enrollment) => {
          console.log('Successfully enrolled admin user "admin"');
          return fabric_client.createUser(
              {username: 'admin',
                  mspid: 'Org1MSP',
                  cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }
              });
        }).then((user) => {
          admin_user = user;
          return fabric_client.setUserContext(admin_user);
        }).catch((err) => {
          console.error('Failed to enroll and persist admin. Error: ' + err.stack ? err.stack : err);
          throw new Error('Failed to enroll admin');
        });
    }
}).then(() => {
    console.log('Assigned the admin user to the fabric client ::' + admin_user.toString());
}).catch((err) => {
    console.error('Failed to enroll admin: ' + err);
});


query.js

'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
 * Chaincode query
 */

var Fabric_Client = require('fabric-client');
var path = require('path');
var util = require('util');
var os = require('os');

//
var fabric_client = new Fabric_Client();

// setup the fabric network
var channel = fabric_client.newChannel('mychannel');
var peer = fabric_client.newPeer('grpc://192.168.99.100:7051');
channel.addPeer(peer);

//
var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
console.log('Store path:'+store_path);
var tx_id = null;

// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
Fabric_Client.newDefaultKeyValueStore({ path: store_path
}).then((state_store) => {
 // assign the store to the fabric client
 fabric_client.setStateStore(state_store);
 var crypto_suite = Fabric_Client.newCryptoSuite();
 // use the same location for the state store (where the users' certificate are kept)
 // and the crypto store (where the users' keys are kept)
 var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
 crypto_suite.setCryptoKeyStore(crypto_store);
 fabric_client.setCryptoSuite(crypto_suite);

 // get the enrolled user from persistence, this user will sign all requests
 return fabric_client.getUserContext('user1', true);
}).then((user_from_store) => {
 if (user_from_store && user_from_store.isEnrolled()) {
  console.log('Successfully loaded user1 from persistence');
  member_user = user_from_store;
 } else {
  throw new Error('Failed to get user1.... run registerUser.js');
 }

 // queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],
 // queryAllCars chaincode function - requires no arguments , ex: args: [''],
 const request = {
  //targets : --- letting this default to the peers assigned to the channel
  chaincodeId: 'healthdata',
  fcn: 'queryPatient',
  args: ['PATIENT1']
 };

 // send the query proposal to the peer
 return channel.queryByChaincode(request);
}).then((query_responses) => {
 console.log("Query has completed, checking results");
 // query_responses could have more than one  results if there multiple peers were used as targets
 if (query_responses && query_responses.length == 1) {
  if (query_responses[0] instanceof Error) {
   console.error("error from query = ", query_responses[0]);
  } else {
   console.log("Response is ", query_responses[0].toString());
  }
 } else {
  console.log("No payloads were returned from query");
 }
}).catch((err) => {
 console.error('Failed to query successfully :: ' + err);
});



registerUser.js


'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
 * Register and Enroll a user
 */

var Fabric_Client = require('fabric-client');
var Fabric_CA_Client = require('fabric-ca-client');

var path = require('path');
var util = require('util');
var os = require('os');

//
var fabric_client = new Fabric_Client();
var fabric_ca_client = null;
var admin_user = null;
var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
console.log(' Store path:'+store_path);

// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
Fabric_Client.newDefaultKeyValueStore({ path: store_path
}).then((state_store) => {
    // assign the store to the fabric client
    fabric_client.setStateStore(state_store);
    var crypto_suite = Fabric_Client.newCryptoSuite();
    // use the same location for the state store (where the users' certificate are kept)
    // and the crypto store (where the users' keys are kept)
    var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
    crypto_suite.setCryptoKeyStore(crypto_store);
    fabric_client.setCryptoSuite(crypto_suite);
    var tlsOptions = {
     trustedRoots: [],
     verify: false
    };
    // be sure to change the http to https when the CA is running TLS enabled
    fabric_ca_client = new Fabric_CA_Client('http://192.168.99.100:7054', null , '', crypto_suite);

    // first check to see if the admin is already enrolled
    return fabric_client.getUserContext('admin', true);
}).then((user_from_store) => {
    if (user_from_store && user_from_store.isEnrolled()) {
        console.log('Successfully loaded admin from persistence');
        admin_user = user_from_store;
    } else {
        throw new Error('Failed to get admin.... run enrollAdmin.js');
    }

    // at this point we should have the admin user
    // first need to register the user with the CA server
    return fabric_ca_client.register({enrollmentID: 'user1', affiliation: 'org1.department1',role: 'client'}, admin_user);
}).then((secret) => {
    // next we need to enroll the user with CA server
    console.log('Successfully registered user1 - secret:'+ secret);

    return fabric_ca_client.enroll({enrollmentID: 'user1', enrollmentSecret: secret});
}).then((enrollment) => {
  console.log('Successfully enrolled member user "user1" ');
  return fabric_client.createUser(
     {username: 'user1',
     mspid: 'Org1MSP',
     cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }
     });
}).then((user) => {
     member_user = user;

     return fabric_client.setUserContext(member_user);
}).then(()=>{
     console.log('User1 was successfully registered and enrolled and is ready to intreact with the fabric network');

}).catch((err) => {
    console.error('Failed to register: ' + err);
 if(err.toString().indexOf('Authorization') > -1) {
  console.error('Authorization failures may be caused by having admin credentials from a previous CA instance.\n' +
  'Try again after deleting the contents of the store directory '+store_path);
 }
});


package.json

{
    "name": "healthdata",
    "version": "1.0.0",
    "private": true,
    "description": "Hyperledger Fabric healthdata Sample Application",
    "main": "healthdata.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "dependencies": {
        "fabric-ca-client": "unstable",
        "fabric-client": "unstable",
        "grpc": "^1.6.0"
    },
    "author": "Sandeep Kanao",
    "license": "Apache-2.0",
    "keywords": [
        "Hyperledger",
        "Fabric",
        "healthdata",
        "Sample",
        "Application"
    ],
    "devDependencies": {
        "lite-server": "^2.3.0"
    }
}



invoke.js

'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
 * Chaincode Invoke
 */

var Fabric_Client = require('fabric-client');
var path = require('path');
var util = require('util');
var os = require('os');

//
var fabric_client = new Fabric_Client();

// setup the fabric network
var channel = fabric_client.newChannel('mychannel');
var peer = fabric_client.newPeer('grpc://192.168.99.100:7051');
channel.addPeer(peer);
var order = fabric_client.newOrderer('grpc://192.168.99.100:7050')
channel.addOrderer(order);

//
var member_user = null;
var store_path = path.join(__dirname, 'hfc-key-store');
console.log('Store path:'+store_path);
var tx_id = null;

// create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
Fabric_Client.newDefaultKeyValueStore({ path: store_path
}).then((state_store) => {
 // assign the store to the fabric client
 fabric_client.setStateStore(state_store);
 var crypto_suite = Fabric_Client.newCryptoSuite();
 // use the same location for the state store (where the users' certificate are kept)
 // and the crypto store (where the users' keys are kept)
 var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
 crypto_suite.setCryptoKeyStore(crypto_store);
 fabric_client.setCryptoSuite(crypto_suite);

 // get the enrolled user from persistence, this user will sign all requests
 return fabric_client.getUserContext('user1', true);
}).then((user_from_store) => {
 if (user_from_store && user_from_store.isEnrolled()) {
  console.log('Successfully loaded user1 from persistence');
  member_user = user_from_store;
 } else {
  throw new Error('Failed to get user1.... run registerUser.js');
 }

 // get a transaction id object based on the current user assigned to fabric client
 tx_id = fabric_client.newTransactionID();
 console.log("Assigning transaction_id: ", tx_id._transaction_id);

 // createCar chaincode function - requires 5 args, ex: args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'],
 // changeCarOwner chaincode function - requires 2 args , ex: args: ['CAR10', 'Dave'],
 // must send the proposal to endorsing peers
 var request = {
  //targets: let default to the peer assigned to the client
  chaincodeId: 'healthdata',
  fcn: 'createPatient',
  args: ['Sam22', 'Delhi', '02-01-1933', 'B-', '5555', '01-03-2018', 'xrp'],
  chainId: 'mychannel',
  txId: tx_id
 };

 // send the transaction proposal to the peers
 return channel.sendTransactionProposal(request);
}).then((results) => {
 var proposalResponses = results[0];
 var proposal = results[1];
 let isProposalGood = false;
 if (proposalResponses && proposalResponses[0].response &&
  proposalResponses[0].response.status === 200) {
   isProposalGood = true;
   console.log('Transaction proposal was good');
  } else {
   console.error('Transaction proposal was bad');
  }
 if (isProposalGood) {
  console.log(util.format(
   'Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s"',
   proposalResponses[0].response.status, proposalResponses[0].response.message));

  // build up the request for the orderer to have the transaction committed
  var request = {
   proposalResponses: proposalResponses,
   proposal: proposal
  };

  // set the transaction listener and set a timeout of 30 sec
  // if the transaction did not get committed within the timeout period,
  // report a TIMEOUT status
  var transaction_id_string = tx_id.getTransactionID(); //Get the transaction ID string to be used by the event processing
  var promises = [];

  var sendPromise = channel.sendTransaction(request);
  promises.push(sendPromise); //we want the send transaction first, so that we know where to check status

  // get an eventhub once the fabric client has a user assigned. The user
  // is required bacause the event registration must be signed
  let event_hub = fabric_client.newEventHub();
  event_hub.setPeerAddr('grpc://192.168.99.100:7053');

  // using resolve the promise so that result status may be processed
  // under the then clause rather than having the catch clause process
  // the status
  let txPromise = new Promise((resolve, reject) => {
   let handle = setTimeout(() => {
    event_hub.disconnect();
    resolve({event_status : 'TIMEOUT'}); //we could use reject(new Error('Trnasaction did not complete within 30 seconds'));
   }, 6000);
   event_hub.connect();
   event_hub.registerTxEvent(transaction_id_string, (tx, code) => {
    // this is the callback for transaction event status
    // first some clean up of event listener
    clearTimeout(handle);
    event_hub.unregisterTxEvent(transaction_id_string);
    event_hub.disconnect();

    // now let the application know what happened
    var return_status = {event_status : code, tx_id : transaction_id_string};
    if (code !== 'VALID') {
     console.error('The transaction was invalid, code = ' + code);
     resolve(return_status); // we could use reject(new Error('Problem with the tranaction, event status ::'+code));
    } else {
     console.log('The transaction has been committed on peer ' + event_hub._ep._endpoint.addr);
     resolve(return_status);
    }
   }, (err) => {
    //this is the callback if something goes wrong with the event registration or processing
    reject(new Error('There was a problem with the eventhub ::'+err));
   });
  });
  promises.push(txPromise);

  return Promise.all(promises);
 } else {
  console.error('Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...');
  throw new Error('Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...');
 }
}).then((results) => {
 console.log('Send transaction promise and event listener promise have completed');
 // check the results in the order the promises were added to the promise all list
 if (results && results[0] && results[0].status === 'SUCCESS') {
  console.log('Successfully sent transaction to the orderer.');
 } else {
  console.error('Failed to order the transaction. Error code: ' + response.status);
 }

 if(results && results[1] && results[1].event_status === 'VALID') {
  console.log('Successfully committed the change to the ledger by the peer');
 } else {
  console.log('Transaction failed to be committed to the ledger due to ::'+results[1].event_status);
 }
}).catch((err) => {
 console.error('Failed to invoke successfully :: ' + err);
});



Patient Medical Data on Hyper-ledger Fabric block chain using go and node.js - Sandeep Kanao

Patient Medical Data smart contract on Hyper-ledger Fabric block chain using go and node.js - Sandeep Kanao


Chaincode - smart contract

/*
 * The smart contract for documentation topic:
 * Writing healthdata smart contract
 */

package main

/* Imports
 * 4 utility libraries for formatting, handling bytes, reading and writing JSON, and string manipulation
 * 2 specific Hyperledger Fabric specific libraries for Smart Contracts
 */
import (
 "bytes"
 "encoding/json"
 "fmt"
 "strconv"

 "github.com/hyperledger/fabric/core/chaincode/shim"
 sc "github.com/hyperledger/fabric/protos/peer"
)

// Define the Smart Contract structure
type SmartContract struct {
}

// Define the patient structure, with  properties.  Structure tags are used by encoding/json library
type Patient struct {
 Name  string `json:"name"`
 Address  string  `json:"address"`
     DOB      string `json:"dob"` 
 BloodGroup   string `json:"bloodgroup"`
 DoctorID  string `json:"doctorid"`
 TreatmentDate   string `json:"treatmentdate"`
 Medicine string  `json:"medicine"`
}

/*
 * The Init method is called when the Smart Contract "healthdata" is instantiated by the blockchain network
 * Best practice is to have any Ledger initialization in separate function -- see initLedger()
 */
func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
 return shim.Success(nil)
}

/*
 * The Invoke method is called as a result of an application request to run the Smart Contract "healthdata"
 * The calling application program has also specified the particular smart contract function to be called, with arguments
 */
func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {

 // Retrieve the requested Smart Contract function and arguments
 function, args := APIstub.GetFunctionAndParameters()
 // Route to the appropriate handler function to interact with the ledger appropriately
 if function == "queryPatient" {
  return s.queryPatient(APIstub, args)
 } else if function == "initLedger" {
  return s.initLedger(APIstub)
 } else if function == "createPatient" {
  return s.createPatient(APIstub, args)
 } else if function == "queryAllPatients" {
  return s.queryAllPatients(APIstub)
 } else if function == "changeDoctorID" {
  return s.changeDoctorID(APIstub, args)
 }

 return shim.Error("Invalid Smart Contract function name.")
}

func (s *SmartContract) queryPatient(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

 if len(args) != 1 {
  return shim.Error("Incorrect number of arguments. Expecting 1")
 }

 patientAsBytes, _ := APIstub.GetState(args[0])
 return shim.Success(patientAsBytes)
}

func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
 patients := []Patient{
 Patient {Name: "John", Address: "New York", DOB: "03-02-1967", BloodGroup : "A+", DoctorID: "1111", TreatmentDate : "01-12-2018", Medicine : "Tynalol"},
Patient {Name: "Ken", Address: "New York", DOB: "06-02-1963", BloodGroup : "A+", DoctorID: "1112", TreatmentDate : "01-11-2018", Medicine : "Helix"},
Patient {Name: "Sam", Address: "New York", DOB: "06-02-1954", BloodGroup : "O", DoctorID: "1112", TreatmentDate : "02-11-2018", Medicine : "Benix"},
Patient {Name: "Kim", Address: "Toronto", DOB: "05-02-1933", BloodGroup : "O", DoctorID: "1113", TreatmentDate : "02-14-2018", Medicine : "Benix"},
Patient {Name: "Kit", Address: "New York", DOB: "06-02-1955", BloodGroup : "B-", DoctorID: "1112", TreatmentDate : "02-11-2018", Medicine : "Qrex"},
}



 i := 0
 for i < len(patients) {
  fmt.Println("i is ", i)
  patientAsBytes, _ := json.Marshal(patients[i])
  APIstub.PutState("PATIENT"+strconv.Itoa(i), patientAsBytes)
  fmt.Println("Added", patients[i])
  i = i + 1
 }

 return shim.Success(nil)
}

func (s *SmartContract) createPatient(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

 if len(args) != 7 {
  return shim.Error("Incorrect number of arguments. Expecting 7")
 }

 var patient = Patient{Name: args[1], Address: args[2], DOB: args[3], BloodGroup: args[4], DoctorID: args[5], TreatmentDate: args[6], Medicine: args[7]}

 patientAsBytes, _ := json.Marshal(patient)
 APIstub.PutState(args[0], patientAsBytes)

 return shim.Success(nil)
}

func (s *SmartContract) queryAllPatients(APIstub shim.ChaincodeStubInterface) sc.Response {

 startKey := "PATIENT1"
 endKey := "PATIENT22"

 resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)
 if err != nil {
  return shim.Error(err.Error())
 }
 defer resultsIterator.Close()

 // buffer is a JSON array containing QueryResults
 var buffer bytes.Buffer
 buffer.WriteString("[")

 bArrayMemberAlreadyWritten := false
 for resultsIterator.HasNext() {
  queryResponse, err := resultsIterator.Next()
  if err != nil {
   return shim.Error(err.Error())
  }
  // Add a comma before array members, suppress it for the first array member
  if bArrayMemberAlreadyWritten == true {
   buffer.WriteString(",")
  }
  buffer.WriteString("{\"Key\":")
  buffer.WriteString("\"")
  buffer.WriteString(queryResponse.Key)
  buffer.WriteString("\"")

  buffer.WriteString(", \"Record\":")
  // Record is a JSON object, so we write as-is
  buffer.WriteString(string(queryResponse.Value))
  buffer.WriteString("}")
  bArrayMemberAlreadyWritten = true
 }
 buffer.WriteString("]")

 fmt.Printf("- queryAllPatients:\n%s\n", buffer.String())

 return shim.Success(buffer.Bytes())
}

func (s *SmartContract) changeDoctorID(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {

 if len(args) != 2 {
  return shim.Error("Incorrect number of arguments. Expecting 2")
 }

 patientAsBytes, _ := APIstub.GetState(args[0])
 patient := Patient{}

 json.Unmarshal(patientAsBytes, &patient)
 patient.DoctorID = args[5]

 patientAsBytes, _ = json.Marshal(patient)
 APIstub.PutState(args[0], patientAsBytes)

 return shim.Success(nil)
}

// The main function is only relevant in unit test mode. Only included here for completeness.
func main() {

 // Create a new Smart Contract
 err := shim.Start(new(SmartContract))
 if err != nil {
  fmt.Printf("Error creating new Smart Contract: %s", err)
 }
}



Ethereum based ERC-20 MedicCoin (alt crypto currency) development - Sandeep Kanao


Ethereum based ERC-20 MedicCoin  (Alt coin) smart contract development - Sandeep Kanao


1 ETH = 800 MedicCoin

Remix is used to develop the following ERC-20 smart contract (aka altcoin) using Solidity


pragma solidity ^0.4.20;

contract Token {

    /// @return total amount of tokens
    function totalSupply() constant returns (uint256 supply) {}

    /// @param _owner The address from which the balance will be retrieved
    /// @return The balance
    function balanceOf(address _owner) constant returns (uint256 balance) {}

    /// @notice send `_value` token to `_to` from `msg.sender`
    /// @param _to The address of the recipient
    /// @param _value The amount of token to be transferred
    /// @return Whether the transfer was successful or not
    function transfer(address _to, uint256 _value) returns (bool success) {}

    /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
    /// @param _from The address of the sender
    /// @param _to The address of the recipient
    /// @param _value The amount of token to be transferred
    /// @return Whether the transfer was successful or not
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}

    /// @notice `msg.sender` approves `_addr` to spend `_value` tokens
    /// @param _spender The address of the account able to transfer the tokens
    /// @param _value The amount of wei to be approved for transfer
    /// @return Whether the approval was successful or not
    function approve(address _spender, uint256 _value) returns (bool success) {}

    /// @param _owner The address of the account owning tokens
    /// @param _spender The address of the account able to transfer the tokens
    /// @return Amount of remaining tokens allowed to spent
    function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

contract StandardToken is Token {

    function transfer(address _to, uint256 _value) returns (bool success) {
        //Default assumes totalSupply can't be over max (2^256 - 1).
        //If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap.
        //Replace the if with this one instead.
        //if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
        if (balances[msg.sender] >= _value && _value > 0) {
            balances[msg.sender] -= _value;
            balances[_to] += _value;
            Transfer(msg.sender, _to, _value);
            return true;
        } else { return false; }
    }

    function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
        //same as above. Replace this line with the following if you want to protect against wrapping uints.
        //if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
        if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) {
            balances[_to] += _value;
            balances[_from] -= _value;
            allowed[_from][msg.sender] -= _value;
            Transfer(_from, _to, _value);
            return true;
        } else { return false; }
    }

    function balanceOf(address _owner) constant returns (uint256 balance) {
        return balances[_owner];
    }

    function approve(address _spender, uint256 _value) returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        return true;
    }

    function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
      return allowed[_owner][_spender];
    }

    mapping (address => uint256) balances;
    mapping (address => mapping (address => uint256)) allowed;
    uint256 public totalSupply;
}

contract MedicCoin is StandardToken { // CHANGE THIS. Update the contract name.

    /* Public variables of the token */

    /*
    NOTE:
    The following variables are OPTIONAL vanities. One does not have to include them.
    They allow one to customise the token contract & in no way influences the core functionality.
    Some wallets/interfaces might not even bother to look at this information.
    */
    string public name;                   // Token Name
    uint8 public decimals;                // How many decimals to show. To be standard complicant keep it 18
    string public symbol;                 // An identifier: eg SBX, XPR etc..
    string public version = 'H1.0'; 
    uint256 public unitsOneEthCanBuy;     // How many units of your coin can be bought by 1 ETH?
    uint256 public totalEthInWei;         // WEI is the smallest unit of ETH (the equivalent of cent in USD or satoshi in BTC). We'll store the total ETH raised via our ICO here.  
    address public fundsWallet;           // Where should the raised ETH go?

    // This is a constructor function 
    // which means the following function name has to match the contract name declared above
    function MedicCoin() {
        balances[msg.sender] = 1000000000000000000000;               // Give the creator all initial tokens. This is set to 1000 for example. If you want your initial tokens to be X and your decimal is 5, set this value to X * 100000. (CHANGE THIS)
        totalSupply = 1000000000000000000000;                        // Update total supply (1000 for example) (CHANGE THIS)
        name = "MedicCoin";                                   // Set the name for display purposes (CHANGE THIS)
        decimals = 18;                                               // Amount of decimals for display purposes (CHANGE THIS)
        symbol = "MEDIC";                                             // Set the symbol for display purposes (CHANGE THIS)
        unitsOneEthCanBuy = 800;                                      // Set the price of your token for the ICO (CHANGE THIS)
        fundsWallet = msg.sender;                                    // The owner of the contract gets ETH
    }

    function() payable{
        totalEthInWei = totalEthInWei + msg.value;
        uint256 amount = msg.value * unitsOneEthCanBuy;
        if (balances[fundsWallet] < amount) {
            return;
        }

        balances[fundsWallet] = balances[fundsWallet] - amount;
        balances[msg.sender] = balances[msg.sender] + amount;

        Transfer(fundsWallet, msg.sender, amount); // Broadcast a message to the blockchain

        //Transfer ether to fundsWallet
        fundsWallet.transfer(msg.value);                               
    }

    /* Approves and then calls the receiving contract */
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);

        //call the receiveApproval function on the contract you want to be notified. This crafts the function signature manually so one doesn't have to include a contract in here just for this.
        //receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData)
        //it is assumed that when does this that the call *should* succeed, otherwise one would use vanilla approve instead.
        if(!_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { throw; }
        return true;
    }
}





Secured and reliable sharing of patient data on the blockchain - Sandeep Kanao


Secured and reliable sharing of  patient data  on the blockchain - Sandeep Kanao

MedicDataX transforms  patients medical history  to insurance,  clinical trial companies, pharma companies through blockchain technology

Business case :
Patient shares data with unique private key on secured  Hyperledger-Fabric block-chain.
Company (Pharma, clinical trial, insurance etc.) pays patient  using MedicToken to access the data.
Patient dictates  data access permissions (whom, which data and compensation token amount etc.) using smart-contract.

Details:
Pharma, clinical trial, insurance companies need real time health data from various people groups. The lack of access to such data not only results in costlier, less efficient care; it causes errors that can lead to serious adverse events, including patent registration for a new drug, financial loss due to false insurance claims etc.

The current system often prevents the timely access of this data across geographic locations and systems. Interoperability has been dubbed by some as health IT’s Holy Grail. It’s a cliché, but it’s true: It’s deeply desired yet seemingly unattainable. Interoperability has long been a goal–and a Meaningful Use requirement–but it has yet to be put into practice. Aside from the technological challenges to interoperability, there are those posed by data blocking[1] and the practice of charging patients for copies of their own electronic records.
Comprehensive patient information should automatically–and securely–follow the patient across the healthcare continuum, and patients should have unrestricted access to their own complete medical records and should be fairly compensated for sharing the data.
MedicDataX, a blockchain-based solution for healthcare, changes the “should” to “will.” Launched in January 2018, MedicDataX is the antithesis of the siloed, centralized data stores that dominate healthcare data today. 
Understanding blockchain
Blockchain was designed as the distributed accounting platform for cryptocurrencies (e.g., Bitcoin). But it’s an industry-agnostic tool to keep data in an encrypted, distributed, ledger–and to control access to that ledger. [2] It’s decentralized, spread across a synchronized network; all the data are visible to anyone with proper authorization. As a result, there’s no single repository for hackers to target. [3]
We’re applying this approach to electronic medical records. MedicDataX is decentralizing data while making it more accessible.
Beyond push, pull and view
Medical data among institutions is generally shared one of three ways: push, pull or view. But each approach varies by vendor, institution and state law. MedicDataX offers a different model altogether.
A patient’s data is stored securely in the community-wide ledger, giving clinicians, hospitals and other relevant organizations access to a patient’s complete health (not just medical) history. Because the entire chain is updated in real time, any changes to medication and allergy lists will be immediately available. That also means there’s no need re-enter the same medical data at each encounter with a new provider or institution. It dramatically reduces the likelihood of  costly duplicative –and therefore– wasteful screenings and services.
The MedicDataX model provides a decentralized control mechanism. Everyone In the chain has a stake, but no one owns it exclusively. A clear audit trail ensures accountability. This approach allows for control of record access without the need to create custom functionality for each vendor.
This approach also gives patients control over their data, unlike most, if not all, current approaches. 
Patient-centered
Patients have full access to their data as well as control over how it is shared.[4] In accomplishing this, MedicDataX achieves the ONC’s call for individuals to “have access to longitudinal electronic health information” to which they can add information and “direct … to any electronic location.” 
This record would include patient-generated data. It would accept data from the patient’s Fitbit (or other wearable), 23andMe profile, biometric data, etc. Patients can build a holistic record of their medical data and authorize others for viewership, such as pharma companies, clinical trial companies [5] This allows individuals to be consumers of healthcare and to control their own data, insurance and care.
Secure and immutable
MedicDataX’s decentralization is the key to its security. Once a record is submitted to the chain, it becomes immutable–it can’t be updated or changed. The records are synced and stored on all nodes worldwide. To hack one record, the hacker would have to hack the entire system and change the information in every computer, simultaneously. Not only would that put everyone on alert, but sheer computational power required would be too much for even the most sophisticated hacker. 
Credentialed users can add to–but not delete or change–the records. Transactions must be verified by the network.
The system also offers protection against inaccurate information being entered. A consensus algorithm newly entered information against the existing record. So, for instance, if a hospital adds data indicating the patient has blood type A, but existing data block indicates the type is O, then the information will not be recorded in a blockchain.
What MedicDataX offers goes far beyond electronic records, however.
Better insurance claims
MedChainX offers the potential of automating claims adjudication and payment processing, eliminating the need for intermediaries and reducing the administrative costs and time for providers and payors (and thereby also reducing cost and frustration for patients). It would also minimize, if not eliminate, the number of claims that are rejected due to incomplete or inaccurate patient information.
A new healthcare economy
MedicDataX will eventually use  Medictoken as value.
Medictokens are digital units of value created and managed inside a closed ecosystem. They can be used for network storage allocation, revenue payment cycles, provider incentives and more.  This will likely launch in 2018. Given that blockchain developed primarily in the financial sector, it makes sense to apply that concept to the healthcare marketplace.
A blockchain mobile wallet system can allow participants to use Medictokens to purchase health insurance, equipment, medication and other services–including storage space. For example, patients will be given an allotted amount of space to store information for free on the MedicDataX  network. These Medictokens tokens would allow them to purchase extra space from nodes set up in hospitals systems. Healthcare organizations use tokens in a similar fashion.
We will be using ERC-20, an open-source blockchain technology focused on real-world transactions.  Patient blockchain will be built using IBM and Linux backed blockchain technology – Hyperledger-Fabric.  Hyperledger-Fabric  binds digital assets, digital identity, smart contracts and a secure communication protocol into its platform.
Building on success
We are not reinventing the wheel. The financial services industry has already demonstrated the power of blockchain. The technologies for data storage, security and encryption already exist and are in use today. MedicDataX brings together the how and why, to better assist patients, providers and health systems. We think electronic medical records can be made more mobile, reliable, trusted, and secure, and we intend to make it happen. 
Ongoing development is underway for  Phase 2.
·        Scalability 
·        Transparency
·        MPI  (master patient index)
·        Data analytics
·        E-prescribing
ICO Development Phase 2 (Medicdatatokens)
********
[1] Report on Health Information Blocking, ONC, April 2015
[2]“Moving Patient Data Is Messy, But Blockchain Is Here to Help,” Wired, Feb. 2017 www.wired.com/2017/02/moving-patient-data-messy-blockchain-help/
[3] “What is Blockchain Technology? A Step-by-Step Guide for Beginners” blockgeeks.com/guides/what-is-blockchain-technology/
[4] Linn L, Koo M. Blockchain for Health Data and Its Potential Use in Health IT and Health Care Related Research. Posted by ONC April 2016
[5] Ekblaw A, Azaria A, Halamka J, Lippman A. “Case Study for Blockchain in Healthcare: ‘MedRec’ Prototype for Electronic Health Records and Medical Research Data.”  posted by the ONC April 2016


Monday, 26 February 2018

Building Smart Contract with Hyperledger Fabric on blockchain - Sandeep Kanao

Building Smart Contract with Hyperledger Fabric on blockchain - setting Hyperledger Fabric on Windows -Sandeep Kanao


Hyperledger Fabric is a blockchain framework implementation and one of the Hyperledger projects hosted by The Linux Foundation. Intended as a foundation for developing applications or solutions with a modular architecture, Hyperledger Fabric allows components, such as consensus and membership services, to be plug-and-play. Hyperledger Fabric leverages container technology to host smart contracts called “chaincode” that comprise the application logic of the system. Hyperledger Fabric was initially contributed by Digital Asset and IBM, as a result of the first hackathon.


Installation on Windows-7 64 bit- getting started :

1. Docker
https://docs.docker.com/toolbox/toolbox_install_windows/

2. curl
https://curl.haxx.se/download.html

Add to  path
C:\curl\curl-7.58.0-win64-mingw\bin

lib
C:\curl\curl-7.58.0-win64-mingw\lib

3. GoLanguage
https://golang.org/dl/
https://golang.org/doc/install?download=go1.10.windows-amd64.msi

update path
PATH=%PATH%:C:/go/bin

update npm beofre installing windows build tools

4. npm install npm@5.6.0 -g

5. Node.js
https://nodejs.org/en/download/

6. npm install --global windows-build-tools (powershell admin)

7. npm install --global grpc (powershell admin)


8. git bash
https://git-scm.com/downloads (minggw64)

setx PATH=%PATH%;C:\Program Files\Docker Toolbox\

set
git config --global core.autocrlf false
git config --global core.longpaths true

open git and verify
git config --get core.autocrlf (should be false)
git config --get core.longpaths (should be true)

9. docker-machine create test (bash)

10 docker-machine.exe env test (bash)

11. eval $("C:\Program Files\Docker Toolbox\docker-machine.exe" env test)  --(bash-to-set all env vars)

12. Hyperledger - sandeep kanao
curl -sSL https://goo.gl/6wtTN5 | bash -s 1.1.0-alpha

13. cd c:/Users/kanao/fabric-samples/first-network
git clone -b master https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
git checkout v1.1.0-alpha

14 from bash
cd
/c/Users/kanao/fabric-samples/first-network

 ./byfn.sh -m generate -c mychannel
./byfn.sh -m up -l node


**** To stop channel
./byfn.sh down





Thursday, 22 February 2018

ERC-20 Smart Contract on Ethereum block chain -Sandeep Kanao-

ERC-20 smart contract on Ethereum block chain using Solidity


Remix Solidity can be accessed at - Remix

Sample Solidity ERC-20 Ethereum Smart contract on Ethereum blockchain (Ropster Test Net)-

pragma solidity ^0.4.4;
contract Counter {
    int private count = 0;
    function incrementCounter() public {
        count += 1;
    }
    function decrementCounter() public {
        count -= 1;
    }
    function getCount() public constant returns (int) {
        return count;
    }
}

2. Make sure Metamax browser extension is installed, and set to Ropsten Test Net account

3. Compile and Run and view transaction at Etherscan