Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SET(FILES ${CMAKE_SOURCE_DIR}/asset_utils.cpp
${CMAKE_SOURCE_DIR}/sc_utils.cpp
${CMAKE_SOURCE_DIR}/test_utils.cpp
${CMAKE_SOURCE_DIR}/wallet_utils.cpp
${CMAKE_SOURCE_DIR}/utils.cpp
)
SET(HEADER_FILES
argparser.h
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Basic config:
Do action although an error has been detected. Currently only implemented for proposals.
-enabletestcontracts
Enable test contract indices and names for commands using a contract index parameter. This flag has to be passed before the contract index/name. The node to connect to needs to have test contracts running.

-print-only <base64 | hex>
Print the raw transaction data without sending it to the network. Useful for offline signing or broadcasting later.
Commands:

[WALLET COMMANDS]
Expand Down
9 changes: 9 additions & 0 deletions argparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void print_help()
printf("\t\tDo action although an error has been detected. Currently only implemented for proposals.\n");
printf("\t-enabletestcontracts\n");
printf("\t\tEnable test contract indices and names for commands using a contract index parameter. This flag has to be passed before the contract index/name. The node to connect to needs to have test contracts running.\n");
printf("\t-print-only <base64 | hex>\n");
printf("\t\tPrint the raw transaction data without sending it to the network. Useful for offline signing or broadcasting later.\n");

printf("\nCommands:\n");
printf("\n[WALLET COMMANDS]\n");
Expand Down Expand Up @@ -696,6 +698,13 @@ void parseArgument(int argc, char** argv)
++i;
continue;
}
if (strcmp(argv[i], "-print-only") == 0)
{
CHECK_NUMBER_OF_PARAMETERS(1);
g_printToScreen = argv[i+1];
i+=2;
continue;
}

/***************************
***** WALLET COMMANDS *****
Expand Down
30 changes: 21 additions & 9 deletions connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,30 @@ std::vector<T> QubicConnection::getLatestVectorPacketAs()

int QubicConnection::sendData(uint8_t* buffer, int sz)
{
int size = sz;
int numberOfBytes;
while (size)
{
if ((numberOfBytes = send(mSocket, (char*)buffer, size, 0)) <= 0)
// also skip printing packets of size 8 (typically used during the preparation step, not the final stage)
if (!std::string(g_printToScreen).empty() && sz != 8) {
std::string printType = g_printToScreen;
// Do not print the first 8 bytes (header)
printBytes(buffer + 8, sz - 8, printType);

// this operation may break the normal flow, we need to skip printing error messages to console
if (!std::freopen("/dev/null", "w", stdout)) {}
if (!std::freopen("/dev/null", "w", stderr)) {}
return 0;
} else {
int size = sz;
int numberOfBytes;
while (size)
{
return 0;
if ((numberOfBytes = send(mSocket, (char*)buffer, size, 0)) <= 0)
{
return 0;
}
buffer += numberOfBytes;
size -= numberOfBytes;
}
buffer += numberOfBytes;
size -= numberOfBytes;
return sz - size;
}
return sz - size;
}

template SpecialCommand QubicConnection::receivePacketWithHeaderAs<SpecialCommand>();
Expand Down
3 changes: 3 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "utils.h"

char* g_printToScreen = (char *)"";
72 changes: 72 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <string>
#include <vector>

extern char* g_printToScreen;

static const char B64_TABLE[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static void byteToHex(const uint8_t* byte, char* hex, const int sizeInByte)
{
for (int i = 0; i < sizeInByte; i++)
Expand Down Expand Up @@ -96,3 +101,70 @@ static std::string unwrapString(const std::string& str, char wrapperCharFront, c
}
return str;
}

static inline std::string base64_encode(const std::vector<uint8_t> &in) {
std::string out;
int val = 0, valb = -6;

for (uint8_t c : in) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
out.push_back(B64_TABLE[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6)
out.push_back(B64_TABLE[((val << 8) >> (valb + 8)) & 0x3F]);

while (out.size() % 4)
out.push_back('=');

return out;
}

static std::string base64_encode(uint8_t *data, size_t length) {
return base64_encode(std::vector<uint8_t>(data, data + length));
}

static std::vector<uint8_t> base64_decode(const std::string &in) {
static int T[256];
static bool init = false;

if (!init) {
for (int i = 0; i < 256; i++) T[i] = -1;
for (int i = 0; i < 64; i++) T[(unsigned char)B64_TABLE[i]] = i;
init = true;
}

std::vector<uint8_t> out;
int val = 0, valb = -8;

for (unsigned char c : in) {
if (T[c] == -1) continue;
val = (val << 6) + T[c];
valb += 6;

if (valb >= 0) {
out.push_back(uint8_t((val >> valb) & 0xFF));
valb -= 8;
}
}

return out;
}

static void printBytes(uint8_t* data, size_t length, std::string type = "base64") {\
printf("---------------- %s ----------------\n", type.c_str());
if (type == "base64") {
std::string encoded = base64_encode(data, length);
printf("%s\n", encoded.c_str());
} else if (type == "hex") {
for (size_t i = 0; i < length; i++) {
printf("%02x", data[i]);
}
printf("\n");
} else {
printf("Unsupported print type: %s\n", type.c_str());
}
}
3 changes: 3 additions & 0 deletions wallet_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ void makeCustomTransaction(const char* nodeIp, int nodePort,
const uint8_t* extraData,
uint32_t scheduledTickOffset)
{
if (extraDataSize < 0)
throw std::invalid_argument("extraDataSize < 0");

auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
Expand Down