forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqutil.cpp
More file actions
130 lines (124 loc) · 4.63 KB
/
qutil.cpp
File metadata and controls
130 lines (124 loc) · 4.63 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
#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,
};
struct SendToManyV1_input {
uint8_t addresses[25][32];
int64_t amounts[25];
};
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");
}