I have written this code to create and submit a new minned block to the bitcoin network:

  unsigned long magic_number = 0xD9B4BEF9;
  unsigned long block_size = 89 + (strlen(coinbase_transaction)-1) + (length*(strlen(root[0])-1));
  unsigned char transaction_counter = length;

  char* block = malloc(block_size);
  memcpy(block, &magic_number, 4); block += 4;
  memcpy(block, &block_size, 4); block += 4;
  memcpy(block, &header, 80); block += 80;
  memcpy(block, &transaction_counter, 1); block += 1;
  memcpy(block, &coinbase_transaction, strlen(coinbase_transaction)-1); block += strlen(coinbase_transaction)-1;
  for(int x=0; x<length; x++) { memcpy(block, &root[x], strlen(root[x])-1); block += strlen(root[x])-1; }

  char* content1 = curl_submitblock(block);
  int size1 = strlen(content1);
  json_char* json1 = (json_char*) content1;
  json_value* value1 = json_parse(json1,size1);
  printf("%s\n", content);

but, according to the documentation, I need submit a hex string of this block. For what I understand, the first 89 characters of the string block in this code is not a hex string.

What the way to make a hex string of all the block content, given the code above (if it's correct)?

  • your memcpy calls rewrite the same destination memory – amaclin Aug 27 '17 at 12:43
  • @amaclin fixed this. now if i just figure out the other issue. – Kleber Mota Aug 27 '17 at 13:12
  • you can not use strlen call for binary data like coinbase_transaction – amaclin Aug 27 '17 at 13:22
  • but coinbase_transaction is not binary (at least in this code), but unsigned char*. Is this wrong? – Kleber Mota Aug 27 '17 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.