Skip to content

Commit b9aefc6

Browse files
authored
Merge pull request #604 from Cypherock/main
2 parents 48eb810 + bb7270a commit b9aefc6

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

apps/btc_family/btc_txn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static bool fetch_valid_input(btc_query_t *query) {
435435

436436
verify_input_data.size_last_chunk = total_size % CHUNK_SIZE;
437437
if (verify_input_data.size_last_chunk < 4) {
438-
verify_input_data.isLocktimeSplit = true;
438+
verify_input_data.is_locktime_split = true;
439439
}
440440
}
441441

apps/btc_family/btc_txn_helpers.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ static void update_hash(btc_verify_input_t *verify_input_data,
339339
}
340340
switch (update) {
341341
case FIRST_CHUNK_HASH: {
342-
if (verify_input_data->isSegwit) {
342+
if (verify_input_data->is_segwit) {
343343
sha256_Update(&(verify_input_data->sha_256_ctx), raw_txn_chunk, 4);
344344
// skip marker and flag
345345
sha256_Update(
@@ -361,7 +361,7 @@ static void update_hash(btc_verify_input_t *verify_input_data,
361361
static void update_locktime(btc_verify_input_t *verify_input_data,
362362
const uint8_t *raw_txn_chunk,
363363
int chunk_index) {
364-
if (verify_input_data->isLocktimeSplit) {
364+
if (verify_input_data->is_locktime_split) {
365365
// last second chunk
366366
if (chunk_index + 2 == verify_input_data->chunk_total) {
367367
memcpy(
@@ -374,7 +374,7 @@ static void update_locktime(btc_verify_input_t *verify_input_data,
374374
verify_input_data->locktime + 4 - verify_input_data->size_last_chunk,
375375
raw_txn_chunk,
376376
verify_input_data->size_last_chunk);
377-
verify_input_data->hasLocktime = true;
377+
verify_input_data->has_locktime = true;
378378
return;
379379
} else {
380380
// wait for subsequent chunks
@@ -384,7 +384,7 @@ static void update_locktime(btc_verify_input_t *verify_input_data,
384384
memcpy(verify_input_data->locktime,
385385
raw_txn_chunk + verify_input_data->size_last_chunk - 4,
386386
4);
387-
verify_input_data->hasLocktime = true;
387+
verify_input_data->has_locktime = true;
388388
} else {
389389
// wait for subsequent chunks
390390
return;
@@ -425,16 +425,18 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
425425
// ignore network version (4-bytes), skip marker & flag (in segwit)
426426
offset += (raw_txn_chunk[4] == 0 ? 6 : 4);
427427
if (6 == offset) {
428-
verify_input_data->isSegwit = true;
428+
verify_input_data->is_segwit = true;
429429
}
430430

431-
// store the number of inputs in the raw_txn
432-
verify_input_data->count = raw_txn_chunk[offset++];
433431
// TODO: Improve varint decode.
434432
// size of variable containing script size and ip-count/op-count
435433
// varies (1-9 Bytes) depending on its value.
436434
// refer:
437435
// https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
436+
verify_input_data->count =
437+
raw_txn_chunk[offset++]; ///< store the number of inputs in the
438+
///< raw_txn
439+
438440
verify_input_data->parsetype = INPUT;
439441
sha256_Init(&(verify_input_data->sha_256_ctx));
440442
} else {
@@ -447,7 +449,7 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
447449
input_case ip_case = verify_input_data->input_parse;
448450
switch (ip_case) {
449451
case PREVIOUS_TX_HASH_PLUS_OP_INDEX_CASE: {
450-
if (offset + 36 > CHUNK_SIZE) {
452+
if (offset + 36 >= CHUNK_SIZE) {
451453
verify_input_data->prev_offset = (offset + 36) - CHUNK_SIZE;
452454
update_hash(
453455
verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE);
@@ -460,7 +462,7 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
460462

461463
case SCRIPT_LENGTH_CASE: {
462464
int64_t script_length = varint_decode(raw_txn_chunk, &offset);
463-
if (offset + script_length + 1 + 4 > CHUNK_SIZE) {
465+
if (offset + script_length + 1 + 4 >= CHUNK_SIZE) {
464466
verify_input_data->prev_offset =
465467
(offset + script_length + 1 + 4) - CHUNK_SIZE;
466468
update_hash(
@@ -485,9 +487,11 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
485487
}
486488

487489
case OP_COUNT: {
488-
if (offset + 1 > CHUNK_SIZE) {
490+
if (offset + 1 >= CHUNK_SIZE) {
489491
// reset prev offset
490-
verify_input_data->prev_offset = -1;
492+
verify_input_data->prev_offset =
493+
offset - CHUNK_SIZE; ///< Did not add +1 as returning back to
494+
///< this stage to read op count
491495
update_hash(verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE);
492496
return 4;
493497
} else {
@@ -503,21 +507,21 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
503507
switch (op_case) {
504508
case VALUE_CASE: {
505509
if (verify_input_data->output_index == input->prev_output_index) {
506-
if (offset + 8 > CHUNK_SIZE) {
510+
if (offset + 8 >= CHUNK_SIZE) {
507511
verify_input_data->prev_offset = (offset + 8) - CHUNK_SIZE;
508512
memcpy(verify_input_data->value,
509513
raw_txn_chunk + offset,
510514
CHUNK_SIZE - offset);
511515
update_hash(
512516
verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE);
513517
verify_input_data->output_parse = VALUE_SPLIT_CASE;
514-
verify_input_data->isSplit = 1;
518+
verify_input_data->is_split = 1;
515519
return 4;
516520
} else {
517521
memcpy(verify_input_data->value, raw_txn_chunk + offset, 8);
518522
}
519523
}
520-
if (offset + 8 > CHUNK_SIZE) {
524+
if (offset + 8 >= CHUNK_SIZE) {
521525
verify_input_data->prev_offset = (offset + 8) - CHUNK_SIZE;
522526
update_hash(
523527
verify_input_data, raw_txn_chunk, chunk_index, CHUNK_SIZE);
@@ -529,17 +533,17 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
529533
}
530534

531535
case VALUE_SPLIT_CASE: {
532-
if (verify_input_data->isSplit) {
536+
if (verify_input_data->is_split) {
533537
memcpy(verify_input_data->value + (8 - offset),
534538
raw_txn_chunk,
535539
offset);
536540
verify_input_data->output_parse = SCRIPT_PUBKEY_CASE;
537-
verify_input_data->isSplit = 0;
541+
verify_input_data->is_split = 0;
538542
}
539543
}
540544

541545
case SCRIPT_PUBKEY_CASE: {
542-
if (offset + raw_txn_chunk[offset] + 1 > CHUNK_SIZE) {
546+
if (offset + raw_txn_chunk[offset] + 1 >= CHUNK_SIZE) {
543547
verify_input_data->prev_offset =
544548
(offset + raw_txn_chunk[offset] + 1) - CHUNK_SIZE;
545549
update_hash(
@@ -565,7 +569,7 @@ int btc_verify_input(const uint8_t *raw_txn_chunk,
565569

566570
case LOCK_TIME: {
567571
update_locktime(verify_input_data, raw_txn_chunk, chunk_index);
568-
if (false == verify_input_data->hasLocktime) {
572+
if (false == verify_input_data->has_locktime) {
569573
return 4;
570574
}
571575
sha256_Update(

apps/btc_family/btc_txn_helpers.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ typedef struct btc_verify_input {
5151
parse_type parsetype;
5252
input_case input_parse;
5353
output_case output_parse;
54-
bool isSegwit;
55-
bool isSplit;
56-
bool hasLocktime;
57-
bool isLocktimeSplit;
54+
bool is_segwit;
55+
bool is_split;
56+
bool has_locktime;
57+
bool is_locktime_split;
5858
int32_t size_last_chunk;
5959
uint8_t value[8];
6060
uint8_t locktime[4];

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
firmware version=000:006:005:000
1+
firmware version=000:006:005:001
22
hardware version=000:001:000:000
33
magic number=45227A01

0 commit comments

Comments
 (0)