WELP.
This commit is contained in:
parent
a32f4ef6b9
commit
e9c8d87695
5
pom.xml
5
pom.xml
|
@ -34,6 +34,11 @@
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>1.2.17</version>
|
<version>1.2.17</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
<version>1.60</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
27
src/main/java/blockchain/user/CAEnrollement.java
Normal file
27
src/main/java/blockchain/user/CAEnrollement.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package blockchain.user;
|
||||||
|
|
||||||
|
import org.hyperledger.fabric.sdk.Enrollment;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
|
||||||
|
public class CAEnrollement implements Enrollment, Serializable {
|
||||||
|
private static final long serialVersionUID = 550416591376968096L;
|
||||||
|
private PrivateKey privateKey;
|
||||||
|
private String cert;
|
||||||
|
|
||||||
|
public CAEnrollement(PrivateKey pkey, String signedPem){
|
||||||
|
this.privateKey=pkey;
|
||||||
|
this.cert=signedPem;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PrivateKey getKey() {
|
||||||
|
return privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCert() {
|
||||||
|
return cert;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,22 @@
|
||||||
package blockchain.utility;
|
package blockchain.utility;
|
||||||
|
|
||||||
|
import blockchain.user.CAEnrollement;
|
||||||
import blockchain.user.UserContext;
|
import blockchain.user.UserContext;
|
||||||
import org.apache.log4j.BasicConfigurator;
|
import org.apache.log4j.BasicConfigurator;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
import org.bouncycastle.crypto.CryptoException;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.Security;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||||
|
|
||||||
public class Util {
|
public class Util {
|
||||||
private static Logger logger = Logger.getLogger(Util.class);
|
private static Logger logger = Logger.getLogger(Util.class);
|
||||||
|
@ -84,4 +96,39 @@ public class Util {
|
||||||
return userContext;
|
return userContext;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CAEnrollement getEnrollement(String keyFolderPath, String keyFileName, String certFolderPath, String certFileName) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, CryptoException {
|
||||||
|
PrivateKey key = null;
|
||||||
|
String certificate = null;
|
||||||
|
InputStream isKey = null;
|
||||||
|
BufferedReader brKey = null;
|
||||||
|
|
||||||
|
try{
|
||||||
|
Security.addProvider(new BouncyCastleProvider());
|
||||||
|
String keyPath = keyFolderPath+"/"+keyFileName;
|
||||||
|
isKey = new FileInputStream(keyPath);
|
||||||
|
brKey = new BufferedReader(new InputStreamReader(isKey));
|
||||||
|
StringBuilder keyBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
for(String line = brKey.readLine(); line != null; line = brKey.readLine()){
|
||||||
|
if(line.indexOf("PRIVATE") == -1){
|
||||||
|
keyBuilder.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
certificate = new String(Files.readAllBytes(Paths.get(certFolderPath,certFileName)));
|
||||||
|
|
||||||
|
byte[] encoded = DatatypeConverter.parseBase64Binary(keyBuilder.toString());
|
||||||
|
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
|
||||||
|
KeyFactory kf = KeyFactory.getInstance("ECDSA");
|
||||||
|
key = kf.generatePrivate(keySpec);
|
||||||
|
|
||||||
|
}finally {
|
||||||
|
isKey.close();
|
||||||
|
brKey.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
CAEnrollement enrollment = new CAEnrollement(key,certificate);
|
||||||
|
return enrollment;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.apache.log4j.Logger;
|
||||||
import org.hyperledger.fabric.sdk.*;
|
import org.hyperledger.fabric.sdk.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -19,8 +20,26 @@ public class QueryTest {
|
||||||
@Test
|
@Test
|
||||||
public void TestAQueryChannels() {
|
public void TestAQueryChannels() {
|
||||||
BasicConfigurator.configure();
|
BasicConfigurator.configure();
|
||||||
UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
|
||||||
|
//UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
|
||||||
|
//TEST FAILED
|
||||||
|
|
||||||
|
UserContext admin = new UserContext();
|
||||||
|
File pkFolder = new File(Config.ADMIN_KEY_PATH);
|
||||||
|
File[] pkFile = pkFolder.listFiles();
|
||||||
|
File certFolder = new File(Config.ADMIN_CERT_PATH);
|
||||||
|
File[] certFile = certFolder.listFiles();
|
||||||
|
admin.setName(Config.ADMIN);
|
||||||
|
admin.setMspId(Config.ORG1_MSP);
|
||||||
|
admin.setAffiliation(Config.ORG1);
|
||||||
|
Enrollment enrollAdmin = Util.getEnrollement(Config.ADMIN_KEY_PATH, pkFile[0].getName(), Config.ADMIN_CERT_PATH, certFile[0].getName());
|
||||||
|
admin.setEnrollment(enrollAdmin);
|
||||||
|
|
||||||
|
//END TEST
|
||||||
|
|
||||||
String chaincode = Config.CHAINCODE_NAME;
|
String chaincode = Config.CHAINCODE_NAME;
|
||||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(admin);
|
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(admin);
|
||||||
ChannelClientWrapper channelClientWrapper = fabricClientWrapper.createChannelClient(Config.CHANNEL_NAME);
|
ChannelClientWrapper channelClientWrapper = fabricClientWrapper.createChannelClient(Config.CHANNEL_NAME);
|
||||||
|
|
37
src/test/java/blockchain/utility/UtilEnrollTest.java
Normal file
37
src/test/java/blockchain/utility/UtilEnrollTest.java
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package blockchain.utility;
|
||||||
|
|
||||||
|
import blockchain.configuration.Config;
|
||||||
|
import org.apache.log4j.BasicConfigurator;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.hyperledger.fabric.sdk.Enrollment;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class UtilEnrollTest {
|
||||||
|
private static Logger logger = Logger.getLogger(UtilTest.class);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void TestAGetEnrollement(){
|
||||||
|
BasicConfigurator.configure();
|
||||||
|
|
||||||
|
try{
|
||||||
|
logger.info("----- START TEST ----");
|
||||||
|
File pkFolder = new File(Config.ADMIN_KEY_PATH);
|
||||||
|
File[] pkFile = pkFolder.listFiles();
|
||||||
|
File certFolder = new File(Config.ADMIN_CERT_PATH);
|
||||||
|
File[] certFile = certFolder.listFiles();
|
||||||
|
logger.info("----- GET ENROLLEMENT ----");
|
||||||
|
Enrollment enrollAdmin = Util.getEnrollement(Config.ADMIN_KEY_PATH, pkFile[0].getName(), Config.ADMIN_CERT_PATH, certFile[0].getName());
|
||||||
|
|
||||||
|
logger.info("KEY : "+enrollAdmin.getKey());
|
||||||
|
logger.info("CERT : "+enrollAdmin.getCert());
|
||||||
|
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
BIN
target/classes/blockchain/user/CAEnrollement.class
Normal file
BIN
target/classes/blockchain/user/CAEnrollement.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
target/test-classes/blockchain/utility/UtilEnrollTest.class
Normal file
BIN
target/test-classes/blockchain/utility/UtilEnrollTest.class
Normal file
Binary file not shown.
Loading…
Reference in a new issue