GithubHelp home page GithubHelp logo

bsn-spartan / did Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 793 KB

The DID system consists of three components: SDK, Service and Smart Contract.

License: Other

Solidity 6.62% Java 93.38%
did blockchain bsn spartan

did's Introduction

Requirements

Latest version of Java 1.8 (minor version > 200)

Build from source code

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

mvn clean install -Dmaven.test.skip=true

Project integration SDK

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

       <dependency>
           <groupId>com.reddate</groupId>
           <artifactId>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.83</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>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>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13.2</version>
           <scope>test</scope>
        </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>org.bitcoinj</groupId>
           <artifactId>bitcoinj-core</artifactId>
           <version>0.15.10</version>
        </dependency>
        <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13.2</version>
           <scope>test</scope>
        </dependency>
        <dependency>
           <groupId>com.google.guava</groupId>
           <artifactId>guava</artifactId>
           <version>29.0-jre</version>
           <scope>compile</scope>
        </dependency>

How to use

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 signature

The signature of DID, uses the Secp256k1 algorithm.

/**
 * @param cryptoType Enumeration value of the encryption algorithm
 * @param message Summary data to be signed
 * @param privateKey The private key
 */
public static String sign(CryptoType cryptoType, String message, String privateKey) {

    try {
        String publicKey = getPublicKey(cryptoType, privateKey);
        Sign.SignatureData signatureData = sign(message.getBytes(), new ECKeyPair(new BigInteger(privateKey), new BigInteger(publicKey)));
        return secp256k1SigBase64Serialization(signatureData);

        } catch (Exception e) {
        e.printStackTrace();
        throw new HubException(ErrorMessage.SIGNATURE_FAILED.getCode(), ErrorMessage.SIGNATURE_FAILED.getMessage());
    }
}

Default global configuration

All request methods have the following global configuration:

public class DidClientTestBase {

    public DidClient getDidClient() {

        String url = "https://didservice.bsn.foundation";
        DidClient didClient = new DidClient(url);
        
        return didClient;
    }
}

RequestBody requestBody = RequestBody.create(JSONObject.toJSONString(requestParam), JSON);
Request request = new Request.Builder()
        .url(url)
        .post(requestBody)
        .header("Connection", "close").build();

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

did's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

furyahub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.