Hyperledger Fabric 2.x Java Block Chain Application

First, explanation

In the previous article, the “Hyperledger Fabric 2.x Custom Intelligence Contract” shared the installation of the intelligent contract and use the CLI client to call the contract; this article will use the Java code based on Fabric-Gateway-Java block chain network Access and transaction and integrate the Springboot framework.

Fabric Gateway SDK implements Fabric’s programming model, providing a series of simple APIs interact with the application with the Fabric block chain network;

Network topology map:

File

Applications delegate their network interactions to their gateways, each gateway understands the network channel topology, including multiple Peer nodes and sort nodes of the organization, allowing applications to focus on business logic; peer nodes can use the Gossip protocol to organize inside and The organization communicates with each other.

Second, MAVN dependence

Add the dependency of the gateway SDK:

Org.hyperledger.fabric groupid>

fabric-gateway-java artifactid>

2.2.3 version>

dependency>

Third, prepare the configuration file

The project’s directory structure is shown below:

3.1. Prepare the network certificate

Create a directory CRYPTO-Config copy the certificate file of the Orderer and the Peer node.

Certificate file Replicates Ordereroanizations with the PeerorGanizations folder from the Test-Network directory of Fabric-Samples:

3.2. Creating a network configuration

Create a file Connection.json content as follows:

{

“Name”: “Basic-Network”,

“Version”: “1.0.0”,

“{“: {

“Organization”: “ORG1”,

“Connection”: {

“timeout”: {

“peer”: {

“endorse”: “300”

}

“ORDERER”: “300”

}

}

}

“Channels”: {

“mychannel”: {

“Ordera”: [

“OrdeeRer.example.com”

],

“peers”: {

“peer0.org1.example.com”: {

“endorsingpeer”: True,

“chaincodequery”: True,

“LedgerQuery”: True,

Eventsource “: TRUE

}

“peer0.org2.example.com”: {

“endorsingpeer”: True,

“chaincodequery”: True,

“LedgerQuery”: True,

Eventsource “: TRUE

}

}

}

}

Organizations: {

“Org1”: {

“MSPID”: “ORG1MSP”,

Peers: [

“peer0.org1.example.com”

],

“CertificateAuthorities”: [

“ca-org1”

],

“adminpriVateKeypem”: {

“pat”: “src/main/resources/crypto-config/peerorganizations/org1.example.com/Users/[email protected]/msp/keystore/priv_sk”

}

“SIGNEDCERTPEM”: {

“Path”: “src / main / resources / crypto-config / peerorganizations / org1.example.com / users / admin @ org1.example.com / msp / signcerts / admin @ org1.example.com-cert.pem”}

}

“ORG2”: {

“MSPID”: “ORG2MSP”,

Peers: [

“peer0.org2.example.com”

],

“CertificateAuthorities”: [

“ca-org2”

],

“adminpriVateKeypem”: {

“pat”: “src/main/resources/crypto-config/peerorganizations/org2.example.com/Users/[email protected]/msp/keystore/priv_sk”

}

“SIGNEDCERTPEM”: {

“path”: “src / main / resources / crypto-config / peerorganizations / org2.example.com / users / admin @ org2.example.com / msp / signcerts / admin @ org2.example.com-cert.pem”

}

}

}

“Ordera”: {

“ORDERER.EXAMPLE.COM”: {

“URL”: “GRPCS: //192.168.28.134: 7050”,

“MSPID”: “Orderermsp”,

“GrpcOptions”: {

“SSL-TARGET-Name-Override”: “Ordeerr.example.com”,

HostnameOverride: “Ordeerr.example.com”

}

“TLSCACERTS”: {

“Path”: “src / main / resources / crypto-config / ordererroanizations / example.com / orderers / orderer.example.com / tls / ca.crt”

}

“adminpriVateKeypem”: {

“path”: “src/main/resources/crypto-config/otereroanizations/example.com/USERS/[email protected]/msp/keystore/priv_sk”

}

“SIGNEDCERTPEM”: {

“Path”: “src / main / resources / crypto-config / orderrganizations / example.com / users / admin @ example.com / msp / sign / admin @ example.com-cert.pem”

}

}

}

“peers”: {

“peer0.org1.example.com”: {

“URL”: “GRPCS: //192.168.28.134: 7051”,

“GrpcOptions”: {

“SSL-Target-Name-Override”: “peer0.org1.example.com”,

“HostnameOverride”: “peer0.org1.example.com”,

“Request-Timeout”: 120001

}

“TLSCACERTS”: {

“path”: “src / main / resources / crypto-config / peerorganizations / org1.example.com / peers / peer0.org1.example.com / tls / ca.crt”

}

}

“peer0.org2.example.com”: {

“URL”: “GRPCS: //192.168.28.134: 9051”,

“GrpcOptions”: {“SSL-Target-Name-Override”: “peer0.org2.example.com”,

“HostnameOverride”: “peer0.org2.example.com”,

“Request-Timeout”: 120001

}

“TLSCACERTS”: {

“pat”: “src / main / resources / crypto-config / peerorganizations / org2.example.com / peers / peer0.org2.example.com / tls / ca.crt”

}

}

}

“CertificateAuthorities”: {

“ca-org1”: {

“URL”: “https://192.168.28.134:7054”,

“GrpcOptions”: {

“verify”: True

}

“TLSCACERTS”: {

“PATH”: “src / main / resources / crypto-config / peerorganizations / org1.example.com / ca / ??ca.org1.example.com-cert.pem”

}

“registrar”: [

{

“enrollid”: “admin”,

“enrollsecret”: “adminPw”

}

]

}

“ca-org2”: {

“URL”: “https://192.168.28.134:8054”,

“GrpcOptions”: {

“verify”: True

}

“TLSCACERTS”: {

“Path”: “src / main / resources / crypto-config / peerorganizations / org2.example.com / ca / ??ca.org2.example.com-cert.pem”

}

“registrar”: [

{

“enrollid”: “admin”,

“enrollsecret”: “adminPw”

}

]

}

}

}

You need to modify the address in the URL according to the actual situation, which contains Channels, Organizations, Orderers, Peers, and CA, respectively.

3.3. Springboot configuration

Add the following in Application.yml to access the relevant configuration of the gateway:

Fabric:

# Wallet folder path (automatically created)

WalletDirectory: Wallet

# Íø ÍøÂç Íø profile path

NetworkconfigPath: Connection.json

# User certificate path

CERTIFICATEPATH: CRYPTO-Config / Peerorganizations / Org1.example.com / users / user1 @ org1.example.com / MSP / Signcerts / User1 @ Org1.example.com-Cert.pem

# Óû§ ˽ ˽ Ô¿ · ·

PrivateKeyPath: Crypto-config/peerorganization/org1.example.com/Users/[email protected]/msp/keystore/priv_sk

# ·Ã ×éÖ¯ ×éÖ¯

MSPID: ORG1MSP

# username

Username: User1

# ͨµÀ Ãû×Ö

ChannelName: MyChannel

#Clinked code name

Contractname: mycc

Fourth, the connection contract

Construction of the bean objects of gateways, channels, and contracts, as follows:

/ **

* Connect the gateway

* /

@Bean

Public Gateway ConnectGateway () THROWS IOException, InvalidKeyException, CertificateException {

// Using the USER1 in ORG1 to initialize a gateway Wallet account for connection network

Wallet Wallet = Wallets.newFileSystemWallet (path.gletdirectory); x509certificate certificate = readx509certificate (path.get (this.certificatePath);

PrivateKey PrivateKey = getPrivateKey (path.get (this.privateKeyPath);

Wallet.put (username, ideness (this.mspid, certificate, privateatekey)

/ / Get a Fabric network connection object according to Connection.json

Gateway.builder builder = Gateway.createBuilder ()

.identity (Wallet, Username)

.NetworkConfig (path.get (this.networkconfigpath);

// Connect the gateway

Return builder.Connect ();

}

/ **

* Get channel

* /

@Bean

Public Network NetWork (Gateway Gateway) {

Return Gateway.getNetWork (this.channelname);

}

/ **

* Get Contracts

* /

@Bean

Public Contract Contract (NetWork Network) {

Return NetWork.getContract (this.contractname);

}

V. Contract call

Create a Controller class to inject the Contract object call contract method:

@Resource

Private contract.

@Resource

Private network network;

@Getmapping (“/ getuser”)

Public string getuser (string userid) throws contractException {

Byte [] queryaresisultbefore = contract.evaluateTransaction (“getuser”, userid;

Return New String (QueryareSultbefore, StandardCharsets.utf_8);

}

@Getmapping (“/ adduser”)

Public String AddUser (String Userid, String UserName, String Money) Throws ContractException, InterruptedException, TimeOutexception {

Byte [] invokeresult = contract.createTransaction (“adduser”)

.sendorsingpeers (NetWork.getChannel (). getpeers (EnumSet.of (Peer.PeerRole.Endorsing_peer))))

. Submit (UserID, UserName, Money);

String TXID = New String (InvokeResult, StandardCharsets.utf_8);

Return TXID;

}

6. Test interface

Call the interface getUser:

Http://127.0.0.1:9001/getuser?Userid=1

return:

{

“Money”: 300,

“name”: “ZLT”,

“UserID”: “1”

}

Call the interface adduser:

Http://127.0.0.1:9001/adduser?Userid=6&username=test6&money=600 Returns:

2AE291BB6A366B5BA01AD49E4237DA8DEF9E9828CC2C982E8C49D4B763AF0157

Seven, code download

Gitee: https://gitee.com/zlt2000/my-fabric-application-java

GitHub: https://github.com/zlt2000/my-fabric-application-java