All Products
Search
Document Center

Client APIs

Last Updated: Sep 08, 2020

Ledger operation API

API

Description

statLedger

Query the current status of the ledger and related statistics

Member operations

API

Description

updateMemberKey

Update ledger member public keys

getMember

Query ledger members

listMembers

Batch query ledger members

enableMember

Activate ledger member

disableMember

Disable ledger members

Logging (data) operation APIs

API

Description

appendTransaction

Add journal

getTransaction

Read journal

existTransaction

Check whether a query journal exists.

listTransactions

Obtain multiple historical journal

getBlockInfo

Obtain the block information

listTimeAnchors

Batch query of trusted timestamps

getLastTimeAnchor

Query the latest trusted timestamp

getProof

Obtain proof of journal

Key generation

API

Description

ECCK1KeyPair key pair generation

The key pair class, including the constructor and statically generated key methods.

Open source client URL

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

Client construction and use

Step1: build and install

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

Step 2: After completing Step 1, add dependencies to your project

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

The client 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.proto.Sender;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;

public class LedgerStart{

  public static void main(String[] args){
    String ledgerId = "******";
    String memberId = "******";
    
    // You can obtain the VPC Endpoint through the VPC configuration function in the LedgerDB console
    String vpcEndpoint = "127.0.0.1"
    
    // Currently, the port is 10077.
    int port = 10077
    
    // ledgerUri: The format is 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();
    // Upload the public key to the LedgerDB console through key management. Then, you can verify the signature of the data.

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

Ledger operation API

statLedger

Method Definition

public StatLedgerResponse statLedger(String ledgerUri,OperationControl opControl)

Examples

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

Member operations

updateMemberKey

Method Definition

public UpdateMemberKeyResponse updateMemberKey(
          String ledgerUri,
          String memberId,
          ByteString publicKey,
          OperationControl operationControl)

Examples

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 com.google.protobuf.ByteString;
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, ByteString.copyFrom(newPublicKey), op);
  }
}

getMember

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Logging (data) operation APIs

appendTransaction

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

public GetTrustPointResponse getTrustPoint(
      String ledgerUri,
      OperationControl op)

Examples

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

Method Definition

public GetLastTimeAnchorResponse getLastTimeAnchor(
      String ledgerUri,
      OperationControl op)

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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

Method Definition

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

Examples

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 key pair generation

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