Copied TCP impl from other project, basic Block implementation, randomx pow, signing via secp256k1

This commit is contained in:
2026-03-29 17:18:23 +02:00
commit 57bfe61c13
26 changed files with 1775 additions and 0 deletions

36
include/block/block.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef BLOCK_H
#define BLOCK_H
#include <stdint.h>
#include <openssl/sha.h>
#include <dynarr.h>
#include <block/transaction.h>
#include <stdbool.h>
#include <string.h>
#include <randomx/librx_wrapper.h>
typedef struct {
uint8_t version;
uint32_t blockNumber;
uint8_t prevHash[32];
uint8_t merkleRoot[32];
uint64_t timestamp;
uint32_t difficultyTarget; // Encoding: [1 byte exponent][3 byte coefficient]; Target = coefficient * 256^(exponent-3)
uint64_t nonce; // Higher nonce for RandomX
} block_header_t;
typedef struct {
block_header_t header;
DynArr* transactions; // Array of signed_transaction_t
} block_t;
block_t* Block_Create();
void Block_CalculateHash(const block_t* block, uint8_t* outHash);
void Block_CalculateRandomXHash(const block_t* block, uint8_t* outHash);
void Block_AddTransaction(block_t* block, signed_transaction_t* tx);
void Block_RemoveTransaction(block_t* block, uint8_t* txHash);
bool Block_HasValidProofOfWork(const block_t* block);
bool Block_AllTransactionsValid(const block_t* block);
void Block_Destroy(block_t* block);
#endif

20
include/block/chain.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef CHAIN_H
#define CHAIN_H
#include <block/block.h>
#include <dynarr.h>
#include <stdlib.h>
typedef struct {
DynArr* blocks;
size_t size;
} blockchain_t;
blockchain_t* Chain_Create();
void Chain_Destroy(blockchain_t* chain);
bool Chain_AddBlock(blockchain_t* chain, block_t* block);
block_t* Chain_GetBlock(blockchain_t* chain, size_t index);
size_t Chain_Size(blockchain_t* chain);
bool Chain_IsValid(blockchain_t* chain);
#endif

View File

@@ -0,0 +1,48 @@
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <stdint.h>
#include <stdbool.h>
#include <crypto/crypto.h>
// Special sender/recipient address marker for coinbase logic: 32 bytes of 0xFF.
static inline bool Address_IsCoinbase(const uint8_t address[32]) {
if (!address) {
return false;
}
for (size_t i = 0; i < 32; ++i) {
if (address[i] != 0xFF) {
return false;
}
}
return true;
}
// 178 bytes total for v1
typedef struct {
uint8_t version;
uint8_t senderAddress[32];
uint8_t recipientAddress[32];
uint64_t amount;
uint64_t fee; // Rewarded to the miner; can be zero, but the miner may choose to ignore transactions with very low fees
uint8_t compressedPublicKey[33];
// Timestamp is dictated by the block
} transaction_t;
typedef struct {
uint8_t txHash[32];
uint8_t signature[64]; // Signature of the hash
} transaction_sig_t;
typedef struct {
transaction_t transaction;
transaction_sig_t signature;
} signed_transaction_t;
void Transaction_CalculateHash(const signed_transaction_t* tx, uint8_t* outHash);
void Transaction_Sign(signed_transaction_t* tx, const uint8_t* privateKey);
bool Transaction_Verify(const signed_transaction_t* tx);
#endif