Skip to content

Commit 554b6e1

Browse files
authored
Merge pull request #79 from WeBankFinTech/release/1.0.7
Release/1.0.7
2 parents e13a60b + f28c933 commit 554b6e1

13 files changed

+232
-158
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ weid-sample 是基于 [WeIdentity](https://weidentity.readthedocs.io/zh_CN/lates
88

99
| WeIdentity-Sample 版本 | WeIdentity 版本 | Contract 版本 |
1010
| :---- | :---- | :---- |
11+
| v1.0.7 | v1.7.0 | v1.2.27 |
1112
| v1.0.6 | v1.6.7 | v1.2.26 |
1213
| v1.0.5 | v1.6.6 | v1.2.24 |
1314
| v1.0.4 | v1.6.5 | v1.2.23 |

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if (!gradle.startParameter.isOffline()) {
2828
apply plugin: 'signing'
2929
}
3030

31-
version = "1.0.6"
31+
version = "1.0.7"
3232
def repo ="${repoType}"
3333
//设置jdk的版本
3434
sourceCompatibility = 1.8
@@ -120,7 +120,7 @@ dependencies {
120120
compile files('./dependencies/weid-java-sdk-pipeline.jar')
121121
} else {
122122
println "No pipeline jar found for WeIdentity Java SDK, using gradle version.."
123-
compile("com.webank:weid-java-sdk:1.6.7") {
123+
compile("com.webank:weid-java-sdk:1.7.0") {
124124
exclude group:"org.slf4j", module: "slf4j-log4j12"
125125
}
126126
}

src/main/java/com/webank/weid/demo/command/DemoCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ private static void issuer() {
133133

134134
// registered authority, "webank" is the authority Name, "0" is default.
135135
demoService.registerAuthorityIssuer(createWeId, "webank" + System.currentTimeMillis(), "0");
136+
137+
138+
BaseBean.print("------------------------------");
139+
BaseBean.print("begin to recognizeAuthorityIssuer...");
140+
demoService.recognizeAuthorityIssuer(createWeId);
136141

137142
BaseBean.print("------------------------------");
138143
BaseBean.print("begin to regist the first Cpt...");

src/main/java/com/webank/weid/demo/command/DemoService.java

Lines changed: 51 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919

2020
package com.webank.weid.demo.command;
2121

22-
import java.util.HashMap;
2322
import java.util.List;
2423
import java.util.Map;
2524

26-
import com.webank.weid.demo.common.util.FileUtil;
2725
import com.webank.weid.protocol.request.*;
2826
import org.slf4j.Logger;
2927
import org.slf4j.LoggerFactory;
@@ -33,11 +31,10 @@
3331
import com.webank.weid.protocol.base.AuthorityIssuer;
3432
import com.webank.weid.protocol.base.Challenge;
3533
import com.webank.weid.protocol.base.CptBaseInfo;
36-
import com.webank.weid.protocol.base.Credential;
3734
import com.webank.weid.protocol.base.CredentialPojo;
38-
import com.webank.weid.protocol.base.CredentialWrapper;
3935
import com.webank.weid.protocol.base.PresentationE;
4036
import com.webank.weid.protocol.base.PresentationPolicyE;
37+
import com.webank.weid.protocol.base.PublicKeyProperty;
4138
import com.webank.weid.protocol.base.WeIdAuthentication;
4239
import com.webank.weid.protocol.base.WeIdDocument;
4340
import com.webank.weid.protocol.base.WeIdPrivateKey;
@@ -46,17 +43,14 @@
4643
import com.webank.weid.rpc.AuthorityIssuerService;
4744
import com.webank.weid.rpc.CptService;
4845
import com.webank.weid.rpc.CredentialPojoService;
49-
import com.webank.weid.rpc.CredentialService;
5046
import com.webank.weid.rpc.WeIdService;
5147
import com.webank.weid.service.impl.AuthorityIssuerServiceImpl;
5248
import com.webank.weid.service.impl.CptServiceImpl;
5349
import com.webank.weid.service.impl.CredentialPojoServiceImpl;
54-
import com.webank.weid.service.impl.CredentialServiceImpl;
5550
import com.webank.weid.service.impl.WeIdServiceImpl;
5651
import com.webank.weid.suite.api.transportation.TransportationFactory;
5752
import com.webank.weid.suite.api.transportation.params.EncodeType;
5853
import com.webank.weid.suite.api.transportation.params.ProtocolProperty;
59-
import com.webank.weid.util.DataToolUtils;
6054

6155
/**
6256
* the service for command.
@@ -70,8 +64,6 @@ public class DemoService {
7064

7165
private CptService cptService = new CptServiceImpl();
7266

73-
private CredentialService credentialService = new CredentialServiceImpl();
74-
7567
private WeIdService weIdService = new WeIdServiceImpl();
7668

7769
private CredentialPojoService credentialPojoService = new CredentialPojoServiceImpl();
@@ -403,83 +395,34 @@ public void registerAuthorityIssuer(
403395
throw new BusinessException(response.getErrorMessage());
404396
}
405397
}
406-
407-
/**
408-
* create a credential for user.
409-
*
410-
* @param weIdResult the data of organization's weId information
411-
* @param cptId CPT number issued by organization
412-
* @param claim the claim data for create credential
413-
* @param expirationDate the validity period of the credential
414-
* @return return the credential
415-
* @throws BusinessException throw a exception when create fail
416-
*/
417-
public Credential createCredential(
418-
CreateWeIdDataResult weIdResult,
419-
Integer cptId,
420-
String claim,
421-
long expirationDate)
422-
throws BusinessException {
423-
424-
logger.info(
425-
"create credential using string-type claim and convert claim from string to map"
426-
);
427-
// converting claim of strings to map.
428-
Map<String, Object> claimDataMap = new HashMap<String, Object>();
429-
claimDataMap =
430-
(Map<String, Object>) DataToolUtils.deserialize(
431-
claim,
432-
claimDataMap.getClass()
433-
);
434-
435-
return this.createCredential(weIdResult, cptId, claimDataMap, expirationDate);
436-
}
437-
398+
438399
/**
439-
* create a credential for user.
400+
* Register a new Authority Issuer on Chain.
440401
*
441-
* @param weIdResult the data of organization's weId information
442-
* @param cptId CPT number issued by organization
443-
* @param claimDataMap the claim data for create credential
444-
* @param expirationDate the validity period of the credential
445-
* @return return the credential
446-
* @throws BusinessException throw a exception when create fail
402+
* @param weIdResult the object of CreateWeIdDataResult
403+
* @param name this is Authority name
404+
* @param accValue this is accValue
405+
* @throws BusinessException throw a exception when register fail
447406
*/
448-
public Credential createCredential(
449-
CreateWeIdDataResult weIdResult,
450-
Integer cptId,
451-
Map<String, Object> claimDataMap,
452-
long expirationDate)
453-
throws BusinessException {
454-
455-
logger.info("create credential using map-type claim");
456-
457-
// build CreateCredentialArgs for createCredential
458-
CreateCredentialArgs args = new CreateCredentialArgs();
459-
args.setClaim(claimDataMap);
460-
args.setCptId(cptId);
461-
args.setExpirationDate(expirationDate);
462-
args.setIssuer(weIdResult.getWeId());
463-
args.setWeIdPrivateKey(
464-
this.buildWeIdPrivateKey(weIdResult.getUserWeIdPrivateKey().getPrivateKey())
465-
);
466-
467-
ResponseData<CredentialWrapper> response = credentialService.createCredential(args);
468-
BaseBean.print("createCredential result:");
407+
public void recognizeAuthorityIssuer(CreateWeIdDataResult weIdResult) throws BusinessException {
408+
WeIdPrivateKey weIdPrivateKey = this.buildWeIdPrivateKey(DemoUtil.SDK_PRIVATE_KEY);
409+
// call the registerAuthorityIssuer on chain.
410+
ResponseData<Boolean> response =
411+
authorityIssuerService.recognizeAuthorityIssuer(weIdResult.getWeId(), weIdPrivateKey);
412+
BaseBean.print("recognizeAuthorityIssuer result:");
469413
BaseBean.print(response);
470414

471415
// throw an exception if it does not succeed.
472-
if (response.getErrorCode() != ErrorCode.SUCCESS.getCode()
473-
|| null == response.getResult()) {
474-
logger.error("failed to call createCredential method, code={}, message={}",
416+
if (response.getErrorCode() != ErrorCode.SUCCESS.getCode()
417+
|| !response.getResult()) {
418+
logger.error("failed to call recognizeAuthorityIssuer method, code={}, message={}",
475419
response.getErrorCode(),
476420
response.getErrorMessage()
477421
);
478422
throw new BusinessException(response.getErrorMessage());
479423
}
480-
return response.getResult().getCredential();
481424
}
482-
425+
483426
/**
484427
* create a credential for user.
485428
* @param arg the CreateCredentialPojoArgs
@@ -497,30 +440,6 @@ public <T> CredentialPojo createCredential(CreateCredentialPojoArgs<T> arg) {
497440
return response.getResult();
498441
}
499442

500-
/**
501-
* verify the credential on chain.
502-
*
503-
* @param credential user-provided credentials
504-
* @return return the result of verify, true is success, false is fail
505-
* @throws BusinessException throw a exception when the result code is not success
506-
*/
507-
public boolean verifyCredential(Credential credential) throws BusinessException {
508-
509-
ResponseData<Boolean> response = credentialService.verify(credential);
510-
BaseBean.print("verifyCredential result:");
511-
BaseBean.print(response);
512-
513-
// throw an exception if it does not succeed.
514-
if (response.getErrorCode() != ErrorCode.SUCCESS.getCode()) {
515-
logger.error("failed to call verify method, code={}, message={}",
516-
response.getErrorCode(),
517-
response.getErrorMessage()
518-
);
519-
throw new BusinessException(response.getErrorMessage());
520-
}
521-
return response.getResult();
522-
}
523-
524443
/**
525444
* create presentation with policy.
526445
* @param credentialList the credentialList of user
@@ -685,5 +604,38 @@ public boolean verifyPresentationE(
685604
throw new BusinessException(response.getErrorMessage());
686605
}
687606
return response.getResult();
688-
}
607+
}
608+
609+
/**
610+
* verify CredentialPojo.
611+
* @param credentialPojo the CredentialPojo
612+
* @return success if true, others fail
613+
*/
614+
public boolean verifyCredentialPojo(CredentialPojo credentialPojo) {
615+
ResponseData<Boolean> verify =
616+
credentialPojoService.verify(credentialPojo.getIssuer(), credentialPojo);
617+
if (verify.getErrorCode() != ErrorCode.SUCCESS.getCode()) {
618+
logger.error("credentialPojoService.verify failed,responseData:{}",
619+
verify);
620+
throw new BusinessException(verify.getErrorMessage());
621+
}
622+
return verify.getResult();
623+
}
624+
625+
/**
626+
* get the public key ID.
627+
* @param weId the weId
628+
* @return the publicKeyId
629+
*/
630+
public String getPublicKeyId(String weId) {
631+
ResponseData<WeIdDocument> weIdDocumentRes = weIdService.getWeIdDocument(weId);
632+
String publicKeyId = null;
633+
for (PublicKeyProperty publicKey : weIdDocumentRes.getResult().getPublicKey()) {
634+
if (publicKey.getOwner().equals(weId)) {
635+
publicKeyId = publicKey.getId();
636+
break;
637+
}
638+
}
639+
return publicKeyId;
640+
}
689641
}

src/main/java/com/webank/weid/demo/command/DemoTest.java

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121

2222
import java.text.ParseException;
2323
import java.util.HashMap;
24+
import java.util.Map;
2425

26+
import com.webank.weid.constant.CredentialType;
2527
import com.webank.weid.constant.JsonSchemaConstant;
2628
import com.webank.weid.protocol.base.CptBaseInfo;
27-
import com.webank.weid.protocol.base.Credential;
29+
import com.webank.weid.protocol.base.CredentialPojo;
30+
import com.webank.weid.protocol.base.WeIdAuthentication;
2831
import com.webank.weid.protocol.base.WeIdDocument;
32+
import com.webank.weid.protocol.request.CreateCredentialPojoArgs;
2933
import com.webank.weid.protocol.response.CreateWeIdDataResult;
3034

3135
/**
@@ -35,11 +39,6 @@
3539
*/
3640
public class DemoTest extends DemoBase {
3741

38-
/**
39-
* set validity period to 360 days by default.
40-
*/
41-
private static final long EXPIRATION_DATE = 1000L * 60 * 60 * 24 * 360;
42-
4342
/**
4443
* main of demo.
4544
* @throws ParseException the parseException
@@ -65,8 +64,11 @@ public static void main(String[] args) throws RuntimeException, ParseException {
6564
BaseBean.print(weIdDom);
6665

6766
// registered authority issuer.
68-
demoService.registerAuthorityIssuer(createWeId, "webank", "0");
69-
67+
demoService.registerAuthorityIssuer(
68+
createWeId, String.valueOf(System.currentTimeMillis()), "0");
69+
// recognize AuthorityIssuer
70+
demoService.recognizeAuthorityIssuer(createWeId);
71+
7072
// registered CPT.
7173
CptBaseInfo cptResult =
7274
demoService.registCpt(
@@ -75,20 +77,17 @@ public static void main(String[] args) throws RuntimeException, ParseException {
7577
);
7678
BaseBean.print(cptResult);
7779

78-
long expirationDate = System.currentTimeMillis() + EXPIRATION_DATE;
79-
8080
// create Credential.
81-
Credential credential =
82-
demoService.createCredential(
83-
createWeId,
84-
cptResult.getCptId(),
85-
DemoTest.buildCptJsonSchemaData(),
86-
expirationDate
87-
);
81+
String publicKeyId = demoService.getPublicKeyId(createWeId.getWeId());
82+
WeIdAuthentication weIdAuthentication = buildWeIdAuthority(createWeId, publicKeyId);
83+
CreateCredentialPojoArgs<Map<String, Object>> createCredentialPojoArgs =
84+
buildCreateCredentialPojoArgs(cptResult.getCptId(), weIdAuthentication);
85+
CredentialPojo credential =
86+
demoService.createCredential(createCredentialPojoArgs);
8887
BaseBean.print(credential);
8988

9089
// verify the credential.
91-
boolean result = demoService.verifyCredential(credential);
90+
boolean result = demoService.verifyCredentialPojo(credential);
9291
if (result) {
9392
BaseBean.print("verify success");
9493
} else {
@@ -150,4 +149,46 @@ public static HashMap<String, Object> buildCptJsonSchemaData() {
150149
cptJsonSchemaData.put("weid", "did:weid:0x566a07b553804266133f130c8c0bf6fede406984");
151150
return cptJsonSchemaData;
152151
}
152+
153+
/**
154+
* 构建创建凭证参数.
155+
* @param weIdAuthentication weId身份信息
156+
* @return 返回创建凭证参数
157+
*/
158+
public static CreateCredentialPojoArgs<Map<String, Object>> buildCreateCredentialPojoArgs(
159+
Integer cptId,
160+
WeIdAuthentication weIdAuthentication
161+
) {
162+
163+
CreateCredentialPojoArgs<Map<String, Object>> createCredentialPojoArgs =
164+
new CreateCredentialPojoArgs<Map<String, Object>>();
165+
166+
createCredentialPojoArgs.setIssuer(weIdAuthentication.getWeId());
167+
createCredentialPojoArgs.setExpirationDate(
168+
System.currentTimeMillis() + (1000 * 60 * 60 * 24));
169+
createCredentialPojoArgs.setWeIdAuthentication(weIdAuthentication);
170+
Map<String, Object> claimMap = new HashMap<String, Object>();
171+
claimMap.put("name", "zhang san");
172+
claimMap.put("gender", "F");
173+
claimMap.put("age", 23);
174+
claimMap.put("id", weIdAuthentication.getWeId());
175+
createCredentialPojoArgs.setClaim(claimMap);
176+
createCredentialPojoArgs.setType(CredentialType.ORIGINAL);
177+
createCredentialPojoArgs.setCptId(cptId);
178+
return createCredentialPojoArgs;
179+
}
180+
181+
/**
182+
* build weId authority.
183+
*/
184+
public static WeIdAuthentication buildWeIdAuthority(
185+
CreateWeIdDataResult createWeId,
186+
String publicKeyId
187+
) {
188+
return new WeIdAuthentication(
189+
createWeId.getWeId(),
190+
createWeId.getUserWeIdPrivateKey().getPrivateKey(),
191+
publicKeyId
192+
);
193+
}
153194
}

0 commit comments

Comments
 (0)