|
8 | 8 | import java.util.*; |
9 | 9 |
|
10 | 10 | /** |
11 | | - * Minimalist demo of selective disclosure. |
| 11 | + * Minimalist demo of selective certificate disclosure (SCD). |
12 | 12 | * |
13 | 13 | * <p>For a better interactive demo, please try our Rust version at |
14 | 14 | * https://github.com/WeBankBlockchain/WeDPR-Lab-Core |
15 | 15 | */ |
16 | 16 | public class ScdDemo { |
17 | | - public static void run(ScdClient scdClient) throws Exception { |
18 | | - System.out.println("\n*******\nSELECTIVE DISCLOSURE RUN\n*******"); |
19 | | - |
20 | | - // issuer make template |
21 | | - ArrayList<String> attributes = new ArrayList<String>(); |
22 | | - attributes.add("name"); |
23 | | - attributes.add("age"); |
24 | | - attributes.add("gender"); |
25 | | - attributes.add("time"); |
26 | | - String encodeAttributeTemplate = scdClient.issuerMakeCertificateSchema(attributes); |
27 | | - System.out.println("Encoded attributeTemplate = " + encodeAttributeTemplate); |
28 | | - |
29 | | - IssuerResult issuerResult = |
30 | | - scdClient.issuerMakeCertificateTemplate(encodeAttributeTemplate); |
31 | | - |
32 | | - String credentialTemplate = issuerResult.certificateTemplate; |
33 | | - String templateSecretKey = issuerResult.templatePrivateKey; |
34 | | - System.out.println("Encoded credentialTemplate = " + credentialTemplate); |
35 | | - System.out.println("Encoded templateSecretKey = " + templateSecretKey); |
36 | | - |
37 | | - // User fill template |
38 | | - Map<String, String> maps = new HashMap<String, String>(); |
39 | | - maps.put("name", "123"); |
40 | | - maps.put("age", "18"); |
41 | | - maps.put("gender", "1"); |
42 | | - maps.put("time", "12345"); |
43 | | - String credentialInfo = scdClient.userMakeAttributeDict(maps); |
44 | | - UserResult userResult = |
45 | | - scdClient.userFillCertificate(credentialInfo, credentialTemplate); |
46 | | - |
47 | | - String signatureRequest = userResult.signCertificateRequest; |
| 17 | + private static final String NAME = "name"; |
| 18 | + private static final String AGE = "age"; |
| 19 | + private static final String GENDER = "gender"; |
| 20 | + private static final String ISSUE_TIME = "issue_time"; |
| 21 | + private static final String DEFAULT_USER_ID = "default_user_id"; |
| 22 | + |
| 23 | + public static void run( |
| 24 | + IssuerClient issuerClient, UserClient userClient, VerifierClient verifierClient) |
| 25 | + throws Exception { |
| 26 | + System.out.println("\n*******\nSCD DEMO RUN\n*******"); |
| 27 | + |
| 28 | + // An issuer defines the certificate schema and generates the certificate template. |
| 29 | + List<String> schema = Arrays.asList(NAME, AGE, GENDER, ISSUE_TIME); |
| 30 | + System.out.println("Encoded schema = " + schema); |
| 31 | + |
| 32 | + IssuerResult issuerResult = issuerClient.makeCertificateTemplate(schema); |
| 33 | + |
| 34 | + String certificateTemplate = issuerResult.certificateTemplate; |
| 35 | + String templatePrivateKey = issuerResult.templatePrivateKey; |
| 36 | + System.out.println("Encoded certificateTemplate = " + certificateTemplate); |
| 37 | + System.out.println("Encoded templatePrivateKey = " + templatePrivateKey); |
| 38 | + |
| 39 | + // A user fills the certificate template and prepares a request for the issuer to sign. |
| 40 | + Map<String, String> certificateDataInput = new HashMap<>(); |
| 41 | + // TODO: Add a utility function to convert any string to a decimal string. |
| 42 | + // Before this utility function is implemented, the attribute value can only be a decimal |
| 43 | + // string. |
| 44 | + certificateDataInput.put(NAME, "123"); |
| 45 | + certificateDataInput.put(AGE, "19"); |
| 46 | + certificateDataInput.put(GENDER, "1"); |
| 47 | + certificateDataInput.put(ISSUE_TIME, "12345"); |
| 48 | + String certificateData = userClient.encodeAttributeDict(certificateDataInput); |
| 49 | + UserResult userResult = userClient.fillCertificate(certificateData, certificateTemplate); |
| 50 | + |
| 51 | + String signCertificateRequest = userResult.signCertificateRequest; |
48 | 52 | String userPrivateKey = userResult.userPrivateKey; |
49 | | - String credentialSecretsBlindingFactors = userResult.certificateSecretsBlindingFactors; |
| 53 | + String certificateSecretsBlindingFactors = userResult.certificateSecretsBlindingFactors; |
50 | 54 | String userNonce = userResult.userNonce; |
51 | | - System.out.println("Encoded signatureRequest = " + signatureRequest); |
| 55 | + System.out.println("Encoded signCertificateRequest = " + signCertificateRequest); |
52 | 56 | System.out.println("Encoded userPrivateKey = " + userPrivateKey); |
53 | 57 | System.out.println( |
54 | | - "Encoded credentialSecretsBlindingFactors = " + credentialSecretsBlindingFactors); |
| 58 | + "Encoded certificateSecretsBlindingFactors = " + certificateSecretsBlindingFactors); |
55 | 59 | System.out.println("Encoded userNonce = " + userNonce); |
56 | 60 |
|
57 | | - // Issuer sign user's request to generate credential |
| 61 | + // The issuer verifies the certificate signing request from the user and signs the certificate. |
58 | 62 | issuerResult = |
59 | | - scdClient.issuerSignCertificate( |
60 | | - credentialTemplate, templateSecretKey, signatureRequest, "id1", userNonce); |
61 | | - |
62 | | - String credentialSignature = issuerResult.certificateSignature; |
| 63 | + issuerClient.signCertificate( |
| 64 | + certificateTemplate, |
| 65 | + templatePrivateKey, |
| 66 | + signCertificateRequest, |
| 67 | + DEFAULT_USER_ID, |
| 68 | + userNonce); |
| 69 | + |
| 70 | + String certificateSignature = issuerResult.certificateSignature; |
63 | 71 | String issuerNonce = issuerResult.issuerNonce; |
64 | | - System.out.println("Encoded credentialSignature = " + credentialSignature); |
| 72 | + System.out.println("Encoded certificateSignature = " + certificateSignature); |
65 | 73 | System.out.println("Encoded issuerNonce = " + issuerNonce); |
66 | 74 |
|
67 | | - // User generate new credentialSignature |
| 75 | + // The user blinds the received certificateSignature to prevent the issuer to track the |
| 76 | + // certificate usage. |
68 | 77 | userResult = |
69 | | - scdClient.userBlindCertificateSignature( |
70 | | - credentialSignature, |
71 | | - credentialInfo, |
72 | | - credentialTemplate, |
| 78 | + userClient.blindCertificateSignature( |
| 79 | + certificateSignature, |
| 80 | + certificateData, |
| 81 | + certificateTemplate, |
73 | 82 | userPrivateKey, |
74 | | - credentialSecretsBlindingFactors, |
| 83 | + certificateSecretsBlindingFactors, |
75 | 84 | issuerNonce); |
76 | 85 |
|
77 | | - String credentialSignatureNew = userResult.certificateSignature; |
78 | | - System.out.println("Encoded credentialSignatureNew = " + credentialSignatureNew); |
| 86 | + String blindedCertificateSignature = userResult.certificateSignature; |
| 87 | + System.out.println("Encoded blindedCertificateSignature = " + blindedCertificateSignature); |
79 | 88 |
|
80 | | - // Verifier set verification rules |
81 | | - VerificationRuleSet verificationRuleSet = VerificationRuleSet.getDefaultInstance(); |
| 89 | + // A verifier sets a verification rule to: |
| 90 | + // Check AGE > 18 and, |
| 91 | + VerificationRuleSet.Builder verificationRuleSetBuilder = VerificationRuleSet.newBuilder(); |
82 | 92 | Predicate predicate = |
83 | 93 | Predicate.newBuilder() |
84 | | - .setAttributeName("age") |
| 94 | + .setAttributeName(AGE) |
85 | 95 | .setPredicateType(PredicateType.GT.name()) |
86 | | - .setPredicateValue(17) |
| 96 | + .setPredicateValue(18) |
87 | 97 | .build(); |
88 | | - verificationRuleSet = verificationRuleSet.toBuilder().addAttributePredicate(predicate).build(); |
| 98 | + verificationRuleSetBuilder.addAttributePredicate(predicate); |
| 99 | + // Reveal the ISSUE_TIME attribute. |
| 100 | + verificationRuleSetBuilder.addRevealedAttributeName(ISSUE_TIME); |
89 | 101 |
|
90 | | - predicate = |
91 | | - Predicate.newBuilder() |
92 | | - .setAttributeName("gender") |
93 | | - .setPredicateType(PredicateType.EQ.name()) |
94 | | - .setPredicateValue(1) |
95 | | - .build(); |
96 | | - verificationRuleSet = verificationRuleSet.toBuilder().addAttributePredicate(predicate).build(); |
| 102 | + String encodedVerificationRuleSet = |
| 103 | + verifierClient.protoToEncodedString(verificationRuleSetBuilder.build()); |
| 104 | + System.out.println("Encoded verificationRuleSet = " + encodedVerificationRuleSet); |
97 | 105 |
|
98 | | - String verificationRuleStr = |
99 | | - ScdClient.protoToEncodedString(verificationRuleSet); |
100 | | - System.out.println("Encoded verificationRuleStr = " + verificationRuleStr); |
| 106 | + String verificationNonce = verifierClient.getVerificationNonce().verificationNonce; |
101 | 107 |
|
102 | | - // User prove by verification rules |
103 | | - String verificationNonce = |
104 | | - scdClient.verifierGetVerificationNonce().verificationNonce; |
| 108 | + // The user proves the signed certificate data satisfying the verification rules and does not |
| 109 | + // reveal any extra data. |
105 | 110 | userResult = |
106 | | - scdClient.userProveSelectiveDisclosure( |
107 | | - verificationRuleStr, |
108 | | - credentialSignatureNew, |
109 | | - credentialInfo, |
110 | | - credentialTemplate, |
| 111 | + userClient.proveSelectiveDisclosure( |
| 112 | + encodedVerificationRuleSet, |
| 113 | + blindedCertificateSignature, |
| 114 | + certificateData, |
| 115 | + certificateTemplate, |
111 | 116 | userPrivateKey, |
112 | 117 | verificationNonce); |
113 | 118 |
|
114 | | - String verificationRequest = userResult.verifyRequest; |
115 | | - System.out.println("Encoded verificationRequest = " + verificationRequest); |
| 119 | + String verifyRequest = userResult.verifyRequest; |
| 120 | + System.out.println("Encoded verifyRequest = " + verifyRequest); |
116 | 121 |
|
117 | | - // Verifier verify proof |
| 122 | + // The verifier verifies the required verification rule is satisfied and extracts the required |
| 123 | + // attribute. |
| 124 | + // This verification should be done before calling revealedAttributeDict. |
118 | 125 | VerifierResult verifierResult = |
119 | | - scdClient.verifierVerifySelectiveDisclosure(verificationRuleStr, verificationRequest); |
120 | | - System.out.println("result = " + verifierResult.boolResult); |
121 | | - |
122 | | - verifierResult = |
123 | | - scdClient.verifierGetRevealedAttrsFromVerifyRequest(verificationRequest); |
124 | | - String revealedAttributeDict = verifierResult.revealedAttributeDict; |
125 | | - AttributeDict attributeDict = |
126 | | - AttributeDict.parseFrom(Utils.stringToBytes(revealedAttributeDict)); |
127 | | - System.out.println("revealedAttributeDict =" + attributeDict); |
| 126 | + verifierClient.verifySelectiveDisclosure(encodedVerificationRuleSet, verifyRequest); |
| 127 | + System.out.println("Proof verification result = " + verifierResult.boolResult); |
| 128 | + |
| 129 | + verifierResult = verifierClient.getRevealedAttributes(verifyRequest); |
| 130 | + String encodedRevealedCertificateData = verifierResult.revealedAttributeDict; |
| 131 | + AttributeDict revealedCertificateData = |
| 132 | + AttributeDict.parseFrom(Utils.stringToBytes(encodedRevealedCertificateData)); |
| 133 | + System.out.println("revealedCertificateData =" + revealedCertificateData); |
128 | 134 | } |
129 | 135 | } |
0 commit comments