# Bytom Java-sdk

# 安装

# 从源代码构建

git clone https://github.com/Bytom/bytom-java-sdk.git
cd java-sdk
mvn package -Dmaven.test.skip=true
mvn install

# 使用方式

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

Client client = Client.generateClient();

注意:你可以创建一个文件 config.properties 来本地配置bytom.api.urlclient.access.token

# 使用案例

For more details, see API methods (opens new window)

# 创建密钥

String alias = "test";
String password = "123456";

Key.Builder builder = new Key.Builder().setAlias(alias).setPassword(password);
Key key = Key.create(client, builder);

# 创建账户

String alias = "sender-account";
Integer quorum = 1;
List<String> root_xpubs = new ArrayList<String>();
root_xpubs.add(senderKey.xpub);

Account.Builder builder = new Account.Builder().setAlias(alias).setQuorum(quorum).setRootXpub(root_xpubs);

Account account = Account.create(client, builder);

# 创建接收者

String alias = receiverAccount.alias;
String id = receiverAccount.id;

Account.ReceiverBuilder receiverBuilder = new Account.ReceiverBuilder().setAccountAlias(alias).setAccountId(id);
Receiver receiver = receiverBuilder.create(client);

# 创建资产

 String alias = "receiver-asset";

List<String> xpubs = receiverAccount.xpubs;

Asset.Builder builder = new Asset.Builder()
                        .setAlias(alias)
                        .setQuorum(1)
                        .setRootXpubs(xpubs);
receiverAsset = builder.create(client);

# 资产上链

更多交易细节: transactions (opens new window)

# 创建交易

Transaction.Template controlAddress = new Transaction.Builder()
        .addAction(
                new Transaction.Action.SpendFromAccount()
                        .setAccountId(senderAccount.id)
                        .setAssetId(senderAsset.id)
                        .setAmount(300000000)
        )
        .addAction(
                new Transaction.Action.ControlWithAddress()
                        .setAddress(receiverAddress.address)
                        .setAssetId(senderAsset.id)
                        .setAmount(200000000)
        ).build(client);

# 签名交易

Transaction.Template singer = new Transaction.SignerBuilder().sign(client,
        controlAddress, "123456");

# 提交交易

Transaction.SubmitResponse txs = Transaction.submit(client, singer);

# 更多的使用案例

案例:doc (opens new window).

测试用例: Junit Test Cases (opens new window)

上次更新: 7/19/2021, 1:46:33 PM