SDK - Java
Chain Connector for Java
Git repository:https://github.com/taiyi-io/chain-connector-java
The Java language SDK for accessing and operating on blockchain platforms is based on openJDK 18.
Project
The project is managed based on Apache Maven.
Compile
$mvn compile
Build JAR
$mvn package
Run testing
$mvn test
Usage
Connect the chain
Initial the connector using the private key data allocated by the chain platform, then connect to the gateway module.
AccessKey accessKey = gson.fromJson(fileContent, AccessKey.class);
ChainConnector connector = ChainConnector.NewConnectorFromAccess(accessKey);
connector.connect(gatewayHost, gatewayPort);
Build and manage digital assets
Define a data schema for digital assets, and then you can add, update, delete, and query documents (digital assets) under the schema. All changes are automatically persistently stored using blockchain and could be queried using getSchemaLog and getDocumentLog.
//create new schema
String schemaName = "sample";
List<DocumentProperty> properties = new ArrayList<>();
properties.add(new DocumentProperty("name", PropertyType.String));
properties.add(new DocumentProperty("age", PropertyType.Integer));
properties.add(new DocumentProperty("available", PropertyType.Boolean));
conn.createSchema(schemaName, properties);
DocumentSchema schema = conn.getSchema(schemaName);
//add a document
String content = "{\"name\": \"hello\", \"age\": 20, \"available\": true}";
String docID = conn.addDocument(schemaName, "", content);
//check a document
if (conn.hasDocument(schemaName, docID)){
//update a existed document
String updatedContent = "{\"name\": \"alice\", \"age\": 18, \"available\": false}";
conn.updateDocument(schemaName, docID, updatedContent);
}
//get change trace of a document
LogRecords logs = conn.getDocumentLogs(schemaName, docID);
//query documents
QueryCondition condition = new QueryBuilder()
.AscendBy("name")
.MaxRecord(20)
.SetOffset(0)
.Build();
DocumentRecords records = conn.queryDocuments(schemaName, condition);
//remove document
conn.removeDocument(schemaName, docID);
Deploy and invoke the Smart Contract
It is necessary to assign a name and execute steps to deploy a Smart Contract. Then initiate execution using the contract name and call parameters. The system can enable the trace option for a contract, which allows the user to review the contract’s execution plan and steps.
String contractName = "contract_create";
List<ContractStep> steps = new ArrayList<>();
steps.add(new ContractStep("create_doc", new String[]{"$s", "@1", "@2"}));
steps.add(new ContractStep("set_property", new String[]{"$s", "catalog", "@3"}));
steps.add(new ContractStep("set_property", new String[]{"$s", "balance", "@4"}));
steps.add(new ContractStep("set_property", new String[]{"$s", "number", "@5"}));
steps.add(new ContractStep("set_property", new String[]{"$s", "available", "@6"}));
steps.add(new ContractStep("set_property", new String[]{"$s", "weight", "@7"}));
steps.add(new ContractStep("update_doc", new String[]{"@1", "$s"}));
steps.add(new ContractStep("submit"));
ContractDefine contractDefine = new ContractDefine(steps);
//check existed contract
if (conn.hasContract(contractName)) {
//withdraw existed contract
conn.withdrawContract(contractName);
System.out.printf("previous contract %s removed\n", contractName);
}
//deploy contact
conn.deployContract(contractName, contractDefine);
//enable trace option
ContractInfo info = conn.getContractInfo(contractName);
if (!info.isEnabled()) {
conn.enableContractTrace(contractName);
}
final String docID = "contract-doc";
String[] parameters = {
schemaName,
docID,
schemaName,
String.valueOf(Math.random()),
String.valueOf((int) (Math.random() * 1000)),
Math.random() > 0.5 ? "true" : "false",
String.format("%.2f", Math.random() * 200)
};
//call contract with parameters
conn.callContract(createContractName, new ArrayList<>(Arrays.asList(parameters)));
Audit the block chain and transaction
Through the SDK, you can obtain and check all the details of chains, blocks, and transactions, which can be used to audit data security and monitor the background operation.
//check chain status
ChainStatus status = conn.getStatus();
//query blocks from height 1 to 10
BlockRecords blockRecords = conn.queryBlocks(1, 10);
for (String blockID : blockRecords.getBlocks()) {
//get block data
BlockData blockData = conn.getBlock(blockID);
//query transactions in a block
TransactionRecords transactionRecords = conn.queryTransactions(blockID, 0, 20);
for (String transID : transactionRecords.getTransactions()) {
//get transaction data
TransactionData transactionData = conn.getTransaction(blockID, transID);
}
}