Update features

This commit is contained in:
GME 2018-10-19 09:47:49 +02:00
parent 57cd29ad6f
commit 4bd2b8d0da
13 changed files with 337 additions and 29 deletions

View file

@ -41,9 +41,9 @@ dependencies {
exclude group:'log4j', module:'log4j'
}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
androidTestImplementation 'com.android.support:support-annotations:26.1.0'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})

View file

@ -34,7 +34,7 @@ public class LearningTest {
System.out.println(appContext.getPackageName());
InputStream is = appContext.getResources().openRawResource(R.raw.config);
/*
try (JsonReader reader = Json.createReader(is)) {
JsonObject jsonConfig = (JsonObject) reader.read();
System.out.println(jsonConfig);
@ -42,7 +42,7 @@ public class LearningTest {
NetworkConfig config;
config = NetworkConfig.fromJsonObject(jsonConfig);
//System.out.println(config!=null);
}
}*/
/*
Writer writer = new StringWriter();

View file

@ -0,0 +1,57 @@
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 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();
}
}
}

View file

@ -0,0 +1,61 @@
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

@ -7,8 +7,8 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application

View file

@ -3,9 +3,14 @@ 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;
@ -14,44 +19,76 @@ import monnethic.mobile.blockchain.utility.Util;
public class CAClientWrapper {
private String caUrl;
private HFCAClient hfcaClient;
private Properties properties;
private String org;
private String url = "http://ca.org1.example.com:7054";
private String mspId = "";
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);
}
void init(String url){
private void init(String url){
try{
this.hfcaClient = HFCAClient.createNewInstance(url,null);
}catch (Exception e){
e.printStackTrace();
}
}
*/
//Method to enroll admin
public void enrollAdmin(Context context, String name, String secret, String org) throws Exception {
//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,name,secret);
if(adminContext!=null){
Logger.getLogger(CAClientWrapper.class.getName()).log(Level.WARNING, "Admin already enrolled, skip");
return;
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 = new UserContext();
//adminContext.setName(name);
//adminContext.setAffiliation(org);
//adminContext.setMspId(mspId);
adminContext.setEnrollment(enrollment);
adminContext.setAffiliation(org);
adminContext.setMspId(mspId);
Util.writeUserContext(context,adminContext);
return adminContext;
}
//Method to register and enroll user

View file

@ -0,0 +1,51 @@
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

@ -0,0 +1,32 @@
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

@ -0,0 +1,27 @@
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 = "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

@ -10,7 +10,7 @@ public class UserContext implements User, Serializable {
private static final long serialVersionUID = 1L;
protected String name;
protected Set<String> roles;
protected Set<String> roles;
protected String account;
protected String affiliation;
protected Enrollment enrollment;
@ -45,7 +45,7 @@ public class UserContext implements User, Serializable {
@Override
public String getAffiliation() {
return null;
return affiliation;
}
public void setAffiliation(String affiliation){

View file

@ -7,6 +7,8 @@ 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;
@ -21,21 +23,26 @@ public class Util {
//Method which write user info into a directory
public static void writeUserContext(Context context, UserContext userContext){
ObjectOutputStream out = null;
FileOutputStream file = 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()){
dir.mkdirs();
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 = null;
file = new FileOutputStream(pathFile);
out = new ObjectOutputStream(file);
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();
@ -48,8 +55,8 @@ public class Util {
e.printStackTrace();
}
try{
if(file != null){
file.close();
if(fileOutputStream != null){
fileOutputStream.close();
}
}catch (Exception e){
e.printStackTrace();

View file

@ -0,0 +1,36 @@
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();
}
}
}