mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-21 17:15:10 +00:00
FGJ-4 split java fabcar into separate classes
Incorporate review comments from previous CR Split enroll/register/transactions into separate classes Change-Id: I384cec59c7f53a37864bfc28be11e785a61421f3 Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
This commit is contained in:
parent
8f92861104
commit
f1c46f033c
6 changed files with 232 additions and 192 deletions
|
|
@ -1,54 +1,56 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<modelVersion>4.0.0</modelVersion>
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
<groupId>fabcar-java</groupId>
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<artifactId>fabcar-java</artifactId>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<version>1.4.0-SNAPSHOT</version>
|
<groupId>fabcar-java</groupId>
|
||||||
<build>
|
<artifactId>fabcar-java</artifactId>
|
||||||
<plugins>
|
<version>1.4.0-SNAPSHOT</version>
|
||||||
<plugin>
|
<build>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<plugins>
|
||||||
<version>3.8.0</version>
|
<plugin>
|
||||||
<configuration>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<source>1.8</source>
|
<version>3.8.0</version>
|
||||||
<target>1.8</target>
|
<configuration>
|
||||||
</configuration>
|
<source>1.8</source>
|
||||||
</plugin>
|
<target>1.8</target>
|
||||||
</plugins>
|
</configuration>
|
||||||
</build>
|
</plugin>
|
||||||
<repositories>
|
</plugins>
|
||||||
<repository>
|
</build>
|
||||||
<id>hyperledger</id>
|
<repositories>
|
||||||
<name>Hyperledger Nexus</name>
|
<repository>
|
||||||
<url>https://nexus.hyperledger.org/content/repositories/snapshots</url>
|
<id>hyperledger</id>
|
||||||
</repository>
|
<name>Hyperledger Nexus</name>
|
||||||
</repositories>
|
<url>https://nexus.hyperledger.org/content/repositories/snapshots</url>
|
||||||
<dependencies>
|
</repository>
|
||||||
<dependency>
|
</repositories>
|
||||||
<groupId>org.hyperledger.fabric</groupId>
|
<dependencies>
|
||||||
<artifactId>fabric-gateway-java</artifactId>
|
<dependency>
|
||||||
<version>1.4.0-SNAPSHOT</version>
|
<groupId>org.hyperledger.fabric</groupId>
|
||||||
</dependency>
|
<artifactId>fabric-gateway-java</artifactId>
|
||||||
<dependency>
|
<version>1.4.0-SNAPSHOT</version>
|
||||||
<groupId>org.junit.platform</groupId>
|
</dependency>
|
||||||
<artifactId>junit-platform-launcher</artifactId>
|
<dependency>
|
||||||
<version>1.4.2</version>
|
<groupId>org.junit.platform</groupId>
|
||||||
</dependency>
|
<artifactId>junit-platform-launcher</artifactId>
|
||||||
<dependency>
|
<version>1.4.2</version>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
</dependency>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<dependency>
|
||||||
<version>5.4.1</version>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<scope>test</scope>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
</dependency>
|
<version>5.4.1</version>
|
||||||
<dependency>
|
<scope>test</scope>
|
||||||
<groupId>org.junit.vintage</groupId>
|
</dependency>
|
||||||
<artifactId>junit-vintage-engine</artifactId>
|
<dependency>
|
||||||
<version>5.4.2</version>
|
<groupId>org.junit.vintage</groupId>
|
||||||
</dependency>
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
<dependency>
|
<version>5.4.2</version>
|
||||||
<groupId>org.assertj</groupId>
|
</dependency>
|
||||||
<artifactId>assertj-core</artifactId>
|
<dependency>
|
||||||
<version>3.12.2</version>
|
<groupId>org.assertj</groupId>
|
||||||
<scope>test</scope>
|
<artifactId>assertj-core</artifactId>
|
||||||
</dependency>
|
<version>3.12.2</version>
|
||||||
</dependencies>
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -2,160 +2,47 @@ package org.example;
|
||||||
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.security.PrivateKey;
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.hyperledger.fabric.gateway.Contract;
|
import org.hyperledger.fabric.gateway.Contract;
|
||||||
import org.hyperledger.fabric.gateway.Gateway;
|
import org.hyperledger.fabric.gateway.Gateway;
|
||||||
import org.hyperledger.fabric.gateway.Network;
|
import org.hyperledger.fabric.gateway.Network;
|
||||||
import org.hyperledger.fabric.gateway.Wallet;
|
import org.hyperledger.fabric.gateway.Wallet;
|
||||||
import org.hyperledger.fabric.gateway.Wallet.Identity;
|
|
||||||
import org.hyperledger.fabric.sdk.Enrollment;
|
|
||||||
import org.hyperledger.fabric.sdk.User;
|
|
||||||
import org.hyperledger.fabric.sdk.security.CryptoSuite;
|
|
||||||
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory;
|
|
||||||
import org.hyperledger.fabric_ca.sdk.EnrollmentRequest;
|
|
||||||
import org.hyperledger.fabric_ca.sdk.HFCAClient;
|
|
||||||
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
|
|
||||||
|
|
||||||
public class ClientApp {
|
public class ClientApp {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
// Create a new file system based wallet for managing identities.
|
// Load a file system based wallet for managing identities.
|
||||||
Path walletPath = Paths.get("wallet");
|
Path walletPath = Paths.get("wallet");
|
||||||
Wallet wallet = Wallet.createFileSystemWallet(walletPath);
|
Wallet wallet = Wallet.createFileSystemWallet(walletPath);
|
||||||
loadWallet(wallet);
|
|
||||||
|
|
||||||
// load a CCP
|
// load a CCP
|
||||||
Path networkConfigPath = Paths.get("..", "..", "first-network", "connection-org1.yaml");
|
Path networkConfigPath = Paths.get("..", "..", "first-network", "connection-org1.yaml");
|
||||||
|
|
||||||
Gateway.Builder builder = Gateway.createBuilder();
|
Gateway.Builder builder = Gateway.createBuilder();
|
||||||
builder.identity(wallet, "user1").networkConfig(networkConfigPath).discovery(true);
|
builder.identity(wallet, "user1").networkConfig(networkConfigPath).discovery(true);
|
||||||
|
|
||||||
// create a gateway connection
|
// create a gateway connection
|
||||||
try (Gateway gateway = builder.connect()) {
|
try (Gateway gateway = builder.connect()) {
|
||||||
|
|
||||||
// get the network and contract
|
// get the network and contract
|
||||||
Network network = gateway.getNetwork("mychannel");
|
Network network = gateway.getNetwork("mychannel");
|
||||||
Contract contract = network.getContract("fabcar");
|
Contract contract = network.getContract("fabcar");
|
||||||
|
|
||||||
byte[] result;
|
byte[] result;
|
||||||
|
|
||||||
result = contract.evaluateTransaction("queryAllCars");
|
result = contract.evaluateTransaction("queryAllCars");
|
||||||
System.out.println(new String(result));
|
System.out.println(new String(result));
|
||||||
|
|
||||||
contract.submitTransaction("createCar", "CAR10", "VW", "Polo", "Grey", "Mary");
|
contract.submitTransaction("createCar", "CAR10", "VW", "Polo", "Grey", "Mary");
|
||||||
|
|
||||||
result = contract.evaluateTransaction("queryCar", "CAR10");
|
result = contract.evaluateTransaction("queryCar", "CAR10");
|
||||||
System.out.println(new String(result));
|
System.out.println(new String(result));
|
||||||
|
|
||||||
contract.submitTransaction("changeCarOwner", "CAR10", "Archie");
|
contract.submitTransaction("changeCarOwner", "CAR10", "Archie");
|
||||||
|
|
||||||
result = contract.evaluateTransaction("queryCar", "CAR10");
|
result = contract.evaluateTransaction("queryCar", "CAR10");
|
||||||
System.out.println(new String(result));
|
System.out.println(new String(result));
|
||||||
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void loadWallet(Wallet wallet) throws Exception {
|
|
||||||
// Create a CA client for interacting with the CA.
|
|
||||||
Properties props = new Properties();
|
|
||||||
props.put("pemFile", "../../first-network/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
|
|
||||||
props.put("allowAllHostNames", "true");
|
|
||||||
HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
|
|
||||||
CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
|
|
||||||
caClient.setCryptoSuite(cryptoSuite);
|
|
||||||
|
|
||||||
enrollAdmin(wallet, caClient);
|
|
||||||
registerUser(wallet, caClient);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void enrollAdmin(Wallet wallet, HFCAClient caClient) throws Exception {
|
|
||||||
// Check to see if we've already enrolled the admin user.
|
|
||||||
boolean adminExists = wallet.exists("admin");
|
|
||||||
if (adminExists) {
|
|
||||||
System.out.println("An identity for the admin user \"admin\" already exists in the wallet");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enroll the admin user, and import the new identity into the wallet.
|
|
||||||
final EnrollmentRequest enrollmentRequestTLS = new EnrollmentRequest();
|
|
||||||
enrollmentRequestTLS.addHost("localhost");
|
|
||||||
enrollmentRequestTLS.setProfile("tls");
|
|
||||||
Enrollment enrollment = caClient.enroll("admin", "adminpw", enrollmentRequestTLS);
|
|
||||||
Identity user = Identity.createIdentity("Org1MSP", enrollment.getCert(), enrollment.getKey());
|
|
||||||
wallet.put("admin", user);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void registerUser(Wallet wallet, HFCAClient caClient) throws Exception {
|
|
||||||
// Check to see if we've already enrolled the user.
|
|
||||||
boolean userExists = wallet.exists("user1");
|
|
||||||
if (userExists) {
|
|
||||||
System.out.println("An identity for the user \"user1\" already exists in the wallet");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Identity adminIdentity = wallet.get("admin");
|
|
||||||
User admin = new User() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return "admin";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<String> getRoles() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAccount() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAffiliation() {
|
|
||||||
return "org1.department1";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Enrollment getEnrollment() {
|
|
||||||
return new Enrollment() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PrivateKey getKey() {
|
|
||||||
return adminIdentity.getPrivateKey();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getCert() {
|
|
||||||
return adminIdentity.getCertificate();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMspId() {
|
|
||||||
return "Org1MSP";
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// Register the user, enroll the user, and import the new identity into the wallet.
|
|
||||||
RegistrationRequest registrationRequest = new RegistrationRequest("user1");
|
|
||||||
registrationRequest.setAffiliation("org1.department1");
|
|
||||||
registrationRequest.setEnrollmentID("user1");
|
|
||||||
String enrollmentSecret = caClient.register(registrationRequest, admin);
|
|
||||||
Enrollment enrollment = caClient.enroll("user1", enrollmentSecret);
|
|
||||||
Identity user = Identity.createIdentity("Org1MSP", enrollment.getCert(), enrollment.getKey());
|
|
||||||
wallet.put("user1", user);
|
|
||||||
System.out.println("Successfully enrolled user \"user1\" and imported it into the wallet");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
46
fabcar/java/src/main/java/org/example/EnrollAdmin.java
Normal file
46
fabcar/java/src/main/java/org/example/EnrollAdmin.java
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.hyperledger.fabric.gateway.Wallet;
|
||||||
|
import org.hyperledger.fabric.gateway.Wallet.Identity;
|
||||||
|
import org.hyperledger.fabric.sdk.Enrollment;
|
||||||
|
import org.hyperledger.fabric.sdk.security.CryptoSuite;
|
||||||
|
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory;
|
||||||
|
import org.hyperledger.fabric_ca.sdk.EnrollmentRequest;
|
||||||
|
import org.hyperledger.fabric_ca.sdk.HFCAClient;
|
||||||
|
|
||||||
|
public class EnrollAdmin {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
// Create a CA client for interacting with the CA.
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("pemFile",
|
||||||
|
"../../first-network/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
|
||||||
|
props.put("allowAllHostNames", "true");
|
||||||
|
HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
|
||||||
|
CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
|
||||||
|
caClient.setCryptoSuite(cryptoSuite);
|
||||||
|
|
||||||
|
// Create a wallet for managing identities
|
||||||
|
Wallet wallet = Wallet.createFileSystemWallet(Paths.get("wallet"));
|
||||||
|
|
||||||
|
// Check to see if we've already enrolled the admin user.
|
||||||
|
boolean adminExists = wallet.exists("admin");
|
||||||
|
if (adminExists) {
|
||||||
|
System.out.println("An identity for the admin user \"admin\" already exists in the wallet");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enroll the admin user, and import the new identity into the wallet.
|
||||||
|
final EnrollmentRequest enrollmentRequestTLS = new EnrollmentRequest();
|
||||||
|
enrollmentRequestTLS.addHost("localhost");
|
||||||
|
enrollmentRequestTLS.setProfile("tls");
|
||||||
|
Enrollment enrollment = caClient.enroll("admin", "adminpw", enrollmentRequestTLS);
|
||||||
|
Identity user = Identity.createIdentity("Org1MSP", enrollment.getCert(), enrollment.getKey());
|
||||||
|
wallet.put("admin", user);
|
||||||
|
System.out.println("Successfully enrolled user \"admin\" and imported it into the wallet");
|
||||||
|
}
|
||||||
|
}
|
||||||
103
fabcar/java/src/main/java/org/example/RegisterUser.java
Normal file
103
fabcar/java/src/main/java/org/example/RegisterUser.java
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.hyperledger.fabric.gateway.Wallet;
|
||||||
|
import org.hyperledger.fabric.gateway.Wallet.Identity;
|
||||||
|
import org.hyperledger.fabric.sdk.Enrollment;
|
||||||
|
import org.hyperledger.fabric.sdk.User;
|
||||||
|
import org.hyperledger.fabric.sdk.security.CryptoSuite;
|
||||||
|
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory;
|
||||||
|
import org.hyperledger.fabric_ca.sdk.HFCAClient;
|
||||||
|
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
|
||||||
|
|
||||||
|
public class RegisterUser {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
// Create a CA client for interacting with the CA.
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("pemFile",
|
||||||
|
"../../first-network/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
|
||||||
|
props.put("allowAllHostNames", "true");
|
||||||
|
HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
|
||||||
|
CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
|
||||||
|
caClient.setCryptoSuite(cryptoSuite);
|
||||||
|
|
||||||
|
// Create a wallet for managing identities
|
||||||
|
Wallet wallet = Wallet.createFileSystemWallet(Paths.get("wallet"));
|
||||||
|
|
||||||
|
// Check to see if we've already enrolled the user.
|
||||||
|
boolean userExists = wallet.exists("user1");
|
||||||
|
if (userExists) {
|
||||||
|
System.out.println("An identity for the user \"user1\" already exists in the wallet");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
userExists = wallet.exists("admin");
|
||||||
|
if (!userExists) {
|
||||||
|
System.out.println("\"admin\" needs to be enrolled and added to the wallet first");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Identity adminIdentity = wallet.get("admin");
|
||||||
|
User admin = new User() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "admin";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getRoles() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAccount() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAffiliation() {
|
||||||
|
return "org1.department1";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enrollment getEnrollment() {
|
||||||
|
return new Enrollment() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PrivateKey getKey() {
|
||||||
|
return adminIdentity.getPrivateKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCert() {
|
||||||
|
return adminIdentity.getCertificate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMspId() {
|
||||||
|
return "Org1MSP";
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// Register the user, enroll the user, and import the new identity into the wallet.
|
||||||
|
RegistrationRequest registrationRequest = new RegistrationRequest("user1");
|
||||||
|
registrationRequest.setAffiliation("org1.department1");
|
||||||
|
registrationRequest.setEnrollmentID("user1");
|
||||||
|
String enrollmentSecret = caClient.register(registrationRequest, admin);
|
||||||
|
Enrollment enrollment = caClient.enroll("user1", enrollmentSecret);
|
||||||
|
Identity user = Identity.createIdentity("Org1MSP", enrollment.getCert(), enrollment.getKey());
|
||||||
|
wallet.put("user1", user);
|
||||||
|
System.out.println("Successfully enrolled user \"user1\" and imported it into the wallet");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,8 @@ public class ClientTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFabCar() throws Exception {
|
public void testFabCar() throws Exception {
|
||||||
|
EnrollAdmin.main(null);
|
||||||
|
RegisterUser.main(null);
|
||||||
ClientApp.main(null);
|
ClientApp.main(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -213,11 +213,11 @@ Java:
|
||||||
Then, install dependencies and run the test using:
|
Then, install dependencies and run the test using:
|
||||||
mvn test
|
mvn test
|
||||||
|
|
||||||
The test will invoke the sample client app which perform the following:
|
The test will invoke the sample client app which perform the following:
|
||||||
- Enroll admin and user1 and import them into the wallet (if they don't already exist there)
|
- Enroll admin and user1 and import them into the wallet (if they don't already exist there)
|
||||||
- Submit a transaction to create a new car
|
- Submit a transaction to create a new car
|
||||||
- Evaluate a transaction (query) to return details of this car
|
- Evaluate a transaction (query) to return details of this car
|
||||||
- Submit a transaction to change the owner of this car
|
- Submit a transaction to change the owner of this car
|
||||||
- Evaluate a transaction (query) to return the updated details of this car
|
- Evaluate a transaction (query) to return the updated details of this car
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue