Complementary Examples

  • ARK C++ Client v1.4.1
  • ARK C++ Crypto v1.1.0

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.

Also note that CMake is the recommended build tool.

Open your project and execute the following commands to install both SDKs. Make sure that those complete without any errors. If you encounter any errors, please open an issue in C++ Client or C++ Crypto with as much information as you can provide so that our developers can have a look and get to the bottom of the issue.

Clone the C++ Client and Crypto repos into your projects external dependencies directory.

1git clone https://github.com/arkecosystem/cpp-client
2git clone https://github.com/arkecosystem/cpp-crypto

Example Setup

Project Layout

1project
2- extern/
3 - cpp-client/
4 - cpp-crypto/
5- src/
6 - | main.c
7- | CMakeLists.txt

Cloning

1git clone https://github.com/arkecosystem/cpp-client extern/cpp-client
2git clone https://github.com/arkecosystem/cpp-crypto extern/cpp-crypto

Example CMake File

CMakeLists.txt

1cmake_minimum_required(VERSION 3.2)
2 
3project(ark_cpp_example_project)
4 
5set(CMAKE_CXX_STANDARD 11)
6 
7if (MSVC)
8 add_definitions(
9 -D_CRT_SECURE_NO_WARNINGS
10 -D_SCL_SECURE_NO_WARNINGS
11 -DNOMINMAX
12 )
13 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
14endif()
15 
16add_subdirectory(extern/cpp-client)
17add_subdirectory(extern/cpp-crypto)
18 
19set(FILE_SRC src/main.cpp)
20 
21add_executable(${PROJECT_NAME} ${FILE_SRC})
22 
23target_link_libraries(${PROJECT_NAME} ark_cpp_client ark_cpp_crypto)

Building and Running the Project

1mkdir build && cd build
2cmake ..
3cmake --build .
4./ark_cpp_example_project

Ready to roll

Now that we’re setup and understand the basic layout and build steps, let’s look into some examples for the most common tasks encountered when interacting with the ARK Blockchain.

Persisting your transaction on the blockchain

The process of getting your transaction verified and persisted on the ARK Blockchain involves a few steps with which our SDKs will help you but lets break them down to get a better idea of what is happening.

  1. Install the Client SDK and configure it to use a node of your choosing to broadcast your transactions to. Always make sure that you have a fallback node that you can use for broadcasting in case your primary node goes offline or acts strange otherwise.
  2. Install the Crypto SDK and configure it to match the configuration of the network. This is the most important part as misconfiguration can lead to a myriad of issues as Core will reject your transactions.
  3. Retrieve the nonce of the sender wallet and increase it by 1. You can read about what a sequential nonce is and why it is important here.
  4. Create an instance of the builder for the type of transaction you want to create. This is the step where we actually create a transaction and sign it so that the ARK Blockchain can later on verify it and decide if it will be accepted, forged and finally. You can read the relevant API documentation if you want more detailed information about the design and usage.
  5. Turn the newly created transaction into JSON and broadcast it to the network through the Client SDK. You can read the relevant API documentation if you want more detailed information about the design and usage.
  6. Process the API response and verify that your transaction was accepted. If the network rejects your transaction you’ll receive the reason as to why that is the case in the response which might mean that you need to create a new transaction and broadcast it.

Troubleshooting

A common issue when trying to get your transaction onto the blockchain is that you’ll receive an error to the effect of Transaction Version 2 is not supported which indicates that your Crypto SDK configuration might be wrong.

The solution to this is to make sure that your Crypto SDK instance is properly configured. This includes both the network preset and the height it’s configured to assume the network has passed, if any of those don’t match up you’ll encounter the aforementioned issue with the version of your transactions.

Mainnet

1#include "common/configuration.hpp"
2#include "transactions/builders/builder.hpp"
3 
4#include "networks/mainnet.hpp"
5 
6auto transaction = Ark::Crypto::transactions::builder::Transfer(Mainnet)
7 .nonce(1)
8 .senderPublicKey("YOUR_SENDER_WALLET_PUBLICKEY")
9 .amount(1)
10 .expiration(0)
11 .recipientId("Address of Recipient")
12 .build(config);

Devnet

1#include "common/configuration.hpp"
2#include "transactions/builders/builder.hpp"
3 
4#include "networks/devnet.hpp"
5 
6// Devnet is the default configuration
7auto transaction = Ark::Crypto::transactions::builder::Transfer()
8 .nonce(1)
9 .senderPublicKey("YOUR_SENDER_WALLET_PUBLICKEY")
10 .amount(1)
11 .expiration(0)
12 .recipientId("Address of Recipient")
13 .build();

Testnet

1#include "common/configuration.hpp"
2#include "transactions/builders/builder.hpp"
3 
4#include "networks/testnet.hpp"
5 
6Ark::Crypto::Configuration config(Testnet);
7 
8auto transaction = Ark::Crypto::transactions::builder::Transfer(config)
9 .nonce(1)
10 .senderPublicKey("YOUR_SENDER_WALLET_PUBLICKEY")
11 .amount(1)
12 .expiration(0)
13 .recipientId("Address of Recipient")
14 .build();

Custom Network

1#include "common/configuration.hpp"
2#include "transactions/builders/builder.hpp"
3 
4#include "common/network.hpp"
5 
6const Ark::Crypto::Network CustomNetwork = {
7 "f39a61f04d6136a690a0b675ef6eedbd053665bd343b4e4f03311f12065fb875",
8 1, 0xCE, 0x41,
9 "2019-10-25T09:05:40.856Z"
10};
11 
12auto transaction = Ark::Crypto::transactions::builder::Transfer(CustomNetwork)
13 .nonce(1)
14 .senderPublicKey("YOUR_SENDER_WALLET_PUBLICKEY")
15 .amount(1)
16 .expiration(0)
17 .recipientId("Address of Recipient")
18 .build();

Creating and Broadcasting a Transfer Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Transaction
37 const auto transaction = builder::Transfer()
38 .nonce(nonce)
39 .amount(1ULL)
40 .expiration(0UL)
41 .recipientId("Address of Recipient")
42 .vendorField("Hello World")
43 .sign("this is a top secret passphrase")
44 .build();
45 
46 // Step 3: Construct the Transaction Payload
47 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
48 
49 // Step 4: Broadcast the Transaction Payload
50 const auto broadcastResponse = client.api.transactions.send(txPayload);
51 
52 // Step 5: Log the response
53 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
54 
55 return 0;
56}

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.

Creating and Broadcasting a Second Signature Registration Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Second PublicKey
37 // - 2nd PublicKey: 03699e966b2525f9088a6941d8d94f7869964a000efe65783d78ac82e1199fe609
38 const uint8_t secondPublicKey[33] = { 3, 105, 158, 150, 107, 37, 37, 249, 8, 138, 105, 65, 216, 217, 79, 120, 105, 150, 74, 0, 14, 254, 101, 120, 61, 120, 172, 130, 225, 25, 159, 230, 9 };
39 
40 // Step 3: Create the Transaction
41 const auto transaction = builder::SecondSignature()
42 .nonce(nonce)
43 .publicKey(secondPublicKey)
44 .sign("this is a top secret passphrase")
45 .build();
46 
47 // Step 4: Construct the Transaction Payload
48 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
49 
50 // Step 5: Broadcast the Transaction Payload
51 const auto broadcastResponse = client.api.transactions.send(txPayload);
52 
53 // Step 6: Log the response
54 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
55 
56 return 0;
57}

Creating and Broadcasting a Delegate Registration Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Transaction
37 const auto transaction = builder::DelegateRegistration()
38 .nonce(nonce)
39 .username("baldninja")
40 .sign("this is a top secret passphrase")
41 .build();
42 
43 // Step 3: Construct the Transaction Payload
44 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
45 
46 // Step 4: Broadcast the Transaction Payload
47 const auto broadcastResponse = client.api.transactions.send(txPayload);
48 
49 // Step 5: Log the response
50 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
51 
52 return 0;
53}

Creating and Broadcasting a Vote Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8#include "networks/mainnet.hpp"
9 
10using namespace Ark::Client;
11using namespace Ark::Crypto;
12using namespace Ark::Crypto::transactions;
13 
14// Configure our API client. For example, this time on a Mainnet peer
15Connection<Api> client("5.196.105.34", 4003);
16 
17// This is just one method of Parsing Json to obtain a Nonce;
18// it uses the built-in tools from C++ Client & Crypto.
19//
20// Other methods of parsing may be used here.
21uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
22 // Request the Wallet's Json from the API Connection
23 const auto walletResponse = client.api.wallets.get(address);
24 
25 // parse the Json Wallet Response to retrieve the nonce
26 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
27 DynamicJsonDocument doc(capacity);
28 deserializeJson(doc, walletResponse);
29 uint64_t nonce = doc["data"]["nonce"];
30 
31 return nonce ?: 1;
32}
33 
34int main() {
35 // Step 1: Retrieve the nonce of the sender wallet
36 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
37 
38 // Step 2: Create the 'Vote'
39 // - votes +022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d
40 // - vote: '+' / 0x01
41 // - unvote: '-' / 0x00
42 const uint8_t unvote[34] = { 1, 2, 44, 202, 149, 41, 236, 151, 167, 114, 21, 108, 21, 42, 0, 170, 209, 85, 238, 103, 8, 36, 62, 101, 201, 210, 17, 165, 137, 203, 93, 67, 35, 77 };
43 
44 // Step 3: Create the Transaction using Mainnet
45 const auto transaction = builder::Vote(Mainnet)
46 .nonce(nonce)
47 .votes(vote)
48 .sign("this is a top secret passphrase")
49 .build();
50 
51 // Step 4: Construct the Transaction Payload
52 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
53 
54 // Step 5: Broadcast the Transaction Payload
55 const auto broadcastResponse = client.api.transactions.send(txPayload);
56 
57 // Step 6: Log the response
58 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
59 
60 return 0;
61}

Information

Note the plus (1) 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.

Creating and Broadcasting an Unvote Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8#include "networks/mainnet.hpp"
9 
10using namespace Ark::Client;
11using namespace Ark::Crypto;
12using namespace Ark::Crypto::transactions;
13 
14// Configure our API client. For example, this time on a Mainnet peer
15Connection<Api> client("5.196.105.34", 4003);
16 
17// This is just one method of Parsing Json to obtain a Nonce;
18// it uses the built-in tools from C++ Client & Crypto.
19//
20// Other methods of parsing may be used here.
21uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
22 // Request the Wallet's Json from the API Connection
23 const auto walletResponse = client.api.wallets.get(address);
24 
25 // parse the Json Wallet Response to retrieve the nonce
26 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
27 DynamicJsonDocument doc(capacity);
28 deserializeJson(doc, walletResponse);
29 uint64_t nonce = doc["data"]["nonce"];
30 
31 return nonce ?: 1;
32}
33 
34int main() {
35 // Step 1: Retrieve the nonce of the sender wallet
36 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
37 
38 // Step 2: Create the 'Unvote'
39 // - votes -022cca9529ec97a772156c152a00aad155ee6708243e65c9d211a589cb5d43234d
40 // - vote: '+' / 0x01
41 // - unvote: '-' / 0x00
42 const uint8_t unvote[34] = { 0, 2, 44, 202, 149, 41, 236, 151, 167, 114, 21, 108, 21, 42, 0, 170, 209, 85, 238, 103, 8, 36, 62, 101, 201, 210, 17, 165, 137, 203, 93, 67, 35, 77 };
43 
44 // Step 3: You can also use a configuration for Mainnet or a custom network
45 Configuration config(Mainnet);
46 
47 // Step 4: Create the Transaction
48 const auto transaction = builder::Vote(config)
49 .nonce(nonce)
50 .votes(unvote)
51 .sign("this is a top secret passphrase")
52 .build();
53 
54 // Step 5: Construct the Transaction Payload
55 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
56 
57 // Step 6: Broadcast the Transaction Payload
58 const auto broadcastResponse = client.api.transactions.send(txPayload);
59 
60 // Step 7: Log the response
61 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
62 
63 return 0;
64}

Information

Note the minus (0) 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.

Creating and Broadcasting an Ipfs Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Ipfs Hash
37 // ipfs: QmYSK2JyM3RyDyB52caZCTKFR3HKniEcMnNJYdk8DQ6KKB
38 const uint8_t ipfsHash[34] = { 18, 32, 150, 8, 24, 77, 108, 238, 43, 154, 248, 230, 194, 164, 111, 201, 49, 138, 223, 115, 50, 154, 235, 138,134, 207, 132, 114, 130, 159, 255, 91, 184, 158 };
39 
40 // Step 3: Create the Transaction for Mainnet
41 const auto transaction = builder::Ipfs()
42 .nonce(nonce)
43 .ipfs(ipfsHash, 34)
44 .sign("this is a top secret passphrase")
45 .build();
46 
47 // Step 4: Construct the Transaction Payload
48 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
49 
50 // Step 5: Broadcast the Transaction Payload
51 const auto broadcastResponse = client.api.transactions.send(txPayload);
52 
53 // Step 6: Log the response
54 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
55 
56 return 0;
57}

Creating and Broadcasting a Multi Payment Transaction

1#include <array>
2#include <vector>
3 
4#include <iostream>
5 
6#include <arkClient.h>
7#include <arkCrypto.h>
8 
9#include "transactions/builders/builder.hpp"
10 
11using namespace Ark::Client;
12using namespace Ark::Crypto;
13using namespace Ark::Crypto::transactions;
14 
15// Configure our API client
16Connection<Api> client("167.114.29.52", 4003);
17 
18// This is just one method of Parsing Json to obtain a Nonce;
19// it uses the built-in tools from C++ Client & Crypto.
20//
21// Other methods of parsing may be used here.
22uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
23 // Request the Wallet's Json from the API Connection
24 const auto walletResponse = client.api.wallets.get(address);
25 
26 // parse the Json Wallet Response to retrieve the nonce
27 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
28 DynamicJsonDocument doc(capacity);
29 deserializeJson(doc, walletResponse);
30 uint64_t nonce = doc["data"]["nonce"];
31 
32 return nonce ?: 1;
33}
34 
35int main() {
36 // Step 1: Retrieve the nonce of the sender wallet
37 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
38 
39 
40 // Step 2: Create the Amounts and Addresses objects
41 const auto amounts = std::vector<uint64_t>({ 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL, 1ULL });
42 // addresses: "DHKxXag9PjfjHBbPg3HQS5WCaQZdgDf6yi",
43 // "DBzGiUk8UVjB2dKCfGRixknB7Ki3Zhqthp",
44 // "DFa7vn1LvWAyTuVDrQUr5NKaM73cfjx2Cp",
45 // "DSGsxX84gif4ipAxZjjCE2k2YpHmsNTJeY",
46 // "DQhzMRvVoCYCiZH2iSyuqCTcayz7z4XTKx",
47 // "DMSD6fFT1Xcxh4ErYExr5MtGnEuDcYu22m",
48 // "D7HCZG8hJuJqu9rANRdyNr6N1vpH2vbyx8",
49 // "DQy6ny2bkvWiDLboQMZ1cxnmoNC5JM228w",
50 // "D7EUgmA78qUaSfsNedfgKs2ALq28FhL3zo",
51 // "DEHyKHdtzHqTghfpwaBcvTzLpgPP5AAUgE"
52 const auto addresses = std::vector<std::array<uint8_t, 21> >({
53 { 30, 133, 175, 101, 173, 106, 163, 161, 223, 91, 201, 166, 245, 155, 159, 61, 57, 79, 100, 58, 35 },
54 { 30, 75, 29, 161, 170, 169, 188, 174, 197, 170, 246, 118, 4, 120, 1, 7, 12, 76, 18, 71, 193 },
55 { 30, 114, 108, 208, 211, 224, 41, 32, 188, 193, 79, 109, 87, 165, 247, 47, 100, 210, 112, 206, 227 },
56 { 30, 231, 211, 119, 107, 126, 111, 22, 25, 170, 218, 56, 165, 252, 113, 167, 183, 243, 185, 186, 200 },
57 { 30, 214, 162, 243, 198, 57, 152, 44, 0, 89, 206, 95, 226, 30, 94, 116, 33, 45, 106, 140, 63 },
58 { 30, 178, 190, 143, 12, 23, 91, 172, 102, 34, 216, 126, 212, 216, 73, 93, 113, 191, 188, 73, 243 },
59 { 30, 23, 120, 154, 45, 2, 165, 13, 103, 200, 74, 101, 226, 221, 246, 68, 137, 57, 210, 11, 124 },
60 { 30, 217, 126, 145, 87, 206, 191, 199, 71, 134, 227, 49, 91, 164, 170, 123, 164, 10, 223, 107, 80 },
61 { 30, 22, 244, 209, 133, 76, 10, 63, 34, 251, 44, 242, 46, 28, 139, 137, 46, 108, 158, 83, 14 },
62 { 30, 100, 102, 216, 129, 91, 2, 33, 119, 141, 36, 36, 214, 55, 142, 101, 144, 24, 178, 175, 62 }})
63 
64 // Step 3: Create the Transaction
65 const auto transaction = builder::MultiPayment()
66 .nonce(nonce)
67 .n_payments(10UL)
68 .amounts(amounts)
69 .addresses(addresses)
70 .sign("this is a top secret passphrase")
71 .build();
72 
73 // Step 4: Construct the Transaction Payload
74 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
75 
76 // Step 5: Broadcast the Transaction Payload
77 const auto broadcastResponse = client.api.transactions.send(txPayload);
78 
79 // Step 6: Log the response
80 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
81 
82 return 0;
83}

Creating and Broadcasting a Delegate Resignation Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Transaction
37 const auto transaction = builder::DelegateResignation()
38 .nonce(nonce)
39 .fee(2500000000ULL)
40 .sign("this is a top secret passphrase")
41 .build();
42 
43 // Step 3: Construct the Transaction Payload
44 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
45 
46 // Step 4: Broadcast the Transaction Payload
47 const auto broadcastResponse = client.api.transactions.send(txPayload);
48 
49 // Step 5: Log the response
50 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
51 
52 return 0;
53}

Information

A delegate resignation has to be sent from the delegate wallet itself to verify its identity.

Creating and Broadcasting a Htlc Lock Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Secret Hash object
37 const uint8_t secretHash[32] = { 9, 185, 162, 131, 147, 239, 208, 47, 205, 118, 162, 27, 15, 15, 85, 186, 42, 173, 143, 54, 64, 255, 140, 174, 134, 222, 3, 58, 156, 251, 215, 140 };
38 
39 // Step 3: Create the Transaction
40 const auto transaction = builder::HtlcLock()
41 .nonce(nonce)
42 .amount(1ULL)
43 .secretHash(secretHash)
44 .expirationType(1U)
45 .expiration(81242895UL)
46 .recipientId("Address of Recipient")
47 .sign("this is a top secret passphrase")
48 .build();
49 
50 // Step 4: Construct the Transaction Payload
51 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
52 
53 // Step 5: Broadcast the Transaction Payload
54 const auto broadcastResponse = client.api.transactions.send(txPayload);
55 
56 // Step 6: Log the response
57 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
58 
59 return 0;
60}

Creating and Broadcasting a Htlc Claim Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Lock TransactionId and Unlock Secret objects
37 // - 50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e
38 const uint8_t lockTxId[32] = { 80, 243, 1, 48, 109, 141, 165, 141, 98, 51, 44, 227, 163, 97, 217, 253, 74, 1, 176, 168, 156, 169, 20, 81, 123, 104, 94, 45, 55, 20, 226, 78 };
39 // - sha256("f5ea877a311ced90cf4524cb489e972f")
40 // - 580fde2946477f24b288890faea69fa120a14cfa5ea4a9819508331a034d9585
41 const uint8_t unlockSecret[32] = { 88, 15, 222, 41, 70, 71, 127, 36, 178, 136, 137, 15, 174, 166, 159, 161, 32, 161, 76, 250, 94, 164, 169, 129, 149, 8, 51, 26, 3, 77, 149, 133 };
42 
43 // Step 3: Create the Transaction
44 const auto transaction = builder::HtlcClaim()
45 .nonce(nonce)
46 .lockTransactionId(lockTxId)
47 .unlockSecret(unlockSecret)
48 .sign("this is a top secret passphrase")
49 .build();
50 
51 // Step 4: Construct the Transaction Payload
52 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
53 
54 // Step 5: Broadcast the Transaction Payload
55 const auto broadcastResponse = client.api.transactions.send(txPayload);
56 
57 // Step 6: Log the response
58 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
59 
60 return 0;
61}

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.

Creating and Broadcasting a Htlc Refund Transaction

1#include <iostream>
2 
3#include <arkClient.h>
4#include <arkCrypto.h>
5 
6#include "transactions/builders/builder.hpp"
7 
8using namespace Ark::Client;
9using namespace Ark::Crypto;
10using namespace Ark::Crypto::transactions;
11 
12// Configure our API client
13Connection<Api> client("167.114.29.52", 4003);
14 
15// This is just one method of Parsing Json to obtain a Nonce;
16// it uses the built-in tools from C++ Client & Crypto.
17//
18// Other methods of parsing may be used here.
19uint64_t getWalletNonce(Connection<Api>& client, const char\* address) {
20 // Request the Wallet's Json from the API Connection
21 const auto walletResponse = client.api.wallets.get(address);
22 
23 // parse the Json Wallet Response to retrieve the nonce
24 const size_t capacity = JSON_OBJECT_SIZE(0) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(6) + 160;
25 DynamicJsonDocument doc(capacity);
26 deserializeJson(doc, walletResponse);
27 uint64_t nonce = doc["data"]["nonce"];
28 
29 return nonce ?: 1;
30}
31 
32int main() {
33 // Step 1: Retrieve the nonce of the sender wallet
34 const auto nonce = getWalletNonce(client, "YOUR_SENDER_WALLET_ADDRESS");
35 
36 // Step 2: Create the Lock TransactionId object
37 // - 50f301306d8da58d62332ce3a361d9fd4a01b0a89ca914517b685e2d3714e24e
38 const uint8_t lockTxId[32] = { 80, 243, 1, 48, 109, 141, 165, 141, 98, 51, 44, 227, 163, 97, 217, 253, 74, 1, 176, 168, 156, 169, 20, 81, 123, 104, 94, 45, 55, 20, 226, 78 };
39 
40 // Step 3: Create the Transaction
41 const auto transaction = builder::HtlcRefund()
42 .nonce(nonce)
43 .lockTransactionId(lockTxId)
44 .sign("this is a top secret passphrase")
45 .build();
46 
47 // Step 4: Construct the Transaction Payload
48 auto txPayload = "{\"transactions\":[" + transaction.toJson() + "]}";
49 
50 // Step 5: Broadcast the Transaction Payload
51 const auto broadcastResponse = client.api.transactions.send(txPayload);
52 
53 // Step 6: Log the response
54 printf("\nbroadcastResponse: %s\n", broadcastResponse.c_str());
55 
56 return 0;
57}
Last updated 3 years ago
Edit Page
Share: