Skip to content

Commit

Permalink
create qx files, refractoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
krypdkat committed Apr 1, 2024
1 parent 03d691f commit 3119232
Show file tree
Hide file tree
Showing 14 changed files with 783 additions and 260 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SET(FILES ${CMAKE_SOURCE_DIR}/connection.cpp
${CMAKE_SOURCE_DIR}/SCUtils.cpp
${CMAKE_SOURCE_DIR}/quottery.cpp
${CMAKE_SOURCE_DIR}/qutil.cpp
${CMAKE_SOURCE_DIR}/qx.cpp
)
SET(HEADER_FILES
K12AndKeyUtil.h
Expand Down
37 changes: 0 additions & 37 deletions SCUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,3 @@
#include "connection.h"
#include "logger.h"

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 = 1;
packet.rcf.contractIndex = 1;
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()){
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);
}
1 change: 0 additions & 1 deletion SCUtils.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#include <cstdint>

void printQxFee(const char* nodeIp, const int nodePort);
22 changes: 0 additions & 22 deletions assetUtil.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
#pragma once
void printOwnedAsset(const char * nodeIp, const int nodePort, const char* requestedIdentity);
void printPossessionAsset(const char * nodeIp, const int nodePort, const char* requestedIdentity);
void transferQxShare(const char* nodeIp, int nodePort,
const char* seed,
const char* possessorIdentity,
const char* newOwnerIdentity,
long long numberOfUnits,
uint32_t scheduledTickOffset);

void qxIssueAsset(const char* nodeIp, int nodePort,
const char* seed,
const char* assetName,
const char* unitOfMeasurement,
int64_t numberOfUnits,
char numberOfDecimalPlaces,
uint32_t scheduledTickOffset);

void qxTransferAsset(const char* nodeIp, int nodePort,
const char* seed,
const char* pAssetName,
const char* pIssuer,
const char* newOwnerIdentity,
long long numberOfUnits,
uint32_t scheduledTickOffset);
166 changes: 7 additions & 159 deletions assetUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ std::vector<RespondPossessedAssets> getPossessionAsset(const char * nodeIp, cons
}
static void printOwnedAsset(Asset owned, Asset iss)
{
char hexIssuer[128];
char issuer[128];
char name[8] = {0};
char unitOfMeasurement[7] = {0};
memcpy(name, iss.varStruct.issuance.name, 7);
memcpy(unitOfMeasurement, iss.varStruct.issuance.unitOfMeasurement, 7);
byteToHex(iss.varStruct.issuance.publicKey, hexIssuer, 32);
LOG("Asset issuer: %s\n", hexIssuer);
getIdentityFromPublicKey(iss.varStruct.issuance.publicKey, issuer, false);
LOG("Asset issuer: %s\n", issuer);
LOG("Asset name: %s\n", name);
LOG("Managing contract index: %d\n", owned.varStruct.ownership.managingContractIndex);
LOG("Issuance Index: %d\n", owned.varStruct.ownership.issuanceIndex);
LOG("Number Of Units: %d\n", owned.varStruct.ownership.numberOfUnits);
}
static void printPossessionAsset(Asset owner, Asset possession, Asset iss)
{
char hexIssuer[128];
char issuer[128];
char name[8] = {0};
char ownerId[128] = {0};
getIdentityFromPublicKey(owner.varStruct.ownership.publicKey, ownerId, false);
memcpy(name, iss.varStruct.issuance.name, 7);
byteToHex(iss.varStruct.issuance.publicKey, hexIssuer, 32);
LOG("Asset issuer: %s\n", hexIssuer);
getIdentityFromPublicKey(iss.varStruct.issuance.publicKey, issuer, false);
LOG("Asset issuer: %s\n", issuer);
LOG("Asset name: %s\n", name);
LOG("Managing contract index: %d\n", possession.varStruct.possession.managingContractIndex);
LOG("Owner index: %u\n", possession.varStruct.possession.ownershipIndex);
Expand All @@ -117,156 +117,4 @@ void printPossessionAsset(const char * nodeIp, const int nodePort, const char* r
printPossessionAsset(rpa.ownershipAsset, rpa.asset, rpa.issuanceAsset);
LOG("Tick: %u\n", rpa.tick);
}
}

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 = 1;
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* pIssuer,
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));
hexToByte(pIssuer, issuer, 32);

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 scheduledTick = 0;
if (scheduledTickOffset < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + scheduledTickOffset;
} else {
scheduledTick = scheduledTickOffset;
}
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = 2;
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");

}
}
11 changes: 11 additions & 0 deletions commonFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "stdint.h"
static bool isArrayZero(uint8_t* ptr, int len){
for (int i = 0; i < len; i++){
if (ptr[i] != 0) return false;
}
return true;
}
static bool isZeroPubkey(uint8_t* pubkey){
return isArrayZero(pubkey, 32);
}
2 changes: 1 addition & 1 deletion defines.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "commonFunctions.h"
#define DEFAULT_SCHEDULED_TICK_OFFSET 20
#define DEFAULT_NODE_PORT 21841
#define DEFAULT_NODE_IP "127.0.0.1"
#define NUMBER_OF_TRANSACTIONS_PER_TICK 1024
#define SIGNATURE_SIZE 64
#define SPECTRUM_DEPTH 24 // Is derived from SPECTRUM_CAPACITY (=N)
#define DEFAULT_SEED "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
#define QX_ADDRESS "BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARMID"
#define ARBITRATOR "AFZPUAIYVPNUYGJRQVLUKOPPVLHAZQTGLYAAUUNBXFTVTAMSBKQBLEIEPCVJ"
#define NUMBER_OF_COMPUTORS 676
#define EXCHANGE_PUBLIC_PEERS 0
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "SCUtils.h"
#include "quottery.h"
#include "qutil.h"
#include "qx.h"

int main(int argc, char *argv[])
{
Expand Down
11 changes: 1 addition & 10 deletions quottery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,6 @@ void quotteryGetBetInfo(const char* nodeIp, const int nodePort, int betId, getBe
}

}
static bool isArrayZero(uint8_t* ptr, int len){
for (int i = 0; i < len; i++){
if (ptr[i] != 0) return false;
}
return true;
}
static bool isZeroPubkey(uint8_t* pubkey){
return isArrayZero(pubkey, 32);
}
void quotteryPrintBetInfo(const char* nodeIp, const int nodePort, int betId){

getBetInfo_output result;
Expand All @@ -348,7 +339,7 @@ void quotteryPrintBetInfo(const char* nodeIp, const int nodePort, int betId){
char buf[128] = {0};
LOG("Bet Id: %u\n", result.betId); // uint32_t betId;
LOG("Number of options: %u\n", result.nOption); // uint8_t nOption; // options number
byteToHex(result.creator, buf, 32);
getIdentityFromPublicKey(result.creator, buf, false);
LOG("Creator: %s\n", buf);
{
memset(buf, 0 , 128);
Expand Down
Loading

0 comments on commit 3119232

Please sign in to comment.