/did-sdk

The decentralized identity of Platform Owners when accessing the BSN-DDC Network.

Primary LanguageJava

English Version | 中文版本

1. Description

BSN DID is Platform Owner's on-chain identity, which is associated with Platform Owner's DDC Service Certificate, chain accounts and the chain accounts of its end users. It is also Platform Owner's basic identity for DDC application and business management. In case the private key is lost or stolen, a new private key of BSN DID can be regenerated by the key update function.

2. Requirements

Latest version of Java 1.8 (minor version > 200)

3. Build from source code

The build process needs to skip the unit test, command:

    mvn clean install -Dmaven.test.skip=true

4. Project integration SDK

Project integration SDK, please add other JAR files with dependencies:

	<dependency>
            <groupId>com.reddate</groupId>
            <artifactId>ddc.did.sdk</artifactId>
            <version>${version}</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/did-sdk-${version}.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.codec</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.79</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.68</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.fisco-bcos.java-sdk</groupId>
            <artifactId>java-sdk</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.fisco-bcos</groupId>
            <artifactId>web3sdk</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>4.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.8.4</version>
        </dependency>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>crypto</artifactId>
            <version>4.8.4</version>
        </dependency>
        <dependency>
      	    <groupId>junit</groupId>
      	    <artifactId>junit</artifactId>
      	    <version>3.8.1</version>
      	    <scope>test</scope>
        </dependency>

5. How to use

DidClientTest.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);
	} 
	
}

6. DID signature

The signature of DID, uses the Secp256k1 algorithm.

ResultData<CreateDidData> 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());
}

7. Default global configuration

All request methods have the following global configuration:

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();

8. Configure timeout

Configure timeout. The default connection timeout is 20 seconds, and the default read timeout is 60 seconds.

OkHttpClient client = new OkHttpClient.Builder()
		.connectTimeout(20, TimeUnit.SECONDS)
		.readTimeout(60, TimeUnit.SECONDS)
		.build();