forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassetUtils.cpp
More file actions
409 lines (384 loc) · 16.3 KB
/
assetUtils.cpp
File metadata and controls
409 lines (384 loc) · 16.3 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <cstdint>
#include <cstring>
#include "structs.h"
#include "walletUtils.h"
#include "keyUtils.h"
#include "assetUtil.h"
#include "connection.h"
#include "logger.h"
#include "nodeUtils.h"
#include "K12AndKeyUtil.h"
#include "utils.h"
#include "sanityCheck.h"
std::vector<RespondOwnedAssets> getOwnedAsset(const char * nodeIp, const int nodePort, const char* requestedIdentity)
{
std::vector<RespondOwnedAssets> result;
uint8_t publicKey[32] = {0};
getPublicKeyFromIdentity(requestedIdentity, publicKey);
struct {
RequestResponseHeader header;
RequestOwnedAssets req;
} packet;
memcpy(packet.req.publicKey, publicKey, 32);
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_OWNED_ASSETS);
auto qc = make_qc(nodeIp, nodePort);
qc->sendData((uint8_t *) &packet, packet.header.size());
return qc->getLatestVectorPacketAs<RespondOwnedAssets>();
}
std::vector<RespondPossessedAssets> getPossessionAsset(const char * nodeIp, const int nodePort, const char* requestedIdentity)
{
std::vector<RespondPossessedAssets> result;
uint8_t publicKey[32] = {0};
getPublicKeyFromIdentity(requestedIdentity, publicKey);
struct {
RequestResponseHeader header;
RequestPossessedAssets req;
} packet;
memcpy(packet.req.publicKey, publicKey, 32);
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_POSSESSED_ASSETS);
auto qc = make_qc(nodeIp, nodePort);
qc->sendData((uint8_t *) &packet, packet.header.size());
return qc->getLatestVectorPacketAs<RespondPossessedAssets>();
}
template<typename T>
void getAssetDigest(T& respondedAsset, uint8_t* assetDigest)
{
// Check if the size of entity is good
const size_t asset_size = sizeof(respondedAsset.asset);
if (asset_size != 48)
{
LOG("Size of asset is unexpected: %lu\n", sizeof(asset_size));
return;
}
// Compute the root hash from the entity and siblings
if (respondedAsset.universeIndex < 0 )
{
LOG("Universe index is invalid: %d\n", respondedAsset.universeIndex);
return;
}
// Compute the spectrum digest
getDigestFromSiblings<32>(
ASSETS_DEPTH,
(uint8_t*)(&respondedAsset.asset),
asset_size,
respondedAsset.universeIndex,
respondedAsset.siblings,
assetDigest);
}
template<typename T>
void printAssetDigest(T& respondedAsset)
{
uint8_t assetDigest[32];
getAssetDigest(respondedAsset, assetDigest);
char hex_digest[65];
byteToHex(assetDigest, hex_digest, 32);
LOG("Asset Digest: %s\n", hex_digest);
}
static void printOwnedAsset(const AssetRecord& owned, const AssetRecord& iss)
{
char issuer[128] = {0};
char name[8] = {0};
char unitOfMeasurement[7] = {0};
memcpy(name, iss.varStruct.issuance.name, 7);
memcpy(unitOfMeasurement, iss.varStruct.issuance.unitOfMeasurement, 7);
getIdentityFromPublicKey(iss.varStruct.issuance.publicKey, issuer, false);
LOG("Asset issuer: %s\n", issuer);
LOG("Asset name: %s\n", name);
LOG("Managing contract index: %d\n", owned.varStruct.ownership.managingContractIndex);
LOG("Issuance Index: %d\n", owned.varStruct.ownership.issuanceIndex);
LOG("Number Of Shares: %lld\n", owned.varStruct.ownership.numberOfShares);
}
static void printPossessionAsset(const AssetRecord& owner, const AssetRecord& possession, const AssetRecord& iss)
{
char issuer[128] = {0};
char name[8] = {0};
char ownerId[128] = {0};
getIdentityFromPublicKey(owner.varStruct.ownership.publicKey, ownerId, false);
memcpy(name, iss.varStruct.issuance.name, 7);
getIdentityFromPublicKey(iss.varStruct.issuance.publicKey, issuer, false);
LOG("Asset issuer: %s\n", issuer);
LOG("Asset name: %s\n", name);
LOG("Managing contract index: %d\n", possession.varStruct.possession.managingContractIndex);
LOG("Owner index: %u\n", possession.varStruct.possession.ownershipIndex);
LOG("Owner ID: %s\n", ownerId);
LOG("Number Of Shares: %lld\n", possession.varStruct.possession.numberOfShares);
}
void printOwnedAsset(const char * nodeIp, const int nodePort, const char* requestedIdentity)
{
LOG("======== OWNERSHIP ========\n");
auto vroa = getOwnedAsset(nodeIp, nodePort, requestedIdentity);
for (auto& roa : vroa)
{
printOwnedAsset(roa.asset, roa.issuanceAsset);
printAssetDigest(roa);
LOG("Tick: %u\n\n", roa.tick);
}
}
void printPossessionAsset(const char * nodeIp, const int nodePort, const char* requestedIdentity)
{
LOG("======== POSSESSION ========\n");
auto vrpa = getPossessionAsset(nodeIp, nodePort, requestedIdentity);
for (auto& rpa : vrpa)
{
printPossessionAsset(rpa.ownershipAsset, rpa.asset, rpa.issuanceAsset);
printAssetDigest(rpa);
LOG("Tick: %u\n\n", rpa.tick);
}
}
void printAssetResponse(const RespondAssets& response, bool verbose)
{
char identity[128] = { 0 };
char name[8] = { 0 };
switch (response.asset.varStruct.issuance.type)
{
case EMPTY:
LOG("Empty slot in universe\n");
break;
case ISSUANCE:
getIdentityFromPublicKey(response.asset.varStruct.issuance.publicKey, identity, false);
memcpy(name, response.asset.varStruct.issuance.name, 7);
LOG("Asset (issuance)\n");
LOG("\tname = %s\n", name);
LOG("\tissuer = %s\n", identity);
LOG("\tnumber of decimal places = %d\n", (int)response.asset.varStruct.issuance.numberOfDecimalPlaces);
// TODO: print response.asset.varStruct.issuance.unitOfMeasurement;
break;
case OWNERSHIP:
getIdentityFromPublicKey(response.asset.varStruct.ownership.publicKey, identity, false);
LOG("Share ownership\n");
LOG("\towner = %s\n", identity);
LOG("\tnumber of shares = %lld\n", response.asset.varStruct.ownership.numberOfShares);
LOG("\tmanaging contract = %u\n", (unsigned int)response.asset.varStruct.ownership.managingContractIndex);
if (verbose)
{
LOG("\tissuance index = %u\n", response.asset.varStruct.ownership.issuanceIndex);
}
break;
case POSSESSION:
getIdentityFromPublicKey(response.asset.varStruct.possession.publicKey, identity, false);
LOG("Share possession\n");
LOG("\tpossessor = %s\n", identity);
LOG("\tnumber of shares = %lld\n", response.asset.varStruct.possession.numberOfShares);
LOG("\tmanaging contract = %u\n", (unsigned int)response.asset.varStruct.possession.managingContractIndex);
if (verbose)
{
LOG("\townership index = %u\n", response.asset.varStruct.possession.ownershipIndex);
}
break;
}
if (verbose)
{
LOG("\tindex = %u\n", response.universeIndex);
LOG("\ttick = %u\n", response.tick);
}
}
void printAssetResponseWithSiblings(const RespondAssetsWithSiblings& response, bool verbose)
{
printAssetResponse(response, verbose);
uint8_t assetDigest[32];
getAssetDigest(response, assetDigest);
char hex_digest[65];
byteToHex(assetDigest, hex_digest, 32);
LOG("\tuniverse digest = %s\n", hex_digest);
}
void printAssetRecordsHelpAndExit()
{
LOG("qubic-cli -queryassets [type] [query]\n");
LOG(" [type] is one of the following (without \"):\n");
LOG(" - \"issuances\" or \"i\": Get asset issuance records. For filtering, [query] can be comma-separated list with:\n");
LOG(" - \"issuer=[identity]\": Only consider assets of the given issuer.\n");
LOG(" - \"name=[name]\": Only consider assets with the given name.\n");
LOG(" - \"ownerships\" or \"o\": Get asset ownership records, [query] is comma-separated list with at least \"name\".\n");
LOG(" - \"issuer=[identity]\": Issuer of shares to consider (skip for NULL_ID / contract shares).\n");
LOG(" - \"name=[name]\": Asset name of shares to consider.\n");
LOG(" - \"owner=[identity]\": Owner of shares to consider (skip for all owners).\n");
LOG(" - \"oc=[index]\": Index of contract managing ownership (skip for all contracts).\n");
LOG(" - \"possessions\" or \"p\": Get asset possession records, [query] is like for ownerships, but may additionally have:\n");
LOG(" - \"possessor=[identity]\": Possessor of shares to consider (skip for all possessors).\n");
LOG(" - \"pc=[index]\": Index of contract managing possession (skip for all contracts).\n");
LOG(" - \"idx\": Get asset record with given universe index, [query] is the index number.\n");
exit(1);
}
void printAssetRecords(const char* nodeIp, const int nodePort, const char* requestType, const char* requestQueryString)
{
#ifdef _MSC_VER
#define strcasecmp _stricmp
#endif
bool withSiblings = false;
bool verbose = true;
struct {
RequestResponseHeader header;
RequestAssets req;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestAssets::type());
memset(&packet.req, 0, sizeof(packet.req));
if (withSiblings)
packet.req.byFilter.flags |= RequestAssets::getSiblings;
bool queryOwnerships = strcasecmp(requestType, "ownerships") == 0 || strcasecmp(requestType, "o") == 0;
bool queryPossessions = strcasecmp(requestType, "possessions") == 0 || strcasecmp(requestType, "p") == 0;
if (strcasecmp(requestType, "idx") == 0)
{
packet.req.assetReqType = RequestAssets::requestByUniverseIdx;
if (requestQueryString == nullptr || sscanf(requestQueryString, "%u", &packet.req.byUniverseIdx.universeIdx) != 1)
{
LOG("Error: Expected positive number (index in universe), found \"%s\"!\n", requestQueryString ? requestQueryString : "");
exit(1);
}
packet.header.setSize(sizeof(packet.header) + sizeof(packet.req.byUniverseIdx));
}
else if (strcasecmp(requestType, "issuances") == 0 || strcasecmp(requestType, "i") == 0)
{
packet.req.assetReqType = RequestAssets::requestIssuanceRecords;
packet.req.byFilter.flags = RequestAssets::anyIssuer | RequestAssets::anyAssetName;
if (requestQueryString != nullptr && strlen(requestQueryString) > 0)
{
auto assignments = splitString(requestQueryString, ",");
for (std::string& assignment : assignments)
{
auto parts = splitString(assignment, "=");
std::string id = parts[0];
std::string value = parts.size() > 1 ? parts[1] : "";
if (strcasecmp(id.c_str(), "issuer") == 0)
{
packet.req.byFilter.flags &= ~RequestAssets::anyIssuer;
sanityCheckIdentity(value.c_str());
getPublicKeyFromIdentity(value.c_str(), packet.req.byFilter.issuer);
}
else if (strcasecmp(id.c_str(), "name") == 0)
{
packet.req.byFilter.flags &= ~RequestAssets::anyAssetName;
sanityCheckValidAssetName(value.c_str());
memcpy(&packet.req.byFilter.assetName, value.c_str(), std::min(size_t(7), value.length()));
}
else
{
LOG("Error: Invalid identifier in query string. Expected \"issuer\" or \"name\", found \"%s\"!\n", id.c_str());
exit(1);
}
}
}
}
else if (queryOwnerships || queryPossessions)
{
if (queryOwnerships)
packet.req.assetReqType = RequestAssets::requestOwnershipRecords;
else
packet.req.assetReqType = RequestAssets::requestPossessionRecords;
packet.req.byFilter.flags = RequestAssets::anyOwner | RequestAssets::anyPossessor | RequestAssets::anyOwnershipManagingContract | RequestAssets::anyPossessionManagingContract;
bool issuerGiven = false, nameGiven = false;
if (requestQueryString != nullptr && strlen(requestQueryString) > 0)
{
auto assignments = splitString(requestQueryString, ",");
for (std::string& assignment : assignments)
{
auto parts = splitString(assignment, "=");
std::string id = parts[0];
std::string value = parts.size() > 1 ? parts[1] : "";
if (strcasecmp(id.c_str(), "issuer") == 0)
{
issuerGiven = true;
sanityCheckIdentity(value.c_str());
getPublicKeyFromIdentity(value.c_str(), packet.req.byFilter.issuer);
}
else if (strcasecmp(id.c_str(), "name") == 0)
{
nameGiven = true;
sanityCheckValidAssetName(value.c_str());
memcpy(&packet.req.byFilter.assetName, value.c_str(), std::min(7, (int)value.length()));
}
else if (strcasecmp(id.c_str(), "owner") == 0)
{
packet.req.byFilter.flags &= ~RequestAssets::anyOwner;
sanityCheckIdentity(value.c_str());
getPublicKeyFromIdentity(value.c_str(), packet.req.byFilter.owner);
}
else if (strcasecmp(id.c_str(), "possessor") == 0)
{
if (queryOwnerships)
{
LOG("Warning: Ignoring %s, because ownership records are queried!\n", assignment.c_str());
continue;
}
packet.req.byFilter.flags &= ~RequestAssets::anyPossessor;
sanityCheckIdentity(value.c_str());
getPublicKeyFromIdentity(value.c_str(), packet.req.byFilter.possessor);
}
else if (strcasecmp(id.c_str(), "oc") == 0)
{
packet.req.byFilter.flags &= ~RequestAssets::anyOwnershipManagingContract;
if (sscanf(value.c_str(), "%hu", &packet.req.byFilter.ownershipManagingContract) != 1)
{
LOG("Error: Expected unsigned short number for ownership managing contract index, found \"%s\"!\n", value.c_str());
exit(1);
}
}
else if (strcasecmp(id.c_str(), "pc") == 0)
{
if (queryOwnerships)
{
LOG("Warning: Ignoring %s, because ownership records are queried!\n", assignment.c_str());
continue;
}
packet.req.byFilter.flags &= ~RequestAssets::anyPossessionManagingContract;
if (sscanf(value.c_str(), "%hu", &packet.req.byFilter.possessionManagingContract) != 1)
{
LOG("Error: Expected unsigned short number for possession managing contract index, found \"%s\"!\n", value.c_str());
exit(1);
}
}
else
{
LOG("Error: Invalid identifier in query string. Found \"%s\"!\n", id.c_str());
printAssetRecordsHelpAndExit();
}
}
}
if (!nameGiven)
{
LOG("Error: You need to specify an asset name with name=[NAME]!\n");
exit(1);
}
if (!issuerGiven)
{
LOG("Warning: No issuer given, assuming NULL_ID (issued by quorum like contract shares).\n");
}
}
else
{
printAssetRecordsHelpAndExit();
}
auto qc = make_qc(nodeIp, nodePort);
qc->sendData((uint8_t*)&packet, packet.header.size());
bool receivedResponses = false;
if (withSiblings)
{
auto responses = qc->getLatestVectorPacketAs<RespondAssetsWithSiblings>();
for (const auto& response : responses)
{
printAssetResponseWithSiblings(response, verbose);
receivedResponses = true;
}
}
else
{
auto responses = qc->getLatestVectorPacketAs<RespondAssets>();
for (const auto& response : responses)
{
printAssetResponse(response, verbose);
receivedResponses = true;
}
}
if (!receivedResponses)
{
LOG("No assets match your query.\n");
}
#ifdef _MSC_VER
#undef strcasecmp
#endif
}