全部产品

客户端API

更新时间:2020-09-08 10:51:32

账本操作API

API

描述

statLedger

查询账本当前状态及相关统计数据

成员操作API

API

描述

updateMemberKey

更新账本成员公钥

getMember

查询账本成员

listMembers

批量查询账本成员

enableMember

激活账本成员

disableMember

禁用账本成员

记录(数据)操作API

API

描述

appendTransaction

新增记录

getTransaction

读取记录

existTransaction

查询记录是否存在

listTransactions

批量获取历史记录

getBlockInfo

获取区块信息

listTimeAnchors

批量查询可信时间戳

getLastTimeAnchor

查询最新的可信时间戳

getProof

获取记录证明

秘钥生成

API

描述

ECCK1KeyPair 秘钥对生成

秘钥对类,包含构造器和静态生成秘钥方法

客户端开源地址

https://github.com/aliyun/alibabacloud-ledgerdb-java-client

客户端构建及使用

Step1: build and install

$ git clone https://github.com/aliyun/alibabacloud-ledgerdb-java-client.git
$ cd alibabacloud-ledgerdb-java-client
$ mvn clean install -DskipTests

Step2: 在完成Step1后,在您的项目中添加依赖

<dependency>
    <groupId>com.antfin.ledgerdb</groupId>
    <artifactId>ledger-client</artifactId>
    <version>0.8.1</version>
</dependency>

客户端API Demo

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.AppendTransactionResponse;
import com.antfin.ledgerdb.client.common.GetTransactionResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.Keys;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;

public class LedgerStart{

  public static void main(String[] args) throws Exception {
    String ledgerId = "******";
    String memberId = "******";

    // VPC Endpoint可通过LedgerDB控制台提供的VPC配置功能获取
    String vpcEndpoint = "127.0.0.1";

    // 目前端口固定为10077
    int port = 10077;

    // ledgerUri参数的格式为 ledger://{VPC Endpoint}:10077/{ledgerID}
    String ledgerUri =
        "ledger://" + vpcEndpoint + ":" + port + "/" + ledgerId;
    LedgerDBLightClient client =
        new LedgerDBLightClient(vpcEndpoint, port);

    ECCK1KeyPair key = Keys.createEcKeyPair();
    byte[] publicKeyInBytes = key.getPublicKey();
    // 请将公钥通过LedgerDB控制台提供的密钥管理功能上传,用于后续对写入数据的签名校验

    SignerProfile profile = new SignerProfile(memberId, Sender.SenderType.REGULAR, key);
    OperationControl op = new OperationControl();
    op.addSignerProfile(profile);

    AppendTransactionResponse res =
        client.appendTransaction(ledgerUri, "hello ledger test".getBytes(StandardCharsets.US_ASCII), op);
    System.out.println(Hex.toHexString(res.getTxHash()));

    long sequence = res.getTotalSequence();
    System.out.println(sequence);

    OperationControl ocGet = new OperationControl();
    ocGet.addSignerProfile(profile);
    GetTransactionResponse getRes = client.getTransaction(ledgerUri, sequence, ocGet);
    byte[] payload =
        getRes.getPayload().getTx().getRequest().getCustomPayload().toByteArray();
    System.out.println("Content: " + new String(payload, StandardCharsets.US_ASCII));
  }
}

账本操作API

statLedger

方法定义

public StatLedgerResponse statLedger(String ledgerUri,OperationControl opControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.common.StatLedgerResponse;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class StatLedgerDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    StatLedgerResponse response = client.statLedger(ledgerUri, op);
  }
}

成员操作API

updateMemberKey

方法定义

public UpdateMemberKeyResponse updateMemberKey(
          String ledgerUri,
          String memberId,
          byte[] publicKey,
          OperationControl operationControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.common.UpdateMemberKeyResponse;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class UpdateMemberKeyDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    byte[] newPublicKey = Hex.decode("new public key in hex");
    UpdateMemberKeyResponse response = client.updateMemberKey(ledgerUri, memberId, newPublicKey, op);
  }
}

getMember

方法定义

public GetMemberResponse getMember(
      String ledgerUri,
      String memberId,
      OperationControl operationControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.GetMemberResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class GetMemberDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    GetMemberResponse response = client.getMember(ledgerUri, memberId, op);
  }
}

listMembers

方法定义

public ListMembersResponse listMembers(
          String ledgerUri,
          String lastMemberId,
          int limit,
          OperationControl operationControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.ListMembersResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class ListMembers{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    ListMembersResponse response = client.listMembers(ledgerUri, "", 20, op);
  }
}

enableMember

方法定义

public EnableMemberResponse enableMember(
      String ledgerUri,
      String memberId,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.EnableMemberResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class EnableMemberDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    String memberToEnable = "*****";
    EnableMemberResponse enableMemberResponse = client.enableMember(ledgerUri, memberToEnable, op);

  }
}

disableMember

方法定义

public DisableMemberResponse disableMember(
      String ledgerUri,
      String memberId,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.DisableMemberResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class DisableMemberDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    String memberToDisable = "*****";
    DisableMemberResponse disableMemberResponse = client.disableMember(ledgerUri, memberToDisable, op);
  }
}

记录(数据)操作API

appendTransaction

方法定义

public AppendTransactionResponse appendTransaction(
      String ledgerUri,
      byte[] data,
      OperationControl opControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.AppendTransactionResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

public class AppendTransactionDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    AppendTransactionResponse response =
        client.appendTransaction(ledgerUri, "Hello world".getBytes(StandardCharsets.UTF_8), op);

  }
}

getTransaction

方法定义

public GetTransactionResponse getTransaction(
      String ledgerUri,
      long txSequence,
      OperationControl opControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.GetTransactionResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class GetTransaction{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    GetTransactionResponse response = client.getTransaction(ledgerUri, 10, op);
  }
}

existTransaction

方法定义

public ExistTransactionResponse existTransaction(
      String ledgerUri,
      long txSequence,
      OperationControl opControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.ExistTransactionResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class ExistTransactionDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    ExistTransactionResponse response = client.existTransaction(ledgerUri, 10, op);
  }
}

listTransactions

方法定义

public ListTransactionsResponse listTransactions(
      String ledgerUri,
      String clue,
      long beginSequence,
      int limit,
      OperationControl opControl)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.ListTransactionsResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class ListTransactionsDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    ListTransactionsResponse response =
        client.listTransactions(ledgerUri, "", 1, 10, op);
  }
}

setTrustPoint

方法定义

public SetTrustPointResponse setTrustPoint(
      String ledgerUri,
      long sequence,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.SetTrustPointResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class SetTrustPointDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    SetTrustPointResponse response = client.setTrustPoint(ledgerUri, 10, op);
  }
}

getTrustPoint

方法定义

public GetTrustPointResponse getTrustPoint(
      String ledgerUri,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.GetTrustPointResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class GetTrustPoint{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    GetTrustPointResponse getTrustPointResponse = client.getTrustPoint(ledgerUri, op);
  }
}

getLastTimeAnchor

方法定义

public GetLastTimeAnchorResponse getLastTimeAnchor(
      String ledgerUri,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.GetLastTimeAnchorResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class GetLastTimeAnchorDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    GetLastTimeAnchorResponse response = client.getLastTimeAnchor(ledgerUri, op);
  }
}

listTimeAnchors

方法定义

public ListTimeAnchorsResponse listTimeAnchors(
      String ledgerUri,
      long startSequence,
      int limit,
      boolean reverse,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.ListTimeAnchorsResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class ListTimeAnchorsDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    ListTimeAnchorsResponse response =
        client.listTimeAnchors(ledgerUri, 10, 10, false, op);
  }
}

getBlockInfo

方法定义

public GetBlockInfoResponse getBlockInfo(
      String ledgerUri,
      long blockSequence,
      OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.GetBlockInfoResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

public class GetBlockInfoDemo{

  String ledgerId = "*****";

  String memberId = "*****";

  String privateKeyInHex = "your private key in hex";

  String publicKeyInHex = "your public key in hex";

  byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);

  byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);

  String ledgerHost = "127.0.0.1";

  Integer ledgerPort = 10099;

  LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);
  SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);

  @Test
  public void test(){
    OperationControl op = new OperationControl();
    op.addSignerProfile(
        new SignerProfile(
            memberId,
            Sender.SenderType.REGULAR,
            signerKeyPair));
    String ledgerUri = "ledger://127.0.0.1:10099/*****";
    GetBlockInfoResponse response = client.getBlockInfo(ledgerUri, 5, op);
  }
}

getProof

方法定义

public GetProofResponse getProof(String ledgerUri, long txSequence, OperationControl op)

示例

import com.antfin.ledgerdb.client.LedgerDBLightClient;
import com.antfin.ledgerdb.client.OperationControl;
import com.antfin.ledgerdb.client.common.GetBlockInfoResponse;
import com.antfin.ledgerdb.client.common.GetProofResponse;
import com.antfin.ledgerdb.client.common.SignerProfile;
import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair;
import com.antfin.ledgerdb.client.crypto.SignerKeyPair;
import com.antfin.ledgerdb.client.proto.Sender;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
public class GetProofDemo {  
    String ledgerId = "*****";  
    String memberId = "*****";  
    String privateKeyInHex = "your private key in hex";  
    String publicKeyInHex = "your public key in hex"; 
    
    byte[] privateKeyInBytes = Hex.decode(privateKeyInHex);  
    byte[] publicKeyInBytes = Hex.decode(publicKeyInHex);  
    String ledgerHost = "127.0.0.1";  
    Integer ledgerPort = 10099;  
    LedgerDBLightClient client = new LedgerDBLightClient(ledgerHost, ledgerPort);  
    SignerKeyPair signerKeyPair = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);  
    @Test
    public void test() {    
        OperationControl op = new OperationControl();    
        op.addSignerProfile(new SignerProfile( memberId,  Sender.SenderType.REGULAR, signerKeyPair));
        String ledgerUri = "ledger://127.0.0.1:10099/*****";
        GetProofResponse response = client.getProof(ledgerUri, 5, op);
    }
}

ECCK1KeyPair 秘钥对生成

import com.antfin.ledgerdb.client.crypto.ECCK1KeyPair; 
import com.antfin.ledgerdb.client.crypto.Keys;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
public class KeyGenTest{  
    @Test
    public void testGen() throws 
           InvalidAlgorithmParameterException, 
           NoSuchAlgorithmException, 
           NoSuchProviderException{    
      
      ECCK1KeyPair key = Keys.createEcKeyPair();    
     // 64 bytes
     byte[] publicKeyInBytes = key.getPublicKey();    
     // 32 bytes
     byte[] privateKeyInBytes = key.getPrivateKey();                                   
     System.out.println(Hex.toHexString(publicKeyInBytes));        
     System.out.print(Hex.toHexString(privateKeyInBytes));  
   }
   @Test
   public void testConstructors() throws 
          InvalidAlgorithmParameterException, 
          NoSuchAlgorithmException, 
          NoSuchProviderException{  
      
     ECCK1KeyPair key = Keys.createEcKeyPair(); 
     // 64 bytes
     byte[] publicKeyInBytes = key.getPublicKey();
     // 32 bytes
     byte[] privateKeyInBytes = key.getPrivateKey();  
     // construct from private key in bytes
     ECCK1KeyPair keyConstructed = new ECCK1KeyPair(privateKeyInBytes);  
     // construct from private key in bytes and public key in bytes
     // public key should be 64 bytes
     ECCK1KeyPair keyConstructed2 = new ECCK1KeyPair(privateKeyInBytes, publicKeyInBytes);}
}