@@ -203,7 +203,8 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
203
203
}
204
204
205
205
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 ) {
207
208
uint8_t prev_transaction_hash [SHA256_DIGEST_LENGTH ] = {0 };
208
209
status = read_byte_stream (
209
210
stream , prev_transaction_hash , sizeof (prev_transaction_hash ));
@@ -245,6 +246,8 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
245
246
return BTC_VALIDATE_ERR_READ_STREAM ;
246
247
}
247
248
sha256_Update (& hash_ctx , sequence_no , sizeof (sequence_no ));
249
+
250
+ in_index ++ ;
248
251
}
249
252
250
253
uint64_t out_counter = decode_varint (stream , & hash_ctx );
@@ -288,21 +291,23 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
288
291
// Optional witness data, not used in calculating hash
289
292
// https://bitcoin.stackexchange.com/questions/113697/what-are-the-parts-of-a-bitcoin-transaction-in-segwit-format
290
293
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 ;
303
310
}
304
-
305
- witness_component_length -= length_to_parse ;
306
311
}
307
312
}
308
313
}
@@ -327,4 +332,4 @@ btc_validation_error_e btc_validate_inputs(byte_stream_t *stream,
327
332
}
328
333
329
334
return BTC_VALIDATE_SUCCESS ;
330
- }
335
+ }
0 commit comments