Skip to content

Commit

Permalink
Merge pull request #33 from darwindu/feature/jsonschema-param-fix
Browse files Browse the repository at this point in the history
Feature/jsonschema param fix
  • Loading branch information
junqizhang-dev committed Jan 18, 2019
2 parents 8cb9472 + 8efb206 commit c4ece7d
Show file tree
Hide file tree
Showing 38 changed files with 1,341 additions and 687 deletions.
3 changes: 3 additions & 0 deletions config/checkstyle/webank_google_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@
value="COMMA, SEMI, POST_INC, POST_DEC, DOT, ELLIPSIS, METHOD_REF"/>
<property name="allowLineBreaks" value="true"/>
</module>
<module name="WhitespaceAfter">
<property name="tokens" value="COMMA"/>
</module>
<module name="ParenPad"/>
<module name="OperatorWrap">
<property name="option" value="NL"/>
Expand Down
16 changes: 10 additions & 6 deletions src/demo/com/webank/weid/demo/DemoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.webank.weid.demo;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -29,11 +30,12 @@
import com.webank.weid.protocol.base.AuthorityIssuer;
import com.webank.weid.protocol.base.CptBaseInfo;
import com.webank.weid.protocol.base.Credential;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.request.CptMapArgs;
import com.webank.weid.protocol.request.CreateCredentialArgs;
import com.webank.weid.protocol.request.RegisterAuthorityIssuerArgs;
import com.webank.weid.protocol.request.RegisterCptArgs;
import com.webank.weid.protocol.request.SetAuthenticationArgs;
import com.webank.weid.protocol.request.SetPublicKeyArgs;
import com.webank.weid.protocol.request.SetServiceArgs;
Expand Down Expand Up @@ -155,14 +157,16 @@ public WeIdDocument getWeIdDom(String weId) throws RuntimeException {
/**
* regist cpt.
*/
public CptBaseInfo registCpt(CreateWeIdDataResult weIdResult, String cptJsonSchema)
public CptBaseInfo registCpt(CreateWeIdDataResult weIdResult, Map<String, Object> cptJsonSchema)
throws RuntimeException {

RegisterCptArgs registerCptArgs = new RegisterCptArgs();
CptMapArgs registerCptArgs = new CptMapArgs();
WeIdPrivateKey weIdPrivateKey = new WeIdPrivateKey();
weIdPrivateKey.setPrivateKey(weIdResult.getUserWeIdPrivateKey().getPrivateKey());
registerCptArgs.setCptPublisher(weIdResult.getWeId());
registerCptArgs.setCptPublisherPrivateKey(weIdPrivateKey);
registerCptArgs.getWeIdAuthentication().setWeIdPrivateKey(weIdPrivateKey);

registerCptArgs.setWeIdAuthentication(new WeIdAuthentication());
registerCptArgs.getWeIdAuthentication().setWeId(weIdResult.getWeId());
registerCptArgs.setCptJsonSchema(cptJsonSchema);
ResponseData<CptBaseInfo> response = cptService.registerCpt(registerCptArgs);
// check result
Expand Down Expand Up @@ -209,7 +213,7 @@ public void registerAuthorityIssuer(
public Credential createCredential(
CreateWeIdDataResult weIdResult,
Integer cptId,
String claim,
Map<String, Object> claim,
long expirationDate)
throws RuntimeException {

Expand Down
51 changes: 32 additions & 19 deletions src/demo/com/webank/weid/demo/DemoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
package com.webank.weid.demo;

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

import com.webank.weid.common.BeanUtil;
import com.webank.weid.protocol.base.CptBaseInfo;
import com.webank.weid.protocol.base.Credential;
import com.webank.weid.protocol.base.WeIdDocument;
import com.webank.weid.protocol.response.CreateWeIdDataResult;
import com.webank.weid.util.DateUtils;
import com.webank.weid.util.JsonUtil;

/**
* WeIdentity DID demo.
Expand All @@ -36,25 +39,31 @@
public class DemoTest extends DemoBase {

/** jsonSchema. */
public final static String SCHEMA =
"{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"title\""
+ ":\"/etc/fstab\",\"description\":\"JSON representation of /etc/fstab\""
+ ",\"type\":\"object\",\"properties\":{\"swap\":{\"$ref\":\"#/definitions/mntent\"}}"
+ ",\"patternProperties\":{\"^/([^/]+(/[^/]+)*)?$\":{\"$ref\":\"#/definitions/mntent\"}}"
+ ",\"required\":[\"/\",\"swap\"],\"additionalProperties\":false,\"definitions\""
+ ":{\"mntent\":{\"title\":\"mntent\",\"description\":\"An fstab entry\",\"type\""
+ ":\"object\",\"properties\":{\"device\":{\"type\":\"string\"},\"fstype\""
+ ":{\"type\":\"string\"},\"options\":{\"type\":\"array\",\"minItems\":1,\"items\""
+ ":{\"type\":\"string\"}},\"dump\":{\"type\":\"integer\",\"minimum\":0},\"fsck\""
+ ":{\"type\":\"integer\",\"minimum\":0}},\"required\":[\"device\",\"fstype\"]"
+ ",\"additionalItems\":false}}}";
public final static String SCHEMA = "{"
+ " \"properties\" : {"
+ " \"name\": {"
+ " \"type\": \"string\", "
+ " \"description\": \"the name of the certificate owner\""
+ " }, "
+ " \"gender\": {"
+ " \"enum\": [\"F\", \"M\"],"
+ " \"type\": \"string\", "
+ " \"description\": \"the gender of the certificate owner\""
+ " }, "
+ " \"age\": {"
+ " \"type\": \"number\", "
+ " \"description\": \"the age of the certificate owner\""
+ " }"
+ " },"
+ " \"required\": [\"name\", \"age\"]"
+ "}";

/** claim. */
public final static String SCHEMADATA =
"{\"/\":{\"device\":\"/dev/sda2\",\"fstype\":\"btrfs\",\"options\":[\"ssd\"]},\"swap\""
+ ":{\"device\":\"/dev/sda2\",\"fstype\":\"swap\"},\"/tmp\":{\"device\""
+ ":\"tmpfs\",\"fstype\":\"tmpfs\",\"options\":[\"size=64M\"]},\"/var/lib/mysql\""
+ ":{\"device\":\"/dev/data/mysql\",\"fstype\":\"btrfs\"}}";
public final static String SCHEMADATA = "{"
+ " \"name\": \"zhangshan\", "
+ " \"gender\": \"F\", "
+ "\"age\": 32"
+ "}";

/**
* main of demo.
Expand Down Expand Up @@ -85,13 +94,17 @@ public static void main(String[] args) throws RuntimeException, ParseException {
demo.registerAuthorityIssuer(createWeId, "webank", "0");

// registCpt
CptBaseInfo cptResult = demo.registCpt(createWeId, SCHEMA);
CptBaseInfo cptResult =
demo.registCpt(
createWeId,
(Map<String, Object>) JsonUtil.jsonStrToObj(new HashMap<String, Object>(), SCHEMA)
);
BeanUtil.print(cptResult);

// create Credential
Credential credential = demo.createCredential(createWeId,
cptResult.getCptId(),
SCHEMADATA,
(Map<String, Object>) JsonUtil.jsonStrToObj(new HashMap<String, Object>(), SCHEMADATA),
DateUtils.convertStringToDate("2019-10-11T18:09:42Z").getTime());
BeanUtil.print(credential);

Expand Down
33 changes: 23 additions & 10 deletions src/demo/com/webank/weid/demo/DemoTest1.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.webank.weid.demo;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

import org.bcos.contract.tools.ToolConf;
import org.slf4j.Logger;
Expand All @@ -32,10 +34,11 @@
import com.webank.weid.protocol.base.AuthorityIssuer;
import com.webank.weid.protocol.base.CptBaseInfo;
import com.webank.weid.protocol.base.Credential;
import com.webank.weid.protocol.base.WeIdAuthentication;
import com.webank.weid.protocol.base.WeIdPrivateKey;
import com.webank.weid.protocol.request.CptMapArgs;
import com.webank.weid.protocol.request.CreateCredentialArgs;
import com.webank.weid.protocol.request.RegisterAuthorityIssuerArgs;
import com.webank.weid.protocol.request.RegisterCptArgs;
import com.webank.weid.protocol.request.SetAuthenticationArgs;
import com.webank.weid.protocol.request.SetPublicKeyArgs;
import com.webank.weid.protocol.response.CreateWeIdDataResult;
Expand All @@ -44,6 +47,7 @@
import com.webank.weid.rpc.CptService;
import com.webank.weid.rpc.CredentialService;
import com.webank.weid.rpc.WeIdService;
import com.webank.weid.util.JsonUtil;

/**
* <p>
Expand Down Expand Up @@ -164,7 +168,7 @@ public static void main(String[] args) {

// The third step: register CPT template
BeanUtil.print("begin regist cpt...");
RegisterCptArgs registerCptArgs = buildRegisterCptArgs(weId, privateKey);
CptMapArgs registerCptArgs = buildRegisterCptArgs(weId, privateKey);

CptService cptService = context.getBean(CptService.class);
ResponseData<CptBaseInfo> cptBaseResult = cptService.registerCpt(registerCptArgs);
Expand Down Expand Up @@ -257,7 +261,11 @@ private static CreateCredentialArgs buildCreateCredentialArgs(
ResponseData<CptBaseInfo> cptBaseResult) {

CreateCredentialArgs createCredentialArgs = new CreateCredentialArgs();
createCredentialArgs.setClaim(DemoTest.SCHEMADATA); // Set data required for template
createCredentialArgs.setClaim(
(Map<String, Object>) JsonUtil.jsonStrToObj(
new HashMap<String, Object>(),
DemoTest.SCHEMADATA)
); // Set data required for template
createCredentialArgs.setCptId(cptBaseResult.getResult().getCptId()); // Set cptId
createCredentialArgs.setIssuer(weId); // Set Creator of voucher
// Set expiration date
Expand All @@ -268,16 +276,21 @@ private static CreateCredentialArgs buildCreateCredentialArgs(
return createCredentialArgs;
}

private static RegisterCptArgs buildRegisterCptArgs(
private static CptMapArgs buildRegisterCptArgs(
String weId,
String privateKey) {

RegisterCptArgs registerCptArgs = new RegisterCptArgs();
registerCptArgs.setCptJsonSchema(DemoTest.SCHEMA); // Set up a template
registerCptArgs.setCptPublisher(weId); // Set template publisher

registerCptArgs.setCptPublisherPrivateKey(new WeIdPrivateKey());
registerCptArgs.getCptPublisherPrivateKey().setPrivateKey(privateKey);
CptMapArgs registerCptArgs = new CptMapArgs();
registerCptArgs.setCptJsonSchema(
(Map<String, Object>) JsonUtil.jsonStrToObj(
new HashMap<String, Object>(),
DemoTest.SCHEMA)
); // Set up a template

registerCptArgs.setWeIdAuthentication(new WeIdAuthentication());
registerCptArgs.getWeIdAuthentication().setWeId(weId); // Set template publisher
registerCptArgs.getWeIdAuthentication().setWeIdPrivateKey(new WeIdPrivateKey());
registerCptArgs.getWeIdAuthentication().getWeIdPrivateKey().setPrivateKey(privateKey);
return registerCptArgs;
}
}
32 changes: 22 additions & 10 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ public enum ErrorCode {
*/
CPT_JSON_SCHEMA_INVALID(100301, "cpt json schema is invalid"),

/**
* The cpt json schema null.
*/
CPT_JSON_SCHEMA_NULL(100302, "cpt json schema is null"),

/**
* cptId is null.
*/
CPT_ID_NULL(100303, "cptId is null"),

/**
* cpt event log is null.
*/
CPT_EVENT_LOG_NULL(100304, "cpt event log is null."),

/**
* Credential main error code.
*/
Expand Down Expand Up @@ -248,16 +263,21 @@ public enum ErrorCode {
/**
* create keypair exception.
*/
WEID_KEYPAIR_CREATE_FAILED(10107, "create keypair faild."),
WEID_KEYPAIR_CREATE_FAILED(100107, "create keypair faild."),

/**
* public key and private key are not a keypair.
*/
WEID_PUBLICKEY_AND_PRIVATEKEY_NOT_MATCHED(
10108,
100108,
"the public key and private key are not matched."
),

/**
* the authority of the weIdentity DID is invalid.
*/
WEID_AUTHORITY_INVALID(100109, "the authority of the weIdentity DID is invalid."),

/**
* transaction timeout.
*/
Expand Down Expand Up @@ -293,14 +313,6 @@ public enum ErrorCode {
*/
DATA_TYPE_CASE_ERROR(160008, "data type cast exception error, please check the error log."),

/**
* weidentity resolve attribute exceptions or error.
*/
DATA_RESOLVE_ATTRIBUTE_ERROR(
160011,
"data resolve attribute exception error, please check the error log."
),

/**
* other uncatched exceptions or error.
*/
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/webank/weid/constant/JsonSchemaConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.webank.weid.constant;

/**
* JsonSchema propertites.
* @author darwindu
*/
public final class JsonSchemaConstant {

/** json schema: $schema key. **/
public static final String SCHEMA_KEY = "$schema";

/** json schema: $schema value. **/
public static final String SCHEMA_VALUE = "http://json-schema.org/draft-04/schema#";

/** json schema: type key. **/
public static final String TYPE_KEY = "type";

/** json schema: data type object. **/
public static final String DATE_TYPE_OBJECT = "object";

/** json schema: data type string. **/
public static final String DATE_TYPE_STRING = "string";

/** json schema: data type number. **/
public static final String DATE_TYPE_NUMBER = "number";

/** json schema: data type enum. **/
public static final String DATE_TYPE_ENUM = "enum";

/** json schema: title. **/
public static final String TITLE_KEY = "title";

/** json schema: description. **/
public static final String DESCRIPTION_KEY = "description";

/** json schema: properties. **/
public static final String PROPERTIES_KEY = "properties";

/** json schema: required. **/
public static final String REQUIRED_KEY = "required";

}
5 changes: 0 additions & 5 deletions src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,4 @@ public final class WeIdConstant {
* 0L.
*/
public static final Long LONG_VALUE_ZERO = 0L;

/**
* -1.
*/
public static final Integer VALUE_NEG_FIRST = -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private static String deployWeIdContract() {
WeIdContract weIdContract =
f.get(DEFAULT_DEPLOY_CONTRACTS_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
String contractAddress = weIdContract.getContractAddress();
writeAddressToFile(contractAddress,"weIdContract.address");
writeAddressToFile(contractAddress, "weIdContract.address");
return contractAddress;
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.error("WeIdContract deploy exception", e);
Expand Down Expand Up @@ -206,7 +206,7 @@ private static String deployCptContracts(
CptController cptController =
f2.get(DEFAULT_DEPLOY_CONTRACTS_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
String cptControllerAddress = cptController.getContractAddress();
writeAddressToFile(cptControllerAddress,"cptController.address");
writeAddressToFile(cptControllerAddress, "cptController.address");
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.error("CptController deploy exception", e);
}
Expand Down Expand Up @@ -312,7 +312,7 @@ private static String deployAuthorityIssuerContracts() {
TimeUnit.SECONDS);
String authorityIssuerControllerAddress =
authorityIssuerController.getContractAddress();
writeAddressToFile(authorityIssuerControllerAddress,"authorityIssuer.address");
writeAddressToFile(authorityIssuerControllerAddress, "authorityIssuer.address");
return authorityIssuerControllerAddress;
} catch (Exception e) {
logger.error("AuthorityIssuerController deployment error:", e);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/webank/weid/protocol/base/Cpt.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.webank.weid.protocol.base;

import java.util.Map;

import lombok.Data;

/**
Expand All @@ -37,7 +39,7 @@ public class Cpt {
/**
* The cpt json schema.
*/
private String cptJsonSchema;
private Map<String, Object> cptJsonSchema;

/**
* The meta data.
Expand Down

0 comments on commit c4ece7d

Please sign in to comment.