Skip to content

caict-4iot-dev/bop-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bop-java-sdk

[bop] sdk to develop with Xinghuo BIF Open Platform

What

The current package is primarily designed to implement interfaces for the Xinghuo Open Platform. Up to now, it has already realized two major modules, namely, the [Basic Service API] and the [Message Subscription Service]. Through these modules, direct communication with the Xinghuo Open Platform is feasible. In addition, relevant protocol components are handled using Protocol Buffers (protobuf).

Installation

First, you need to declare the two libraries (i.e., bop-http and bop-websocket) that your Java project depens on by providing in the Maven pom.xml file:

<dependency>
    <groupId>cn.bitfactory</groupId>
    <artifactId>bop-java-sdk</artifactId>
    <version>1.0.0</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>cn.bitfactory</groupId>
    <artifactId>bop-java-subscription</artifactId>
    <version>1.0.0</version>
    <scope>compile</scope>
</dependency>

Use example

https service

import cn.bitfactory.core.utils.JsonUtils;
import api.cn.bitfactory.bop.BOPSDK;
import request.model.cn.bitfactory.bop.BOPAccountGetInfoRequest;
import response.model.cn.bitfactory.bop.BOPAccountGetInfoResponse;
import org.testng.Assert;

public class BopWsDemoTest {
    public static void testGetAccount() {
        BOPSDK sdk = BOPSDK.getInstance("https://bif-testnet.bitfactory.cn", "xxx",
                "xxxx");
        BOPAccountGetInfoRequest bifAccountGetInfoRequest = new BOPAccountGetInfoRequest();
        bifAccountGetInfoRequest.setAddress("did:bid:efUGVkkJ746m4iCKgSpECXcni4v1cUaQ");

        BOPAccountGetInfoResponse bopAccountGetInfoResponse =
                sdk.getBOPAccountService().GetAccount(bifAccountGetInfoRequest);
        System.out.println(JsonUtils.toJSONString(bopAccountGetInfoResponse));
        Assert.assertEquals(bopAccountGetInfoResponse.getErrorCode(), 0);
        Assert.assertEquals(bopAccountGetInfoResponse.getErrorDesc(), "ok");
    }

    public static void main(String[] args) {
        testGetAccount();
    }
}

ws service

import cn.bif.common.JsonUtils;
import common.cn.bitfactory.bop.LedgerHeader;
import common.cn.bitfactory.bop.TxSubscribeMsg;
import websocket.cn.bitfactory.bop.BopWsAdapter;


import java.io.IOException;

public class BopWsDemoTest {

    String webSocketUrl = "ws://domestic-testnet.bitfactory.cn/bif/subscribe";

    private BopWsAdapter chain_message_one_;

    public static void main(String[] argv) throws IOException {
        BopWsDemoTest chainTest = new BopWsDemoTest();
        chainTest.Initialize();        
    }

    public void Stop() {
        chain_message_one_.Stop();
    }
    
    public void Initialize() throws IOException {
        chain_message_one_ = new BopWsAdapter(webSocketUrl);
        chain_message_one_.AddChainResponseMethod(TxSubscribeMsg.MessageType.HEADER.getCode(), msg -> {
            System.out.println("got block header: " + JsonUtils.toJSONString(msg.getMessage()));
            LedgerHeader ledgerHeader = JsonUtils.toJavaObject(msg.getMessage(), LedgerHeader.class);
            System.out.println("seq :" + ledgerHeader.getSeq());
        });
        
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //subscribe block header
        TxSubscribeMsg<String> headerReq = new TxSubscribeMsg<>();
        headerReq.setType(TxSubscribeMsg.MessageType.HEADER.getCode());
        if (!chain_message_one_.Send(JsonUtils.toJSONString(headerReq))) {
            System.out.println("failed send subscribe header");
        }
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages