Skip to content

Commit

Permalink
Merge pull request #79 from WeBankFinTech/release/1.0.7
Browse files Browse the repository at this point in the history
Release/1.0.7
  • Loading branch information
yanggang-JV authored Nov 23, 2020
2 parents e13a60b + f28c933 commit 554b6e1
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 158 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ weid-sample 是基于 [WeIdentity](https://weidentity.readthedocs.io/zh_CN/lates

| WeIdentity-Sample 版本 | WeIdentity 版本 | Contract 版本 |
| :---- | :---- | :---- |
| v1.0.7 | v1.7.0 | v1.2.27 |
| v1.0.6 | v1.6.7 | v1.2.26 |
| v1.0.5 | v1.6.6 | v1.2.24 |
| v1.0.4 | v1.6.5 | v1.2.23 |
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (!gradle.startParameter.isOffline()) {
apply plugin: 'signing'
}

version = "1.0.6"
version = "1.0.7"
def repo ="${repoType}"
//设置jdk的版本
sourceCompatibility = 1.8
Expand Down Expand Up @@ -120,7 +120,7 @@ dependencies {
compile files('./dependencies/weid-java-sdk-pipeline.jar')
} else {
println "No pipeline jar found for WeIdentity Java SDK, using gradle version.."
compile("com.webank:weid-java-sdk:1.6.7") {
compile("com.webank:weid-java-sdk:1.7.0") {
exclude group:"org.slf4j", module: "slf4j-log4j12"
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/webank/weid/demo/command/DemoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ private static void issuer() {

// registered authority, "webank" is the authority Name, "0" is default.
demoService.registerAuthorityIssuer(createWeId, "webank" + System.currentTimeMillis(), "0");


BaseBean.print("------------------------------");
BaseBean.print("begin to recognizeAuthorityIssuer...");
demoService.recognizeAuthorityIssuer(createWeId);

BaseBean.print("------------------------------");
BaseBean.print("begin to regist the first Cpt...");
Expand Down
150 changes: 51 additions & 99 deletions src/main/java/com/webank/weid/demo/command/DemoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

package com.webank.weid.demo.command;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.webank.weid.demo.common.util.FileUtil;
import com.webank.weid.protocol.request.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,11 +31,10 @@
import com.webank.weid.protocol.base.AuthorityIssuer;
import com.webank.weid.protocol.base.Challenge;
import com.webank.weid.protocol.base.CptBaseInfo;
import com.webank.weid.protocol.base.Credential;
import com.webank.weid.protocol.base.CredentialPojo;
import com.webank.weid.protocol.base.CredentialWrapper;
import com.webank.weid.protocol.base.PresentationE;
import com.webank.weid.protocol.base.PresentationPolicyE;
import com.webank.weid.protocol.base.PublicKeyProperty;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.base.WeIdPrivateKey;
Expand All @@ -46,17 +43,14 @@
import com.webank.weid.rpc.AuthorityIssuerService;
import com.webank.weid.rpc.CptService;
import com.webank.weid.rpc.CredentialPojoService;
import com.webank.weid.rpc.CredentialService;
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.service.impl.AuthorityIssuerServiceImpl;
import com.webank.weid.service.impl.CptServiceImpl;
import com.webank.weid.service.impl.CredentialPojoServiceImpl;
import com.webank.weid.service.impl.CredentialServiceImpl;
import com.webank.weid.service.impl.WeIdServiceImpl;
import com.webank.weid.suite.api.transportation.TransportationFactory;
import com.webank.weid.suite.api.transportation.params.EncodeType;
import com.webank.weid.suite.api.transportation.params.ProtocolProperty;
import com.webank.weid.util.DataToolUtils;

/**
* the service for command.
Expand All @@ -70,8 +64,6 @@ public class DemoService {

private CptService cptService = new CptServiceImpl();

private CredentialService credentialService = new CredentialServiceImpl();

private WeIdService weIdService = new WeIdServiceImpl();

private CredentialPojoService credentialPojoService = new CredentialPojoServiceImpl();
Expand Down Expand Up @@ -403,83 +395,34 @@ public void registerAuthorityIssuer(
throw new BusinessException(response.getErrorMessage());
}
}

/**
* create a credential for user.
*
* @param weIdResult the data of organization's weId information
* @param cptId CPT number issued by organization
* @param claim the claim data for create credential
* @param expirationDate the validity period of the credential
* @return return the credential
* @throws BusinessException throw a exception when create fail
*/
public Credential createCredential(
CreateWeIdDataResult weIdResult,
Integer cptId,
String claim,
long expirationDate)
throws BusinessException {

logger.info(
"create credential using string-type claim and convert claim from string to map"
);
// converting claim of strings to map.
Map<String, Object> claimDataMap = new HashMap<String, Object>();
claimDataMap =
(Map<String, Object>) DataToolUtils.deserialize(
claim,
claimDataMap.getClass()
);

return this.createCredential(weIdResult, cptId, claimDataMap, expirationDate);
}


/**
* create a credential for user.
* Register a new Authority Issuer on Chain.
*
* @param weIdResult the data of organization's weId information
* @param cptId CPT number issued by organization
* @param claimDataMap the claim data for create credential
* @param expirationDate the validity period of the credential
* @return return the credential
* @throws BusinessException throw a exception when create fail
* @param weIdResult the object of CreateWeIdDataResult
* @param name this is Authority name
* @param accValue this is accValue
* @throws BusinessException throw a exception when register fail
*/
public Credential createCredential(
CreateWeIdDataResult weIdResult,
Integer cptId,
Map<String, Object> claimDataMap,
long expirationDate)
throws BusinessException {

logger.info("create credential using map-type claim");

// build CreateCredentialArgs for createCredential
CreateCredentialArgs args = new CreateCredentialArgs();
args.setClaim(claimDataMap);
args.setCptId(cptId);
args.setExpirationDate(expirationDate);
args.setIssuer(weIdResult.getWeId());
args.setWeIdPrivateKey(
this.buildWeIdPrivateKey(weIdResult.getUserWeIdPrivateKey().getPrivateKey())
);

ResponseData<CredentialWrapper> response = credentialService.createCredential(args);
BaseBean.print("createCredential result:");
public void recognizeAuthorityIssuer(CreateWeIdDataResult weIdResult) throws BusinessException {
WeIdPrivateKey weIdPrivateKey = this.buildWeIdPrivateKey(DemoUtil.SDK_PRIVATE_KEY);
// call the registerAuthorityIssuer on chain.
ResponseData<Boolean> response =
authorityIssuerService.recognizeAuthorityIssuer(weIdResult.getWeId(), weIdPrivateKey);
BaseBean.print("recognizeAuthorityIssuer result:");
BaseBean.print(response);

// throw an exception if it does not succeed.
if (response.getErrorCode() != ErrorCode.SUCCESS.getCode()
|| null == response.getResult()) {
logger.error("failed to call createCredential method, code={}, message={}",
if (response.getErrorCode() != ErrorCode.SUCCESS.getCode()
|| !response.getResult()) {
logger.error("failed to call recognizeAuthorityIssuer method, code={}, message={}",
response.getErrorCode(),
response.getErrorMessage()
);
throw new BusinessException(response.getErrorMessage());
}
return response.getResult().getCredential();
}

/**
* create a credential for user.
* @param arg the CreateCredentialPojoArgs
Expand All @@ -497,30 +440,6 @@ public <T> CredentialPojo createCredential(CreateCredentialPojoArgs<T> arg) {
return response.getResult();
}

/**
* verify the credential on chain.
*
* @param credential user-provided credentials
* @return return the result of verify, true is success, false is fail
* @throws BusinessException throw a exception when the result code is not success
*/
public boolean verifyCredential(Credential credential) throws BusinessException {

ResponseData<Boolean> response = credentialService.verify(credential);
BaseBean.print("verifyCredential result:");
BaseBean.print(response);

// throw an exception if it does not succeed.
if (response.getErrorCode() != ErrorCode.SUCCESS.getCode()) {
logger.error("failed to call verify method, code={}, message={}",
response.getErrorCode(),
response.getErrorMessage()
);
throw new BusinessException(response.getErrorMessage());
}
return response.getResult();
}

/**
* create presentation with policy.
* @param credentialList the credentialList of user
Expand Down Expand Up @@ -685,5 +604,38 @@ public boolean verifyPresentationE(
throw new BusinessException(response.getErrorMessage());
}
return response.getResult();
}
}

/**
* verify CredentialPojo.
* @param credentialPojo the CredentialPojo
* @return success if true, others fail
*/
public boolean verifyCredentialPojo(CredentialPojo credentialPojo) {
ResponseData<Boolean> verify =
credentialPojoService.verify(credentialPojo.getIssuer(), credentialPojo);
if (verify.getErrorCode() != ErrorCode.SUCCESS.getCode()) {
logger.error("credentialPojoService.verify failed,responseData:{}",
verify);
throw new BusinessException(verify.getErrorMessage());
}
return verify.getResult();
}

/**
* get the public key ID.
* @param weId the weId
* @return the publicKeyId
*/
public String getPublicKeyId(String weId) {
ResponseData<WeIdDocument> weIdDocumentRes = weIdService.getWeIdDocument(weId);
String publicKeyId = null;
for (PublicKeyProperty publicKey : weIdDocumentRes.getResult().getPublicKey()) {
if (publicKey.getOwner().equals(weId)) {
publicKeyId = publicKey.getId();
break;
}
}
return publicKeyId;
}
}
77 changes: 59 additions & 18 deletions src/main/java/com/webank/weid/demo/command/DemoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import com.webank.weid.constant.CredentialType;
import com.webank.weid.constant.JsonSchemaConstant;
import com.webank.weid.protocol.base.CptBaseInfo;
import com.webank.weid.protocol.base.Credential;
import com.webank.weid.protocol.base.CredentialPojo;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.request.CreateCredentialPojoArgs;
import com.webank.weid.protocol.response.CreateWeIdDataResult;

/**
Expand All @@ -35,11 +39,6 @@
*/
public class DemoTest extends DemoBase {

/**
* set validity period to 360 days by default.
*/
private static final long EXPIRATION_DATE = 1000L * 60 * 60 * 24 * 360;

/**
* main of demo.
* @throws ParseException the parseException
Expand All @@ -65,8 +64,11 @@ public static void main(String[] args) throws RuntimeException, ParseException {
BaseBean.print(weIdDom);

// registered authority issuer.
demoService.registerAuthorityIssuer(createWeId, "webank", "0");

demoService.registerAuthorityIssuer(
createWeId, String.valueOf(System.currentTimeMillis()), "0");
// recognize AuthorityIssuer
demoService.recognizeAuthorityIssuer(createWeId);

// registered CPT.
CptBaseInfo cptResult =
demoService.registCpt(
Expand All @@ -75,20 +77,17 @@ public static void main(String[] args) throws RuntimeException, ParseException {
);
BaseBean.print(cptResult);

long expirationDate = System.currentTimeMillis() + EXPIRATION_DATE;

// create Credential.
Credential credential =
demoService.createCredential(
createWeId,
cptResult.getCptId(),
DemoTest.buildCptJsonSchemaData(),
expirationDate
);
String publicKeyId = demoService.getPublicKeyId(createWeId.getWeId());
WeIdAuthentication weIdAuthentication = buildWeIdAuthority(createWeId, publicKeyId);
CreateCredentialPojoArgs<Map<String, Object>> createCredentialPojoArgs =
buildCreateCredentialPojoArgs(cptResult.getCptId(), weIdAuthentication);
CredentialPojo credential =
demoService.createCredential(createCredentialPojoArgs);
BaseBean.print(credential);

// verify the credential.
boolean result = demoService.verifyCredential(credential);
boolean result = demoService.verifyCredentialPojo(credential);
if (result) {
BaseBean.print("verify success");
} else {
Expand Down Expand Up @@ -150,4 +149,46 @@ public static HashMap<String, Object> buildCptJsonSchemaData() {
cptJsonSchemaData.put("weid", "did:weid:0x566a07b553804266133f130c8c0bf6fede406984");
return cptJsonSchemaData;
}

/**
* 构建创建凭证参数.
* @param weIdAuthentication weId身份信息
* @return 返回创建凭证参数
*/
public static CreateCredentialPojoArgs<Map<String, Object>> buildCreateCredentialPojoArgs(
Integer cptId,
WeIdAuthentication weIdAuthentication
) {

CreateCredentialPojoArgs<Map<String, Object>> createCredentialPojoArgs =
new CreateCredentialPojoArgs<Map<String, Object>>();

createCredentialPojoArgs.setIssuer(weIdAuthentication.getWeId());
createCredentialPojoArgs.setExpirationDate(
System.currentTimeMillis() + (1000 * 60 * 60 * 24));
createCredentialPojoArgs.setWeIdAuthentication(weIdAuthentication);
Map<String, Object> claimMap = new HashMap<String, Object>();
claimMap.put("name", "zhang san");
claimMap.put("gender", "F");
claimMap.put("age", 23);
claimMap.put("id", weIdAuthentication.getWeId());
createCredentialPojoArgs.setClaim(claimMap);
createCredentialPojoArgs.setType(CredentialType.ORIGINAL);
createCredentialPojoArgs.setCptId(cptId);
return createCredentialPojoArgs;
}

/**
* build weId authority.
*/
public static WeIdAuthentication buildWeIdAuthority(
CreateWeIdDataResult createWeId,
String publicKeyId
) {
return new WeIdAuthentication(
createWeId.getWeId(),
createWeId.getUserWeIdPrivateKey().getPrivateKey(),
publicKeyId
);
}
}
Loading

0 comments on commit 554b6e1

Please sign in to comment.