forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetUtils.cpp
120 lines (119 loc) · 4.49 KB
/
assetUtils.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
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
#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"
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());
std::vector<uint8_t> buffer;
qc->receiveDataAll(buffer);
uint8_t* data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RESPOND_OWNED_ASSETS){
auto roa = (RespondOwnedAssets *)(data + ptr + sizeof(RequestResponseHeader));
result.push_back(* roa);
}
ptr+= header->size();
}
return result;
}
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;
RequestOwnedAssets 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());
std::vector<uint8_t> buffer;
qc->receiveDataAll(buffer);
uint8_t* data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RESPOND_POSSESSED_ASSETS){
auto rpa = (RespondPossessedAssets *)(data + ptr + sizeof(RequestResponseHeader));
result.push_back(* rpa);
}
ptr+= header->size();
}
return result;
}
static void printOwnedAsset(Asset owned, Asset 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 Units: %d\n", owned.varStruct.ownership.numberOfUnits);
}
static void printPossessionAsset(Asset owner, Asset possession, Asset 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 Units: %d\n", possession.varStruct.possession.numberOfUnits);
}
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);
LOG("Tick: %u\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);
LOG("Tick: %u\n", rpa.tick);
}
}