Clean project from blockchain

This commit is contained in:
GME 2018-10-20 15:20:05 +02:00
parent cc8918c8ed
commit f16ec4c8fc
10 changed files with 0 additions and 718 deletions

View file

@ -1,97 +0,0 @@
package monnethic.mobile.test.blockchain;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.junit.Test;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import monnethic.mobile.blockchain.client.CAClientWrapper;
import monnethic.mobile.blockchain.client.ChannelClientWrapper;
import monnethic.mobile.blockchain.client.FabriClientWrapper;
import monnethic.mobile.blockchain.network.Config;
import monnethic.mobile.blockchain.participant.UserContext;
import monnethic.mobile.test.blockchain.Utility.UtilTest;
public class TestQuery {
@Test
public void TestAEnrollAdmin() throws Exception {
try{
Context appContext = InstrumentationRegistry.getTargetContext();
String caUrl = Config.CA_ORG1_URL;
CAClientWrapper caClientWrapper = new CAClientWrapper(caUrl,null,null,null);
UserContext adminContext = new UserContext();
adminContext.setName(Config.ADMIN);
adminContext.setAffiliation(Config.ORG1);
adminContext.setMspId(Config.ORG1_MSP);
caClientWrapper.setAdminContext(adminContext);
adminContext = caClientWrapper.enrollAdmin(appContext,Config.ADMIN,Config.ADMIN_PASSWORD,Config.ORG1);
Logger.getLogger(TestQuery.class.getName()).log(Level.INFO, "adminContext Enrollement : "+adminContext.getEnrollment().toString());
}catch (Exception e){
e.printStackTrace();
}
}
/*
@Test
public void TestAQueryChainCode() throws Exception {
// Test query for user a on blockchain
try{
String query = "query";
String[] args1 = {"a"};
Context appContext = InstrumentationRegistry.getTargetContext();
String caUrl = Config.CA_ORG1_URL;
CAClientWrapper caClientWrapper = new CAClientWrapper(caUrl,null,null,null);
UserContext adminContext = new UserContext();
adminContext.setName(Config.ADMIN);
adminContext.setAffiliation(Config.ORG1);
adminContext.setMspId(Config.ORG1_MSP);
caClientWrapper.setAdminContext(adminContext);
adminContext = caClientWrapper.enrollAdmin(appContext,Config.ADMIN,Config.ADMIN_PASSWORD,Config.ORG1);
FabriClientWrapper fabriClientWrapper = new FabriClientWrapper(adminContext);
ChannelClientWrapper channelClientWrapper = fabriClientWrapper.createChannelClient(Config.CHANNEL_NAME);
Logger.getLogger(TestQuery.class.getName()).log(Level.INFO, "Querying for USER A ...");
Collection<ProposalResponse> responsesQuery = channelClientWrapper.queryByChainCode(Config.CHAINCODE_NAME,query,args1);
for(ProposalResponse pres : responsesQuery){
String stringResponse = new String(pres.getChaincodeActionResponsePayload());
Logger.getLogger(TestQuery.class.getName()).log(Level.INFO, stringResponse);
}
} catch (Exception e){
e.printStackTrace();
}
}
*/
/*
@Test
public void TestBQuery() throws Exception {
try{
UserContext userContext = new UserContext();
userContext.setName(Config.ADMIN);
userContext.setAffiliation(Config.ORG1);
userContext.setMspId(Config.ORG1_MSP);
FabriClientWrapper client = new FabriClientWrapper(userContext);
}catch (Exception e){
e.printStackTrace();
}
}
*/
}

View file

@ -1,63 +0,0 @@
package monnethic.mobile.test.blockchain.Utility;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.logging.Level;
import java.util.logging.Logger;
import monnethic.mobile.blockchain.participant.UserContext;
import monnethic.mobile.blockchain.utility.Util;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UtilTest {
/*
@Test
public void readEmptyUserContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
}*/
@Test
public void testAreadNonUserContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
UserContext uc = Util.readUserContext(appContext,"tata","abdel");
if(uc!=null){
Logger.getLogger(UtilTest.class.getName()).log(Level.INFO, "UserContext is : "+uc.toString());
}else{
Logger.getLogger(UtilTest.class.getName()).log(Level.INFO, "UserContext doesn't exist");
}
}
@Test
public void testBwriteUserContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
UserContext user = new UserContext();
user.setName("abdel");
user.setAffiliation("toto");
user.setEnrollment(null);
user.setMspId("test");
Util.writeUserContext(appContext, user);
}
@Test
public void testCreadUserContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
UserContext uc = Util.readUserContext(appContext,"toto","abdel");
Logger.getLogger(UtilTest.class.getName()).log(Level.INFO, "UserContext is : "+uc.toString());
}
}

View file

@ -1,149 +0,0 @@
package monnethic.mobile.blockchain.client;
import android.content.Context;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import monnethic.mobile.blockchain.participant.UserContext;
import monnethic.mobile.blockchain.utility.Util;
public class CAClientWrapper {
private String caUrl;
private HFCAClient hfcaClient;
private Properties properties;
private String org;
private String mspId;
private UserContext adminContext;
public UserContext getAdminContext() {
return adminContext;
}
public void setAdminContext(UserContext adminContext){
this.adminContext=adminContext;
}
public CAClientWrapper(String caUrl, Properties properties, String org, String mspId) throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException {
this.caUrl=caUrl;
this.properties=properties;
this.org=org;
this.mspId=mspId;
init();
}
public void init() throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
hfcaClient = HFCAClient.createNewInstance(caUrl,properties);
hfcaClient.setCryptoSuite(cryptoSuite);
}
public HFCAClient getHfcaClient(){
return hfcaClient;
}
/*
public CAClientWrapper(String url, String org){
this.org=org;
init(url);
}
private void init(String url){
try{
this.hfcaClient = HFCAClient.createNewInstance(url,null);
}catch (Exception e){
e.printStackTrace();
}
}
*/
//Method to enroll admin and save his info
public UserContext enrollAdmin(Context context, String name, String secret, String org) throws Exception {
UserContext adminContext;
adminContext = Util.readUserContext(context,org,name);
if(adminContext!=null){
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.WARNING, "Admin already enrolled, skip");
return adminContext;
}
Enrollment enrollment = hfcaClient.enroll(name,secret);
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.INFO, "Admin enrolled");
//adminContext = new UserContext();
//adminContext.setName(name);
//adminContext.setAffiliation(org);
//adminContext.setMspId(mspId);
adminContext.setEnrollment(enrollment);
Util.writeUserContext(context,adminContext);
return adminContext;
}
//Method to register and enroll user
public void regiserUser(Context context, String userName, String registrarAdmin) throws Exception{
UserContext userContext;
userContext = Util.readUserContext(context,org,userName);
if(userContext!=null){
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.WARNING, "User already registered");
return;
}
RegistrationRequest registrationRequest = new RegistrationRequest(userName,org);
UserContext registrarContext = Util.readUserContext(context,org,registrarAdmin);
if(registrarAdmin==null){
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.SEVERE, "Registar not enrolled");
throw new Exception("registrar context not found");
}
String enrollSecret = hfcaClient.register(registrationRequest, registrarContext);
Enrollment enrollment = hfcaClient.enroll(userName,enrollSecret);
userContext = new UserContext();
userContext.setMspId(mspId);
userContext.setAffiliation(org); //TODO GET MSPID IN ORGANIZATIONS, ORGNAME
userContext.setEnrollment(enrollment);
userContext.setName(userName);
Util.writeUserContext(context,userContext);
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.INFO, "User has been successfully registered");
}
//Method to get userContext
public static UserContext getUserContext(Context context, String userName, String org){
UserContext userContext;
userContext = Util.readUserContext(context,org,userName);
if(userContext!=null){
return userContext;
}
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.SEVERE, "User context not found : "+userName+" - "+org);
return userContext;
}
//Method to enroll user.
public UserContext enrollUser(Context context, String userName, String enrollSecret, String org) throws Exception {
Enrollment enrollment = hfcaClient.enroll(userName,enrollSecret);
UserContext userContext = new UserContext();
userContext.setMspId(mspId);
userContext.setAffiliation(this.org);
userContext.setEnrollment(enrollment);
userContext.setName(userName);
Util.writeUserContext(context, userContext);
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.INFO, "UserName - "+userName+" enrolled");
return userContext;
}
}

View file

@ -1,51 +0,0 @@
package monnethic.mobile.blockchain.client;
import org.hyperledger.fabric.sdk.ChaincodeID;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ChannelClientWrapper {
FabriClientWrapper fabriClientWrapper;
Channel channel;
String name;
public Channel getChannel() {
return channel;
}
public String getName() {
return name;
}
public FabriClientWrapper getFabriClientWrapper() {
return fabriClientWrapper;
}
public ChannelClientWrapper(String name, Channel channel, FabriClientWrapper fabriClientWrapper){
this.name=name;
this.channel=channel;
this.fabriClientWrapper=fabriClientWrapper;
}
public Collection<ProposalResponse> queryByChainCode(String chaincodeName, String functionName, String[] args) throws InvalidArgumentException, ProposalException {
Logger.getLogger(ChannelClientWrapper.class.getName()).log(Level.INFO, "Querying "+chaincodeName+" with "+functionName);
QueryByChaincodeRequest request = fabriClientWrapper.getHfClient().newQueryProposalRequest();
ChaincodeID chaincodeId = ChaincodeID.newBuilder().setName(chaincodeName).build();
request.setChaincodeID(chaincodeId);
request.setFcn(functionName);
if(args != null){
request.setArgs(args);
}
Collection<ProposalResponse> response = channel.queryByChaincode(request);
return response;
}
}

View file

@ -1,32 +0,0 @@
package monnethic.mobile.blockchain.client;
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;
import monnethic.mobile.blockchain.participant.UserContext;
public class FabriClientWrapper {
private HFClient hfClient;
public HFClient getHfClient() {
return hfClient;
}
public FabriClientWrapper(UserContext userContext) throws CryptoException, InvalidArgumentException, IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
hfClient = HFClient.createNewInstance();
hfClient.setCryptoSuite(cryptoSuite);
hfClient.setUserContext(userContext);
}
public ChannelClientWrapper createChannelClient(String name) throws InvalidArgumentException {
Channel channel = hfClient.newChannel(name);
ChannelClientWrapper client = new ChannelClientWrapper(name, channel, this);
return client;
}
}

View file

@ -1,27 +0,0 @@
package monnethic.mobile.blockchain.network;
public class Config {
//TODO SET STATIC FINAL VAR FOR NETWORK CONFIG, REPLACE LOADCONFIGINFO
public static final String ORG1_MSP = "Org1MSP";
public static final String ORG1 = "org1";
public static final String ADMIN = "AdminOrg1";
public static final String ADMIN_PASSWORD = "AdminOrg1Pwd";
public static final String CA_ORG1_URL = "http://vps577432.ovh.net:7054";
public static final String ORDERER_URL = "vps577432.ovh.net:7050";
public static final String ORDERER_NAME = "orderer.example.com";
public static final String CHANNEL_NAME = "mychannel";
public static final String CHAINCODE_NAME = "mycc";
public static final String ORG1_PEER_0 = "peer0.org1.example.com";
public static final String ORG1_PEER_0_URL = "vps577432.ovh.net:7051";
}

View file

@ -1,86 +0,0 @@
package monnethic.mobile.blockchain.network;
import android.content.Context;
import com.example.monnthic.monnethicmobile.R;
//import org.hyperledger.fabric.sdk.NetworkConfig;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.logging.XMLFormatter;
import javax.json.JsonObject;
public class LoadConfigInfo {
private static final Integer lock = 0;
private static JSONObject jsonObject;
//Generate JSONObject from configFile
private LoadConfigInfo(Context context) {
InputStream is = context.getResources().openRawResource(R.raw.config);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} catch (Exception e){
e.getMessage();
} finally {
try{
if(is!=null) {
is.close();
}
}catch (Exception e){
e.getMessage();
}
}
String jsonString = writer.toString();
try{
jsonObject = new JSONObject(jsonString);
}catch (Exception e){
e.getMessage();
}
}
private static void getInstanceFile(Context context){
try{
synchronized (lock){
if(jsonObject == null){
new LoadConfigInfo(context);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
//TODO GET ORGANIZATION INFOS
public static String getOrgInfo(Context context, String org) {
getInstanceFile(context);
return "";
}
//TODO GET ORGANIZATION NAME
public static String getOrgName(Context context){
getInstanceFile(context);
return "";
}
//TODO GET MSPID IN ORG NAME
public static String getMspId(Context context,String orgName){
getInstanceFile(context);
return "";
}
}

View file

@ -1,72 +0,0 @@
package monnethic.mobile.blockchain.participant;
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<String> roles;
protected String account;
protected String affiliation;
protected Enrollment enrollment;
protected String mspId;
@Override
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
@Override
public Set<String> getRoles() {
return roles;
}
public void setRoles(Set<String> roles){
this.roles=roles;
}
@Override
public String getAccount() {
return account;
}
public void setAccount(String account){
this.account=account;
}
@Override
public String getAffiliation() {
return affiliation;
}
public void setAffiliation(String affiliation){
this.affiliation=affiliation;
}
@Override
public Enrollment getEnrollment() {
return enrollment;
}
public void setEnrollment(Enrollment enrollment){
this.enrollment=enrollment;
}
@Override
public String getMspId() {
return mspId;
}
public void setMspId(String mspId){
this.mspId=mspId;
}
}

View file

@ -1,105 +0,0 @@
package monnethic.mobile.blockchain.utility;
import android.content.Context;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import monnethic.mobile.blockchain.participant.UserContext;
public class Util {
private Context context;
public Util(Context context){
this.context=context;
}
//Method which write user info into a directory
public static void writeUserContext(Context context, UserContext userContext){
ObjectOutputStream out = null;
FileOutputStream fileOutputStream = null;
try{
String filename = userContext.getName()+".context";
String dirPath = context.getFilesDir()+"userInfos/"+userContext.getAffiliation();
String pathFile = dirPath+"/"+filename;
Logger.getLogger(Util.class.getName()).log(Level.INFO, "pathFile is : "+pathFile);
File dir = new File(dirPath);
if(!dir.exists()){
Boolean res = dir.mkdirs();
if(res){
Logger.getLogger(Util.class.getName()).log(Level.INFO, "RESPONSE IS TRUE");
}else{
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, "RESPONSE IS FALSE");
}
}
File file = new File(pathFile);
fileOutputStream = new FileOutputStream(file);
out = new ObjectOutputStream(fileOutputStream);
Logger.getLogger(Util.class.getName()).log(Level.INFO, "out is : "+out);
out.writeObject(userContext);
}catch (Exception e){
e.printStackTrace();
} finally {
try {
if (out != null){
out.close();
}
}catch (Exception e){
e.printStackTrace();
}
try{
if(fileOutputStream != null){
fileOutputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
//Method to read user in file
public static UserContext readUserContext(Context context, String affiliation, String username){
UserContext userContext = null;
FileInputStream fileInputStream = null;
ObjectInputStream in = null;
try{
String filename = username+".context";
String dirPath = context.getFilesDir()+"userInfos/"+affiliation;
String pathFile = dirPath+"/"+filename;
File file = new File(pathFile);
if(file.exists()){
fileInputStream = new FileInputStream(pathFile);
in = new ObjectInputStream(fileInputStream);
userContext = (UserContext) in.readObject();
}
} catch (Exception e){
e.printStackTrace();
} finally {
try {
if(in != null){
in.close();
}
}catch (Exception e){
e.printStackTrace();
}
try {
if(fileInputStream!=null){
fileInputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return userContext;
}
}

View file

@ -1,36 +0,0 @@
package monnethic.mobile.test.blockchain;
import android.content.Context;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import monnethic.mobile.blockchain.client.CAClientWrapper;
import monnethic.mobile.blockchain.network.Config;
import monnethic.mobile.blockchain.participant.UserContext;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestQuery {
@Test
void TestAQueryChainCode(){
try{
//context appContext = InstrumentationRegistry.getTargetContext();
String caUrl = Config.CA_ORG1_URL;
CAClientWrapper caClientWrapper = new CAClientWrapper(caUrl,null,null,null);
UserContext adminContext = new UserContext();
adminContext.setName(Config.ADMIN);
adminContext.setAffiliation(Config.ORG1);
adminContext.setMspId(Config.ORG1_MSP);
caClientWrapper.setAdminContext(adminContext);
//adminContext = caClientWrapper.enrollAdmin(appContext);
} catch (Exception e){
e.printStackTrace();
}
}
}