Skip to content

Commit

Permalink
Merge pull request #185 from WeBankFinTech/feature/customize-pubkey-type
Browse files Browse the repository at this point in the history
* Allow to customize public key type w/ delegate
  • Loading branch information
chaoxinhu committed Apr 26, 2020
2 parents 26169de + 098b322 commit a0af436
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
script:
- chmod u+x .ci/script/build-ci.sh
- .ci/script/build-ci.sh
#- travis_wait 45 ./gradlew check
#- ./gradlew jacocoTestReport
#after_success:
#- bash <(curl -s https://codecov.io/bash)
- travis_wait 40 ./gradlew check
- ./gradlew jacocoTestReport
after_success:
- bash <(curl -s https://codecov.io/bash)
- stage: "dependency pipeline"
script:
- chmod u+x .ci/script/weid-http-service/build-ci.sh
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/webank/weid/constant/WeIdConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,29 @@ public final class WeIdConstant {

public static final Integer ON_CHAIN_STRING_LENGTH = 2097152;

public static final String EVIDENCE_ATTRIBUTE_DELIMETER = "|";
public static enum PublicKeyType {
RSA("RSA"),
SECP256K1("Secp256k1");

/**
* The Type Name of the Credential Proof.
*/
private String typeName;

/**
* Constructor.
*/
PublicKeyType(String typeName) {
this.typeName = typeName;
}

/**
* Getter.
*
* @return typeName
*/
public String getTypeName() {
return typeName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import lombok.Data;

import com.webank.weid.constant.WeIdConstant.PublicKeyType;

/**
* The base data structure for AuthenticationProperty.
*
Expand All @@ -32,7 +34,7 @@ public class AuthenticationProperty {
/**
* Required: The type.
*/
private String type = "Secp256k1";
private String type = PublicKeyType.SECP256K1.getTypeName();

/**
* Required: The public key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PublicKeyProperty {
/**
* Required: The type.
*/
private String type = "Secp256k1";
private String type;

/**
* Required: The owner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import lombok.Data;

import com.webank.weid.constant.WeIdConstant.PublicKeyType;

/**
* The Arguments when setting Public Key for WeIdentity DID.
*
Expand All @@ -37,7 +39,7 @@ public class PublicKeyArgs {
/**
* Required: The type.
*/
private String type = "Secp256k1";
private PublicKeyType type = PublicKeyType.SECP256K1;

/**
* Required: The owner.
Expand All @@ -54,7 +56,7 @@ public class PublicKeyArgs {
*
* @param type the public key type
*/
public void setType(String type) {
this.type = "Secp256k1";
public void setType(PublicKeyType type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import lombok.Data;

import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.constant.WeIdConstant.PublicKeyType;
import com.webank.weid.protocol.base.WeIdPrivateKey;

/**
Expand All @@ -39,7 +41,7 @@ public class SetPublicKeyArgs {
/**
* Required: The type.
*/
private String type = "Secp256k1";
private PublicKeyType type = PublicKeyType.SECP256K1;

/**
* Required: The owner.
Expand All @@ -60,7 +62,7 @@ public class SetPublicKeyArgs {
* nothing to do.
* @param type the public key type
*/
public void setType(String type) {
this.type = "Secp256k1";
public void setType(PublicKeyType type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.constant.WeIdConstant.PublicKeyType;
import com.webank.weid.exception.LoadContractException;
import com.webank.weid.exception.PrivateKeyIllegalException;
import com.webank.weid.protocol.base.AuthenticationProperty;
Expand Down Expand Up @@ -376,14 +377,16 @@ public ResponseData<Boolean> setPublicKey(SetPublicKeyArgs setPublicKeyArgs) {

String privateKey = setPublicKeyArgs.getUserWeIdPrivateKey().getPrivateKey();
return processSetPubKey(
setPublicKeyArgs.getType(),
setPublicKeyArgs.getType().getTypeName(),
weAddress,
owner,
pubKey,
privateKey,
false);
}



/**
* Set Service.
*
Expand Down Expand Up @@ -700,7 +703,7 @@ public ResponseData<Boolean> delegateSetPublicKey(
String privateKey = delegateAuth.getWeIdPrivateKey().getPrivateKey();

return processSetPubKey(
publicKeyArgs.getType(),
publicKeyArgs.getType().getTypeName(),
weAddress,
owner,
pubKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.ResolveEventLogStatus;
import com.webank.weid.constant.WeIdConstant;
import com.webank.weid.constant.WeIdConstant.PublicKeyType;
import com.webank.weid.constant.WeIdEventConstant;
import com.webank.weid.contract.v2.WeIdContract;
import com.webank.weid.contract.v2.WeIdContract.WeIdAttributeChangedEventResponse;
Expand Down Expand Up @@ -150,10 +151,10 @@ private static ResolveEventLogResult resolveAttributeEvent(
private static void buildupWeIdAttribute(
String key, String value, String weId, WeIdDocument result) {
if (StringUtils.startsWith(key, WeIdConstant.WEID_DOC_PUBLICKEY_PREFIX)) {
buildWeIdPublicKeys(value, weId, result);
buildWeIdPublicKeys(key, value, weId, result);
} else if (StringUtils.startsWith(key, WeIdConstant.WEID_DOC_AUTHENTICATE_PREFIX)) {
if (!value.contains(WeIdConstant.REMOVED_PUBKEY_TAG)) {
buildWeIdPublicKeys(value, weId, result);
buildWeIdPublicKeys(null, value, weId, result);
}
buildWeIdAuthentication(value, weId, result);
} else if (StringUtils.startsWith(key, WeIdConstant.WEID_DOC_SERVICE_PREFIX)) {
Expand All @@ -163,12 +164,22 @@ private static void buildupWeIdAttribute(
}
}

private static void buildWeIdPublicKeys(String value, String weId, WeIdDocument result) {
private static void buildWeIdPublicKeys(String key, String value, String weId,
WeIdDocument result) {

logger.info("method buildWeIdPublicKeys() parameter::value:{}, weId:{}, "
+ "result:{}", value, weId, result);
List<PublicKeyProperty> pubkeyList = result.getPublicKey();

String type = PublicKeyType.SECP256K1.getTypeName();
// Identify explicit type from key
if (!StringUtils.isEmpty(key)) {
String[] keyArray = StringUtils.splitByWholeSeparator(key, "/");
if (keyArray.length > 2) {
type = keyArray[2];
}
}

// Only store the latest public key
// OBSOLETE and non-OBSOLETE public keys are regarded as the same
String trimmedPubKey = StringUtils
Expand All @@ -193,6 +204,7 @@ private static void buildWeIdPublicKeys(String value, String weId, WeIdDocument
String owner = WeIdUtils.convertAddressToWeId(weAddress);
pubKey.setOwner(owner);
}
pubKey.setType(type);
result.getPublicKey().add(pubKey);
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/webank/weid/full/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.webank.weid.full;

import com.webank.weid.constant.WeIdConstant.PublicKeyType;

/**
* The function of this class is to test the basic data required.
*
Expand All @@ -30,7 +32,7 @@ public class TestData {
* you may need to add public key after you create WeIdentity DID,This property is the public
* key type,but for now the value is fixed and you don't need to modify it.
*/
public static final String PUBLIC_KEY_TYPE = "Secp256k1";
public static final String PUBLIC_KEY_TYPE = PublicKeyType.SECP256K1.getTypeName();

/**
* after you create WeIdentity DID,you may need to add service information which indicates the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void testSerialize_serializeCredentialPojo() {
@Test
public void testSerialize_serializeEmptyCredentialPojo() {

CredentialPojo credentialPojo1 = new CredentialPojo();
CredentialPojo credentialPojo1 = copyCredentialPojo(credentialPojo);
ResponseData<String> response =
TransportationFactory.newQrCodeTransportation().specify(verifier)
.serialize(credentialPojo1, new ProtocolProperty(EncodeType.ORIGINAL));
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/webank/weid/full/weid/TestGetWeIdDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@
import com.webank.weid.common.LogUtil;
import com.webank.weid.common.PasswordKey;
import com.webank.weid.constant.ErrorCode;
import com.webank.weid.constant.WeIdConstant.PublicKeyType;
import com.webank.weid.full.TestBaseService;
import com.webank.weid.full.TestBaseUtil;
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.PublicKeyArgs;
import com.webank.weid.protocol.request.SetAuthenticationArgs;
import com.webank.weid.protocol.request.SetPublicKeyArgs;
import com.webank.weid.protocol.request.SetServiceArgs;
import com.webank.weid.protocol.response.CreateWeIdDataResult;
import com.webank.weid.protocol.response.ResponseData;
import com.webank.weid.util.DataToolUtils;

/**
* getWeIdDocument method for testing WeIdService.
Expand Down Expand Up @@ -122,6 +127,38 @@ public void testGetWeIdDocument_twoServiceAndAuthentication() {
Assert.assertEquals(1, weIdDoc.getResult().getPublicKey().size());
}

@Test
public void testDifferentPublicKeyType() {
CreateWeIdDataResult createWeIdResult = super.createWeId();
SetPublicKeyArgs setPublicKeyArgs = new SetPublicKeyArgs();
setPublicKeyArgs.setWeId(createWeIdResult.getWeId());
setPublicKeyArgs.setPublicKey("bcabu298t876Buc");
setPublicKeyArgs.setUserWeIdPrivateKey(new WeIdPrivateKey());
setPublicKeyArgs.getUserWeIdPrivateKey()
.setPrivateKey(createWeIdResult.getUserWeIdPrivateKey().getPrivateKey());
setPublicKeyArgs.setType(PublicKeyType.RSA);
setPublicKeyArgs.setOwner(createWeIdResult.getWeId());
weIdService.setPublicKey(setPublicKeyArgs);
ResponseData<WeIdDocument> weIdDoc = weIdService
.getWeIdDocument(createWeIdResult.getWeId());
Assert.assertEquals(weIdDoc.getResult().getPublicKey().size(), 2);
Assert.assertTrue(weIdDoc.getResult().getPublicKey().get(0).getType().equals("RSA"));
// test delegate
PublicKeyArgs publicKeyArgs = new PublicKeyArgs();
publicKeyArgs.setOwner(setPublicKeyArgs.getOwner());
publicKeyArgs.setPublicKey("abcabac123123");
publicKeyArgs.setType(PublicKeyType.RSA);
publicKeyArgs.setWeId(setPublicKeyArgs.getWeId());
WeIdAuthentication weIdAuthentication = new WeIdAuthentication();
weIdAuthentication.setWeId(DataToolUtils.convertPrivateKeyToDefaultWeId(privateKey));
weIdAuthentication.setWeIdPrivateKey(new WeIdPrivateKey());
weIdAuthentication.getWeIdPrivateKey().setPrivateKey(privateKey);
ResponseData<Boolean> resp = weIdService
.delegateSetPublicKey(publicKeyArgs, weIdAuthentication);
weIdDoc = weIdService.getWeIdDocument(createWeIdResult.getWeId());
Assert.assertEquals(weIdDoc.getResult().getPublicKey().size(), 3);
}

/**
* case: WeIdentity DID is not exists.
*/
Expand Down

0 comments on commit a0af436

Please sign in to comment.