diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c95b08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,83 @@ +# Created by https://www.gitignore.io/api/intellij + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + + +# End of https://www.gitignore.io/api/intellij \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..3f8ab6c --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..b26911b --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/java-api.iml b/java-api.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/java-api.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/msp/toto/abdel.context b/msp/toto/abdel.context new file mode 100644 index 0000000..95e9850 Binary files /dev/null and b/msp/toto/abdel.context differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bddd040 --- /dev/null +++ b/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + com.monnethic + java-api + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + junit + junit + 4.12 + + + org.hyperledger.fabric-sdk-java + fabric-sdk-java + 1.2.1 + + + org.springframework.boot + spring-boot-starter-web + + + log4j + log4j + 1.2.17 + + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/src/main/java/blockchain/client/CAClientWrapper.java b/src/main/java/blockchain/client/CAClientWrapper.java new file mode 100644 index 0000000..15e5b71 --- /dev/null +++ b/src/main/java/blockchain/client/CAClientWrapper.java @@ -0,0 +1,48 @@ +package blockchain.client; + +import blockchain.user.UserContext; +import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi; +import org.hyperledger.fabric.sdk.exception.CryptoException; +import org.hyperledger.fabric.sdk.exception.InvalidArgumentException; +import org.hyperledger.fabric.sdk.security.CryptoSuite; +import org.hyperledger.fabric_ca.sdk.HFCAClient; + +import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; +import java.util.Properties; + +public class CAClientWrapper { + private String caUrl; + private Properties properties; + private HFCAClient hfcaClient; + private UserContext adminContext; + + public void setAdminContext(UserContext adminContext) { + this.adminContext = adminContext; + } + + public HFCAClient getHfcaClient(){ + return hfcaClient; + } + + public CAClientWrapper(String caUrl, Properties properties) throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException { + this.caUrl=caUrl; + this.properties=properties; + init(); + } + + private void init() throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException { + CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite(); + hfcaClient = HFCAClient.createNewInstance(caUrl, properties); + hfcaClient.setCryptoSuite(cryptoSuite); + } + + //TODO + public UserContext enrollAdmin(String username, String password) throws Exception{ + UserContext userContext = new UserContext(); + + return null; + } + + +} diff --git a/src/main/java/blockchain/client/ChannelClientWrapper.java b/src/main/java/blockchain/client/ChannelClientWrapper.java new file mode 100644 index 0000000..5db76b1 --- /dev/null +++ b/src/main/java/blockchain/client/ChannelClientWrapper.java @@ -0,0 +1,28 @@ +package blockchain.client; + + +import org.hyperledger.fabric.sdk.Channel; + +public class ChannelClientWrapper { + private String name; + private Channel channel; + private FabricClientWrapper fabricClientWrapper; + + public String getName(){ + return name; + } + + public Channel getChannel(){ + return channel; + } + + public FabricClientWrapper getFabricClientWrapper(){ + return fabricClientWrapper; + } + + public ChannelClientWrapper(String name, Channel channel, FabricClientWrapper fabricClientWrapper){ + this.name=name; + this.channel=channel; + this.fabricClientWrapper=fabricClientWrapper; + } +} diff --git a/src/main/java/blockchain/client/FabricClientWrapper.java b/src/main/java/blockchain/client/FabricClientWrapper.java new file mode 100644 index 0000000..eeb3ee9 --- /dev/null +++ b/src/main/java/blockchain/client/FabricClientWrapper.java @@ -0,0 +1,31 @@ +package blockchain.client; + +import blockchain.user.UserContext; +import org.hyperledger.fabric.sdk.Channel; +import org.hyperledger.fabric.sdk.HFClient; +import org.hyperledger.fabric.sdk.exception.CryptoException; +import org.hyperledger.fabric.sdk.exception.InvalidArgumentException; +import org.hyperledger.fabric.sdk.security.CryptoSuite; + +import java.lang.reflect.InvocationTargetException; + +public class FabricClientWrapper { + private HFClient client; + + public HFClient getClient(){ + return client; + } + + public FabricClientWrapper(UserContext userContext) throws CryptoException, InvalidArgumentException, IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException { + CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite(); + client = HFClient.createNewInstance(); + client.setCryptoSuite(cryptoSuite); + client.setUserContext(userContext); + } + + public ChannelClientWrapper createChannelClient(String name) throws InvalidArgumentException { + Channel channel = client.newChannel(name); + ChannelClientWrapper channelClientWrapper = new ChannelClientWrapper(name, channel, this); + return channelClientWrapper; + } +} diff --git a/src/main/java/blockchain/user/UserContext.java b/src/main/java/blockchain/user/UserContext.java new file mode 100644 index 0000000..47fa4dd --- /dev/null +++ b/src/main/java/blockchain/user/UserContext.java @@ -0,0 +1,72 @@ +package blockchain.user; + +import org.hyperledger.fabric.sdk.Enrollment; +import org.hyperledger.fabric.sdk.User; + +import java.io.Serializable; +import java.util.Set; + +public class UserContext implements User, Serializable { + + private static final long serialVersionUID = 1L; + protected String name; + protected Set roles; + protected String account; + protected String affiliation; + protected Enrollment enrollment; + protected String mspId; + + public void setName(String name){ + this.name=name; + } + + public void setRoles(Set roles){ + this.roles=roles; + } + + public void setAccount(String account) { + this.account = account; + } + + public void setAffiliation(String affiliation) { + this.affiliation = affiliation; + } + + public void setEnrollment(Enrollment enrollment) { + this.enrollment = enrollment; + } + + public void setMspId(String mspId) { + this.mspId = mspId; + } + + @Override + public String getName() { + return name; + } + + @Override + public Set getRoles() { + return roles; + } + + @Override + public String getAccount() { + return account; + } + + @Override + public String getAffiliation() { + return affiliation; + } + + @Override + public Enrollment getEnrollment() { + return enrollment; + } + + @Override + public String getMspId() { + return mspId; + } +} diff --git a/src/main/java/blockchain/utility/Util.java b/src/main/java/blockchain/utility/Util.java new file mode 100644 index 0000000..6d29293 --- /dev/null +++ b/src/main/java/blockchain/utility/Util.java @@ -0,0 +1,85 @@ +package blockchain.utility; + +import blockchain.user.UserContext; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Logger; + +import java.io.*; + +public class Util { + private static Logger logger = Logger.getLogger(Util.class); + + public static void writeUserContext(UserContext userContext) { + BasicConfigurator.configure(); + + ObjectOutputStream out = null; + FileOutputStream fileOutputStream = null; + try { + String directoryPath = "msp/" + userContext.getAffiliation(); + String filePath = directoryPath + "/" + userContext.getName() + ".context"; + File directory = new File(directoryPath); + + if (!directory.exists()){ + logger.info("Create directory at path "+directoryPath); + directory.mkdirs(); + } + + File file = new File(filePath); + fileOutputStream = new FileOutputStream(file); + out = new ObjectOutputStream(fileOutputStream); + logger.info("Try write object for user "+userContext.getName()); + out.writeObject(userContext); + } catch (IOException e) { + logger.error("1st IOException"); + e.printStackTrace(); + } finally { + try { + if (out != null){ + logger.info("Close ObjectOutputStream"); + out.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + try { + if (fileOutputStream != null) { + logger.info("Close fileOutputStream"); + fileOutputStream.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static UserContext readUserContext(String affiliation, String username) { + UserContext userContext = null; + FileInputStream fileStream = null; + ObjectInputStream in = null; + try { + String filePath = "msp/" + affiliation + "/" + username + ".context"; + File file = new File(filePath); + if (file.exists()) { + fileStream = new FileInputStream(filePath); + in = new ObjectInputStream(fileStream); + userContext = (UserContext) in.readObject(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (in != null) + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + if (fileStream != null) + fileStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + return userContext; + } + } +} diff --git a/src/test/java/blockchain/user/UserContextTest.java b/src/test/java/blockchain/user/UserContextTest.java new file mode 100644 index 0000000..7aeeafc --- /dev/null +++ b/src/test/java/blockchain/user/UserContextTest.java @@ -0,0 +1,20 @@ +package blockchain.user; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class UserContextTest { + @Test + public void TestACreateUser(){ + UserContext userContext = new UserContext(); + userContext.setName("admin"); + userContext.setAffiliation("org1"); + userContext.setMspId("MspidOrg1"); + userContext.setEnrollment(null); + + assertEquals("org1",userContext.getAffiliation()); + assertEquals("admin",userContext.getName()); + assertEquals(null,userContext.getEnrollment()); + } +} diff --git a/src/test/java/blockchain/utility/UtilTest.java b/src/test/java/blockchain/utility/UtilTest.java new file mode 100644 index 0000000..af9979e --- /dev/null +++ b/src/test/java/blockchain/utility/UtilTest.java @@ -0,0 +1,54 @@ +package blockchain.utility; + +import blockchain.user.UserContext; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Logger; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class UtilTest { + private static Logger logger = Logger.getLogger(UtilTest.class); + + @Test + public void TestAReadNullUser(){ + BasicConfigurator.configure(); + + logger.info("----- START TEST READ NULL USER ----"); + UserContext uc = Util.readUserContext("tata","abdel"); + if(uc!=null){ + logger.info("UserContext is : "+uc.toString()); + }else{ + logger.info("UserContext doesn't exist"); + } + } + + + @Test + public void TestBWriteUser(){ + BasicConfigurator.configure(); + logger.info("----- START TEST WRITE NULL USER ----"); + UserContext user = new UserContext(); + user.setName("abdel"); + user.setAffiliation("toto"); + user.setEnrollment(null); + user.setMspId("test"); + + Util.writeUserContext(user); + + } + + @Test + public void TestCReadCreatedUser(){ + BasicConfigurator.configure(); + logger.info("----- START TEST READ CREATED USER ----"); + UserContext uc = Util.readUserContext("toto","abdel"); + if(uc!=null){ + logger.info("UserContext is : "+uc.toString()); + }else{ + logger.info("UserContext doesn't exist"); + } + } + +} diff --git a/target/classes/blockchain/client/CAClientWrapper.class b/target/classes/blockchain/client/CAClientWrapper.class new file mode 100644 index 0000000..6e94daa Binary files /dev/null and b/target/classes/blockchain/client/CAClientWrapper.class differ diff --git a/target/classes/blockchain/client/ChannelClientWrapper.class b/target/classes/blockchain/client/ChannelClientWrapper.class new file mode 100644 index 0000000..a349f42 Binary files /dev/null and b/target/classes/blockchain/client/ChannelClientWrapper.class differ diff --git a/target/classes/blockchain/client/FabricClientWrapper.class b/target/classes/blockchain/client/FabricClientWrapper.class new file mode 100644 index 0000000..d40738f Binary files /dev/null and b/target/classes/blockchain/client/FabricClientWrapper.class differ diff --git a/target/classes/blockchain/user/UserContext.class b/target/classes/blockchain/user/UserContext.class new file mode 100644 index 0000000..88c8e70 Binary files /dev/null and b/target/classes/blockchain/user/UserContext.class differ diff --git a/target/classes/blockchain/utility/Util.class b/target/classes/blockchain/utility/Util.class new file mode 100644 index 0000000..5f9ec2d Binary files /dev/null and b/target/classes/blockchain/utility/Util.class differ diff --git a/target/test-classes/blockchain/user/UserContextTest.class b/target/test-classes/blockchain/user/UserContextTest.class new file mode 100644 index 0000000..f6e3c6a Binary files /dev/null and b/target/test-classes/blockchain/user/UserContextTest.class differ diff --git a/target/test-classes/blockchain/utility/UtilTest.class b/target/test-classes/blockchain/utility/UtilTest.class new file mode 100644 index 0000000..7dcb7ca Binary files /dev/null and b/target/test-classes/blockchain/utility/UtilTest.class differ