Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple example of Chain's Java SDK
import com.chain.*;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
private static String projectID = "proj0ACRX7YMG091G";
private static Client chainClient;
public static void main(String [] args) {
try {
String label = String.format("test-%d-", ThreadLocalRandom.current().nextInt());
chainClient = new Client(new URL("https://[redacted]@sandbox-api.chain.com/"));
IssuerNode in = createIssuerNode(label + "issuer");
Asset testAsset = chainClient.createAsset(in.ID, label + "asset");
ManagerNode mn = createManager(label + "manager");
Account a = createAccount(mn, label + "account");
issueAssets(in, a, testAsset, 86);
} catch (Exception e) {
System.out.printf("error: %s", e);
}
}
public static void issueAssets(IssuerNode in, Account a, Asset asset, Integer qty) throws ChainException {
List<Asset.IssueOutput> outs = new ArrayList<>();
outs.add(new Asset.IssueOutput(a.ID, null, BigInteger.valueOf(qty)));
chainClient.issue(asset.ID, outs);
}
public static IssuerNode createIssuerNode(String label) throws ChainException {
List<IssuerNode.CreateRequest.Key> keys = new ArrayList<>();
keys.add(new IssuerNode.CreateRequest.Key().Generated());
IssuerNode.CreateRequest req = new IssuerNode.CreateRequest(label, 1, keys);
IssuerNode in = chainClient.createIssuerNode(projectID, req);
chainClient.getKeyStore().add(new XPrvKey(in.keys[0].xprv, true));
return in;
}
public static ManagerNode createManager(String label) throws ChainException {
List<ManagerNode.CreateRequest.Key> keys = new ArrayList<>();
keys.add(ManagerNode.CreateRequest.Key.Generated());
ManagerNode.CreateRequest req = new ManagerNode.CreateRequest(label, 1, keys);
return chainClient.createManagerNode(projectID, req);
}
public static Account createAccount(ManagerNode mn, String label) throws ChainException {
Account.CreateRequest acr = new Account.CreateRequest(label);
return chainClient.createAccount(mn.ID, acr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment