# Bytom Java-sdk

# Install

# Build from source code

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

# Generate client

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

Note: You can create a fileconfig.properties to configure bytom.api.url and client.access.token locally

# Use cases

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

# Create private key

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

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

# Create an account

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

# Create a receiver

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

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

# Create assets

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

# Put assets on-chain

For more details, check transactions (opens new window)

# Build transaction

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

# Sign transaction

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

# Submit transaction

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

# More use cases

Use cases:doc (opens new window).

Test cases:Junit Test Cases (opens new window)

Last Updated: 7/28/2021, 6:43:31 PM