-
Notifications
You must be signed in to change notification settings - Fork 23
/
qutil.cpp
197 lines (187 loc) · 7.17 KB
/
qutil.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
#include "qutil.h"
#include "keyUtils.h"
#include "structs.h"
#include "logger.h"
#include "nodeUtils.h"
#include "K12AndKeyUtil.h"
#include "connection.h"
#include "walletUtils.h"
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
constexpr int QUTIL_CONTRACT_ID = 4;
enum qutilFunctionId{
GetSendToManyV1Fee = 1,
};
enum qutilProcedureId{
SendToManyV1 = 1,
BurnQubic = 2,
};
struct SendToManyV1_input {
uint8_t addresses[25][32];
int64_t amounts[25];
};
struct BurnQubic_input
{
long long amount;
};
struct BurnQubic_output
{
long long amount;
};
void readPayoutList(const char* payoutListFile, std::vector<std::string>& addresses, std::vector<int64_t>& amounts)
{
addresses.resize(0);
amounts.resize(0);
std::ifstream infile(payoutListFile);
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
std::string a;
int64_t b;
if (!(iss >> a >> b)) { break; } // error
addresses.push_back(a);
amounts.push_back(b);
}
}
long long getSendToManyV1Fee(QCPtr qc)
{
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 = qutilFunctionId::GetSendToManyV1Fee;
packet.rcf.contractIndex = QUTIL_CONTRACT_ID;
qc->sendData((uint8_t *) &packet, packet.header.size());
auto fee = qc->receivePacketAs<GetSendToManyV1Fee_output>();
return fee.fee;
}
void qutilSendToManyV1(const char* nodeIp, int nodePort, const char* seed, const char* payoutListFile, uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
std::vector<std::string> addresses;
std::vector<int64_t> amounts;
readPayoutList(payoutListFile, addresses, amounts);
if (addresses.size() > 25){
LOG("WARNING: payout list has more than 25 addresses, only the first 25 addresses will be paid\n");
}
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);
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
SendToManyV1_input stm;
unsigned char signature[64];
} packet;
memset(&packet.stm, 0, sizeof(SendToManyV1_input));
packet.transaction.amount = 0;
for (int i = 0; i < std::min(25, int(addresses.size())); i++){
getPublicKeyFromIdentity(addresses[i].data(), packet.stm.addresses[i]);
packet.stm.amounts[i] = amounts[i];
packet.transaction.amount += amounts[i];
}
long long fee = getSendToManyV1Fee(qc);
LOG("Send to many V1 fee: %lld\n", fee);
packet.transaction.amount += fee; // fee
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::SendToManyV1;
packet.transaction.inputSize = sizeof(SendToManyV1_input);
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(SendToManyV1_input),
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(SendToManyV1_input) + SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("SendToManyV1 tx 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");
}
void qutilBurnQubic(const char* nodeIp, int nodePort, const char* seed, long long amount, 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);
((uint64_t*)destPublicKey)[0] = QUTIL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
BurnQubic_input bqi;
unsigned char signature[64];
} packet;
packet.bqi.amount = amount;
packet.transaction.amount = amount;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
uint32_t currentTick = getTickNumberFromNode(qc);
packet.transaction.tick = currentTick + scheduledTickOffset;
packet.transaction.inputType = qutilProcedureId::BurnQubic;
packet.transaction.inputSize = sizeof(BurnQubic_input);
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(packet.transaction) + sizeof(BurnQubic_input),
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(BurnQubic_input) + SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("BurnQubic tx 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");
}