Skip to content

Commit

Permalink
Merge pull request #199 from yg3630536/feature/selective-for-evidence
Browse files Browse the repository at this point in the history
* support selective asynchronous processing
  • Loading branch information
junqizhang-dev committed May 20, 2020
2 parents 69e0be2 + d688d51 commit a2a48a6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,12 @@ public enum ErrorCode {
"presentation from pdf transportation, please use verifyPresentationFromPDF function"),

/**
* the credential does not support selective disclosure.
* the error code shows that the credential passed in the
* function is not supported by this function.
*/
CREDENTIAL_NOT_SUPPORT_SELECTIVE_DISCLOSURE(100440,
"the credential does not support selective disclosure."),
"the error code shows that the credential passed in the "
+ "function is not supported by this function."),

/**
* credential verify fail.
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/webank/weid/constant/ProcessingMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

/**
* 上链处理模式.
* @author v_wbgyang
*
*/
public enum ProcessingMode {

/**
* 立即上链模式,此模式下会立即将数据发送至区块链节点.
*/
IMMEDIATE,

/**
* 批量延迟上链模式,此模式下会先将数据存入介质中,然后异步去上链处理.
*/
PERIODIC_AND_BATCH
}
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,8 @@ public ResponseData<CredentialPojo> createSelectiveCredential(
&& (credential.getType().contains(CredentialType.LITE1.getName())
|| credential.getType().contains(CredentialType.ZKP.getName()))) {
logger.error(
"[createSelectiveCredential] the credential does not support selective "
+ "disclosure, type = {}.", credential.getType());
"[createSelectiveCredential] Lite Credential and ZKP Credential DO NOT support "
+ "this function(createSelectiveCredential), type = {}.", credential.getType());
return new ResponseData<CredentialPojo>(null,
ErrorCode.CREDENTIAL_NOT_SUPPORT_SELECTIVE_DISCLOSURE);
}
Expand Down
40 changes: 21 additions & 19 deletions src/main/java/com/webank/weid/service/impl/EvidenceServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.slf4j.LoggerFactory;

import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.ParamKeyConstant;
import com.webank.weid.constant.ProcessingMode;
import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.protocol.base.EvidenceInfo;
import com.webank.weid.protocol.base.EvidenceSignInfo;
Expand All @@ -43,7 +43,6 @@
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.rpc.EvidenceService;
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.service.impl.inner.PropertiesService;
import com.webank.weid.util.BatchTransactionUtils;
import com.webank.weid.util.DataToolUtils;
import com.webank.weid.util.DateUtils;
Expand All @@ -59,6 +58,22 @@ public class EvidenceServiceImpl extends AbstractService implements EvidenceServ
private static final Logger logger = LoggerFactory.getLogger(EvidenceServiceImpl.class);

private WeIdService weIdService = new WeIdServiceImpl();

private ProcessingMode processingMode = ProcessingMode.IMMEDIATE;

public EvidenceServiceImpl() {
super();
}

/**
* 传入processingMode来决定上链模式.
*
* @param processingMode 上链模式
*/
public EvidenceServiceImpl(ProcessingMode processingMode) {
super();
this.processingMode = processingMode;
}

@Override
public ResponseData<Boolean> createRawEvidenceWithCustomKey(
Expand Down Expand Up @@ -234,10 +249,7 @@ private ResponseData<String> hashToNewEvidence(String hashValue, String privateK
DataToolUtils.base64Encode(DataToolUtils.simpleSignatureSerialization(sigData)),
StandardCharsets.UTF_8);
Long timestamp = DateUtils.getCurrentTimeStamp();

boolean flag = getOfflineFlag();
if (flag) {

if (processingMode == ProcessingMode.PERIODIC_AND_BATCH) {
String[] args = new String[5];
args[0] = hashValue;
args[1] = signature;
Expand Down Expand Up @@ -435,10 +447,8 @@ public ResponseData<String> createEvidenceWithLogAndCustomKey(
DataToolUtils.base64Encode(DataToolUtils.simpleSignatureSerialization(sigData)),
StandardCharsets.UTF_8);
Long timestamp = DateUtils.getCurrentTimeStamp();

boolean flag = getOfflineFlag();
if (flag) {


if (processingMode == ProcessingMode.PERIODIC_AND_BATCH) {
String[] args = new String[6];
args[0] = hashValue;
args[1] = signature;
Expand All @@ -464,6 +474,7 @@ public ResponseData<String> createEvidenceWithLogAndCustomKey(
return new ResponseData<>(hashValue, ErrorCode.OFFLINE_EVIDENCE_SAVE_FAILED);
}
}

return evidenceServiceEngine.createEvidenceWithCustomKey(
hashValue,
signature,
Expand Down Expand Up @@ -497,13 +508,4 @@ public ResponseData<EvidenceInfo> getEvidenceByCustomKey(String customKey) {
private boolean isChainStringLengthValid(String string) {
return string.length() < WeIdConstant.ON_CHAIN_STRING_LENGTH;
}

private boolean getOfflineFlag() {
String flag = PropertiesService.getInstance()
.getProperty(ParamKeyConstant.ENABLE_OFFLINE);
if (StringUtils.isNotBlank(flag)) {
return new Boolean(flag);
}
return false;
}
}

0 comments on commit a2a48a6

Please sign in to comment.