-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.cpp
82 lines (66 loc) · 2.4 KB
/
src.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// Set up the Counterparty API endpoint
const std::string CP_API = "http://localhost:14000/api/";
// Helper function to send an HTTP POST request to the Counterparty API
std::string send_request(const std::string& method, const json& data) {
std::string url = CP_API + method;
std::string post_data = data.dump();
CURL *curl;
CURLcode res;
std::string response;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char* ptr, size_t size, size_t nmemb, std::string* userdata){
userdata->append(ptr, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "Error sending HTTP request: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return response;
}
int main() {
// Create a new asset
std::string asset_name = "MyNFT";
std::string asset_description = "My first NFT on Counterparty";
json asset_data = {
{"artist", "John Smith"},
{"year", 2023},
{"medium", "Oil on canvas"}
};
json issuance_data = {
{"source", "myaddress"},
{"asset", asset_name},
{"quantity", 1},
{"description", asset_description},
{"divisible", false},
{"callable", false},
{"transfer_destination", "myaddress"},
{"data", asset_data}
};
std::string issuance_response = send_request("create_issuance", issuance_data);
json issuance_result = json::parse(issuance_response);
std::cout << "Asset created: " << issuance_result.dump() << std::endl;
// Issue the new asset
json tx_data = {
{"tx_hex", issuance_result},
{"allow_unconfirmed_inputs", true}
};
std::string tx_response = send_request("send_tx", tx_data);
json tx_result = json::parse(tx_response);
std::cout << "Asset issued: " << tx_result.dump() << std::endl;
return 0;
}