Skip to content

Commit d650f64

Browse files
authored
Merge pull request #609 from Cypherock/fix/btc/add-witness-counter
fix(app): Add btc verify ips witness counter
2 parents f104097 + a5a9620 commit d650f64

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

apps/btc_family/btc_inputs_validator.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
203203
}
204204

205205
uint64_t in_counter = decode_varint(stream, &hash_ctx);
206-
while (in_counter--) {
206+
uint64_t in_index = 0;
207+
while (in_index < in_counter) {
207208
uint8_t prev_transaction_hash[SHA256_DIGEST_LENGTH] = {0};
208209
status = read_byte_stream(
209210
stream, prev_transaction_hash, sizeof(prev_transaction_hash));
@@ -245,6 +246,8 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
245246
return BTC_VALIDATE_ERR_READ_STREAM;
246247
}
247248
sha256_Update(&hash_ctx, sequence_no, sizeof(sequence_no));
249+
250+
in_index++;
248251
}
249252

250253
uint64_t out_counter = decode_varint(stream, &hash_ctx);
@@ -288,21 +291,23 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
288291
// Optional witness data, not used in calculating hash
289292
// https://bitcoin.stackexchange.com/questions/113697/what-are-the-parts-of-a-bitcoin-transaction-in-segwit-format
290293
if (is_segwit) {
291-
uint64_t witness_count = decode_varint(stream, NULL);
292-
while (witness_count--) {
293-
uint64_t witness_component_length = decode_varint(stream, NULL);
294-
while (witness_component_length > 0) {
295-
uint64_t length_to_parse = SLICE_SIZE;
296-
if (witness_component_length < length_to_parse) {
297-
length_to_parse = witness_component_length;
298-
}
299-
300-
status = skip_byte_stream(stream, length_to_parse);
301-
if (status != BYTE_STREAM_SUCCESS) {
302-
return BTC_VALIDATE_ERR_READ_STREAM;
294+
while (in_counter--) {
295+
uint64_t witness_count = decode_varint(stream, NULL);
296+
while (witness_count--) {
297+
uint64_t witness_component_length = decode_varint(stream, NULL);
298+
while (witness_component_length > 0) {
299+
uint64_t length_to_parse = SLICE_SIZE;
300+
if (witness_component_length < length_to_parse) {
301+
length_to_parse = witness_component_length;
302+
}
303+
304+
status = skip_byte_stream(stream, length_to_parse);
305+
if (status != BYTE_STREAM_SUCCESS) {
306+
return BTC_VALIDATE_ERR_READ_STREAM;
307+
}
308+
309+
witness_component_length -= length_to_parse;
303310
}
304-
305-
witness_component_length -= length_to_parse;
306311
}
307312
}
308313
}
@@ -327,4 +332,4 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
327332
}
328333

329334
return BTC_VALIDATE_SUCCESS;
330-
}
335+
}

apps/btc_family/btc_txn.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ static bool fetch_valid_input(btc_query_t *query) {
424424
// for explanation. Currently, the device can spend P2PKH or P2WPKH inputs
425425
const btc_script_type_e type = btc_get_script_type(
426426
txin->script_pub_key.bytes, txin->script_pub_key.size);
427+
LOG_ERROR("btc_get_script_type: %d", type);
427428

428429
// clone the input details into btc_txn_context
429430
btc_txn_input_t *input = &btc_txn_context->inputs[idx];
@@ -475,7 +476,7 @@ static bool fetch_valid_input(btc_query_t *query) {
475476

476477
btc_validation_error_e validation_result =
477478
btc_validate_inputs(&stream, txin);
478-
479+
LOG_ERROR("btc_validate_inputs validation_result: %d", validation_result);
479480
btc_result_t response = init_btc_result(BTC_RESULT_SIGN_TXN_TAG);
480481
// Send last chunk ack to host
481482
response.sign_txn.which_response =

0 commit comments

Comments
 (0)