比原链Bytom JAVA SDK使用指南

最近在开源社区协助比原链完成了 java sdk,这里跟大家分享下哈。

Bytom Java SDK

This SDK contains methods for easily interacting with the Bytom API.
Below are examples to get you started. For more information, please see Bytom API reference
documentation at https://github.com/Bytom/bytom/wiki

SDK 比较容易接入,以下是其中一个例子,更多的例子请查看https://github.com/Bytom/bytom/wiki

Installation 安装

There are various ways to install and use this sdk. We’ll elaborate on a couple here. Note that the Bytom JAVA SDK requires JAVA 7 or newer.
有多种方式可以安装Bytom JAVA SDK,例举如下,注意SDK需要Java 7版本及更高版本。

Maven

在Maven配置文件中添加如下配置:

<dependency>
  <groupId>io.bytom</groupId>
  <artifactId>bytom-sdk-java</artifactId>
  <version>1.0.1</version>
</dependency>

Building from source code 源码安装

To clone, compile, and install in your local maven repository (or copy the artifacts from the target/ directory to wherever you need them):

从github上clone,编译并安装到你本地的Maven仓库中:

git clone [email protected]:chainworld/java-bytom.git
cd java-bytom
mvn install

5-Minute Guide 5分钟指南:

This guide will walk you through the basic functions of Bytom:
这个指南会带你快速使用Bytom的基础功能。

Initialize the SDK

Create an instance of the SDK. By default, the SDK will try to access a core located at http://127.0.0.1:9888, which is the default if you’re running Bytom Wallet locally.

创建一个SDK的实例,默认会连接你本地(http://127.0.0.1:9888)的core:


public static Client generateClient() throws BytomException {
    String coreURL = Configuration.getValue("bytom.api.url");
    String accessToken = Configuration.getValue("client.access.token");
    if (coreURL == null || coreURL.isEmpty()) {
        coreURL = "http://127.0.0.1:9888/";
    }
    return new Client(coreURL, accessToken);
}

Create Keys 创建一个Key

Key key = Key.create(client, "alias", "password");

It will create a key whose alias is ‘alias’ while password is ‘password’.
创建一个别名为“alias”、密码为“password”的密钥。

Create an Asset 创建一笔资产

Create a new asset, providing an alias, key, and quorum.
创建资产需要指定别名,签名密钥和数量。

String asset = "GOLD";
Asset testAsset = new Asset.Builder()
              .setAlias(asset)
              .addRootXpub(key.xpub)
              .setQuorum(1)
              .create(client);

Create an Account 创建一个账户

Create an account, providing an alias, key, and quorum.

Account account = new Account.Builder()
              .setAlias("alice")
              .addXpub(key.xpub)
              .setQuorum(1)
              .create(client);

Create an Account Address 创建账户地址

new Account.ReceiverBuilder()
   .setAccountId(account.id)
   .create(client);

Build the Transaction 创建交易

Transaction.Template controlAddressTx = new Transaction.Builder()
            .addAction(new Transaction.Action.SpendFromAccount()
                    .setAccountId(account.id)
                    .setAssetId(asset.id)
                    .setAmount(300000000))
            .addAction(new Transaction.Action.ControlWithAddress()
                    .setAddress(address.id)
                    .setAssetId(asset.id)
                    .setAmount(200000000))
                    .build(client);

Sign the Transaction 为交易签名

Transaction.Template singerTx = new Transaction.SignerBuilder()
                                   .sign(client,controlAddressTx, "password");

Submit the Transaction 提交交易

Transaction.submit(client, singerTx); 

All usage examples 所有的示例,请参考文档:

You find more detailed documentation at /doc.
Also you can find Test Cases at Junit Test Cases

Support and Feedback 支持和反馈:

If you find a bug, please submit the issue in Github directly.
Bytom-JAVA-SDK Issues

License

Bytom JAVA SDK is based on the MIT protocol.

http://www.opensource.org/licenses/MIT

猜你喜欢

转载自blog.csdn.net/niyuelin1990/article/details/80505361