forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalletUtils.cpp
362 lines (346 loc) · 14.7 KB
/
walletUtils.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
#include <chrono>
#include <thread>
#include <cstdint>
#include <cstring>
#include "utils.h"
#include "nodeUtils.h"
#include "keyUtils.h"
#include "logger.h"
#include "structs.h"
#include "connection.h"
#include "K12AndKeyUtil.h"
void printWalletInfo(const char* seed)
{
uint8_t privateKey[32] = {0};
uint8_t publicKey[32] = {0};
uint8_t subseed[32] = {0};
char privateKeyQubicFormat[128] = {0};
char publicKeyQubicFormat[128] = {0};
char publicIdentity[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, publicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(publicKey, publicIdentity, isLowerCase);
getIdentityFromPublicKey(privateKey, privateKeyQubicFormat, true);
getIdentityFromPublicKey(publicKey, publicKeyQubicFormat, true);
LOG("Seed: %s\n", seed);
LOG("Private key: %s\n", privateKeyQubicFormat);
LOG("Public key: %s\n", publicKeyQubicFormat);
LOG("Identity: %s\n", publicIdentity);
}
RespondedEntity getBalance(const char* nodeIp, const int nodePort, const uint8_t* publicKey)
{
RespondedEntity result;
memset(&result, 0, sizeof(RespondedEntity));
std::vector<uint8_t> buffer;
buffer.resize(0);
uint8_t tmp[1024] = {0};
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestedEntity req;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_ENTITY);
memcpy(packet.req.publicKey, publicKey, 32);
qc->sendData((uint8_t *) &packet, packet.header.size());
int recvByte = qc->receiveData(tmp, 1024);
while (recvByte > 0)
{
buffer.resize(recvByte + buffer.size());
memcpy(buffer.data() + buffer.size() - recvByte, tmp, recvByte);
recvByte = qc->receiveData(tmp, 1024);
}
uint8_t* data = buffer.data();
recvByte = buffer.size();
int ptr = 0;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RESPOND_ENTITY){
auto entity = (RespondedEntity*)(data + ptr + sizeof(RequestResponseHeader));
result = *entity;
}
ptr+= header->size();
}
return result;
}
void printBalance(const char* publicIdentity, const char* nodeIp, int nodePort)
{
uint8_t publicKey[32] = {0};
getPublicKeyFromIdentity(publicIdentity, publicKey);
auto entity = getBalance(nodeIp, nodePort, publicKey);
LOG("Identity: %s\n", publicIdentity);
LOG("Balance: %lld\n", entity.entity.incomingAmount - entity.entity.outgoingAmount);
LOG("Incoming Amount: %lld\n", entity.entity.incomingAmount);
LOG("Outgoing Amount: %lld\n", entity.entity.outgoingAmount);
LOG("Number Of Incoming Transfers: %ld\n", entity.entity.numberOfIncomingTransfers);
LOG("Number Of Outgoing Transfers: %ld\n", entity.entity.numberOfOutgoingTransfers);
LOG("Latest Incoming Transfer Tick: %u\n", entity.entity.latestIncomingTransferTick);
LOG("Latest Outgoing Transfer Tick: %u\n", entity.entity.latestOutgoingTransferTick);
}
void printReceipt(Transaction& tx, const char* txHash, const uint8_t* extraData)
{
char sourceIdentity[128] = {0};
char dstIdentity[128] = {0};
char txHashClean[128] = {0};
bool isLowerCase = false;
getIdentityFromPublicKey(tx.sourcePublicKey, sourceIdentity, isLowerCase);
getIdentityFromPublicKey(tx.destinationPublicKey, dstIdentity, isLowerCase);
LOG("~~~~~RECEIPT~~~~~\n");
if (txHash != nullptr) {
memcpy(txHashClean, txHash, 60);
LOG("TxHash: %s\n", txHashClean);
}
LOG("From: %s\n", sourceIdentity);
LOG("To: %s\n", dstIdentity);
LOG("Input type: %u\n", tx.inputType);
LOG("Amount: %lu\n", tx.amount);
LOG("Tick: %u\n", tx.tick);
LOG("Extra data size: %u\n", tx.inputSize);
if (extraData != nullptr && tx.inputSize){
char hex_tring[1024] = {0};
for (int i = 0; i < tx.inputSize; i++)
sprintf(hex_tring + i * 2, "%02x", extraData[i]);
LOG("Extra data: %s\n", hex_tring);
}
LOG("~~~~~END-RECEIPT~~~~~\n");
}
bool verifyTx(Transaction& tx, const uint8_t* extraData, const uint8_t* signature)
{
std::vector<uint8_t> buffer;
uint8_t digest[32] = {0};
buffer.resize(sizeof(Transaction) + tx.inputSize);
memcpy(buffer.data(), &tx, sizeof(Transaction));
if (extraData && tx.inputSize) memcpy(buffer.data() + sizeof(Transaction), extraData, tx.inputSize);
KangarooTwelve(buffer.data(),
buffer.size(),
digest,
32);
return verify(tx.sourcePublicKey, digest, signature);
}
void makeStandardTransaction(const char* nodeIp, int nodePort, const char* seed,
const char* targetIdentity, const uint64_t amount, uint32_t scheduledTickOffset,
int waitUntilFinish)
{
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};
char publicIdentity[128] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(sourcePublicKey, publicIdentity, isLowerCase);
getPublicKeyFromIdentity(targetIdentity, destPublicKey);
struct {
RequestResponseHeader header;
Transaction transaction;
unsigned char signature[64];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = amount;
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = 0;
packet.transaction.inputSize = 0;
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet.header)+sizeof(packet.transaction) + 64);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + 64,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, nullptr);
if (waitUntilFinish){
LOG("Waiting for tick:\n");
while (currentTick <= packet.transaction.tick){
LOG("%d/%d\n", currentTick, packet.transaction.tick);
Q_SLEEP(1000);
currentTick = getTickNumberFromNode(qc);
}
checkTxOnTick(nodeIp, nodePort, txHash, packet.transaction.tick);
} else {
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
}
void makeCustomTransaction(const char* nodeIp, int nodePort,
const char* seed,
const char* targetIdentity,
uint16_t txType,
uint64_t amount,
int extraDataSize,
const uint8_t* extraData,
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};
char publicIdentity[128] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(sourcePublicKey, publicIdentity, isLowerCase);
getPublicKeyFromIdentity(targetIdentity, destPublicKey);
struct {
RequestResponseHeader header;
Transaction transaction;
} temp_packet;
std::vector<uint8_t> packet;
memcpy(temp_packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(temp_packet.transaction.destinationPublicKey, destPublicKey, 32);
temp_packet.transaction.amount = amount;
uint32_t currentTick = getTickNumberFromNode(qc);
temp_packet.transaction.tick = currentTick + scheduledTickOffset;
temp_packet.transaction.inputType = txType;
temp_packet.transaction.inputSize = extraDataSize;
packet.resize(sizeof(RequestResponseHeader) + sizeof(Transaction) + extraDataSize + SIGNATURE_SIZE);
memcpy(packet.data() + sizeof(RequestResponseHeader), &temp_packet.transaction, sizeof(Transaction));
memcpy(packet.data() + sizeof(RequestResponseHeader) + sizeof(Transaction), extraData, extraDataSize);
KangarooTwelve(packet.data() + sizeof(RequestResponseHeader),
sizeof(Transaction) + extraDataSize,
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.data() + sizeof(RequestResponseHeader) + sizeof(Transaction) + extraDataSize, signature, SIGNATURE_SIZE);
temp_packet.header.setSize(sizeof(RequestResponseHeader) + sizeof(Transaction) + extraDataSize + SIGNATURE_SIZE);
temp_packet.header.zeroDejavu();
temp_packet.header.setType(BROADCAST_TRANSACTION);
memcpy(packet.data(), &temp_packet.header, sizeof(RequestResponseHeader));
qc->sendData(packet.data(), packet.size());
KangarooTwelve(packet.data() + sizeof(RequestResponseHeader),
sizeof(Transaction) + extraDataSize + 64,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(temp_packet.transaction, txHash, extraData);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
void makeIPOBid(const char* nodeIp, int nodePort,
const char* seed,
uint32_t contractIndex,
uint64_t pricePerShare,
uint16_t numberOfShare,
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};
char publicIdentity[128] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subseed);
getPrivateKeyFromSubSeed(subseed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
const bool isLowerCase = false;
getIdentityFromPublicKey(sourcePublicKey, publicIdentity, isLowerCase);
// Contracts are identified by their index stored in the first 64 bits of the id, all
// other bits are zeroed. However, the max number of contracts is limited to 2^32 - 1,
// only 32 bits are used for the contract index.
((uint64_t*)destPublicKey)[0] = contractIndex;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
ContractIPOBid ipo;
unsigned char signature[64];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = 0;
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = 0;
packet.transaction.inputSize = sizeof(packet.ipo);
packet.ipo.price = pricePerShare;
packet.ipo.quantity = numberOfShare;
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(packet.ipo),
digest,
32);
sign(subseed, sourcePublicKey, digest, signature);
memcpy(packet.signature, signature, 64);
packet.header.setSize(sizeof(packet));
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(packet.ipo) + SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("IPO bidding has been sent!\n");
printReceipt(packet.transaction, txHash, nullptr);
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", currentTick + scheduledTickOffset, txHash);
LOG("to check your tx confirmation status\n");
}
RespondContractIPO _getIPOStatus(const char* nodeIp, int nodePort, uint32_t contractIndex){
RespondContractIPO result;
memset(&result, 0, sizeof(RespondContractIPO));
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractIPO req;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(REQUEST_CONTRACT_IPO);
packet.req.contractIndex = contractIndex;
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_CONTRACT_IPO){
auto rcipo = (RespondContractIPO*)(data + ptr + sizeof(RequestResponseHeader));
result = *rcipo;
}
ptr+= header->size();
}
return result;
}
void printIPOStatus(const char* nodeIp, int nodePort, uint32_t contractIndex){
RespondContractIPO status = _getIPOStatus(nodeIp, nodePort, contractIndex);
LOG("Identity - Price per share\n");
for (int i = 0; i < NUMBER_OF_COMPUTORS; i++){
if (!isZeroPubkey(status.publicKeys[i])){
char identity[128] = {0};
getIdentityFromPublicKey(status.publicKeys[i], identity, false);
LOG("%s: %lld\n", identity, status.prices[i]);
}
}
}