diff --git a/common/iso4217.c b/common/iso4217.c index 4238f6ced..f00ade86e 100644 --- a/common/iso4217.c +++ b/common/iso4217.c @@ -1,6 +1,6 @@ #include +#include #include -#include /* Wikipedia leads me to: https://www.currency-iso.org/en/home/tables/table-a1.html @@ -191,10 +191,12 @@ static const struct iso4217_name_and_divisor iso4217[] = { { "ZWL", 2 }, }; -const struct iso4217_name_and_divisor *find_iso4217(const char *prefix) +const struct iso4217_name_and_divisor *find_iso4217(const utf8 *prefix, + size_t len) { for (size_t i = 0; i < ARRAY_SIZE(iso4217); i++) { - if (memcmp(iso4217[i].name, prefix, ISO4217_NAMELEN) == 0) + if (memeq(iso4217[i].name, strlen(iso4217[i].name), + prefix, len)) return &iso4217[i]; } return NULL; diff --git a/common/iso4217.h b/common/iso4217.h index aafe566c6..aca72b4b0 100644 --- a/common/iso4217.h +++ b/common/iso4217.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_ISO4217_H #define LIGHTNING_COMMON_ISO4217_H #include "config.h" +#include /* BOLT-offers #12: * @@ -15,5 +16,6 @@ struct iso4217_name_and_divisor { #define ISO4217_NAMELEN 3 -const struct iso4217_name_and_divisor *find_iso4217(const char *prefix); +const struct iso4217_name_and_divisor *find_iso4217(const utf8 *prefix, + size_t len); #endif /* LIGHTNING_COMMON_ISO4217_H */ diff --git a/devtools/bolt12-cli.c b/devtools/bolt12-cli.c index 8c336c235..b6ab3efdf 100644 --- a/devtools/bolt12-cli.c +++ b/devtools/bolt12-cli.c @@ -112,14 +112,14 @@ static bool print_amount(const struct bitcoin_blkid *chains, minor_unit = 11; } else { const struct iso4217_name_and_divisor *iso; - currency = tal_strndup(tmpctx, iso4217, tal_bytelen(iso4217)); - iso = find_iso4217(currency); - if (iso) + iso = find_iso4217(iso4217, tal_bytelen(iso4217)); + if (iso) { minor_unit = iso->minor_unit; - else { + currency = iso->name; + } else { minor_unit = 0; - currency = tal_fmt(tmpctx, "%s (UNKNOWN CURRENCY)", - currency); + currency = tal_fmt(tmpctx, "%.*s (UNKNOWN CURRENCY)", + (int)tal_bytelen(iso4217), iso4217); ok = false; } }