mining: add merkle root calculation

This commit is contained in:
johnny9 2023-05-04 17:00:17 -04:00
parent 23e588c94e
commit 1c21a43cc5
2 changed files with 22 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#include <string.h>
#include "mining.h"
#include "utils.h"
char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
const char * extranonce, int extranonce_2_len)
@ -20,7 +21,25 @@ char * construct_coinbase_tx(const char * coinbase_1, const char * coinbase_2,
return coinbase_tx;
}
char * calculate_merkle_hash(const char * coinbase_tx, const uint8_t ** merkle_branches)
char * calculate_merkle_root_hash(const char * coinbase_tx, const uint8_t ** merkle_branches, const int num_merkle_branches)
{
return NULL;
size_t coinbase_tx_bin_len = strlen(coinbase_tx) / 2;
uint8_t * coinbase_tx_bin = malloc(coinbase_tx_bin_len);
hex2bin(coinbase_tx, coinbase_tx_bin, coinbase_tx_bin_len);
uint8_t both_merkles[64];
uint8_t * new_root = double_sha256_bin(coinbase_tx_bin, coinbase_tx_bin_len);
memcpy(both_merkles, new_root, 32);
free(new_root);
for (int i = 0; i < num_merkle_branches; i++)
{
memcpy(both_merkles + 32, merkle_branches + 32*i, 32);
uint8_t * new_root = double_sha256_bin(both_merkles, 64);
memcpy(both_merkles, new_root, 32);
free(new_root);
}
char * merkle_root_hash = malloc(33);
bin2hex(both_merkles, 32, merkle_root_hash, 33);
return merkle_root_hash;
}

View File

@ -120,4 +120,4 @@ uint8_t * double_sha256_bin(const uint8_t * data, const size_t data_len)
mbedtls_sha256(first_hash_output, 32, second_hash_output, 0);
return second_hash_output;
}
}