# did-sdk
**Repository Path**: yyc0507/did-sdk
## Basic Information
- **Project Name**: did-sdk
- **Description**: 同步github项目
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-12-01
- **Last Updated**: 2022-12-01
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
#### 中文版本 | [English Version](README.md)
### 一、说明
BSN DID 是平台方的链上身份凭证标识,关联平台方的业务开通凭证、链账户以及其下终端用户的链账户,是平台方开展 DDC 应用和管理业务的基础标识。SDK 内包含注册 DID、更新密钥、验证 DID 三个方法,平台方只需注册一次 DID 即可,所以需妥善保存和备份好 BSN DID 的控制私钥,如私钥丢失或泄漏,通过更新密钥方法重新生成 BSN DID 的控制私钥。
### 二、要求
Java 1.8 最新版本(小版本大于200)
### 三、从源码构建
构建过程需跳过单元测试,命令:
```
mvn clean install -Dmaven.test.skip=true
```
### 四、 项目集成SDK
项目集成SDK, 请添加其它依赖的 JAR 包
```
com.reddate
ddc.did.sdk
${version}
system
${basedir}/lib/did-sdk-${version}.jar
org.apache.directory.studio
org.apache.commons.codec
1.8
org.apache.commons
commons-lang3
3.5
com.alibaba
fastjson
1.2.79
org.bouncycastle
bcprov-jdk15on
1.68
cn.hutool
hutool-all
5.6.3
org.fisco-bcos.java-sdk
java-sdk
2.7.0
org.fisco-bcos
web3sdk
2.6.4
com.squareup.okhttp3
okhttp
4.9.0
com.squareup.okhttp3
logging-interceptor
4.9.0
org.web3j
core
4.8.4
org.web3j
crypto
4.8.4
junit
junit
3.8.1
test
```
### 五、用法
DidClientTest.java
```java
package com.reddate.did;
import org.junit.Test;
import com.alibaba.fastjson.JSONObject;
import com.reddate.did.sdk.DidClient;
import com.reddate.did.sdk.param.req.DidSign;
import com.reddate.did.sdk.param.req.ResetDidAuth;
import com.reddate.did.sdk.param.resp.DidDataWrapper;
import com.reddate.did.sdk.protocol.common.KeyPair;
import com.reddate.did.sdk.util.ECDSAUtils;
import static org.junit.Assert.*;
public class DidClientTest {
private DidClient getDidClient() {
DidClient didClient = new DidClient();
return didClient;
}
@Test
public void generateDidtest() {
DidClient didClient = this.getDidClient();
DidDataWrapper didDataWrapper = didClient.createDid();
assertNotNull(didDataWrapper);
assertNotNull(didDataWrapper.getDid());
assertNotNull(didDataWrapper.getDocument());
assertNotNull(didDataWrapper.getAuthKeyInfo());
assertNotNull(didDataWrapper.getRecyKeyInfo());
}
@Test
public void resetDidAuthTest() throws InterruptedException {
DidClient didClient = this.getDidClient();
DidDataWrapper didDataWrapper = didClient.createDid();
ResetDidAuth restDidAuth = new ResetDidAuth();
restDidAuth.setDid(didDataWrapper.getDid());
KeyPair resetDidAuthKey = new KeyPair();
resetDidAuthKey.setPrivateKey(didDataWrapper.getRecyKeyInfo().getPrivateKey());
resetDidAuthKey.setPublicKey(didDataWrapper.getRecyKeyInfo().getPublicKey());
resetDidAuthKey.setType(didDataWrapper.getRecyKeyInfo().getType());
restDidAuth.setRecoveryKey(resetDidAuthKey);
Thread.currentThread().sleep(2000);
KeyPair newKeyPair = didClient.resetDidAuth(restDidAuth);
assertNotNull(newKeyPair);
assertNotNull(newKeyPair.getPrivateKey());
assertNotNull(newKeyPair.getPublicKey());
assertNotNull(newKeyPair.getType());
}
@Test
public void resetDidAuthTest2() throws Exception {
DidClient didClient = this.getDidClient();
DidDataWrapper didDataWrapper = didClient.createDid();
ResetDidAuth restDidAuth = new ResetDidAuth();
restDidAuth.setDid(didDataWrapper.getDid());
restDidAuth.setPrimaryKeyPair(ECDSAUtils.createKey());
KeyPair resetDidAuthKey = new KeyPair();
resetDidAuthKey.setPrivateKey(didDataWrapper.getRecyKeyInfo().getPrivateKey());
resetDidAuthKey.setPublicKey(didDataWrapper.getRecyKeyInfo().getPublicKey());
resetDidAuthKey.setType(didDataWrapper.getRecyKeyInfo().getType());
restDidAuth.setRecoveryKey(resetDidAuthKey);
Thread.currentThread().sleep(2000);
KeyPair newKeyPair = didClient.resetDidAuth(restDidAuth);
assertNotNull(newKeyPair);
assertNotNull(newKeyPair.getPrivateKey());
assertNotNull(newKeyPair.getPublicKey());
assertNotNull(newKeyPair.getType());
}
@Test
public void verifyDIdSign() throws Exception {
DidClient didClient = this.getDidClient();
DidDataWrapper didDataWrapper = didClient.createDid();
DidSign didSign = new DidSign();
didSign.setDid(didDataWrapper.getDid());
didSign.setDidSign(didDataWrapper.getDidSign());
Boolean verify = didClient.verifyDIdSign(didSign);
assertTrue(verify);
}
}
```
### 六、DID签名
DID标识符签名,默认签名使用了Secp256k1算法。
```java
ResultData createDoc = createDidDocument();
if(!createDoc.isSuccess()) {
return ResultData.error(createDoc.getCode(),createDoc.getMsg(), DidDataWrapper.class);
}
logger.debug("create did information is :"+JSONObject.toJSONString(createDoc));
String didSign = null;
try {
didSign = ECDSAUtils.sign(createDoc.getData().getDid(), createDoc.getData().getAuthKeyInfo().getPrivateKey());
} catch (Exception e) {
e.printStackTrace();
throw new DidException(ErrorMessage.GENERATE_DID_FAIL.getCode(),ErrorMessage.GENERATE_DID_FAIL.getMessage());
}
```
### 七、默认全局配置
所有请求方法都有如下的全局配置。
```java
private static final String DID_SERVICE_URL = "https://didservice.bsngate.com:18602";
private static final String DID_SERVICE_PROJECT_ID = "8320935187";
private static final String DID_SERVICE_TOKEN = "3wxYHXwAm57grc9JUr2zrPHt9HC";
public DidClient() {
didService = new DidService(DID_SERVICE_URL, DID_SERVICE_PROJECT_ID, DID_SERVICE_TOKEN);
}
RequestBody requestBody = RequestBody.create(JSONObject.toJSONString(requestParam), JSON);
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.addHeader("token", token)
.addHeader("projectId", requestParam.getProjectId())
.build();
```
### 八、配置超时时间
配置超时时间,默认的连接超时时间是20秒,默认的读超时时间是60秒。
```java
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
```