Skip to content

Commit

Permalink
Merge pull request #117 from WeBankFinTech/release/1.5.1
Browse files Browse the repository at this point in the history
Merge release/1.5.1 to master
  • Loading branch information
chaoxinhu committed Jan 22, 2020
2 parents d1bdddf + 13439a5 commit f24e41a
Show file tree
Hide file tree
Showing 14 changed files with 422 additions and 23 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### V1.5.1 (2020-01-22)
- Features:
1. Queried CPT can be cached to local machine (require MySQL setup).
2. New interface generateHash(), accepting File, Credential and String as hashable input.

- Bugfixes:
1. Multiple miscellaneous bugfixes and development experience enhancements.

### V1.5.0 (2019-12-30)
- Features:
1. CredentialPojo support Zero-Knowledge-Proof based disclosure.
Expand Down
17 changes: 11 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (!gradle.startParameter.isOffline()) {

group 'com.webank'

version = "1.5.0"
version = "1.5.1"

// Specify JDK version - may vary in different scenarios
sourceCompatibility = 1.8
Expand Down Expand Up @@ -66,7 +66,8 @@ List logger = [
"org.apache.logging.log4j:log4j-core:2.9.0",
"org.apache.logging.log4j:log4j-slf4j-impl:2.9.0",
"org.apache.logging.log4j:log4j-web:2.9.0",
"org.apache.logging.log4j:log4j-jcl:2.9.0"
"org.apache.logging.log4j:log4j-jcl:2.9.0",
"com.lmax:disruptor:3.3.7"
]

// junit test
Expand Down Expand Up @@ -113,6 +114,10 @@ List protobuf = [
"com.google.protobuf:protobuf-java:3.9.1"
]

List caffeine = [
"com.github.ben-manes.caffeine:caffeine:2.8.0"
]

configurations {
localDeps
}
Expand All @@ -122,12 +127,12 @@ dependencies {
localDeps 'org.projectlombok:lombok:1.18.10'
if (gradleVer.startsWith("4")) {
if (!gradle.startParameter.isOffline()) {
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf
compile logger, lombok, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine
compile("com.webank:weid-contract-java:1.2.12") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile files("lib/WeDPR-Java-SDK.jar")
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf
testCompile logger, lombok, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine
} else {
compile fileTree(dir: 'dist/lib', include: '*.jar')
}
Expand All @@ -138,12 +143,12 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok:1.18.10'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
testCompileOnly 'org.projectlombok:lombok:1.18.10'
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf
compile logger, apache_commons, json, mysql_driver, zxing, rpc, pdfbox, protobuf, caffeine
compile("com.webank:weid-contract-java:1.2.12") {
exclude group: "org.slf4j", module: "slf4j-log4j12"
}
compile files("lib/WeDPR-Java-SDK.jar")
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf
testCompile logger, apache_commons, json, junit, jmockit, rpc, pdfbox, protobuf, caffeine
} else {
compileOnly files('dist/lib/lombok-1.18.10.jar')
annotationProcessor files('dist/lib/lombok-1.18.10.jar')
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/webank/weid/rpc/EvidenceService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018-2019) WeBank Co., Ltd.
* Copyright© (2018-2020) WeBank Co., Ltd.
*
* This file is part of weid-java-sdk.
*
Expand All @@ -22,6 +22,7 @@
import java.util.List;

import com.webank.weid.protocol.base.EvidenceInfo;
import com.webank.weid.protocol.base.HashString;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.inf.Hashable;
import com.webank.weid.protocol.response.ResponseData;
Expand Down Expand Up @@ -105,4 +106,13 @@ ResponseData<Boolean> setHashValue(String hashValue, String evidenceAddress,
* @return true if succeeds, false otherwise
*/
ResponseData<Boolean> verify(Hashable object, String evidenceAddress);

/**
* Generate hash value of any passed-in param.
*
* @param object param to be hashed
* @param <T> type of param
* @return the hash string
*/
<T> ResponseData<HashString> generateHash(T object);
}
2 changes: 1 addition & 1 deletion src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public abstract class BaseService {
NativeUtils.loadLibraryFromJar("/WeDPR_dynamic_lib/libeay32md.dll");
NativeUtils.loadLibraryFromJar("/WeDPR_dynamic_lib/ssleay32md.dll");
} catch (IOException e) {
logger.error("[BaseService] the blockchain orgId is blank.");
logger.error("[BaseService] Cannot find SSL libraries (in Windows).");
}
}
}
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/com/webank/weid/service/impl/CptServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import com.webank.weid.service.BaseService;
import com.webank.weid.service.impl.engine.CptServiceEngine;
import com.webank.weid.service.impl.engine.EngineFactory;
import com.webank.weid.suite.cache.CacheManager;
import com.webank.weid.suite.cache.CacheNode;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.WeIdUtils;

Expand All @@ -57,6 +59,10 @@ public class CptServiceImpl extends BaseService implements CptService {

private CptServiceEngine cptServiceEngine = EngineFactory.createCptServiceEngine();

//获取CPT缓存节点
private static CacheNode<ResponseData<Cpt>> cptCahceNode =
CacheManager.registerCacheNode("SYS_CPT", 1000 * 3600 * 24L);

/**
* Register a new CPT with a pre-set CPT ID, to the blockchain.
*
Expand Down Expand Up @@ -200,8 +206,15 @@ public ResponseData<Cpt> queryCpt(Integer cptId) {
if (cptId == null || cptId < 0) {
return new ResponseData<>(null, ErrorCode.CPT_ID_ILLEGAL);
}

return cptServiceEngine.queryCpt(cptId);
String cptIdStr = String.valueOf(cptId);
ResponseData<Cpt> result = cptCahceNode.get(cptIdStr);
if (result == null) {
result = cptServiceEngine.queryCpt(cptId);
if (result.getErrorCode().intValue() == ErrorCode.SUCCESS.getCode()) {
cptCahceNode.put(cptIdStr, result);
}
}
return result;
} catch (Exception e) {
logger.error("[updateCpt] query cpt failed due to unknown error. ", e);
return new ResponseData<>(null, ErrorCode.UNKNOW_ERROR);
Expand Down Expand Up @@ -268,8 +281,12 @@ public ResponseData<CptBaseInfo> updateCpt(CptMapArgs args, Integer cptId) {
cptJsonSchemaNew,
weIdPrivateKey);
String address = WeIdUtils.convertWeIdToAddress(weId);
return cptServiceEngine.updateCpt(cptId, address, cptJsonSchemaNew, rsvSignature,
weIdPrivateKey.getPrivateKey());
ResponseData<CptBaseInfo> result = cptServiceEngine.updateCpt(cptId, address,
cptJsonSchemaNew, rsvSignature, weIdPrivateKey.getPrivateKey());
if (result.getErrorCode().intValue() == ErrorCode.SUCCESS.getCode()) {
cptCahceNode.remove(String.valueOf(cptId));
}
return result;
} catch (Exception e) {
logger.error("[updateCpt] update cpt failed due to unkown error. ", e);
return new ResponseData<>(null, ErrorCode.UNKNOW_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public class CredentialPojoServiceImpl extends BaseService implements Credential
private static CptService cptService = new CptServiceImpl();
private static Persistence dataDriver = new MysqlDriver();


/**
* Salt generator. Automatically fillin the map structure in a recursive manner.
*
Expand Down Expand Up @@ -622,7 +621,7 @@ private static ErrorCode verifyCptFormat(Integer cptId, Map<String, Object> clai
if (cpt == null) {
logger.error(ErrorCode.CREDENTIAL_CPT_NOT_EXISTS.getCodeDesc());
return ErrorCode.CREDENTIAL_CPT_NOT_EXISTS;
}
}
//String cptJsonSchema = JsonUtil.objToJsonStr(cpt.getCptJsonSchema());
String cptJsonSchema = DataToolUtils.serialize(cpt.getCptJsonSchema());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright© (2018-2019) WeBank Co., Ltd.
* Copyright© (2018-2020) WeBank Co., Ltd.
*
* This file is part of weid-java-sdk.
*
Expand All @@ -19,6 +19,7 @@

package com.webank.weid.service.impl;

import java.io.File;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -27,6 +28,8 @@
import java.util.Set;
import java.util.regex.Pattern;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.lang3.StringUtils;
import org.bcos.web3j.abi.datatypes.Address;
import org.bcos.web3j.crypto.ECKeyPair;
Expand All @@ -40,6 +43,7 @@
import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.exception.WeIdBaseException;
import com.webank.weid.protocol.base.EvidenceInfo;
import com.webank.weid.protocol.base.HashString;
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.inf.Hashable;
Expand Down Expand Up @@ -197,6 +201,7 @@ public ResponseData<Boolean> addSignature(Hashable object, String evidenceAddres
* @param weIdPrivateKey the signer WeID's private key
* @return true if succeed, false otherwise
*/
@Override
public ResponseData<Boolean> setHashValue(String hashValue, String evidenceAddress,
WeIdPrivateKey weIdPrivateKey) {
if (!verifyHashValueFormat(hashValue)) {
Expand Down Expand Up @@ -249,6 +254,53 @@ public ResponseData<Boolean> setHashValue(String hashValue, String evidenceAddre
}
}

/* (non-Javadoc)
* @see com.webank.weid.rpc.generateHash
* #generateHash(T object)
*/
@Override
public <T> ResponseData<HashString> generateHash(T object) {
if (object == null) {
return new ResponseData<>(null, ErrorCode.ILLEGAL_INPUT);
}
if (object instanceof Hashable) {
ResponseData<String> hashResp = getHashValue((Hashable) object);
if (StringUtils.isEmpty(hashResp.getResult())) {
return new ResponseData<>(null, hashResp.getErrorCode(),
hashResp.getErrorMessage());
}
return new ResponseData<>(new HashString(hashResp.getResult()), ErrorCode.SUCCESS);
}
if (object instanceof File) {
// This will convert all types of file into String stream
String rawData = convertFileToString((File) object);
if (StringUtils.isEmpty(rawData)) {
logger.error("Failed to convert file into String: {}", ((File) object).getName());
return new ResponseData<>(null, ErrorCode.ILLEGAL_INPUT);
}
return new ResponseData<>(new HashString(DataToolUtils.sha3(rawData)),
ErrorCode.SUCCESS);
}
if (object instanceof String) {
if (StringUtils.isEmpty((String) object)) {
logger.error("Input String is blank, ignored..");
return new ResponseData<>(null, ErrorCode.ILLEGAL_INPUT);
}
return new ResponseData<>(new HashString(DataToolUtils.sha3((String) object)),
ErrorCode.SUCCESS);
}
logger.error("Unsupported input object type: {}", object.getClass().getCanonicalName());
return new ResponseData<>(null, ErrorCode.ILLEGAL_INPUT);
}

private String convertFileToString(File file) {
try {
return Files.asByteSource(file).asCharSource(Charsets.UTF_8).read();
} catch (Exception e) {
logger.error("Failed to load file as String.");
return StringUtils.EMPTY;
}
}

/**
* Obtain the hash value of a given object - supports Credential, Wrapper and Pojo, and also
Expand Down Expand Up @@ -363,8 +415,6 @@ private ResponseData<Boolean> verify(String hashValue, String evidenceAddress) {
return verifyHashToEvidenceSignature(hashValue, evidenceInfo);
}

;

/**
* Verify a Credential based on the provided Evidence info.
*
Expand Down Expand Up @@ -475,10 +525,7 @@ private ResponseData<Boolean> verifySignatureToSigner(
}

private boolean verifyHashValueFormat(String hashValue) {
if (StringUtils.isEmpty(hashValue)
|| !Pattern.compile(WeIdConstant.HASH_VALUE_PATTERN).matcher(hashValue).matches()) {
return false;
}
return true;
return !StringUtils.isEmpty(hashValue)
&& Pattern.compile(WeIdConstant.HASH_VALUE_PATTERN).matcher(hashValue).matches();
}
}

0 comments on commit f24e41a

Please sign in to comment.