Skip to content

Commit

Permalink
Merge pull request #126 from WeBankFinTech/feature/optimize-zkp-usage
Browse files Browse the repository at this point in the history
Feature/optimize zkp usage
  • Loading branch information
junqizhang-dev committed Feb 20, 2020
2 parents 7613a96 + 3142ad1 commit dd3555c
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 43 deletions.
Binary file modified lib/WeDPR-Java-SDK.jar
Binary file not shown.
74 changes: 74 additions & 0 deletions src/main/java/com/webank/weid/constant/CptType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright© (2018-2020) WeBank Co., Ltd.
*
* This file is part of weid-java-sdk.
*
* weid-java-sdk is free software: you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* weid-java-sdk is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* weid-java-sdk. If not, see <https://www.gnu.org/licenses/>.
*/

package com.webank.weid.constant;

/**
* CPT type enum.
*
* @author tonychen 2020年2月17日
*/
public enum CptType {

/**
* original type, used to create original type credential.
*/
ORIGINAL(0, "original"),

/**
* zkp type, used to create zkp type credential.
*/
ZKP(1, "zkp");

/**
* type code.
*/
private Integer code;
/**
* type name.
*/
private String name;

/**
* constructor.
*
* @param code cpt type code
* @param name cpt type name
*/
CptType(Integer code, String name) {
this.code = code;
this.name = name;
}

/**
* get type code.
*
* @return type code
*/
public Integer getCode() {
return this.code;
}

/**
* get type name.
*
* @return type name
*/
public String getName() {
return this.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public final class CredentialConstant {
*/
public static final String DEFAULT_CREDENTIAL_TYPE = "VerifiableCredential";

/**
* cpt type.
*/
public static final String CPT_TYPE_KEY = "cptType";

/**
* The Constant zkp Credential type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import lombok.Data;

import com.webank.weid.constant.CptType;
import com.webank.weid.protocol.base.WeIdAuthentication;

/**
Expand All @@ -37,8 +38,14 @@ public class CptMapArgs {
* Required: weId authority for this CPT.
*/
private WeIdAuthentication weIdAuthentication;

/**
* Required: The json schema content defined for this CPT.
*/
private Map<String, Object> cptJsonSchema;

/**
* cpt type, "ORIGINAL" or "ZKP". default:"ORIGINAL".
*/
private CptType cptType = CptType.ORIGINAL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import lombok.Data;

import com.webank.weid.constant.CptType;
import com.webank.weid.protocol.base.WeIdAuthentication;

/**
Expand All @@ -41,4 +42,8 @@ public class CptStringArgs {
*/
private String cptJsonSchema;

/**
* cpt type, "ORIGINAL" or "ZKP". default:"ORIGINAL".
*/
private CptType cptType = CptType.ORIGINAL;
}
10 changes: 0 additions & 10 deletions src/main/java/com/webank/weid/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ public abstract class BaseService {
if (StringUtils.isEmpty(fiscoConfig.getCurrentOrgId())) {
logger.error("[BaseService] the blockchain orgId is blank.");
}
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {

try {
NativeUtils.loadLibraryFromJar("/WeDPR_dynamic_lib/libeay32md.dll");
NativeUtils.loadLibraryFromJar("/WeDPR_dynamic_lib/ssleay32md.dll");
} catch (IOException e) {
logger.error("[BaseService] Cannot find SSL libraries (in Windows).");
}
}
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/webank/weid/service/impl/AmopServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,19 @@ public class AmopServiceImpl extends BaseService implements AmopService {
/**
* persistence service.
*/
private static Persistence dataDriver = new MysqlDriver();

private static Persistence dataDriver;
/**
* credentialpojo service.
*/
private static CredentialPojoService credentialPojoService = new CredentialPojoServiceImpl();

private static Persistence getDataDriver() {
if (dataDriver == null) {
dataDriver = new MysqlDriver();
}
return dataDriver;
}

@Override
public ResponseData<PolicyAndChallenge> getPolicyAndChallenge(
String targetOrgId,
Expand Down Expand Up @@ -368,7 +374,7 @@ private void blindCredentialSignature(RequestIssueCredentialResponse response, S
CredentialTemplateEntity template = resp1.getResult();
String id = new StringBuffer().append(userId).append("_").append(cptId)
.toString();
ResponseData<String> dbResp = dataDriver
ResponseData<String> dbResp = getDataDriver()
.get(DataDriverConstant.DOMAIN_USER_MASTER_SECRET, id);
if (dbResp.getErrorCode().intValue() != ErrorCode.SUCCESS.getCode()) {
throw new DatabaseException("database error!");
Expand All @@ -392,7 +398,7 @@ private void blindCredentialSignature(RequestIssueCredentialResponse response, S
// .get(CredentialConstant.CREDENTIAL_META_KEY_ID);
String dbKey = credentialPojo.getId();
ResponseData<Integer> dbResponse =
dataDriver.saveOrUpdate(
getDataDriver().saveOrUpdate(
DataDriverConstant.DOMAIN_USER_CREDENTIAL_SIGNATURE,
dbKey,
newCredentialSignature);
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/com/webank/weid/service/impl/CptServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.CredentialConstant;
import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.JsonSchemaConstant;
import com.webank.weid.constant.WeIdConstant;
Expand Down Expand Up @@ -56,12 +57,10 @@
public class CptServiceImpl extends BaseService implements CptService {

private static final Logger logger = LoggerFactory.getLogger(CptServiceImpl.class);

private CptServiceEngine cptServiceEngine = EngineFactory.createCptServiceEngine();

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

/**
* Register a new CPT with a pre-set CPT ID, to the blockchain.
Expand Down Expand Up @@ -141,7 +140,7 @@ public ResponseData<CptBaseInfo> registerCpt(CptMapArgs args, Integer cptId) {

String weId = args.getWeIdAuthentication().getWeId();
WeIdPrivateKey weIdPrivateKey = args.getWeIdAuthentication().getWeIdPrivateKey();
String cptJsonSchemaNew = this.cptSchemaToString(args.getCptJsonSchema());
String cptJsonSchemaNew = this.cptSchemaToString(args);
RsvSignature rsvSignature = sign(
weId,
cptJsonSchemaNew,
Expand Down Expand Up @@ -180,7 +179,7 @@ public ResponseData<CptBaseInfo> registerCpt(CptMapArgs args) {

String weId = args.getWeIdAuthentication().getWeId();
WeIdPrivateKey weIdPrivateKey = args.getWeIdAuthentication().getWeIdPrivateKey();
String cptJsonSchemaNew = this.cptSchemaToString(args.getCptJsonSchema());
String cptJsonSchemaNew = this.cptSchemaToString(args);
RsvSignature rsvSignature = sign(
weId,
cptJsonSchemaNew,
Expand Down Expand Up @@ -275,14 +274,18 @@ public ResponseData<CptBaseInfo> updateCpt(CptMapArgs args, Integer cptId) {

String weId = args.getWeIdAuthentication().getWeId();
WeIdPrivateKey weIdPrivateKey = args.getWeIdAuthentication().getWeIdPrivateKey();
String cptJsonSchemaNew = this.cptSchemaToString(args.getCptJsonSchema());
String cptJsonSchemaNew = this.cptSchemaToString(args);
RsvSignature rsvSignature = sign(
weId,
cptJsonSchemaNew,
weIdPrivateKey);
String address = WeIdUtils.convertWeIdToAddress(weId);
ResponseData<CptBaseInfo> result = 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));
}
Expand Down Expand Up @@ -369,12 +372,15 @@ private ErrorCode validateCptJsonSchemaMap(
* @param cptJsonSchema Map
* @return String
*/
private String cptSchemaToString(Map<String, Object> cptJsonSchema) throws Exception {
private String cptSchemaToString(CptMapArgs args) throws Exception {

Map<String, Object> cptJsonSchema = args.getCptJsonSchema();
Map<String, Object> cptJsonSchemaNew = new HashMap<String, Object>();
cptJsonSchemaNew.put(JsonSchemaConstant.SCHEMA_KEY, JsonSchemaConstant.SCHEMA_VALUE);
cptJsonSchemaNew.put(JsonSchemaConstant.TYPE_KEY, JsonSchemaConstant.DATA_TYPE_OBJECT);
cptJsonSchemaNew.putAll(cptJsonSchema);
String cptType = args.getCptType().getName();
cptJsonSchemaNew.put(CredentialConstant.CPT_TYPE_KEY, cptType);
return DataToolUtils.serialize(cptJsonSchemaNew);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ public class CredentialPojoServiceImpl extends BaseService implements Credential
CredentialFieldDisclosureValue.EXISTED.getStatus().toString();
private static WeIdService weIdService = new WeIdServiceImpl();
private static CptService cptService = new CptServiceImpl();
private static Persistence dataDriver = new MysqlDriver();
private static Persistence dataDriver;

private static Persistence getDataDriver() {
if (dataDriver == null) {
dataDriver = new MysqlDriver();
}
return dataDriver;
}

/**
* Salt generator. Automatically fillin the map structure in a recursive manner.
Expand Down Expand Up @@ -621,7 +628,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 Expand Up @@ -654,6 +661,7 @@ private static ResponseData<Boolean> verifyZkpCredential(CredentialPojo credenti
if (verifierResult.wedprErrorMessage == null) {
return new ResponseData<Boolean>(true, ErrorCode.SUCCESS);
}

return new ResponseData<Boolean>(false, ErrorCode.CREDENTIAL_ERROR);
}

Expand Down Expand Up @@ -697,7 +705,7 @@ private static UserResult makeCredential(
//String id=(String)preCredential.getClaim().get(CredentialConstant.CREDENTIAL_META_KEY_ID);

//save masterSecret and credentialSecretsBlindingFactors to persistence.
ResponseData<Integer> dbResp = dataDriver
ResponseData<Integer> dbResp = getDataDriver()
.saveOrUpdate(DataDriverConstant.DOMAIN_USER_MASTER_SECRET, id, json);
if (dbResp.getErrorCode().intValue() != ErrorCode.SUCCESS.getCode()) {
logger.error(
Expand Down Expand Up @@ -1776,14 +1784,14 @@ private ResponseData<CredentialPojo> createZkpCredential(
.build();
String encodedVerificationRule = Utils.protoToEncodedString(verificationRule);
ResponseData<String> dbResp =
dataDriver.get(
getDataDriver().get(
DataDriverConstant.DOMAIN_USER_CREDENTIAL_SIGNATURE,
credential.getId());
Integer cptId = credentialClone.getCptId();
String id = new StringBuffer().append(userId).append("_").append(cptId).toString();
String newCredentialSignature = dbResp.getResult();
ResponseData<String> masterKeyResp =
dataDriver.get(
getDataDriver().get(
DataDriverConstant.DOMAIN_USER_MASTER_SECRET,
id);
HashMap<String, String> userCredentialInfo = DataToolUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class CptServiceEngineV1 extends BaseEngine implements CptServiceEngine {

private static String CREDENTIALTEMPLATETOPIC;

private static Persistence dataDriver = new MysqlDriver();
private static Persistence dataDriver;

static {
Event event = new Event(
Expand Down Expand Up @@ -224,6 +224,13 @@ public static ResponseData<CptBaseInfo> getResultByResolveEvent(
return new ResponseData<>(result, ErrorCode.SUCCESS, info);
}

private Persistence getDataDriver() {
if (dataDriver == null) {
dataDriver = new MysqlDriver();
}
return dataDriver;
}

/* (non-Javadoc)
* @see com.webank.weid.service.impl.engine.CptEngineController
* #updateCpt(int, java.lang.String, java.lang.String,
Expand Down Expand Up @@ -371,7 +378,7 @@ public ResponseData<CptBaseInfo> registerCpt(String address, String cptJsonSchem

private ErrorCode processTemplate(Integer cptId, String cptJsonSchemaNew) {

if (!CredentialPojoUtils.isZkpCpt(cptId)) {
if (!CredentialPojoUtils.isZkpCpt(cptJsonSchemaNew)) {
return ErrorCode.SUCCESS;
}
List<String> attributeList;
Expand All @@ -382,7 +389,7 @@ private ErrorCode processTemplate(Integer cptId, String cptJsonSchemaNew) {
CredentialTemplateEntity template = issuerResult.credentialTemplateEntity;
String templateSecretKey = issuerResult.templateSecretKey;
ResponseData<Integer> resp =
dataDriver.saveOrUpdate(
this.getDataDriver().saveOrUpdate(
DataDriverConstant.DOMAIN_ISSUER_TEMPLATE_SECRET,
String.valueOf(cptId),
templateSecretKey);
Expand Down

0 comments on commit dd3555c

Please sign in to comment.