forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qx.cpp
516 lines (481 loc) · 19.8 KB
/
qx.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#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 "qx.h"
#include "qxStruct.h"
#define QX_CONTRACT_INDEX 1
#define QX_ADDRESS "BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARMID"
// QX PROCEDURES
#define QX_GET_FEE_PR 1
#define QX_GET_ASSET_ASK_ORDER_PR 2
#define QX_GET_ASSET_BID_ORDER_PR 3
#define QX_GET_ENTITY_ASK_ORDER_PR 4
#define QX_GET_ENTITY_BID_ORDER_PR 5
// QX FUNCTIONS
#define QX_ISSUE_ASSET_FN 1
#define QX_TRANSFER_SHARE_FN 2
#define QX_PLACEHOLDER0_FN 3
#define QX_PLACEHOLDER1_FN 4
#define QX_ADD_ASK_ORDER_FN 5
#define QX_ADD_BID_ORDER_FN 6
#define QX_REMOVE_ASK_ORDER_FN 7
#define QX_REMOVE_BID_ORDER_FN 8
void getQxFees(const char* nodeIp, const int nodePort, QxFees_output& result){
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = 0;
packet.rcf.inputType = QX_GET_FEE_PR;
packet.rcf.contractIndex = QX_CONTRACT_INDEX;
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;
memset(&result, 0, sizeof(result));
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RespondContractFunction::type()){
if (recvByte - ptr - sizeof(RequestResponseHeader) >= sizeof(QxFees_output)){
auto fees = (QxFees_output*)(data + ptr + sizeof(RequestResponseHeader));
result = *fees;
}
}
ptr+= header->size();
}
}
void printQxFee(const char* nodeIp, const int nodePort){
QxFees_output result;
getQxFees(nodeIp, nodePort, result);
LOG("Asset issuance fee: %u\n", result.assetIssuanceFee);
LOG("Transfer fee: %u\n", result.transferFee);
LOG("Trade fee: %u\n", result.tradeFee);
}
void qxIssueAsset(const char* nodeIp, int nodePort,
const char* seed,
const char* assetName,
const char* unitOfMeasurement,
int64_t numberOfUnits,
char numberOfDecimalPlaces,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
char assetNameS1[8] = {0};
char UoMS1[8] = {0};
memcpy(assetNameS1, assetName, strlen(assetName));
for (int i = 0; i < 7; i++) UoMS1[i] = unitOfMeasurement[i] - 48;
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QX_ADDRESS, destPublicKey);
struct {
RequestResponseHeader header;
Transaction transaction;
IssueAsset_input ia;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = 1000000000;
uint32_t scheduledTick = 0;
if (scheduledTickOffset < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + scheduledTickOffset;
} else {
scheduledTick = scheduledTickOffset;
}
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QX_ISSUE_ASSET_FN;
packet.transaction.inputSize = sizeof(IssueAsset_input);
// fill the input
memcpy(&packet.ia.name, assetNameS1, 8);
memcpy(&packet.ia.unitOfMeasurement, UoMS1, 8);
packet.ia.numberOfUnits = numberOfUnits;
packet.ia.numberOfDecimalPlaces = numberOfDecimalPlaces;
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(IssueAsset_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(IssueAsset_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(IssueAsset_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.ia));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
void qxTransferAsset(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const char* newOwnerIdentity,
long long numberOfUnits,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
uint8_t issuer[32] = {0};
uint8_t newOwnerPublicKey[32] = {0};
char txHash[128] = {0};
char assetNameU1[8] = {0};
memcpy(assetNameU1, pAssetName, strlen(pAssetName));
if (strlen(pIssuerInQubicFormat) != 60){
LOG("WARNING: Stop supporting hex format, please use qubic format 60-char length addresses\n");
exit(0);
}
getPublicKeyFromIdentity(pIssuerInQubicFormat, issuer);
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QX_ADDRESS, destPublicKey);
getPublicKeyFromIdentity(newOwnerIdentity, newOwnerPublicKey);
struct {
RequestResponseHeader header;
Transaction transaction;
TransferAssetOwnershipAndPossession_input ta;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = 1000000;
uint32_t currentTick = getTickNumberFromNode(qc);
uint32_t scheduledTick = currentTick + scheduledTickOffset;
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QX_TRANSFER_SHARE_FN;
packet.transaction.inputSize = sizeof(TransferAssetOwnershipAndPossession_input);
// fill the input
memcpy(&packet.ta.assetName, assetNameU1, 8);
memcpy(packet.ta.issuer, issuer, 32);
memcpy(packet.ta.newOwnerAndPossessor, newOwnerPublicKey, 32);
packet.ta.numberOfUnits = numberOfUnits;
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(TransferAssetOwnershipAndPossession_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(TransferAssetOwnershipAndPossession_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(TransferAssetOwnershipAndPossession_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.ta));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
template <int functionNumber>
void qxOrderAction(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long price,
const long long numberOfShares,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
uint8_t issuer[32] = {0};
char txHash[128] = {0};
char assetNameU1[8] = {0};
memcpy(assetNameU1, pAssetName, strlen(pAssetName));
if (strlen(pIssuerInQubicFormat) != 60){
LOG("WARNING: Stop supporting hex format, please use qubic format 60-char length addresses\n");
exit(0);
}
getPublicKeyFromIdentity(pIssuerInQubicFormat, issuer);
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QX_ADDRESS, destPublicKey);
struct {
RequestResponseHeader header;
Transaction transaction;
qxOrderAction_input qoa;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = 1; // free
if (functionNumber == QX_ADD_BID_ORDER_FN){
packet.transaction.amount = price * numberOfShares;
}
uint32_t currentTick = getTickNumberFromNode(qc);
uint32_t scheduledTick = currentTick + scheduledTickOffset;
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = functionNumber;
packet.transaction.inputSize = sizeof(qxOrderAction_input);
// DEBUG LOG
LOG("\n-------------------------------------\n\n");
LOG("Sending QX action - functionNumber: %d\n", functionNumber);
LOG("Issuer: %s\n", pIssuerInQubicFormat);
LOG("assetName: %s\n", assetNameU1);
LOG("price: %lld\n", price);
LOG("numberOfShares: %lld\n", numberOfShares);
LOG("\n-------------------------------------\n\n");
// fill the input
memcpy(&packet.qoa.assetName, assetNameU1, 8);
memcpy(packet.qoa.issuer, issuer, 32);
packet.qoa.price = price;
packet.qoa.numberOfShares = numberOfShares;
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(qxOrderAction_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(qxOrderAction_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(qxOrderAction_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.qoa));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
void qxAddToAskOrder(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long price,
const long long numberOfShares,
uint32_t scheduledTickOffset)
{
qxOrderAction<QX_ADD_ASK_ORDER_FN>(nodeIp, nodePort, seed, pAssetName, pIssuerInQubicFormat, price, numberOfShares, scheduledTickOffset);
}
void qxAddToBidOrder(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long price,
const long long numberOfShares,
uint32_t scheduledTickOffset)
{
qxOrderAction<QX_ADD_BID_ORDER_FN>(nodeIp, nodePort, seed, pAssetName, pIssuerInQubicFormat, price, numberOfShares, scheduledTickOffset);
}
void qxRemoveToAskOrder(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long price,
const long long numberOfShares,
uint32_t scheduledTickOffset)
{
qxOrderAction<QX_REMOVE_ASK_ORDER_FN>(nodeIp, nodePort, seed, pAssetName, pIssuerInQubicFormat, price, numberOfShares, scheduledTickOffset);
}
void qxRemoveToBidOrder(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long price,
const long long numberOfShares,
uint32_t scheduledTickOffset)
{
qxOrderAction<QX_REMOVE_BID_ORDER_FN>(nodeIp, nodePort, seed, pAssetName, pIssuerInQubicFormat, price, numberOfShares, scheduledTickOffset);
}
void printAssetOrders(qxGetAssetOrder_output& orders)
{
int N = sizeof(orders) /sizeof(orders.orders[0]);
LOG("Entity\t\tPrice\tNumberOfShares\n");
for (int i = 0; i < N; i++){
if (!isZeroPubkey(orders.orders[i].entity))
{
char iden[120];
memset(iden, 0, 120);
getIdentityFromPublicKey(orders.orders[i].entity, iden, false);
LOG("%s\t%lld\t%lld\n", iden, orders.orders[i].price, orders.orders[i].numberOfShares);
} else {
break;
}
}
}
template <int procedureNumber>
void qxGetAssetOrder(const char* nodeIp, int nodePort,
const char* pAssetName,
const char* pIssuer,
const long long offset)
{
uint8_t issuer[32] = {0};
char assetNameU1[8] = {0};
memcpy(assetNameU1, pAssetName, strlen(pAssetName));
if (strlen(pIssuer) != 60){
LOG("WARNING: Stop supporting hex format, please use qubic format 60-char length addresses\n");
exit(0);
}
getPublicKeyFromIdentity(pIssuer, issuer);
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
qxGetAssetOrder_input qgao;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = sizeof(qxGetAssetOrder_input);
packet.rcf.inputType = procedureNumber;
packet.rcf.contractIndex = QX_CONTRACT_INDEX;
memcpy(packet.qgao.issuer, issuer, 32);
memcpy(&packet.qgao.assetName, assetNameU1, 8);
packet.qgao.offset = offset;
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() == RespondContractFunction::type()){
if (recvByte - ptr - sizeof(RequestResponseHeader) >= sizeof(qxGetAssetOrder_output)){
auto orders = (qxGetAssetOrder_output*)(data + ptr + sizeof(RequestResponseHeader));
printAssetOrders(*orders);
}
}
ptr+= header->size();
}
}
void qxGetAssetAskOrder(const char* nodeIp, int nodePort,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long offset)
{
qxGetAssetOrder<QX_GET_ASSET_ASK_ORDER_PR>(nodeIp, nodePort, pAssetName, pIssuerInQubicFormat, offset);
}
void qxGetAssetBidOrder(const char* nodeIp, int nodePort,
const char* pAssetName,
const char* pIssuerInQubicFormat,
const long long offset)
{
qxGetAssetOrder<QX_GET_ASSET_BID_ORDER_PR>(nodeIp, nodePort, pAssetName, pIssuerInQubicFormat, offset);
}
void printEntityOrders(qxGetEntityOrder_output& orders)
{
int N = sizeof(orders) /sizeof(orders.orders[0]);
LOG("Issuer\t\tAssetName\tPrice\tNumberOfShares\n");
for (int i = 0; i < N; i++){
if (orders.orders[i].price ||
orders.orders[i].numberOfShares)
{
char iden[120];
char assetName[8] = {'N','O',' ', 'N','A','M','E', 0};
memset(iden, 0, 120);
getIdentityFromPublicKey(orders.orders[i].issuer, iden, false);
if (orders.orders[i].assetName)
memcpy(assetName, &orders.orders[i].assetName, 8);
LOG("%s\t%s\t%lld\t%lld\n", iden, assetName, orders.orders[i].price, orders.orders[i].numberOfShares);
} else {
break;
}
}
}
template <int procedureNumber>
void qxGetEntityOrder(const char* nodeIp, int nodePort,
const char* pEntity,
const long long offset)
{
uint8_t entity[32] = {0};
if (strlen(pEntity) != 60){
LOG("WARNING: Stop supporting hex format, please use qubic format 60-char length addresses\n");
exit(0);
}
getPublicKeyFromIdentity(pEntity, entity);
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
qxGetEntityOrder_input qgeo;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = sizeof(qxGetEntityOrder_input);
packet.rcf.inputType = procedureNumber;
packet.rcf.contractIndex = QX_CONTRACT_INDEX;
memcpy(packet.qgeo.entity, entity, 32);
packet.qgeo.offset = offset;
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() == RespondContractFunction::type()){
if (recvByte - ptr - sizeof(RequestResponseHeader) >= sizeof(qxGetEntityOrder_output)){
auto orders = (qxGetEntityOrder_output*)(data + ptr + sizeof(RequestResponseHeader));
printEntityOrders(*orders);
}
}
ptr+= header->size();
}
}
void qxGetEntityAskOrder(const char* nodeIp, int nodePort,
const char* pHexEntity,
const long long offset)
{
qxGetEntityOrder<QX_GET_ENTITY_ASK_ORDER_PR>(nodeIp, nodePort, pHexEntity, offset);
}
void qxGetEntityBidOrder(const char* nodeIp, int nodePort,
const char* pHexEntity,
const long long offset)
{
qxGetEntityOrder<QX_GET_ENTITY_BID_ORDER_PR>(nodeIp, nodePort, pHexEntity, offset);
}