Hyperledger fabric-sdk-java Basics Tutorial

Lukas Kolisko
Feb 20, 2018 · 5 min read

Tips:

# !!! THIS WILL REMOVE ALL YOUR DOCKER CONTAINERS AND IMAGES !!!
# remove all containers
$ docker rm $(docker ps -qa)
# remove all mages
$ docker rmi --force $(docker images -qa)
# prune networks
$ docker network prune
git clone https://github.com/lkolisko/scratch.git tutorial

Enrolling admin

CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
HFCAClient caClient = HFCAClient.createNewInstance(“grpc://localhost:7054”, null);
caClient.setCryptoSuite(cryptoSuite);

Enroll Admin

Enrollment adminEnrollment = caClient.enroll("admin", "adminpw");
AppUser admin = new AppUser("admin","org1", "Org1MSP", adminEnrollment);

Register and Enroll user

RegistrationRequest rr = new RegistrationRequest("hfuser", "org1");
String userSecret = caClient.register(rr, registrar);
Enrollment userEnrollment = caClient.enroll("hfuser", userSecret);
AppUser appUser = new AppUser("hfuser", "org1","Org1MSP", userEnrollment);

Initialize HF Client

CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
HFClient client = HFClient.createNewInstance();
client.setCryptoSuite(cryptoSuite);
client.setUserContext(appUser);

Initialize Channel object

Peer peer = client.newPeer("peer", "grpc://localhost:7051"); EventHub eventHub = client.newEventHub("eventhub", "grpc://localhost:7053");
Orderer orderer = client.newOrderer("orderer", "grpc://localhost:7050");
Channel channel = client.newChannel("mychannel");
channel.addPeer(peer);
channel.addEventHub(eventHub);
channel.addOrderer(orderer);
channel.initialize();

Invoking chain code

QueryByChaincodeRequest qpr = client.newQueryProposalRequest();ChaincodeID fabcarCCId = ChaincodeID.newBuilder().setName("fabcar").build();
qpr.setChaincodeID(fabcarCCId);
qpr.setFcn("queryAllCars");
Collection<ProposalResponse> res = channel.queryByChaincode(qpr);
for (ProposalResponse pres : res) {
String stringResponse = new String(pres.getChaincodeActionResponsePayload());
log.info(stringResponse);
}

Lukas Kolisko

Written by

Passionate about science, tech and photography. Interested in machine learning, distributed ledger technology and Java platform.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade