BSN-DID 研究 -- 主题二:发证方函数
- 2024-04-07 浙江
本文字数:7794 字
阅读完需:约 26 分钟
本文开始对帮助手册的 API 进行调用,分析具体的输入参数和返回结果。
主题二 :注册发证方、查询发证方、注册凭证模板、查询凭证模板列表、查询凭证模板、更新凭证模板,主要是帮助手册的 14.4.2 发证方。
01
注册发证方
方法名:registerAuthIssuer (RegisterAuthorityIssuer register)
方法描述:DID 用户申请成为发证方角色,注册成功则发证方信息上链。
【说明】 参数中包含了一个私钥,就是 DID 的主私钥。该私钥不能丢失, 以后发证时要用这个私钥签名的。
这个私钥并不会传送给服务端,只是在函数内计算公钥签名,对请求数据进行签名的。以后相关的函数中也会要求传入私钥进行计算。
public static void registerAuthIssuerTest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterAuthorityIssuer register = new RegisterAuthorityIssuer();
register.setDid(didDataWrapper.getDid());
register.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
register.setName("MetaPorts company");
System.out.println("registerAuthIssuerTest() register = "+JSONArray.toJSONString(register));
boolean registerresult = didClient.registerAuthIssuer(register);
System.out.println("registerAuthIssuerTest() registerresult = "+registerresult);
}
运行结果:
registerAuthIssuerTest() register =
{"did":"did:bsn:4GpA5R4hvix2cLxUDPjLsuLZFD2t","name":"MetaPorts company","privateKey":"19735796550396724256944876838556861168110780296787174707640481151280248118014"}
registerAuthIssuerTest() registerresult = true
02
查询发证方
方法名:queryAuthIssuerList (AuthIssuerList query)
方法描述:通过 DID 标识符查询是否是发证方,以及通过名称可识别出可签发哪类凭证。
【说明】输入参数 did,如果指定为具体的 did 字符串,查询结果就是一条。
page,指定第几页。size:指定每页多少条记录。
目前来看, 发证方的测试数据大约 400 条。允许每页 50 条记录,条数再多的话(size=100)就报告字符串溢出了。
public static void queryAuthIssuerListTetst() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterAuthorityIssuer register = new RegisterAuthorityIssuer();
register.setDid(didDataWrapper.getDid());
register.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
register.setName("xxxx company");
boolean registerresult = didClient.registerAuthIssuer(register);
AuthIssuer queryIssuer = new AuthIssuer();
queryIssuer.setPage(1);
queryIssuer.setSize(10);
// queryIssuer.setDid(didDataWrapper.getDid());
queryIssuer.setDid("");
Pages<AuthorityIssuer> issuerList = didClient.queryAuthIssuerList(queryIssuer);
List<AuthorityIssuer> result = issuerList.getResult();
// AuthorityIssuer issuer = result.get(0); //这种做法是不行的,必须用下面的写法才能解析出具体对象
System.out.println("=================="+JSONArray.toJSONString(result));
String ret = JSONArray.toJSONString(result);
JSONArray jarray = JSONArray.parseArray(ret);
for(int i=0;i<jarray.size();i++){
System.out.println(jarray.get(i));
JSONObject jobj = jarray.getJSONObject(i);
System.out.println("name : "+ jobj.get("name"));
System.out.println("did : "+ jobj.get("did"));
}
}
指定
did="did":"did:bsn:21UKCBwnCgaYYKpEG5Jd2DDzyR99",运行结果:
==================[{"name":"xxxx company","did":"did:bsn:21UKCBwnCgaYYKpEG5Jd2DDzyR99"}]
{"name":"xxxx company","did":"did:bsn:21UKCBwnCgaYYKpEG5Jd2DDzyR99"}
name : xxxx company
did : did:bsn:21UKCBwnCgaYYKpEG5Jd2DDzyR99
指定 did="", 运行结果:
==================[{"name":"testname","did":"did:bsn:uWEju29UN5kce13iRrwa7Zk8HPJ"},{"name":"xxxx company","did":"did:bsn:Nd9gpybVfCB3LnnFRitGNNXyjrL"},{"name":"xxxx company","did":"did:bsn:aNs5oYjCytvAU1mTWfhk731FS29"},{"name":"xxxx company","did":"did:bsn:4CzoqqbeGir2RbiMSxhudjwXXD8d"},{"name":"xxxx company","did":"did:bsn:3LCSy1v9WDFhG2GZjQ4iniscAHiz"},{"name":"xxxx company","did":"did:bsn:4TwbnUN2CbWVwHdBaR98shg2GxDV"},{"name":"xxxx company","did":"did:bsn:3SCN2AUmh3pJVuwvTDb3Coy6koxa"},{"name":"xxxx company","did":"did:bsn:4RxVPyL6ae2bH6TYECzfmVhPfoVH"},{"name":"xxxx company","did":"did:bsn:4KiRgTu36MxpxmMi7BiD8zXZq9Uv"},{"name":"xxxx company","did":"did:bsn:4Z9qpPZeSWiikvyMZksEnKPx97Pk"}]
{"name":"testname","did":"did:bsn:uWEju29UN5kce13iRrwa7Zk8HPJ"}
name : testname
did : did:bsn:uWEju29UN5kce13iRrwa7Zk8HPJ
{"name":"xxxx company","did":"did:bsn:Nd9gpybVfCB3LnnFRitGNNXyjrL"}
name : xxxx company
did : did:bsn:Nd9gpybVfCB3LnnFRitGNNXyjrL
{"name":"xxxx company","did":"did:bsn:aNs5oYjCytvAU1mTWfhk731FS29"}
name : xxxx company
did : did:bsn:aNs5oYjCytvAU1mTWfhk731FS29
{"name":"xxxx company","did":"did:bsn:4CzoqqbeGir2RbiMSxhudjwXXD8d"}
name : xxxx company
did : did:bsn:4CzoqqbeGir2RbiMSxhudjwXXD8d
{"name":"xxxx company","did":"did:bsn:3LCSy1v9WDFhG2GZjQ4iniscAHiz"}
name : xxxx company
did : did:bsn:3LCSy1v9WDFhG2GZjQ4iniscAHiz
{"name":"xxxx company","did":"did:bsn:4TwbnUN2CbWVwHdBaR98shg2GxDV"}
name : xxxx company
did : did:bsn:4TwbnUN2CbWVwHdBaR98shg2GxDV
{"name":"xxxx company","did":"did:bsn:3SCN2AUmh3pJVuwvTDb3Coy6koxa"}
name : xxxx company
did : did:bsn:3SCN2AUmh3pJVuwvTDb3Coy6koxa
{"name":"xxxx company","did":"did:bsn:4RxVPyL6ae2bH6TYECzfmVhPfoVH"}
name : xxxx company
did : did:bsn:4RxVPyL6ae2bH6TYECzfmVhPfoVH
{"name":"xxxx company","did":"did:bsn:4KiRgTu36MxpxmMi7BiD8zXZq9Uv"}
name : xxxx company
did : did:bsn:4KiRgTu36MxpxmMi7BiD8zXZq9Uv
{"name":"xxxx company","did":"did:bsn:4Z9qpPZeSWiikvyMZksEnKPx97Pk"}
name : xxxx company
did : did:bsn:4Z9qpPZeSWiikvyMZksEnKPx97Pk
03
注册凭证模板
方法名:registerCpt (RegisterCpt registerCpt)
方法描述:发证方自定义凭证模板,可以约定哪些属性值申请人要必须提供的。比如高校毕业证的模板,可以约定“姓名”、“学号”为必须填写的信息。
【说明】注册模板时无需指定 cptId,等到注册成功返回 cptId。
public static void registerCptTest() {
DidDataWrapper didDataWrapper = didClient.createDid(true);
RegisterAuthorityIssuer register = new RegisterAuthorityIssuer();
register.setDid(didDataWrapper.getDid());
register.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
register.setName("xxxx company");
boolean registerresult = didClient.registerAuthIssuer(register);
RegisterCpt registerCpt = new RegisterCpt();
registerCpt.setDid(didDataWrapper.getDid());
registerCpt.setTitle("test cpt template 01");
registerCpt.setDescription("test cpt tempalte 01 long descriptuion");
registerCpt.setType("Proof");
registerCpt.setPrivateKey(didDataWrapper.getAuthKeyInfo().getPrivateKey());
Map<String, JsonSchema> cptJsonSchemas = new HashMap<>();
JsonSchema jsonSchema1 = new JsonSchema();
jsonSchema1.setDescription("user name");
jsonSchema1.setRequired(true);
jsonSchema1.setType("String");
cptJsonSchemas.put("name", jsonSchema1);
JsonSchema jsonSchema2 = new JsonSchema();
jsonSchema2.setDescription("user sex");
jsonSchema2.setRequired(false);
jsonSchema2.setType("String");
cptJsonSchemas.put("sex", jsonSchema2);
JsonSchema jsonSchema3 = new JsonSchema();
jsonSchema3.setDescription("user age");
jsonSchema3.setRequired(true);
jsonSchema3.setType("Number");
cptJsonSchemas.put("age", jsonSchema3);
registerCpt.setCptJsonSchema(cptJsonSchemas);
System.out.println("registerCptTest() registerCpt = "+JSONArray.toJSONString(registerCpt));
CptBaseInfo cptBaseInfo = didClient.registerCpt(registerCpt);
System.out.println("registerCptTest() cptBaseInfo = "+JSONArray.toJSONString(cptBaseInfo));
}
运行结果:
registerCptTest() registerCpt =
{
"cptJsonSchema":{
"sex":{
"description":"user sex",
"required":false,
"type":"String"
},
"name":{
"description":"user name",
"required":true,
"type":"String"
},
"age":{
"description":"user age",
"required":true,
"type":"Number"
}
},
"description":"test cpt tempalte 01 long descriptuion",
"did":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr",
"privateKey":"23084912555970257285541363210643018905213545822808257650593849408195914596083",
"title":"test cpt template 01",
"type":"Proof"
}
registerCptTest() cptBaseInfo = {"cptId":732210021407470105,"cptVersion":1}
04
查询凭证模板列表
方法名: queryCptListByDid(QueryCpt query)
方法描述:任何人都可通过 DID 标识符查询其所有的凭证模板。同一个个人/组织有可能会注册多个凭证模板。比如高校除了毕业证模板外,可能还有学位证模板、肄业证模板等。
public static void queryCptListByDidTest() {
QueryCpt query = new QueryCpt();
query.setDid("did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr"); //查指定DID的所有证书模板, 必填字段,不能为null
query.setPage(1);
query.setSize(10);
try {
Pages<CptInfo> pages = didClient.queryCptListByDid(query);
System.out.println("queryCptListByDidTest() size = "+pages.getResult().size());
//不能这样写代码,必须用JSONObject
// for(CptInfo cpt : pages.getResult()){
// System.out.println(cpt.getCptId() + ", "+cpt.getTitle());
// }
System.out.println("=================="+JSONArray.toJSONString(pages.getResult()));
String ret = JSONArray.toJSONString(pages.getResult());
JSONArray jarray = JSONArray.parseArray(ret);
for(int i=0;i<jarray.size();i++){
System.out.println(jarray.get(i));
JSONObject jobj = jarray.getJSONObject(i);
}
}catch (Exception e){
System.out.println("query cptList failed:query cpt list by did failed:server process failed\n "+e.getMessage());
}
System.out.println("==================");
}
运行结果:
queryCptListByDidTest() size = 1
==================
[
{
"cptJsonSchema":{
"sex":{
"description":"user sex",
"type":"String",
"required":false
},
"name":{
"description":"user name",
"type":"String",
"required":true
},
"age":{
"description":"user age",
"type":"Number",
"required":true
}
},
"cptId":732210021407470105,
"description":"test cpt tempalte 01 long descriptuion",
"create":"2022-10-02 14:07:47",
"update":"2022-10-02 14:07:47",
"proof":{
"creator":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr",
"type":"Secp256k1",
"signatureValue":"FQGx87YB9jTW/p8cNXdhGYL3G51ULZ2xJD50hEmq4BEwg3uX+Bw66Y1MOykKNAFnmOnrHkCpxA1PkU3dpBTg/wE="
},
"cptVersion":1,
"title":"test cpt template 01",
"publisherDid":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr"
}
]
{"cptJsonSchema":{"sex":{"description":"user sex","type":"String","required":false},"name":{"description":"user name","type":"String","required":true},"age":{"description":"user age","type":"Number","required":true}},"cptId":732210021407470105,"description":"test cpt tempalte 01 long descriptuion","create":"2022-10-02 14:07:47","update":"2022-10-02 14:07:47","proof":{"creator":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr","type":"Secp256k1","signatureValue":"FQGx87YB9jTW/p8cNXdhGYL3G51ULZ2xJD50hEmq4BEwg3uX+Bw66Y1MOykKNAFnmOnrHkCpxA1PkU3dpBTg/wE="},"cptVersion":1,"title":"test cpt template 01","publisherDid":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr"}
05
查询凭证模板
方法名:queryCptById(Long cptId)
方法描述:通过某个具体的凭证模板 ID 查询其内容。
【说明】cptId 是个 Long 类型数字
public static void queryCptByIdTest() {
CptInfo cptInfo = didClient.queryCptById(732210021407470105L);
System.out.println(JSONArray.toJSONString(cptInfo));
}
运行结果:
{
"cptId":732210021407470105,
"cptJsonSchema":{
"sex":{
"description":"user sex",
"required":false,
"type":"String"
},
"name":{
"description":"user name",
"required":true,
"type":"String"
},
"age":{
"description":"user age",
"required":true,
"type":"Number"
}
},
"cptVersion":1,
"create":"2022-10-02 14:07:47",
"description":"test cpt tempalte 01 long descriptuion",
"proof":{
"creator":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr",
"signatureValue":"FQGx87YB9jTW/p8cNXdhGYL3G51ULZ2xJD50hEmq4BEwg3uX+Bw66Y1MOykKNAFnmOnrHkCpxA1PkU3dpBTg/wE=",
"type":"Secp256k1"
},
"publisherDid":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr",
"title":"test cpt template 01",
"update":"2022-10-02 14:07:47"
}
06
更新凭证模板
方法名: updateCpt (RegisterCpt registerCpt)
方法描述:发证方对自己已注册过的凭证模板内容进行更新。凭证模板更新不影响已签发的凭证。
【说明】 第一次注册凭证模板时, 模板的版本号为 1。以后每次更新模板,版本号+1。
更新模板不会改变模板编号 cptId。
public static void updateCptTest(){
//这是原来模板内容
RegisterCpt registerCpt = new RegisterCpt();
registerCpt.setDid("did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr");
registerCpt.setTitle("test cpt template 01");
registerCpt.setDescription("test cpt tempalte 01 long descriptuion");
registerCpt.setType("Proof");
registerCpt.setPrivateKey("23084912555970257285541363210643018905213545822808257650593849408195914596083");
Map<String, JsonSchema> cptJsonSchemas = new HashMap<>();
JsonSchema jsonSchema1 = new JsonSchema();
jsonSchema1.setDescription("user name");
jsonSchema1.setRequired(true);
jsonSchema1.setType("String");
cptJsonSchemas.put("name", jsonSchema1);
JsonSchema jsonSchema2 = new JsonSchema();
jsonSchema2.setDescription("user sex");
jsonSchema2.setRequired(false);
jsonSchema2.setType("String");
cptJsonSchemas.put("sex", jsonSchema2);
JsonSchema jsonSchema3 = new JsonSchema();
jsonSchema3.setDescription("user age");
jsonSchema3.setRequired(true);
jsonSchema3.setType("Number");
cptJsonSchemas.put("age", jsonSchema3);
registerCpt.setCptJsonSchema(cptJsonSchemas);
//新增地址字段
JsonSchema jsonSchema4 = new JsonSchema();
jsonSchema4.setDescription("user address");
jsonSchema4.setRequired(true);
jsonSchema4.setType("String");
cptJsonSchemas.put("address", jsonSchema4);
registerCpt.setCptId(732210021407470105L);
CptBaseInfo cptBaseInfo2 = didClient.updateCpt(registerCpt);
System.out.println(JSONArray.toJSONString(cptBaseInfo2));
CptInfo cptInfo = didClient.queryCptById(cptBaseInfo2.getCptId());
System.out.println(JSONArray.toJSONString(cptInfo));
}
运行结果:
{"cptId":732210021407470105,"cptVersion":2}
{"cptId":732210021407470105,"cptJsonSchema":{"address":{"description":"user address","required":true,"type":"String"},"sex":
{"description":"user sex","required":false,"type":"String"},"name":{"description":"user name","required":true,"type":"String"},"age":
{"description":"user age","required":true,"type":"Number"}},"cptVersion":2,"create":"2022-10-02 14:07:47","description":"test cpt tempalte 01 long descriptuion","proof":
{"creator":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr","signatureValue":"zMbSjUkua3qdO7DeIETpKQEXNDHs+zmHv7c8+7RMVkoB2LRjwpcqVvQDFJ03W824pDXchfXcE/u9g11EB1LHyQE=","type":"Secp256k1"},"publisherDid":"did:bsn:fEsNK8gjTFCBEgtXfvR2avPgJzr","title":"test cpt template 01","update":"2022-10-02 14:38:25"}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 原文链接:
https://blog.csdn.net/u012084827/article/details/127175225
文章原标题:《BSN-DID 研究--主题 2:发证方函数》
旨在传播区块链相关技术,如有侵权请与我们联系删除。
BSN研习社
还未添加个人签名 2021-11-05 加入
还未添加个人简介
评论