mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-10 21:09:24 +02:00
mining: add merkle root calculation
This commit is contained in:
parent
23e588c94e
commit
1c21a43cc5
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user