diff --git a/asset-transfer-basic/application-java/.gitignore b/asset-transfer-basic/application-java/.gitignore new file mode 100755 index 00000000..3aa04dcb --- /dev/null +++ b/asset-transfer-basic/application-java/.gitignore @@ -0,0 +1,7 @@ +/bin/ +/target/ +.settings/ +.classpath +.project +wallet +!wallet/.gitkeep \ No newline at end of file diff --git a/asset-transfer-basic/application-java/pom.xml b/asset-transfer-basic/application-java/pom.xml new file mode 100644 index 00000000..97bc5a74 --- /dev/null +++ b/asset-transfer-basic/application-java/pom.xml @@ -0,0 +1,85 @@ + + 4.0.0 + asset-transfer-basic + application-java + 1.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.1.1 + + + 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/asset-transfer-basic/application-java/src/main/java/org/example/ClientApp.java b/asset-transfer-basic/application-java/src/main/java/org/example/ClientApp.java new file mode 100755 index 00000000..6c08cd5e --- /dev/null +++ b/asset-transfer-basic/application-java/src/main/java/org/example/ClientApp.java @@ -0,0 +1,177 @@ +/* +SPDX-License-Identifier: Apache-2.0 +*/ + +package org.example; + +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.hyperledger.fabric.gateway.Contract; +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 ClientApp { + + static { + System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true"); + } + + // helper function for getting connected to the gateway + public static Gateway Connect() throws Exception{ + // Load a file system based wallet for managing identities. + Path walletPath = Paths.get("wallet"); + Wallet wallet = Wallets.newFileSystemWallet(walletPath); + // load a CCP + Path networkConfigPath = Paths.get("..", "..", "test-network", "organizations", "peerOrganizations", "org1.example.com", "connection-org1.yaml"); + + Gateway.Builder builder = Gateway.createBuilder(); + builder.identity(wallet, "appUser").networkConfig(networkConfigPath).discovery(true); + return builder.connect(); + + } + + // Connects to the chaincode and calls InitLedger() + public static void main(String[] args) throws Exception { + + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + byte[] result; + + contract.submitTransaction("InitLedger"); + System.out.println("Ledger has been initialized with 6 assets"); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } + + // Connects to the chaincode and calls CreateAsset() + public static void CreateAsset(final String assetID, final String color, final String size, + final String owner, final String appraisedValue) throws Exception { + + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + contract.submitTransaction("CreateAsset", assetID, color, size, owner, appraisedValue); + System.out.println("Created asset: [assetID=" + assetID + ", color="+ color + ", size=" + size + + ", owner=" + owner + ", appraisedValue=" + appraisedValue +"]"); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } + + public static void ReadAsset(final String assetID) throws Exception { + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + byte[] result; + + result = contract.evaluateTransaction("ReadAsset", assetID); + System.out.println(new String(result)); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } + + // Connects to the chaincode and calls CreateAsset() + public static void UpdateAsset(final String assetID, final String color, final String size, + final String owner, final String appraisedValue) throws Exception { + + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + contract.submitTransaction("UpdateAsset", assetID, color, size, owner, appraisedValue); + System.out.println("Updated asset: [assetID=" + assetID + ", color="+ color + ", size=" + size + + ", owner=" + owner + ", appraisedValue=" + appraisedValue +"]"); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } + + // Connects to the chaincode and calls CreateAsset() + public static void DeleteAsset(final String assetID) throws Exception { + + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + contract.submitTransaction("DeleteAsset", assetID); + System.out.println("Deleted: " + assetID); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } + + // Connects to the chaincode and calls CreateAsset() + public static void TransferAsset(final String assetID, final String newOwner) throws Exception { + + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + byte[] result; + + result = contract.submitTransaction("TransferAsset", assetID, newOwner); + System.out.println(new String(result)); + System.out.println("Transferred " + assetID + " to " + newOwner); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } + + public static void GetAllAssets() throws Exception { + // create a gateway connection + try (Gateway gateway = Connect()) { + + // get the network and contract + Network network = gateway.getNetwork("mychannel"); + Contract contract = network.getContract("basic"); + + byte[] result; + + result = contract.evaluateTransaction("GetAllAssets"); + System.out.println(new String(result)); + gateway.close(); + } + catch(Exception e){ + System.out.println("Error connecting to gateway: " + e); + } + } +} diff --git a/asset-transfer-basic/application-java/src/main/java/org/example/EnrollAdmin.java b/asset-transfer-basic/application-java/src/main/java/org/example/EnrollAdmin.java new file mode 100644 index 00000000..8b3e2f59 --- /dev/null +++ b/asset-transfer-basic/application-java/src/main/java/org/example/EnrollAdmin.java @@ -0,0 +1,55 @@ +/* +SPDX-License-Identifier: Apache-2.0 +*/ + +package org.example; + +import java.nio.file.Paths; +import java.util.Properties; + +import org.hyperledger.fabric.gateway.Wallet; +import org.hyperledger.fabric.gateway.Wallets; +import org.hyperledger.fabric.gateway.Identities; +import org.hyperledger.fabric.gateway.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 { + + static { + System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true"); + } + + public static void main(String[] args) throws Exception { + + // Create a CA client for interacting with the CA. + Properties props = new Properties(); + props.put("pemFile", + "../../test-network/organizations/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 = Wallets.newFileSystemWallet(Paths.get("wallet")); + + // Check to see if we've already enrolled the admin user. + if (wallet.get("admin") != null) { + 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 = Identities.newX509Identity("Org1MSP", enrollment); + wallet.put("admin", user); + System.out.println("Successfully enrolled user \"admin\" and imported it into the wallet"); + } +} diff --git a/asset-transfer-basic/application-java/src/main/java/org/example/RegisterUser.java b/asset-transfer-basic/application-java/src/main/java/org/example/RegisterUser.java new file mode 100644 index 00000000..c116df49 --- /dev/null +++ b/asset-transfer-basic/application-java/src/main/java/org/example/RegisterUser.java @@ -0,0 +1,111 @@ +/* +SPDX-License-Identifier: Apache-2.0 +*/ + +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.Wallets; +import org.hyperledger.fabric.gateway.Identities; +import org.hyperledger.fabric.gateway.Identity; +import org.hyperledger.fabric.gateway.X509Identity; +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 { + + static { + System.setProperty("org.hyperledger.fabric.sdk.service_discovery.as_localhost", "true"); + } + + public static void main(String[] args) throws Exception { + + // Create a CA client for interacting with the CA. + Properties props = new Properties(); + props.put("pemFile", + "../../test-network/organizations/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 = Wallets.newFileSystemWallet(Paths.get("wallet")); + + // Check to see if we've already enrolled the user. + if (wallet.get("appUser") != null) { + System.out.println("An identity for the user \"appUser\" already exists in the wallet"); + return; + } + + X509Identity adminIdentity = (X509Identity)wallet.get("admin"); + if (adminIdentity == null) { + System.out.println("\"admin\" needs to be enrolled and added to the wallet first"); + return; + } + User admin = new User() { + + @Override + public String getName() { + return "admin"; + } + + @Override + public Set 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 Identities.toPemString(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("appUser"); + registrationRequest.setAffiliation("org1.department1"); + registrationRequest.setEnrollmentID("appUser"); + String enrollmentSecret = caClient.register(registrationRequest, admin); + Enrollment enrollment = caClient.enroll("appUser", enrollmentSecret); + Identity user = Identities.newX509Identity("Org1MSP", adminIdentity.getCertificate(), adminIdentity.getPrivateKey()); + wallet.put("appUser", user); + System.out.println("Successfully enrolled user \"appUser\" and imported it into the wallet"); + } + +} diff --git a/asset-transfer-basic/application-java/src/test/java/org/example/TestApp.java b/asset-transfer-basic/application-java/src/test/java/org/example/TestApp.java new file mode 100644 index 00000000..f4d20fd0 --- /dev/null +++ b/asset-transfer-basic/application-java/src/test/java/org/example/TestApp.java @@ -0,0 +1,57 @@ +/* +SPDX-License-Identifier: Apache-2.0 +*/ + +package org.example; + +import org.junit.Test; + +public class TestApp { + + /* + * Enrolls an admin + * Registers a user + * Initializes the ledger with some assets + */ + @Test + public void start() throws Exception { + EnrollAdmin.main(null); + RegisterUser.main(null); + ClientApp.main(null); + } + + // Creates asset7, modify the parameters to create other assets + @Test + public void CreateAsset() throws Exception { + ClientApp.CreateAsset("asset7", "magenta", "20", "kenysha", "800"); + } + + // Reads asset1, modify the parameters to read other assets + @Test + public void ReadAsset() throws Exception { + ClientApp.ReadAsset("asset7"); + } + + // Updates asset3, modify the parameters to read other assets + @Test + public void UpdateAsset() throws Exception { + ClientApp.UpdateAsset("asset3", "blue", "20", "Jin Soo", "600"); + } + + // Deletes asset2, modify the parameters to delete other assets + @Test + public void DeleteAsset() throws Exception { + ClientApp.DeleteAsset("asset2"); + } + + // Transfers asset1 to arturo, modify the parameters to transfer other assets + @Test + public void TransferAsset() throws Exception { + ClientApp.TransferAsset("asset1", "arturo"); + } + // Outputs all the assets + @Test + public void GetAllAssets() throws Exception { + ClientApp.GetAllAssets(); + } +}