Code examples

In this article, we demonstrate a few implementation examples to execute basic tasks with our Java library.

These code examples are divided into several topics:

Tip: if you're new to our Java library, we suggest you begin with our Get Started guide.

General

Initializing a Universe

To bootstrap to the ALPHANET test network:

RadixUniverse.bootstrap(Bootstrap.ALPHANET);

To bootstrap to the BETANET test network:

RadixUniverse.bootstrap(Bootstrap.BETANET);

Note: No network connections will be made yet until it is required.

Initializing the DApp API

The Radix Application API is a client-side API exposing high-level abstractions to make DApp creation easier.

To initialize the API:

RadixUniverse.bootstrap(Bootstrap.ALPHANET); // This must be called before RadixApplicationAPI.create()
RadixApplicationAPI api = RadixApplicationAPI.create(identity);

Manage atoms

Signing an Atom

To sign an unsigned atom using the current account's signature, use the Identity’s sign(...) method:

Atom mySignedAtom = api.getMyIdentity().sign(<my_unsigned_atom>);

Submitting an Atom to the ledger

To submit an atom to the ledger, just use the API’s submitAtom(...) method:

api.submitAtom(<my_atom>);

Sending an Atom as message

In the following example, we serialize an atom and send it as a message to a destination address:

String address = “JHB89drvftPj6zVCNjnaijURk8D8AMFw4mVja19aoBGmRXWchnJ”;
byte[] data = atom.toDson();
RadixAddress receiver = RadixAddress.from(address);
api.sendMessage(data, false, receiver);

Note: this method does not submit the atom to the ledger. It only sends it as an arbitrary data byte array. There is no validation performed on the atom.

Manage identities

Creating an Identity

To create or load an identity from a file:

RadixIdentity identity = RadixIdentities.loadOrCreateEncryptedFile("filename.key", "password");

This will either create or load a file with a public/private key and encrypted with the given password.

Generating an Address

An address is a reference to an account and allows a user to receive tokens and/or data from other users.

You can get your own address by:

RadixAddress myAddress = api.getMyAddress();

Or from a base58 string:

RadixAddress anotherAddress = RadixAddress.fromString("JHB89drvftPj6zVCNjnaijURk8D8AMFw4mVja19aoBGmRXWchnJ");

Manage transactions

Storing data

Immutable data can be stored on the ledger. The data can be encrypted so that only selected identities can read the data.

To store the encrypted string Hello which only the user can read:

ECPublicKey myPublicKey = api.getMyPublicKey();
Data data = new DataBuilder()
.bytes("Hello".getBytes(StandardCharsets.UTF_8))
.addReader(myPublicKey)
.build();
Result result = api.storeData(data, <address>);

To store the unencrypted string Hello:

Data data = new DataBuilder()
.bytes("Hello".getBytes(StandardCharsets.UTF_8))
.unencrypted()
.build();
Result result = api.storeData(data, <address>);

The returned Result object exposes RXJava interfaces from which you can get notified of the status of the storage action:

result.toCompletable().subscribe(<on-success>, <on-error>);

Retrieving data

To read (and decrypt if necessary) all the readable data at an address:

Observable<UnencryptedData> readable = api.getReadableData(<address>);
readable.subscribe(data -> { ... });

Note: data that can't be decrypted by the user's key is simply ignored.

Manage tokens

NOTE: these examples requires BETANET network access.

Creating a token

To create your own token on Radix, you can use the createToken(...) method provided by the Java DApp API:

api.createToken(<token_name>, <iso_name>, <description>,
<amount>, TokenUnitConversions.getMinimumGranularity(),
TokenSupplyType.MUTABLE);

For example, to create 10000 UST tokens:

api.createToken(
"UST Example Token", // Name
"UST", // ISO
"This is a token created by me", // Description
BigDecimal.valueOf(10000), // Amount
TokenUnitConversions.getMinimumGranularity(), // Granularity: Default is 10^18
TokenSupplyType.MUTABLE // TokenSupplyType
)

Minting tokens

When you need to mint new tokens, you can simply call the mintTokens(...) method as shown below:

api.mintTokens(<iso_token_name>, <amount_to_mint>);

Getting a token’s Resource Identifier

To get the Radix Resource Identifier (RRI) for a token created by a previous call to createToken(...), we have to:

RRI myRRid = RRI.of(api.getMyAddress(), <iso_token_name>);

Note: no validation is made whether the returned RRI actually points to a valid token in the internal address space of the client.