Skip to content

Commit

Permalink
Merge pull request #223 from WeBankFinTech/feature/full-authority-issuer
Browse files Browse the repository at this point in the history
- Authority Issuer includes description and extra info lists
  • Loading branch information
chaoxinhu committed Jun 7, 2020
2 parents 3458313 + 1474ca5 commit c18192d
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 26 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/webank/weid/constant/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,16 @@ public enum ErrorCode {
"the specific issuer type is illegal"
),

AUTORITY_ISSUER_DESCRIPTION_ILLEGAL(
100209,
"authority issuer description illegal"
),

AUTHORITY_ISSUER_EXTRA_PARAM_ILLEGAL(
100210,
"authority issuer extra param illegal"
),

/**
* the key of the data is empty.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public final class WeIdConstant {
*/
public static final Integer AUTHORITY_ISSUER_ARRAY_LEGNTH = 16;

/**
* The Constant Authority Issuer extra param list max length.
*/
public static final Integer AUTHORITY_ISSUER_EXTRA_PARAM_LENGTH = 10;

/**
* The default accumulator value.
*/
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/webank/weid/protocol/base/AuthorityIssuer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

package com.webank.weid.protocol.base;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import com.webank.weid.util.DateUtils;

/**
* The base data structure to handle Authority Issuer info.
Expand Down Expand Up @@ -48,4 +54,59 @@ public class AuthorityIssuer {
* Required: The accumulator value of the Authority Issuer.
*/
private String accValue;

/**
* Optional: The description of this Authority Issuer.
*/
private String description;

/**
* Optional: The extra String type information stored on chain.
*/
private List<String> extraStr32;

/**
* Optional: The extra Integer type information stored on chain.
*/
private List<Integer> extraInt;

/**
* Constructor with initialized values.
*
* @param weId the WeID
* @param name the name
* @param accValue the accumulator value (currently unused)
* @param description the description
* @param extraStr32 the extra String list
* @param extraInt the extra Integer list
*/
public AuthorityIssuer(
String weId,
String name,
String accValue,
String description,
List<String> extraStr32,
List<Integer> extraInt
) {
this.weId = weId;
this.name = name;
this.created = DateUtils.getNoMillisecondTimeStamp();
this.accValue = accValue;
this.description = StringUtils.isEmpty(description) ? StringUtils.EMPTY : description;
this.extraStr32 = extraStr32 == null ? new ArrayList<>() : extraStr32;
this.extraInt = extraInt == null ? new ArrayList<>() : extraInt;
}

/**
* Empty Constructor.
*/
public AuthorityIssuer() {
this.weId = StringUtils.EMPTY;
this.name = StringUtils.EMPTY;
this.created = DateUtils.getNoMillisecondTimeStamp();
this.accValue = StringUtils.EMPTY;
this.description = StringUtils.EMPTY;
this.extraStr32 = new ArrayList<>();
this.extraInt = new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,6 +174,7 @@ public ResponseData<List<AuthorityIssuer>> getAllAuthorityIssuerList(Integer ind
* @param issuerType the specified issuer type
* @return Execution result
*/
@Override
public ResponseData<Boolean> registerIssuerType(
WeIdAuthentication callerAuth,
String issuerType
Expand Down Expand Up @@ -203,6 +205,7 @@ public ResponseData<Boolean> registerIssuerType(
* @param targetIssuerWeId the weId of the issuer who will be marked as a specific issuer type
* @return Execution result
*/
@Override
public ResponseData<Boolean> addIssuerIntoIssuerType(
WeIdAuthentication callerAuth,
String issuerType,
Expand Down Expand Up @@ -231,6 +234,7 @@ public ResponseData<Boolean> addIssuerIntoIssuerType(
* @param targetIssuerWeId the weId of the issuer to be removed from a specific issuer list
* @return Execution result
*/
@Override
public ResponseData<Boolean> removeIssuerFromIssuerType(
WeIdAuthentication callerAuth,
String issuerType,
Expand Down Expand Up @@ -260,6 +264,7 @@ public ResponseData<Boolean> removeIssuerFromIssuerType(
* @param targetIssuerWeId the WeId
* @return true if yes, false otherwise
*/
@Override
public ResponseData<Boolean> isSpecificTypeIssuer(
String issuerType,
String targetIssuerWeId
Expand Down Expand Up @@ -288,6 +293,7 @@ public ResponseData<Boolean> isSpecificTypeIssuer(
* @param num the number of issuers
* @return the list
*/
@Override
public ResponseData<List<String>> getAllSpecificTypeIssuerList(
String issuerType,
Integer index,
Expand Down Expand Up @@ -413,7 +419,7 @@ private ErrorCode checkAuthorityIssuerArgsValidity(AuthorityIssuer args) {
return ErrorCode.WEID_INVALID;
}
String name = args.getName();
if (!isValidAuthorityIssuerName(name)) {
if (!isValidAuthorityIssuerBytes32Param(name)) {
return ErrorCode.AUTHORITY_ISSUER_NAME_ILLEGAL;
}
String accValue = args.getAccValue();
Expand All @@ -428,10 +434,35 @@ private ErrorCode checkAuthorityIssuerArgsValidity(AuthorityIssuer args) {
return ErrorCode.AUTHORITY_ISSUER_ACCVALUE_ILLEAGAL;
}

// Check additional, optional params
if (!StringUtils.isEmpty(args.getDescription())
&& !isValidAuthorityIssuerBytes32Param(args.getDescription())) {
logger.error("Authority Issuer description length illegal: ", args.getDescription());
return ErrorCode.AUTORITY_ISSUER_DESCRIPTION_ILLEGAL;
}
List<String> extraStr32s = args.getExtraStr32();
List<Integer> extraInts = args.getExtraInt();
if (!CollectionUtils.isEmpty(extraStr32s)) {
if (extraStr32s.size() > WeIdConstant.AUTHORITY_ISSUER_EXTRA_PARAM_LENGTH) {
logger.error("Authority Issuer extra param size exceeds maximum.");
return ErrorCode.AUTHORITY_ISSUER_EXTRA_PARAM_ILLEGAL;
}
for (String extraStr : extraStr32s) {
if (!isValidAuthorityIssuerBytes32Param(extraStr)) {
logger.error("Authority Issuer extra String length illegal.");
return ErrorCode.AUTHORITY_ISSUER_EXTRA_PARAM_ILLEGAL;
}
}
}
if (!CollectionUtils.isEmpty(extraInts)
&& extraInts.size() > WeIdConstant.AUTHORITY_ISSUER_EXTRA_PARAM_LENGTH) {
logger.error("Authority Issuer extra param size exceeds maximum.");
return ErrorCode.AUTHORITY_ISSUER_EXTRA_PARAM_ILLEGAL;
}
return ErrorCode.SUCCESS;
}

private boolean isValidAuthorityIssuerName(String name) {
private boolean isValidAuthorityIssuerBytes32Param(String name) {
return !StringUtils.isEmpty(name)
&& name.getBytes(StandardCharsets.UTF_8).length
< WeIdConstant.MAX_AUTHORITY_ISSUER_NAME_LENGTH
Expand All @@ -440,7 +471,7 @@ private boolean isValidAuthorityIssuerName(String name) {

@Override
public ResponseData<String> getWeIdByOrgId(String orgId) {
if (!isValidAuthorityIssuerName(orgId)) {
if (!isValidAuthorityIssuerBytes32Param(orgId)) {
return new ResponseData<>(StringUtils.EMPTY, ErrorCode.AUTHORITY_ISSUER_NAME_ILLEGAL);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.fisco.bcos.web3j.abi.datatypes.Address;
import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
Expand Down Expand Up @@ -106,9 +107,22 @@ public ResponseData<Boolean> addAuthorityIssuer(RegisterAuthorityIssuerArgs args
String weAddress = WeIdUtils.convertWeIdToAddress(authorityIssuer.getWeId());
List<byte[]> stringAttributes = new ArrayList<byte[]>();
stringAttributes.add(authorityIssuer.getName().getBytes());
if (!StringUtils.isEmpty(authorityIssuer.getDescription())) {
stringAttributes.add(authorityIssuer.getDescription().getBytes());
}
List<String> extraStr32s = authorityIssuer.getExtraStr32();
for (int index = 0; index < extraStr32s.size(); index++) {
stringAttributes.add(extraStr32s.get(index).getBytes());
}
List<BigInteger> longAttributes = new ArrayList<>();
Long createDate = DateUtils.getNoMillisecondTimeStamp();
longAttributes.add(BigInteger.valueOf(createDate));
// The second integer value is updated to be added
longAttributes.add(BigInteger.valueOf(createDate));
List<Integer> extraInts = authorityIssuer.getExtraInt();
for (int index = 0; index < extraInts.size(); index++) {
longAttributes.add(BigInteger.valueOf(extraInts.get(index)));
}
try {
AuthorityIssuerController authorityIssuerController = reloadContract(
fiscoConfig.getIssuerAddress(),
Expand Down Expand Up @@ -261,19 +275,44 @@ public ResponseData<AuthorityIssuer> getAuthorityIssuerInfoNonAccValue(String we

AuthorityIssuer result = new AuthorityIssuer();
result.setWeId(weId);
String name = DataToolUtils.byte32ListToString(
bytes32Attributes,
WeIdConstant.AUTHORITY_ISSUER_ARRAY_LEGNTH
);

String name = new String(bytes32Attributes.get(0)).trim();
result.setName(name);

if (!DataToolUtils.isByteArrayEmpty(bytes32Attributes.get(1))) {
String desc = new String(bytes32Attributes.get(1)).trim();
result.setDescription(desc);
}

List<String> extraStr32s = new ArrayList<>();
for (int index = 0; index < WeIdConstant.AUTHORITY_ISSUER_EXTRA_PARAM_LENGTH; index++) {
byte[] extraByte = bytes32Attributes.get(index + 2);
if (!DataToolUtils.isByteArrayEmpty(extraByte)) {
extraStr32s.add(new String(extraByte).trim());
}
}
result.setExtraStr32(extraStr32s);

// 0 is created, 1 is reserved for updated
List<Integer> extraInts = new ArrayList<>();
for (int index = 0; index < WeIdConstant.AUTHORITY_ISSUER_EXTRA_PARAM_LENGTH; index++) {
Integer intValue = int256Attributes.get(index + 2).intValue();
if (intValue == 0) {
continue;
}
extraInts.add(intValue);
}
result.setExtraInt(extraInts);

Long createDate = Long
.valueOf(int256Attributes.get(0).longValue());
if (StringUtils.isEmpty(name) && createDate.equals(WeIdConstant.LONG_VALUE_ZERO)) {
return new ResponseData<>(
null, ErrorCode.AUTHORITY_ISSUER_CONTRACT_ERROR_NOT_EXISTS
);
}
result.setName(name);
result.setCreated(createDate);

// Accumulator Value is unable to load due to Solidity 0.4.4 restrictions - left blank.
result.setAccValue("");
resultData.setResult(result);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/webank/weid/util/DataToolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,21 @@ public static String generateUnformattedCptJsonSchema() {
return DataToolUtils.objToJsonStrWithNoPretty(cptSchemaMap);
}

/**
* Check if the byte array is empty.
*
* @param byteArray the byte[]
* @return true if empty, false otherwise
*/
public static boolean isByteArrayEmpty(byte[] byteArray) {
for (int index = 0; index < byteArray.length; index++) {
if (byteArray[index] != 0) {
return false;
}
}
return true;
}

/**
* convert byte32List to String.
*
Expand Down

0 comments on commit c18192d

Please sign in to comment.