Skip to content

Commit 08a8c96

Browse files
committed
common/bolt11: Fix BOLT11 hash calculation for unknown fallback address versions
Changelog-Fixed: Fixed hash calculation inconsistency when processing invoices with unknown fallback address versions.
1 parent b97278f commit 08a8c96

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

common/bolt11.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,18 @@ static const char *decode_f(struct bolt11 *b11,
375375
size_t orig_len = *field_len;
376376
const char *err;
377377

378+
/* Read version but don't commit to hash yet */
379+
err = pull_uint(NULL, &orig_data, &orig_len, &version, 5);
380+
if (err)
381+
return tal_fmt(b11, "f: %s", err);
382+
383+
bool is_known_version = version == 17 || version == 18 || version < 17;
384+
385+
if (!is_known_version) {
386+
return unknown_field(b11, hu5, data, field_len, 'f');
387+
}
388+
389+
/* For known versions, process with hash */
378390
err = pull_uint(hu5, data, field_len, &version, 5);
379391
if (err)
380392
return tal_fmt(b11, "f: %s", err);
@@ -427,12 +439,12 @@ static const char *decode_f(struct bolt11 *b11,
427439
tal_count(f));
428440
}
429441
fallback = scriptpubkey_witness_raw(b11, version,
430-
f, tal_count(f));
442+
f, tal_count(f));
431443
} else {
432-
/* Restore version for unknown field! */
433-
*data = orig_data;
434-
*field_len = orig_len;
435-
return unknown_field(b11, hu5, data, field_len, 'f');
444+
// This should be unreachable because all valid versions (17, 18, or <17)
445+
// are handled above. If hit, it indicates either a bug or future version
446+
// not yet supported.
447+
return tal_fmt(b11, "f: unknown version %"PRIu64, version);
436448
}
437449

438450
if (b11->fallbacks == NULL)
@@ -1372,4 +1384,4 @@ char *bolt11_encode_(const tal_t *ctx,
13721384
output = tal_free(output);
13731385

13741386
return output;
1375-
}
1387+
}

0 commit comments

Comments
 (0)