19
19
20
20
package com .webank .weid .demo .command ;
21
21
22
- import java .util .HashMap ;
23
22
import java .util .List ;
24
23
import java .util .Map ;
25
24
26
- import com .webank .weid .demo .common .util .FileUtil ;
27
25
import com .webank .weid .protocol .request .*;
28
26
import org .slf4j .Logger ;
29
27
import org .slf4j .LoggerFactory ;
33
31
import com .webank .weid .protocol .base .AuthorityIssuer ;
34
32
import com .webank .weid .protocol .base .Challenge ;
35
33
import com .webank .weid .protocol .base .CptBaseInfo ;
36
- import com .webank .weid .protocol .base .Credential ;
37
34
import com .webank .weid .protocol .base .CredentialPojo ;
38
- import com .webank .weid .protocol .base .CredentialWrapper ;
39
35
import com .webank .weid .protocol .base .PresentationE ;
40
36
import com .webank .weid .protocol .base .PresentationPolicyE ;
37
+ import com .webank .weid .protocol .base .PublicKeyProperty ;
41
38
import com .webank .weid .protocol .base .WeIdAuthentication ;
42
39
import com .webank .weid .protocol .base .WeIdDocument ;
43
40
import com .webank .weid .protocol .base .WeIdPrivateKey ;
46
43
import com .webank .weid .rpc .AuthorityIssuerService ;
47
44
import com .webank .weid .rpc .CptService ;
48
45
import com .webank .weid .rpc .CredentialPojoService ;
49
- import com .webank .weid .rpc .CredentialService ;
50
46
import com .webank .weid .rpc .WeIdService ;
51
47
import com .webank .weid .service .impl .AuthorityIssuerServiceImpl ;
52
48
import com .webank .weid .service .impl .CptServiceImpl ;
53
49
import com .webank .weid .service .impl .CredentialPojoServiceImpl ;
54
- import com .webank .weid .service .impl .CredentialServiceImpl ;
55
50
import com .webank .weid .service .impl .WeIdServiceImpl ;
56
51
import com .webank .weid .suite .api .transportation .TransportationFactory ;
57
52
import com .webank .weid .suite .api .transportation .params .EncodeType ;
58
53
import com .webank .weid .suite .api .transportation .params .ProtocolProperty ;
59
- import com .webank .weid .util .DataToolUtils ;
60
54
61
55
/**
62
56
* the service for command.
@@ -70,8 +64,6 @@ public class DemoService {
70
64
71
65
private CptService cptService = new CptServiceImpl ();
72
66
73
- private CredentialService credentialService = new CredentialServiceImpl ();
74
-
75
67
private WeIdService weIdService = new WeIdServiceImpl ();
76
68
77
69
private CredentialPojoService credentialPojoService = new CredentialPojoServiceImpl ();
@@ -403,83 +395,34 @@ public void registerAuthorityIssuer(
403
395
throw new BusinessException (response .getErrorMessage ());
404
396
}
405
397
}
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
+
438
399
/**
439
- * create a credential for user .
400
+ * Register a new Authority Issuer on Chain .
440
401
*
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
447
406
*/
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:" );
469
413
BaseBean .print (response );
470
414
471
415
// 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={}" ,
475
419
response .getErrorCode (),
476
420
response .getErrorMessage ()
477
421
);
478
422
throw new BusinessException (response .getErrorMessage ());
479
423
}
480
- return response .getResult ().getCredential ();
481
424
}
482
-
425
+
483
426
/**
484
427
* create a credential for user.
485
428
* @param arg the CreateCredentialPojoArgs
@@ -497,30 +440,6 @@ public <T> CredentialPojo createCredential(CreateCredentialPojoArgs<T> arg) {
497
440
return response .getResult ();
498
441
}
499
442
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
-
524
443
/**
525
444
* create presentation with policy.
526
445
* @param credentialList the credentialList of user
@@ -685,5 +604,38 @@ public boolean verifyPresentationE(
685
604
throw new BusinessException (response .getErrorMessage ());
686
605
}
687
606
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
+ }
689
641
}
0 commit comments