重要通知
由于Bintray & jcenter将逐步停止服务(详见Into the Sunset on May 1st: Bintray, JCenter, GoCenter, and ChartCenter),对
jcenter
的引用需要替换为mavenCentral
。同时,出于安全考虑,我们将不再进行打包以及上传maven的操作,请自行从GitHubclone并打包。
概述
trident-java是一个极其轻量的SDK。它包含了一系列库,方便开发者使用波场网络的系统与智能合约。
我们相信trident-java可以很大程度帮助你使用java开发波场相关的应用程序。
安装trident-java
要想在工程中使用trident-java,首先需要添加trident-java的包作为依赖。
从GitHub clone代码之后,需要先对代码进行编译。
请使用java version 1.8.0_231 以及 Gradle 5.6.4 进行编译和打包。
trident-java已为jdk8设置了兼容性,可作为依赖使用。也可在编译前自行添加兼容性设置,位于trident-java/build.gradle的subproject标签下:
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
// Add any compatibility
}
依赖配置
关于外部依赖
开发者需要将trident引用的第三方库添加到自己的项目中。
Gradle配置:
repositories {
maven {
mavenCentral()
}
}
选择需要的库
trident-java有三个包:
abi | 包含了ABI中使用的各种数据类型,以及编码/解码工具。 |
core | 包含了封装的方法,便于使用波场系统合约与智能合约。对常用内容有单独封装(如:TRC-20)。 |
utils | 包含了各种常用工具,例如:sha256计算,地址格式转换,等。 |
在Gradle中添加依赖
trident-java的封装函数中,使用GRPC与波场网络进行交互,所以在添加依赖时,需要添加Google Protobuf以及GRPC相关的依赖。
dependencies {
// protobuf & grpc
implementation 'com.google.protobuf:protobuf-java:3.11.0'
implementation 'io.grpc:grpc-netty-shaded:1.31.0'
implementation 'io.grpc:grpc-netty:1.31.0'
implementation 'io.grpc:grpc-protobuf:1.31.0'
implementation 'io.grpc:grpc-stub:1.31.0'
implementation "org.bouncycastle:bcprov-jdk15on:1.68"
implementation fileTree(dir:'../core')
implementation fileTree(dir:'../utils')
implementation fileTree(dir:'../abi')
//if you are using the *.jar files, ues the following line
implementation fileTree(dir:'your path', include: '*.jar')
implementation 'com.google.guava:guava:28.0-jre'
}
在Maven中添加依赖
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>abi</artifactId>
<version>0.3.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>utils</artifactId>
<version>0.3.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.3.0</version>
<scope>system</scope>
<systemPath>your path</systemPath>
</dependency>
使用trident-java
core包中的ApiWrapper
类,是各个API以及智能合约调用的封装方法的入口。
新建一个ApiWrapper实例
在使用ApiWrapper包含的方法之前,需要在初始化时,将一个私钥绑定到ApiWrapper的实例。
ApiWrapper wrapper = new ApiWrapper("grpc endpoint", "solidity grpc endpoint", "hex private key");
你可以使用任何节点/TronGrid。除此之外,ApiWrapper有封装好的波场主网、Shasta以及Nile测试网的初始化方法,分别使用TronGrid和Nile官方网站上提供的节点。
从0.1.1版本开始,由于主网使用TronGrid时需要API key,在使用ofMainnet初始化ApiWrapper时,需要添加API key。
//main net, using TronGrid
ApiWrapper wrapper = ApiWrapper.ofMainnet("hex private key", "API key");
//Shasta test net, using TronGrid
ApiWrapper wrapper = ApiWrapper.ofShasta("hex private key");
//Nile test net, using a node from Nile official website
ApiWrapper wrapper = ApiWrapper.ofNile("hex private key");
绑定私钥
私钥的唯一作用是给交易签名。也就是说,当你只需要做查询操作时,可以使用任何符合长度的十六进制字符串当做私钥来初始化ApiWrapper实例。
JavaDoc
core
包中的方法有完善的JavaDoc。在需要的时候,手动生成它!