Complementary Examples
Prerequisites
Before we get started we need to make sure that all of the required dependencies are installed. These dependencies are the Crypto SDK and Client SDK. You can head on over to their documentations to read more about them but for now we are only concerned with installing them to get up and running.
Open your project and include the following dependencies for both SDKs. If you encounter any errors, please open an issue with as much information as you can provide so that our developers can have a look and get to the bottom of the issue.
Gradle
1implementation 'org.arkecosystem:crypto:1.2.9'
1implementation 'org.arkecosystem:client:1.2.9'
Maven
1<dependency>2 <groupId>org.arkecosystem</groupId>3 <artifactId>crypto</artifactId>4 <version>1.2.9</version>5</dependency>
1<dependency>2 <groupId>org.arkecosystem</groupId>3 <artifactId>client</artifactId>4 <version>1.2.9</version>5</dependency>
Complementary Examples
Transfer - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.TransferBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class Transfer {13 public static void main(String[] args) throws IOException {14 // Set the network15 Network.set(new Devnet());16 17 // Make configurations and connect to the node18 HashMap<String, Object> configurations = new HashMap<>();19 configurations.put("host", "https://dexplorer.ark.io/api/");20 configurations.put("content-type","application/json");21 Connection connection = new Connection(configurations);22 23 // Retrieve the sequential nonce24 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()25 .wallets26 .show("YOUR_SENDER_WALLET_ADDRESS")27 .get("data"))28 .get("nonce")29 .toString());30 31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new TransferBuilder()36 .recipient("Address of Recipient")37 .amount(10^8)38 .nonce(nonce)39 .sign("this is a top secret passphrase")40 .transaction;41 42 // Add transaction to payload43 ArrayList<HashMap> payload = new ArrayList<>();44 payload.add(actual.toHashMap());45 46 // Broadcast the transaction47 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);48 49 // Log the response50 System.out.println(broadcastResponse);51 52 }53}
Information
The vendorField is optional and limited to a length of 255 characters. It can be a good idea to add a vendor field to your transactions if you want to be able to easily track them in the future.
Second Signature - Creating and Broadcasting
1package transactions; 2 3import com.google.gson.internal.LinkedTreeMap; 4import org.arkecosystem.client.Connection; 5import org.arkecosystem.crypto.configuration.Network; 6import org.arkecosystem.crypto.networks.Devnet; 7import org.arkecosystem.crypto.transactions.builder.SecondSignatureRegistrationBuilder; 8import org.arkecosystem.crypto.transactions.types.Transaction; 9 10import java.io.IOException;11import java.util.ArrayList;12import java.util.HashMap;13 14public class SecondSignature {15 public static void main(String[] args) throws IOException {16 // Set the network17 Network.set(new Devnet());18 19 // Make configurations and connect to the node20 HashMap<String, Object> configurations = new HashMap<>();21 configurations.put("host", "https://dexplorer.ark.io/api/");22 configurations.put("content-type", "application/json");23 Connection connection = new Connection(configurations);24 25 // Retrieve the sequential nonce26 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()27 .wallets28 .show("YOUR_SENDER_WALLET_ADDRESS")29 .get("data"))30 .get("nonce").toString());31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new SecondSignatureRegistrationBuilder()36 .nonce(nonce)37 .signature("this is a top secret second passphrase")38 .sign("this is a top secret passphrase")39 .transaction;40 41 // Add transaction to payload42 ArrayList<HashMap> payload = new ArrayList<>();43 payload.add(actual.toHashMap());44 45 // Broadcast the transaction46 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);47 48 // Log the response49 System.out.println(broadcastResponse);50 }51}
Delegate Registration - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.DelegateRegistrationBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class DelegateRegistration {13 public static void main(String[] args) throws IOException {14 // Set the network15 Network.set(new Devnet());16 // Make configurations and connect to the node17 HashMap<String, Object> configurations = new HashMap<>();18 configurations.put("host", "https://dexplorer.ark.io/api/");19 configurations.put("content-type", "application/json");20 Connection connection = new Connection(configurations);21 22 // Retrieve the sequential nonce23 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()24 .wallets25 .show("YOUR_SENDER_WALLET_ADDRESS")26 .get("data"))27 .get("nonce")28 .toString());29 30 // Increment it by one31 nonce++;32 33 // Create the transaction34 Transaction actual = new DelegateRegistrationBuilder()35 .username("johndoe")36 .nonce(nonce)37 .sign("this is a top secret passphrase")38 .transaction;39 40 // Add transaction to payload41 ArrayList<HashMap> payload = new ArrayList<>();42 payload.add(actual.toHashMap());43 44 // Broadcast the transaction45 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);46 47 // Log the response48 System.out.println(broadcastResponse);49 50 }51}
Vote - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.VoteBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.Arrays;11import java.util.HashMap;12 13public class Vote {14 public static void main(String[] args) throws IOException {15 // Set the network16 Network.set(new Devnet());17 // Make configurations and connect to the node18 HashMap<String, Object> configurations = new HashMap<>();19 configurations.put("host", "https://dexplorer.ark.io/api/");20 configurations.put("content-type", "application/json");21 Connection connection = new Connection(configurations);22 23 // Retrieve the sequential nonce24 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()25 .wallets26 .show("YOUR_SENDER_WALLET_ADDRESS")27 .get("data"))28 .get("nonce")29 .toString());30 31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new VoteBuilder()36 .addVotes(Arrays.asList("+public_key_of_a_delegate_wallet"))37 .nonce(nonce)38 .sign("this is a top secret passphrase")39 .transaction;40 41 // Add transaction to payload42 ArrayList<HashMap> payload = new ArrayList<>();43 payload.add(actual.toHashMap());44 45 // Broadcast the transaction46 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);47 48 // Log the response49 System.out.println(broadcastResponse);50 51 }52 53}
Information
Note the plus prefix for the public key that is passed to the votes function. This prefix denotes that this is a transaction to remove a vote from the given delegate.
Unvote - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.VoteBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.Arrays;11import java.util.HashMap;12 13public class Vote {14 public static void main(String[] args) throws IOException {15 // Set the network16 Network.set(new Devnet());17 // Make configurations and connect to the node18 HashMap<String, Object> configurations = new HashMap<>();19 configurations.put("host", "https://dexplorer.ark.io/api/");20 configurations.put("content-type", "application/json");21 Connection connection = new Connection(configurations);22 23 // Retrieve the sequential nonce24 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()25 .wallets26 .show("YOUR_SENDER_WALLET_ADDRESS")27 .get("data"))28 .get("nonce")29 .toString());30 31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new VoteBuilder()36 .addVotes(Arrays.asList("-public_key_of_a_delegate_wallet"))37 .nonce(nonce)38 .sign("this is a top secret passphrase")39 .transaction;40 41 // Add transaction to payload42 ArrayList<HashMap> payload = new ArrayList<>();43 payload.add(actual.toHashMap());44 45 // Broadcast the transaction46 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);47 48 // Log the response49 System.out.println(broadcastResponse);50 51 }52 53}
Information
Note the minus prefix for the public key that is passed to the votes function. This prefix denotes that this is a transaction to add a vote to the given delegate.
IPFS - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.IpfsBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class Ipfs {13 public static void main(String[] args) throws IOException {14 // Set the network15 Network.set(new Devnet());16 // Make configurations and connect to the node17 HashMap<String, Object> configurations = new HashMap<>();18 configurations.put("host", "https://dexplorer.ark.io/api/");19 configurations.put("content-type", "application/json");20 Connection connection = new Connection(configurations);21 22 // Retrieve the sequential nonce23 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()24 .wallets25 .show("YOUR_SENDER_WALLET_ADDRESS")26 .get("data"))27 .get("nonce")28 .toString());29 30 // Increment it by one31 nonce++;32 33 // Create the transaction34 Transaction actual = new IpfsBuilder()35 .ipfsAsset("QmR45FmbVVrixReBwJkhEKde2qwHYaQzGxu4ZoDeswuF9c")36 .nonce(nonce)37 .sign("this is a top secret passphrase")38 .transaction;39 40 // Add transaction to payload41 ArrayList<HashMap> payload = new ArrayList<>();42 payload.add(actual.toHashMap());43 44 // Broadcast the transaction45 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);46 47 // Log the response48 System.out.println(broadcastResponse);49 50 }51}
Multi Payment - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.MultiPaymentBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class MultiPayment {13 public static void main(String[] args) throws IOException {14 // Set the network15 Network.set(new Devnet());16 17 // Make configurations and connect to the node18 HashMap<String, Object> configurations = new HashMap<>();19 configurations.put("host", "https://dexplorer.ark.io/api/");20 configurations.put("content-type","application/json");21 Connection connection = new Connection(configurations);22 23 // Retrieve the sequential nonce24 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()25 .wallets26 .show("YOUR_SENDER_WALLET_ADDRESS")27 .get("data"))28 .get("nonce")29 .toString());30 31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new MultiPaymentBuilder()36 .addPayment("Address of Recipient Wallet 1", 10^8)37 .addPayment("Address of Recipient Wallet 2", 10^8)38 .addPayment("Address of Recipient Wallet 3", 10^8)39 .nonce(nonce)40 .sign("this is a top secret passphrase")41 .transaction;42 43 // Add transaction to payload44 ArrayList<HashMap> payload = new ArrayList<>();45 payload.add(actual.toHashMap());46 47 // Broadcast the transaction48 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);49 50 // Log the response51 System.out.println(broadcastResponse);52 }53}
Delegate Resignation - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.DelegateResignationBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class DelegateResignation {13 public static void main(String[] args) throws IOException {14 // Set the network15 Network.set(new Devnet());16 // Make configurations and connect to the node17 HashMap<String, Object> configurations = new HashMap<>();18 configurations.put("host", "https://dexplorer.ark.io/api/");19 configurations.put("content-type", "application/json");20 Connection connection = new Connection(configurations);21 22 // Retrieve the sequential nonce23 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()24 .wallets25 .show("YOUR_SENDER_WALLET_ADDRESS")26 .get("data"))27 .get("nonce")28 .toString());29 30 // Increment it by one31 nonce++;32 33 // Create the transaction34 Transaction actual = new DelegateResignationBuilder()35 .nonce(nonce)36 .sign("this is a top secret passphrase")37 .transaction;38 39 // Add transaction to payload40 ArrayList<HashMap> payload = new ArrayList<>();41 payload.add(actual.toHashMap());42 43 // Broadcast the transaction44 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);45 46 // Log the response47 System.out.println(broadcastResponse);48 49 }50}
Information
A delegate resignation has to be sent from the delegate wallet itself to verify its identity.
HTLC Lock - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.enums.HtlcLockExpirationType; 5import org.arkecosystem.crypto.networks.Devnet; 6import org.arkecosystem.crypto.transactions.builder.HtlcLockBuilder; 7import org.arkecosystem.crypto.transactions.types.Transaction; 8 9import java.io.IOException;10import java.util.ArrayList;11import java.util.HashMap;12 13public class HtlcLock {14 public static void main(String[] args) throws IOException {15 // Set the network16 Network.set(new Devnet());17 // Make configurations and connect to the node18 HashMap<String, Object> configurations = new HashMap<>();19 configurations.put("host", "https://dexplorer.ark.io/api/");20 configurations.put("content-type", "application/json");21 Connection connection = new Connection(configurations);22 23 // Retrieve the sequential nonce24 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()25 .wallets26 .show("YOUR_SENDER_WALLET_ADDRESS")27 .get("data"))28 .get("nonce")29 .toString());30 31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new HtlcLockBuilder()36 .secretHash("0f128d401958b1b30ad0d10406f47f9489321017b4614e6cb993fc63913c5454")37 .expirationType(HtlcLockExpirationType.BLOCK_HEIGHT, 43671000)38 .amount(10^8)39 .recipientId("Address of Recipient")40 .nonce(nonce)41 .sign("this is a top secret passphrase")42 .transaction;43 44 45 // Add transaction to payload46 ArrayList<HashMap> payload = new ArrayList<>();47 payload.add(actual.toHashMap());48 49 // Broadcast the transaction50 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);51 52 // Log the response53 System.out.println(broadcastResponse);54 55 }56}
HTLC Claim - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.HtlcClaimBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class HtlcClaim {13 14 public static void main(String[] args) throws IOException {15 // Set the network16 Network.set(new Devnet());17 // Make configurations and connect to the node18 HashMap<String, Object> configurations = new HashMap<>();19 configurations.put("host", "https://dexplorer.ark.io/api/");20 configurations.put("content-type", "application/json");21 Connection connection = new Connection(configurations);22 23 // Retrieve the sequential nonce24 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()25 .wallets26 .show("YOUR_SENDER_WALLET_ADDRESS")27 .get("data"))28 .get("nonce")29 .toString());30 31 // Increment it by one32 nonce++;33 34 // Create the transaction35 Transaction actual = new HtlcClaimBuilder()36 .htlcClaimAsset("943c220691e711c39c79d437ce185748a0018940e1a4144293af9d05627d2eb4",37 "c27f1ce845d8c29eebc9006be932b604fd06755521b1a8b0be4204c65377151a")38 .nonce(nonce)39 .sign("this is a top secret passphrase")40 .transaction;41 42 43 // Add transaction to payload44 ArrayList<HashMap> payload = new ArrayList<>();45 payload.add(actual.toHashMap());46 47 // Broadcast the transaction48 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);49 50 // Log the response51 System.out.println(broadcastResponse);52 53 }54}
Information
The unlockSecret has to be a SHA256 hash of the plain text secret that you shared with the person that is allowed to claim the transaction.
HTLC Refund - Creating and Broadcasting
1import com.google.gson.internal.LinkedTreeMap; 2import org.arkecosystem.client.Connection; 3import org.arkecosystem.crypto.configuration.Network; 4import org.arkecosystem.crypto.networks.Devnet; 5import org.arkecosystem.crypto.transactions.builder.HtlcRefundBuilder; 6import org.arkecosystem.crypto.transactions.types.Transaction; 7 8import java.io.IOException; 9import java.util.ArrayList;10import java.util.HashMap;11 12public class HtlcRefund {13 public static void main(String[] args) throws IOException {14 // Set the network15 Network.set(new Devnet());16 // Make configurations and connect to the node17 HashMap<String, Object> configurations = new HashMap<>();18 configurations.put("host", "https://dexplorer.ark.io/api/");19 configurations.put("content-type", "application/json");20 Connection connection = new Connection(configurations);21 22 // Retrieve the sequential nonce23 long nonce = Long.parseLong(((LinkedTreeMap<String, Object>) connection.api()24 .wallets25 .show("YOUR_SENDER_WALLET_ADDRESS")26 .get("data"))27 .get("nonce")28 .toString());29 30 // Increment it by one31 nonce++;32 33 // Create the transaction34 Transaction actual = new HtlcRefundBuilder()35 .htlcRefundAsset("2fad9edeafbdbaa7253e2a56e0aa077957da613497883923a053fca5f6fae8d6")36 .nonce(nonce)37 .sign("this is a top secret passphrase")38 .transaction;39 40 41 // Add transaction to payload42 ArrayList<HashMap> payload = new ArrayList<>();43 payload.add(actual.toHashMap());44 45 // Broadcast the transaction46 LinkedTreeMap<String, Object> broadcastResponse = connection.api().transactions.create(payload);47 48 // Log the response49 System.out.println(broadcastResponse);50 51 }52}