From ee10e603861aaa96fea35ab38cdd22a83095bdf4 Mon Sep 17 00:00:00 2001 From: Baohua Yang Date: Wed, 18 Aug 2021 14:17:14 -0700 Subject: [PATCH] Add sdk java sample --- .../v2.2.1/gateway-java/auto.sh | 11 ++ .../v2.2.1/gateway-java/connection.json | 122 ++++++++++++++++++ .../v2.2.1/gateway-java/pom.xml | 86 ++++++++++++ .../src/main/java/sample/Sample.java | 57 ++++++++ 4 files changed, 276 insertions(+) create mode 100644 hyperledger_fabric/v2.2.1/gateway-java/auto.sh create mode 100644 hyperledger_fabric/v2.2.1/gateway-java/connection.json create mode 100644 hyperledger_fabric/v2.2.1/gateway-java/pom.xml create mode 100644 hyperledger_fabric/v2.2.1/gateway-java/src/main/java/sample/Sample.java diff --git a/hyperledger_fabric/v2.2.1/gateway-java/auto.sh b/hyperledger_fabric/v2.2.1/gateway-java/auto.sh new file mode 100644 index 00000000..c1555d9b --- /dev/null +++ b/hyperledger_fabric/v2.2.1/gateway-java/auto.sh @@ -0,0 +1,11 @@ +# Do patch on the grpc-java +#rm ~/.m2/repository/io/grpc/grpc-netty-shaded/1.38.0/grpc-netty-shaded-1.38.0.jar +#mvn install:install-file -Dfile=grpc-netty-shaded-build/grpc-netty-shaded-1.38.0.jar -DgroupId=io.grpc -DartifactId=grpc-netty-shaded -Dversion=1.38.0 -Dpackaging=jar + +# Compile if the source code is changed +mvn compile + +# Run the method +mvn exec:java \ + -Dexec.cleanupDaemonThreads=false \ + -Dexec.mainClass="sample.Sample" diff --git a/hyperledger_fabric/v2.2.1/gateway-java/connection.json b/hyperledger_fabric/v2.2.1/gateway-java/connection.json new file mode 100644 index 00000000..09af16f5 --- /dev/null +++ b/hyperledger_fabric/v2.2.1/gateway-java/connection.json @@ -0,0 +1,122 @@ +{ + "name": "basic-network", + "version": "1.0.0", + "client": { + "organization": "Org1", + "connection": { + "timeout": { + "peer": { + "endorser": "300" + }, + "orderer": "300" + } + } + }, + "channels": { + "mychannel": { + "orderers": [ + "orderer0.example.com" + ], + "peers": { + "peer0.org1.example.com": { + "endorsingPeer": true, + "chaincodeQuery": true, + "ledgerQuery": true, + "eventSource": true + } + } + } + }, + "organizations": { + "Org1": { + "mspid": "Org1MSP", + "peers": [ + "peer0.org1.example.com" + ], + "certificateAuthorities": [ + "ca-org1" + ], + "adminPrivateKeyPEM": { + "path": "/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk" + }, + "signedCertPEM": { + "path": "/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem" + } + } + }, + "orderers": { + "orderer0.example.com": { + "url": "grpcs://orderer0.example.com:7050", + "mspid": "OrdererMSP", + "grpcOptions": { + "ssl-target-name-override": "orderer0.example.com", + "hostnameOverride": "orderer0.example.com" + }, + "tlsCACerts": { + "path": "/opt/crypto-config/ordererOrganizations/example.com/orderers/orderer0.example.com/tls/ca.crt" + }, + "adminPrivateKeyPEM": { + "path": "/opt/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/priv_sk" + }, + "signedCertPEM": { + "path": "/opt/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/Admin@example.com-cert.pem" + } + } + }, + "peers": { + "peer0.org1.example.com": { + "url": "grpcs://peer0.org1.example.com:7051", + "grpcOptions": { + "ssl-target-name-override": "peer0.org1.example.com", + "hostnameOverride": "peer0.org1.example.com", + "request-timeout": 120001 + }, + "tlsCACerts": { + "path": "/opt/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" + } + }, + "peer0.org2.example.com": { + "url": "grpcs://peer0.org2.example.com:7051", + "grpcOptions": { + "ssl-target-name-override": "peer0.org2.example.com", + "hostnameOverride": "peer0.org2.example.com", + "request-timeout": 120001 + }, + "tlsCACerts": { + "path": "/opt/crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" + } + } + }, + "certificateAuthorities": { + "ca-org1": { + "url": "https://localhost:7054", + "grpcOptions": { + "verify": true + }, + "tlsCACerts": { + "path": "/opt/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem" + }, + "registrar": [ + { + "enrollId": "admin", + "enrollSecret": "adminpw" + } + ] + }, + "ca-org2": { + "url": "https://localhost:8054", + "grpcOptions": { + "verify": true + }, + "tlsCACerts": { + "path": "/opt/crypto-config/peerOrganizations/org2.example.com/ca/ca.org2.example.com-cert.pem" + }, + "registrar": [ + { + "enrollId": "admin", + "enrollSecret": "adminpw" + } + ] + } + } +} diff --git a/hyperledger_fabric/v2.2.1/gateway-java/pom.xml b/hyperledger_fabric/v2.2.1/gateway-java/pom.xml new file mode 100644 index 00000000..0d16a2d5 --- /dev/null +++ b/hyperledger_fabric/v2.2.1/gateway-java/pom.xml @@ -0,0 +1,86 @@ + + 4.0.0 + sample + sample + 1.0.0 + + + + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.0 + + + + package + + shade + + + + + false + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + oss-sonatype + OSS Sonatype + https://oss.sonatype.org/content/repositories/snapshots + + + + + org.hyperledger.fabric + fabric-gateway-java + 2.2.0 + + + org.junit.platform + junit-platform-launcher + 1.4.2 + + + org.junit.jupiter + junit-jupiter-engine + 5.4.1 + test + + + org.junit.vintage + junit-vintage-engine + 5.4.2 + + + org.assertj + assertj-core + 3.12.2 + test + + + + diff --git a/hyperledger_fabric/v2.2.1/gateway-java/src/main/java/sample/Sample.java b/hyperledger_fabric/v2.2.1/gateway-java/src/main/java/sample/Sample.java new file mode 100644 index 00000000..fcdb7ccc --- /dev/null +++ b/hyperledger_fabric/v2.2.1/gateway-java/src/main/java/sample/Sample.java @@ -0,0 +1,57 @@ +package sample; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.concurrent.TimeoutException; + +import org.hyperledger.fabric.gateway.Contract; +import org.hyperledger.fabric.gateway.ContractException; +import org.hyperledger.fabric.gateway.Gateway; +import org.hyperledger.fabric.gateway.Network; +import org.hyperledger.fabric.gateway.Wallet; +import org.hyperledger.fabric.gateway.Wallets; + +public class Sample { + public static void main(String[] args) throws IOException { + String connectProfilePath="connection.json"; + String channelName="businesschannel"; + String walletPath="/opt/test/wallet"; + + String chaincodeName="exp02"; + + // Load an existing wallet holding identities used to access the network. + Path walletDirectory = Paths.get(walletPath); + Wallet wallet = Wallets.newFileSystemWallet(walletDirectory); + + // Path to a common connection profile describing the network. + Path networkConfigFile = Paths.get(connectProfilePath); + + // Configure the gateway connection used to access the network. + Gateway.Builder builder = Gateway.createBuilder() + .identity(wallet, "admin") + .networkConfig(networkConfigFile); + + // Create a gateway connection + try (Gateway gateway = builder.connect()) { + + // Obtain a smart contract deployed on the network. + Network network = gateway.getNetwork(channelName); + Contract contract = network.getContract(chaincodeName); + + // Submit transactions that store state to the ledger. + byte[] createCarResult = contract.createTransaction("invoke") + .submit( "a", "b", "10"); + System.out.println(new String(createCarResult, StandardCharsets.UTF_8)); + + // Evaluate transactions that query state from the ledger. + byte[] queryAllCarsResult = contract.evaluateTransaction("query", "a"); + System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8)); + + } catch (ContractException | TimeoutException | InterruptedException e) { + e.printStackTrace(); + } + System.exit(0); + } +}