BSN-DID集中部署了DID合约,划分了一片存储空间,专门用来
存储用户凭证内容,称为隐私数据存储区
。同时部署了服务,对外提供单次授权、单次访问、访问者身份验证等服务,保证了证书的安全可控。
本文就是针对隐私数据操作函数进行代码验证分析。
主题4 :存储、删除、修改、查询资源,创建授权, 查询权限,解密资源, 内容来源于帮助手册的14.4.4 隐私区域。
1 通过公钥注册Hub用户
方法名:
registerHubByDid(String did)
方法描述:
通过DID标识符注册Hub,成功则返回用户在Hub内的ID。
2 通过DID注册Hub用户
方法名:
registerHub(String id, String publicKey, CryptoType cryptoType)
方法描述: 通过公钥注册Hub,支持用户自定义ID,注册成功便返回用户在Hub内的ID。
【说明】手册中的参数列表是错误的,正确的用法请看下面代码。
如果指定了did,注册后的uid就是did。
如果没有指定did,注册后的uid是个16进制字符串,怎么计算出来的不知道!!
public static void registerHubtTest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult1 = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),CryptoType.ECDSA);
System.out.println(JSONObject.toJSONString(registerHubResult1));
System.out.println(registerHubResult1.isSuccess());
RegisterHubResult registerHubResult2 = didClient.registerHubByDid("did:bsn:2nbUugNA1D7M4ZRxsMqqYYYFkX5W");
System.out.println(JSONObject.toJSONString(registerHubResult2));
}
运行结果:
{"success":true,"uid":"did:bsn:3G12xevH5UEBrpw4Yn91r869nXS"}
true
{"success":true,"uid":"did:bsn:2nbUugNA1D7M4ZRxsMqqYYYFkX5W"}
public static void registerHubByPublicKeyTest() {
com.reddate.did.sdk.protocol.common.KeyPair authKeyPair = null;
try {
authKeyPair = ECDSAUtils.createKey();
System.out.println("PublicKey="+authKeyPair.getPublicKey());
System.out.println("PrivateKey="+authKeyPair.getPrivateKey());
} catch (Exception e) {
throw new RuntimeException(e);
}
RegisterHubResult registerHubResult = didClient.registerHub(authKeyPair.getPublicKey(),CryptoType.ECDSA);
System.out.println(JSONObject.toJSONString(registerHubResult));
System.out.println(registerHubResult.isSuccess());
}
运行结果:
PublicKey=2416903095993991605361108159639220805242131148326949625462531781897915420565224191623604002179416060745907263807891554878114232423948690971430143346418516
PrivateKey=63811034992853959270115807875929935914108196789532332193402757686863053894518
{"success":true,"uid":"0xd63a063d2cf84efbbc25e9dcbe28b3c9bc350839"}
true
3 存储资源
方法名:
saveResource(SaveResource saveResource)
方法描述:
向Hub内存储资源。如果用户自己存储则uid和ownerUid应一致,此时不需要创建权限直接调用即可;
如果是发证方在签发完凭证后替用户存储,则uid应为发证方,ownerUid应为用户,此时必须是用户已经为其创建了“WRITE”权限,否则将存储失败。
public static void saveResourceECDSATest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
System.out.println("saveResource = "+ JSONObject.toJSONString(saveResource));
System.out.println("=================="+JSONObject.toJSONString(saveResourceResult));
System.out.println(saveResourceResult.getEncryptKey());
System.out.println(saveResourceResult.getUrl());
}
运行结果:
==================
saveResource = {
"content":"aaaaaaaaaaaaa",
"grant":"WRITE",
"ownerUid":"did:bsn:3fHe28XNL23q61mMnJQ9a9EXmXzs",
"privateKey":"87178177855952035645987187257769141349090417129152143785721124927720834027325",
"uid":"did:bsn:3fHe28XNL23q61mMnJQ9a9EXmXzs"
}
saveResourceResult =
{
"encryptKey":"048ba253f2244060814ca4358fb7b2137ba4825ffd80f3404fe09b0b712bb3ac42743bf2c51c70eb922ede8cb4e22e8feb5d5548a5654971451436c48de9168331e58f0e53edf3d715866bee8d717e11e31de01d5e2391458872157d48a19151ee878f2e8803e7ef7c1123f669f03d5af553377962",
"url":"did:bsn:3fHe28XNL23q61mMnJQ9a9EXmXzs3a7e7d67761245a58b31023a00db4f0e"
}
注意结果url的前半截就是uid, encryptKey就是加密后的密文密钥。
public static void saveResourceTest2() {
com.reddate.did.sdk.param.KeyPair keyPair = Secp256Util.createKeyPair(CryptoType.ECDSA);
String userId = Secp256Util.getAddress(CryptoType.ECDSA, keyPair.getPrivateKey());
System.out.println("userId = "+userId);
RegisterHubResult registerHubResult = didClient.registerHub(userId, keyPair.getPublicKey(),CryptoType.ECDSA);
SaveResource saveResource = new SaveResource();
saveResource.setUid(userId);
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(userId);
saveResource.setPrivateKey(keyPair.getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
System.out.println("saveResource = "+ JSONObject.toJSONString(saveResource));
System.out.println("=================="+JSONObject.toJSONString(saveResourceResult));
System.out.println("saveResourceResult = "+JSONObject.toJSONString(saveResourceResult));
}
运行结果:
userId = 0x7157a1ad4ee9d2d28fd8136e852b58fc50268851
saveResource =
{
"content":"aaaaaaaaaaaaa",
"grant":"WRITE",
"ownerUid":"0x7157a1ad4ee9d2d28fd8136e852b58fc50268851",
"privateKey":"18186972916645775904471443546379585030211453293047885503044268117592811129792",
"uid":"0x7157a1ad4ee9d2d28fd8136e852b58fc50268851"
}
saveResourceResult =
{
"encryptKey":"04eefadfa7c885186a6039ae0fcbab17e90e5d1392dc4286bffe511db31105a5d29c391ae3c09c80b2ab7067d1b0ed82d0afcfb3207e25f0c03b8ae0c3481905e09f2323a4fe5b3f782c02e67240b76def3c8b1ef61b39acaa59301e79dd9390994332973f34939407fabc2a63e3dcf6f2ca4eed43",
"url":"0x7157a1ad4ee9d2d28fd8136e852b58fc50268851ff6f1754c5a840c189611e5c20b6e1e4"
}
4 获取资源
方法名:
getResource(String did,String privateKey, String url)
方法描述:
访问Hub并读取指定资源。用户可直接访问,第三方需获得用户的授权后才可访问。
解密资源
方法名:
decrypt(String content, String encptyKey, String privateKey)
方法描述:
使用获取资源接口返回的密文密钥对获取资源接口返回的密文资源内容进行解密,得到明文资源内容。
public static void getResourceECDSATest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("[email protected]");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
System.out.println("=================="+ JSONObject.toJSONString(saveResourceResult));
QueryResourceResp queryResourceResp = didClient.getResource(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPrivateKey(), saveResourceResult.getUrl());
System.out.println("=================="+JSONObject.toJSONString(queryResourceResp));
String content2 = didClient.decrypt(queryResourceResp.getContent(), queryResourceResp.getKey(), didDataWrapper.getAuthKeyInfo().getPrivateKey());
System.out.println(content2);
}
运行结果:
==================
{
"encryptKey":"0474abba04cb37b515263264539a2621410310997d6c3414c5535fed488325798017e724dabee2b72cd96ad35c445bb70ef5768ad20b4bc6fc88dc8b8198429f3a3c3e9e17319ce6978fb87a2199022ea7225e75456fe8e572f426552d775d41f0b5b34cf8bfc16c21a0c201b3e150c269205bca67",
"url":"did:bsn:3PLKPTmZEVskiMkuw8WB5Cn8u2xo63005fb3d009458397290afa47b3dd45"
}
==================
{
"content":"88D63B93AFDF68F9766BA0443E2D3C051C889EAD0567C3B5C1AA3D3224288758",
"key":"0474abba04cb37b515263264539a2621410310997d6c3414c5535fed488325798017e724dabee2b72cd96ad35c445bb70ef5768ad20b4bc6fc88dc8b8198429f3a3c3e9e17319ce6978fb87a2199022ea7225e75456fe8e572f426552d775d41f0b5b34cf8bfc16c21a0c201b3e150c269205bca67"}
解密后的内容:
注意这里已经获取了资源,并把密文使用私钥解密。
5 删除资源
方法名:
deleteResource(String did,String privateKey, String url)
方法描述:
资源的Owner可调,对Hub内的某个资源进行删除。
public static void deleteResourceECDSATest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
QueryResourceResp queryResourceResp = didClient.getResource(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPrivateKey(), saveResourceResult.getUrl());
System.out.println("====== before delete ======="+JSONObject.toJSONString(queryResourceResp));
Boolean delete = didClient.deleteResource(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPrivateKey(), saveResourceResult.getUrl());
System.out.println(delete);
queryResourceResp = didClient.getResource(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPrivateKey(), saveResourceResult.getUrl());
System.out.println("====== after delete ======="+JSONObject.toJSONString(queryResourceResp));
}
运行结果: 删除资源后再次访问就出现异常“资源不存在”
====== before delete ======={"content":"A8DD8ACEFB99CA49EF1CC5CDB58AFE70","key":"045da762de1d279d96902c130e9eca4759dde106d9aecb7130c6adad18fe1f181b79f90ac3324c1eaabe505096a2a4351447d2e6b00f9c21ed51732c31bde208d8d365ba2e2aa4efbfebb466f7a33d9af38f2df3485dc2d5bc060299af37d4e6ab02e89145a4ae7be88c3d2e76af95044134ccfda0"}
true
Exception in thread "main" com.reddate.did.sdk.exception.DidException: Resource does not exist
at com.reddate.did.sdk.util.HttpUtils.postCall(HttpUtils.java:75)
at com.reddate.did.sdk.service.HubService.getResource(HubService.java:255)
6 创建权限
方法名: createPermission(CreatePermission createPermission)
方法描述:资源的Owner为第三方创建访问Hub内资源的权限。WRITE表示代存资源、UPDATE表示代更新资源、READ表示读取资源。
一次授权仅能被访问一次,相同uid相同权限的授权在未访问情况下不能再次创建,但是UPDATE和READ权限服务侧对每次的调用都返回相同的结果。
public static void createPermissionECDSATest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
CreatePermission createPermission = new CreatePermission();
createPermission.setUid(didDataWrapper.getDid()); //授权者
createPermission.setUrl(null); //资源存储的Url路径,WRITE权限可为空,READ/UPDATE权限必填
createPermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
createPermission.setGrant(Operation.WRITE); //权限
createPermission.setGrantUid(didDataWrapper2.getDid()); //被授权者
createPermission.setGrantPublicKey(didDataWrapper2.getAuthKeyInfo().getPublicKey()); //被授权者公钥
CreatePermissionResp createPermissionResp = didClient.createPermission(createPermission);
System.out.println("=================="+JSONObject.toJSONString(createPermissionResp));
System.out.println(createPermissionResp.getUrl());
System.out.println(createPermissionResp.getKey());
}
运行结果:
==================
{"key":"Tmy0ZMUlkufWIHj23KpvVHkrUk5ildU8","url":"did:bsn:3cXoJrSd2a73pLFFuREoXahaX28ncec3393f403845239812d65ccb083c42"}
//完整的资源创建、授权、读访问。
public static void getResourceECDSATest3() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
//Did1保存资源
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
System.out.println("=================="+JSONObject.toJSONString(saveResourceResult));
//DID1授权给DID2读权限
CreatePermission createPermission = new CreatePermission();
createPermission.setUid(didDataWrapper.getDid()); //DID1
createPermission.setUrl(saveResourceResult.getUrl()); //资源url
createPermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
createPermission.setGrant(Operation.READ);
createPermission.setGrantUid(didDataWrapper2.getDid()); //DID2
createPermission.setGrantPublicKey(didDataWrapper2.getAuthKeyInfo().getPublicKey());
CreatePermissionResp createPermissionResp = didClient.createPermission(createPermission);
System.out.println("=================="+JSONObject.toJSONString(createPermissionResp));
//DID2 读资源
QueryResourceResp queryResourceResp = didClient.getResource(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPrivateKey(), createPermissionResp.getUrl());
System.out.println("=================="+JSONObject.toJSONString(queryResourceResp));
}
运行结果: DID2获得了资源内容。
=================={"encryptKey":"0487c89ea0d04d59d0fe00edeb31dd07d28d8bdbca56da1d9166f7b8259363eaaf6450f71a2063d1f76bc2616617fbd29ff1935841d5be61946487b811789e7164979932f9fc578aed5324f261de2dbdc833e694e583cd7ad2a4325b4fe81eb31a673010eca36858f428de57ab518c1edb85f0361b","url":"did:bsn:26KxjzgUDcKmMTXP11Hvj556vVTu3a6cb19e8356426a9bea08042d3810f3"}
=================={"key":"S7U62ma2rqFkbr1kIRi78wSvREVfUf7i","url":"did:bsn:26KxjzgUDcKmMTXP11Hvj556vVTu3a6cb19e8356426a9bea08042d3810f3"}
=================={"content":"2C5C3B871483464DB36FCA9A1CB02225","key":"04cc68eafacc1fb906a90c057a903a06057f6db50e71b4d716526a5220d685a841280e525855057475efaf1e1fb75ca816dca289ab42b260f22afcd5f7b34aa3e3eba3c1680a383f7a1d6b54daf7d18f970540f1699db5e00b1f1b4ea22049401961d38f9c0eb2e5ae35e56e791646c9dc414cdf77"}
上述访问后,再次读取资源必然失败,因为授权是一次性的, 访问后就无效了。想要再次访问必须重新授权。
7 删除权限
方法名:
deletePermission(DeletePermission deletePermission)
方法描述:
资源的Owner调用,对第三方还未访问的权限进行删除操作。
public static void deletePermissionECDSA() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
CreatePermission createPermission = new CreatePermission();
createPermission.setUid(didDataWrapper.getDid());
createPermission.setUrl(null);
createPermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
createPermission.setGrant(Operation.WRITE);
createPermission.setGrantUid(didDataWrapper2.getDid());
createPermission.setGrantPublicKey(didDataWrapper2.getAuthKeyInfo().getPublicKey());
CreatePermissionResp createPermissionResp = didClient.createPermission(createPermission);
System.out.println("=================="+JSONObject.toJSONString(createPermissionResp));
DeletePermission deletePermission = new DeletePermission();
deletePermission.setUid(didDataWrapper.getDid());
deletePermission.setUrl(createPermissionResp.getUrl());
deletePermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
deletePermission.setGrant(Operation.WRITE);
deletePermission.setGrantUid(didDataWrapper2.getDid());
DeletePermissionResp deletePermissionResp = didClient.deletePermission(deletePermission);
System.out.println("=================="+JSONObject.toJSONString(deletePermissionResp));
}
运行结果:
=================={"key":"jib3H4CpMe2fYixsHgj2PseKYwLp09JD","url":"did:bsn:j6YA8ezpShYqSubscGUKWmoxTZ37f9e1f5c69b742c8ac49f25576e493eb"}
=================={"succes":true}
8 查询权限
方法名:
queryPermission(QueryPermission queryPermission)
方法描述:
资源的Owner调用,查询已经创建过的权限。
public static void queryPermissionECDSA() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
CreatePermission createPermission = new CreatePermission();
createPermission.setUid(didDataWrapper.getDid());
createPermission.setUrl(null);
createPermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
createPermission.setGrant(Operation.WRITE);
createPermission.setGrantUid(didDataWrapper2.getDid());
createPermission.setGrantPublicKey(didDataWrapper2.getAuthKeyInfo().getPublicKey());
CreatePermissionResp createPermissionResp = didClient.createPermission(createPermission);
System.out.println("=================="+JSONObject.toJSONString(createPermissionResp));
QueryPermission queryPermission = new QueryPermission();
queryPermission.setUid(didDataWrapper.getDid());
queryPermission.setGrantUid(didDataWrapper2.getDid());
queryPermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
List<PermissionInfo> permissionList = didClient.queryPermission(queryPermission);
System.out.println("=================="+JSONObject.toJSONString(permissionList));
queryPermission.setFlag(UsedFlag.NO);
//queryPermission.setFlag(UsedFlag.YES); //查找访问过的权限,默认是NO表示未访问
List<PermissionInfo> permissionList2 = didClient.queryPermission(queryPermission);
System.out.println("=================="+JSONObject.toJSONString(permissionList2));
System.out.println(permissionList.size() > 0);
System.out.println(permissionList2.size() > 0);
}
运行结果:
=================={"key":"IvgCqoNX9CZURTRzq1CU8wXZTzqgpOm7","url":"did:bsn:kUsvPGurqMz9onh2gimVHtBfkmPe268569cae534d60baeecd3e1ff54c01"}
permissionList ==================[
{
"createTime":"2022-10-02T12:59:28.765",
"flag":"NO",
"grant":"WRITE",
"grantUid":"did:bsn:ZP6RFCGVpuNUGrhXLFae4vem1a2",
"key":"046709e356318a2d038eb86bb2878dd177209fb26e0f206755873efca46dd72c7a02a6858ad35f5b8bd25b051b82af50bfc1b0b987684c28fe6d5d248799e1b61deef6ad14df41eacbc5eb08b21c7c5aac67910131323fb721589065a5123396b569c557286a8d37b8a7cd21bcd17a7fe001d17f60",
"status":1,
"uid":"did:bsn:kUsvPGurqMz9onh2gimVHtBfkmP",
"url":"did:bsn:kUsvPGurqMz9onh2gimVHtBfkmPe268569cae534d60baeecd3e1ff54c01"
}
]
==================[{"createTime":"2022-10-02T12:59:28.765","flag":"NO","grant":"WRITE","grantUid":"did:bsn:ZP6RFCGVpuNUGrhXLFae4vem1a2","key":"046709e356318a2d038eb86bb2878dd177209fb26e0f206755873efca46dd72c7a02a6858ad35f5b8bd25b051b82af50bfc1b0b987684c28fe6d5d248799e1b61deef6ad14df41eacbc5eb08b21c7c5aac67910131323fb721589065a5123396b569c557286a8d37b8a7cd21bcd17a7fe001d17f60","status":1,"uid":"did:bsn:kUsvPGurqMz9onh2gimVHtBfkmP","url":"did:bsn:kUsvPGurqMz9onh2gimVHtBfkmPe268569cae534d60baeecd3e1ff54c01"}]
true
true
9 查询被授权记录
方法名:
queryGrantedPermission(QueryGrantedPermission queryPermission)
方法描述:
用户可以从资源所有者的uid、是否已访问和权限类型三个维度去查询所有或部分授权给自己的权限记录。
public static void queryGrantedPermissionTest(){
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
CreatePermission createPermission = new CreatePermission();
createPermission.setUid(didDataWrapper.getDid());
createPermission.setUrl(null);
createPermission.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
createPermission.setGrant(Operation.WRITE);
createPermission.setGrantUid(didDataWrapper2.getDid());
createPermission.setGrantPublicKey(didDataWrapper2.getAuthKeyInfo().getPublicKey());
CreatePermissionResp createPermissionResp = didClient.createPermission(createPermission);
System.out.println("=================="+JSONObject.toJSONString(createPermissionResp));
//查询did2用户的授权记录
QueryGrantedPermission queryPermission = new QueryGrantedPermission();
queryPermission.setUid(registerHubResult2.getUid()); //自己的uid
// queryPermission.setOwnerUid(didDataWrapper.getDid()); //资源所有者的uid
// queryPermission.setGrant(Operation.WRITE);
// queryPermission.setFlag(UsedFlag.NO);
queryPermission.setPrivateKey(didDataWrapper2.getAuthKeyInfo().getPrivateKey());
List<GrantPermissionInfo> permissionList = didClient.queryGrantedPermission(queryPermission);
System.out.println("=================="+JSONObject.toJSONString(permissionList));
}
运行结果:
=================={"key":"nPa1W2kEvWpt8bONBNJF4IaavrkHEVk4","url":"did:bsn:3UdxjTCNMAioVBi1fAf7UwYKzXX5ef7a3ea4f18d417b81b748fa73a0d289"}
==================[
{
"createTime":"2022-10-02T15:34:21.036",
"flag":"NO",
"grant":"WRITE",
"key":"04ed1641ce5471b440cec7209abeb66ee2c2bf730b3e4899d17815c78c348384f369f37a3d493db23227d58bfdb99056b4331722f9d0d20bb9b45b6b5f58f01df902545fd912908dead8e160f4ac04452b308224cc119c708693218437cbc6abd5ccbb6f0e77c60359038b892d23217833e69fc7c3",
"ownerKey":"04f151f6c98ce4ae552d5ad7601659a088ee5c58c2d3ed95fe6e713857c3992b6faa8bb0095dd63e92a22b01add9e8a5495a3c6c5c8cedb3f830b7fb50646867a98d3a3b085aaf259cf38b6b140e57ac9f7adbc70dfff9290d2eddfb51e77879d928cf22f6f53aa7d22bcc836276291027e86ff2ca",
"ownerUid":"did:bsn:3UdxjTCNMAioVBi1fAf7UwYKzXX5",
"status":1,
"url":"did:bsn:3UdxjTCNMAioVBi1fAf7UwYKzXX5ef7a3ea4f18d417b81b748fa73a0d289"
}
]
10 查询资源的操作历史
方法名:
queryResourceHistory(QueryResourceHistory queryResourceHistory)
方法描述:
资源的拥有者查询资源的历史操作记录。
public static void queryResourceHistoryTest(){
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
//创建资源1
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
System.out.println("saveResource ="+JSONObject.toJSONString(saveResource));
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
System.out.println("saveResourceResult ="+JSONObject.toJSONString(saveResourceResult));
//创建资源2
SaveResource saveResource2 = new SaveResource();
saveResource2.setUid(didDataWrapper.getDid());
saveResource2.setContent("bbbbbbbbbbbbb");
saveResource2.setGrant(Operation.WRITE);
saveResource2.setOwnerUid(didDataWrapper.getDid());
saveResource2.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult2 = didClient.saveResource(saveResource2);
System.out.println("saveResourceResult2 ="+JSONObject.toJSONString(saveResourceResult2));
QueryResourceHistory queryResourceHistory = new QueryResourceHistory();
queryResourceHistory.setUid(didDataWrapper.getDid()); //资源拥有者(owner)uid
queryResourceHistory.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey()); //资源拥有者(owner)私钥
// queryResourceHistory.setOperation(Operation.WRITE); //可选项
// queryResourceHistory.setUrl(""); //可选项
List<ResourceHistoryInfo> resourceHistoryList = didClient.queryResourceHistory(queryResourceHistory);
System.out.println("resourceHistoryList ="+JSONObject.toJSONString(resourceHistoryList));
}
运行结果有2条记录:
saveResource ={"content":"aaaaaaaaaaaaa","grant":"WRITE","ownerUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU","privateKey":"54892858907091377312369040445394163275313158320271619686254887101699568251242","uid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU"}
saveResourceResult ={"encryptKey":"04d2d26ce4fe54cc18ded4cce13ca6cb877dcc8d442f3b32279d5a705b55bd02414200e0c39c489d0b56ece3f9066adb2bcf65357316e4657fb3dff857272b2af68394e0fe6674151a2f55fad04527c8f0c5eea465dbb527cd47fa38c91aa49d132cff19faafe4f88179b5a74fa8f956b00ad9aa20","url":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiUf884f5e238ed4823852028c20382a68c"}
saveResourceResult2 ={"encryptKey":"04104d6c0713b87099566d0e083c8dd08c9d24445327b25f3485758973ae28e581a6ee92d60e862a14071251e7da48e5e19f3f5fec736ac6c83796f3e001243164eae419d37ec8a2f4f44611959b2e2db8f270514aec06b100b6cb44812a021315bf741100acfcc3a8413a197338386081a7808869","url":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiUf3f38d8e16b54a5584d7400e8121a32d"}
resourceHistoryList =[
{
"content":"D3E4C8BC18565A9B4B723699147DEDE1",
"key":"04104d6c0713b87099566d0e083c8dd08c9d24445327b25f3485758973ae28e581a6ee92d60e862a14071251e7da48e5e19f3f5fec736ac6c83796f3e001243164eae419d37ec8a2f4f44611959b2e2db8f270514aec06b100b6cb44812a021315bf741100acfcc3a8413a197338386081a7808869",
"operation":"WRITE",
"operationTime":"2022-10-02T16:25:31.421",
"operationUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU",
"ownerUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU",
"url":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiUf3f38d8e16b54a5584d7400e8121a32d"
},
{
"content":"C966B20F206CB43B07D56EF68B7EF34C",
"key":"04d2d26ce4fe54cc18ded4cce13ca6cb877dcc8d442f3b32279d5a705b55bd02414200e0c39c489d0b56ece3f9066adb2bcf65357316e4657fb3dff857272b2af68394e0fe6674151a2f55fad04527c8f0c5eea465dbb527cd47fa38c91aa49d132cff19faafe4f88179b5a74fa8f956b00ad9aa20",
"operation":"WRITE",
"operationTime":"2022-10-02T16:25:30.799",
"operationUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU",
"ownerUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU",
"url":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiUf884f5e238ed4823852028c20382a68c"
}
]
public static void queryResourceHistoryTest2(){
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
QueryResourceHistory queryResourceHistory = new QueryResourceHistory();
queryResourceHistory.setUid("did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU"); //资源拥有者(owner)uid
queryResourceHistory.setPrivateKey("54892858907091377312369040445394163275313158320271619686254887101699568251242"); //资源拥有者(owner)私钥
// queryResourceHistory.setOperation(Operation.WRITE); //可选项
queryResourceHistory.setUrl("did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiUf3f38d8e16b54a5584d7400e8121a32d"); //可选项
List<ResourceHistoryInfo> resourceHistoryList = didClient.queryResourceHistory(queryResourceHistory);
System.out.println("resourceHistoryList ="+JSONObject.toJSONString(resourceHistoryList));
}
指定URL的查询结果只有一条:
resourceHistoryList =[
{
"content":"D3E4C8BC18565A9B4B723699147DEDE1",
"key":"04104d6c0713b87099566d0e083c8dd08c9d24445327b25f3485758973ae28e581a6ee92d60e862a14071251e7da48e5e19f3f5fec736ac6c83796f3e001243164eae419d37ec8a2f4f44611959b2e2db8f270514aec06b100b6cb44812a021315bf741100acfcc3a8413a197338386081a7808869",
"operation":"WRITE",
"operationTime":"2022-10-02T16:25:31.421",
"operationUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU",
"ownerUid":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiU",
"url":"did:bsn:4VY366pCJMqhd3rAKeNV7vv5kCiUf3f38d8e16b54a5584d7400e8121a32d"
}
]
11 变更资源所有者
方法名:
transferOwner(TransferOwner transferOwner)
方法描述:
变更Hub内资源的所有者。
【说明】 转移Owner需要设置新owner的did、公钥。解密的时候要用新owner的私钥。
public static void transferOwnerTest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterHubResult registerHubResult = didClient.registerHub(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
DidDataWrapper didDataWrapper2 = didClient.createDid(true);
RegisterHubResult registerHubResult2 = didClient.registerHub(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPublicKey(),didClient.getHubCryptoType());
//创建资源
SaveResource saveResource = new SaveResource();
saveResource.setUid(didDataWrapper.getDid());
saveResource.setContent("aaaaaaaaaaaaa");
saveResource.setGrant(Operation.WRITE);
saveResource.setOwnerUid(didDataWrapper.getDid());
saveResource.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
SaveResourceResult saveResourceResult = didClient.saveResource(saveResource);
QueryResourceResp queryResourceResp1 = didClient.getResource(didDataWrapper.getDid(), didDataWrapper.getAuthKeyInfo().getPrivateKey(), saveResourceResult.getUrl());
System.out.println("queryResourceResp1 ="+JSONObject.toJSONString(queryResourceResp1));
String content = didClient.decrypt(queryResourceResp1.getContent(), queryResourceResp1.getKey(), didDataWrapper.getAuthKeyInfo().getPrivateKey());
System.out.println("decode content = " + content);
//DID1转移资源给DID2
TransferOwner transferOwner = new TransferOwner();
transferOwner.setUid(registerHubResult.getUid());
transferOwner.setUrl(saveResourceResult.getUrl());
transferOwner.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
transferOwner.setNewOwnerUid(registerHubResult2.getUid());
transferOwner.setNewOwnerPublicKey(didDataWrapper2.getAuthKeyInfo().getPublicKey());
System.out.println("=================="+JSONObject.toJSONString(transferOwner));
Boolean transferResult = didClient.transferOwner(transferOwner);
System.out.println("=================="+transferResult);
QueryResourceResp queryResourceResp2 = didClient.getResource(didDataWrapper2.getDid(), didDataWrapper2.getAuthKeyInfo().getPrivateKey(), saveResourceResult.getUrl());
System.out.println("queryResourceResp2 ="+JSONObject.toJSONString(queryResourceResp2));
String content2 = didClient.decrypt(queryResourceResp2.getContent(), queryResourceResp2.getKey(), didDataWrapper2.getAuthKeyInfo().getPrivateKey());
System.out.println("decode content2 = " + content2);
}
运行结果:
queryResourceResp1 ={"content":"5E2F229D8E7E414B17F1476D1F90F2D7","key":"044932aed26c519da781fb60481a4e74e4651a308a557ebf6fd278e0816ec20a6d7e6b47e7cc3a69b88df303a95e7db8bd98880e63ea2a2a178a1cfcd43d8ac27b64ecf89272bfc316ffb670754791fd0d12f708347285bfb2a1b9807c54a2036040078a475fb7618f2ec7d81be66755cf86214634"}
decode content = aaaaaaaaaaaaa
=================={"newOwnerPublicKey":"8514820004472101050159187430214024036148275200608153042199546095977797108609442638883092531666181667521649434155064678320442016600208612281192316366283925","newOwnerUid":"did:bsn:2CvLTzVg8ctGxBwFKYFXhXCQARcp","privateKey":"43699457679852203339264470008983566507231546687550176568901893900223899427331","uid":"did:bsn:3cghDT5JbLd4gDMuDDUNVrFpF9Dn","url":"did:bsn:3cghDT5JbLd4gDMuDDUNVrFpF9Dn7573431ce19b4f95a1c70c14cd6727d4"}
==================true
queryResourceResp2 ={"content":"5E2F229D8E7E414B17F1476D1F90F2D7","key":"04f354e1ae1852396d158fbc9492d43de3d9626e703b6f6ad9d2083a8a3667ecf3a747846ce2ee12a34b148abd98ce9aee57aaeeeb5962036706371ebc000a7b044bb9cc7face007f10a9c738f80b17923c2cba2b423ad768f8d12774d85c7d86cbe1449567f2355f3fe4bfe2c40395c2fd105b417"}
decode content2 = aaaaaaaaaaaaa