Browse Source

common/iso4217: make find_iso4217 a little more usable.

We often have the currency as a tal string.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa
Rusty Russell 4 years ago
committed by Christian Decker
parent
commit
0f2c9cf5d5
  1. 8
      common/iso4217.c
  2. 4
      common/iso4217.h
  3. 12
      devtools/bolt12-cli.c

8
common/iso4217.c

@ -1,6 +1,6 @@
#include <ccan/array_size/array_size.h>
#include <ccan/mem/mem.h>
#include <common/iso4217.h>
#include <string.h>
/* 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;

4
common/iso4217.h

@ -1,6 +1,7 @@
#ifndef LIGHTNING_COMMON_ISO4217_H
#define LIGHTNING_COMMON_ISO4217_H
#include "config.h"
#include <wire/wire.h>
/* 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 */

12
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;
}
}

Loading…
Cancel
Save