Node
A node contains multiple blockchains and is a point of service. It connects with other nodes and binds together the nature of a blockchain.
Creating a node instance
To create a node instance do the following:
#include "ULC.hpp"
using namespace ULC;
using namespace ULC::NODE;
UL_Node node{};
Setting the URL of the node
To set the URL of the node on the network do the following:
node.SetNodeUrl("my_node_url");
When running a node locally, you can use "UL_DEFAULT_NODE_URL".
Setting the atomic clock URL for a node instance
The atomic clock is used for atomicly timing transactions on a node across a network. To set the atomic clock URL for a node instance do the following:
node.SetAtomicClockUrl("my_atomic_clock_url");
By default the atomic clock URL is set to "UL_DEFAULT_ATOMIC_CLOCK_URL".
Setting the node Id for the node instance
To set the Id of a node instance do the following:
node.SetNodeId("my_node_id");
Giving authority to a node instance
To give authentication credentials to a node instance do the following:
A certificate file is only needed when you experience issues with openssl.
// Add the using declaration for brevity
using namespace ULC::AUTH;
// Create the auth instance
UL_Auth auth{};
auth.SetCertFilePath("cacert.pem"); // This is your certificate file included with ULC.
auth.SetBearer("My JWT Token"); // For use with private blockchains.
auth.SetApiKey("My api key"); // Interchangeable with the bearer, but this allows explicitly setting a key for an api.
node.SetAuth(auth);
The same auth instance can be used for anything that requires authentication. This is needed for when interacting with private blockchains.
Setting options for a node instance
To set options on a node instance do the following:
// Add the using declaration for brevity
using namespace ULC::HTTP;
UL_Options options{};
options.VerboseLogs(true); // For verbose curl logs.
options.UnsafeRequest(false); // For debugging those pesky calls, do not leave true for a production network as this leaves you vulnerable to man in the middle attacks.
options.Timeout = UL_DEFAULT_HTTP_REQUEST_TIMEOUT; // How many seconds before timeout on http calls.
node.SetOptions(options);
Getting the version of a node
Getting the version of a node is a great way to ping the service and check that the node is accessible.
To get the version of a node do the following:
// Include string
#include <string>
// Get the version of the bms
std::string versionJson;
bool success = node.GetVersion(versionJson);
if (!success) {
// Handle error
}
else {
std::cout << "Version: " << versionJson.c_str() << std::endl;
}
Registering a wallet with a blockchain on a node
Registering a wallet with a blockchain is required to make transactions with that wallet.
To register a wallet that you created, see Create a wallet, do the following:
...// Wallet was created here
bool success = node.RegisterWalletWithBlockchain("my_blockchain_id", wallet.GetPublicKey());
if (!success) {
// Handle error.
}
else {
std::cout << "Wallet Registered" << std::endl;
}
Getting a timestamp from the atomic clock service (ACS)
To get a timestamp from the atomic clock service do the following:
UL_Timestamp timestamp{}; // Timestamps are made up of the timestamp and the index at which the timestamp was created.
bool success = node.GetTimestamp(timestamp);
Posting a transaction to a node's blockchain
This requires a registered wallet to perform.
To post a transaction to a node's blockchain with the wallet do the following:
...// Wallet was created here
// Create a transaction to send
UL_NodeTransaction transaction{};
transaction.From = wallet.GetAddress();
transaction.To = "to_wallet_address";
transaction.AtomicTimestamp = to_string(timestamp.Timestamp) + "_" + to_string(timestamp.Index);
// The payload must be valid json, it can be an empty object {}
transaction.Payload = "{}";
transaction.Payload = R"({"myPayload":"HelloThere"})";
transaction.PayloadType = "DATA";
// The from signature is the signature of the from address and the atomic timestamp.
transaction.FromSignature = wallet.GetFromSignature(transaction.Payload, transaction.AtomicTimestamp); // The payload is added to the signature to ensure the payload was not tampered in transit.
UL_TransactionResponse response{};
success = node.PostTransaction("my_blockchain_id", transaction, response);
if (!success) {
// Handle error.
}
else {
std::cout << "Transaction: " << response.ToString() << std::endl;
}