| 39 | } |
| 40 | |
| 41 | static struct command_result *param_amount(struct command *cmd, |
| 42 | const char *name, |
| 43 | const char *buffer, |
| 44 | const jsmntok_t *tok, |
| 45 | struct tlv_offer *offer) |
| 46 | { |
| 47 | const struct iso4217_name_and_divisor *isocode; |
| 48 | jsmntok_t number, whole, frac; |
| 49 | u64 cents; |
| 50 | |
| 51 | if (msat_or_any(buffer, tok, offer)) |
| 52 | return NULL; |
| 53 | |
| 54 | offer->amount = tal(offer, u64); |
| 55 | |
| 56 | /* BOLT-offers #12: |
| 57 | * |
| 58 | * - MUST specify `iso4217` as an ISO 4712 three-letter code. |
| 59 | * - MUST specify `amount` in the currency unit adjusted by the ISO 4712 |
| 60 | * exponent (e.g. USD cents). |
| 61 | */ |
| 62 | if (tok->end - tok->start < ISO4217_NAMELEN) |
| 63 | return command_fail_badparam(cmd, name, buffer, tok, |
| 64 | "should be 'any', msatoshis or <amount>[.<amount>]<ISO-4712>"); |
| 65 | |
| 66 | isocode = find_iso4217(buffer + tok->end - ISO4217_NAMELEN, ISO4217_NAMELEN); |
| 67 | if (!isocode) |
| 68 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 69 | "Unknown currency suffix %.*s", |
| 70 | ISO4217_NAMELEN, |
| 71 | buffer + tok->end - ISO4217_NAMELEN); |
| 72 | |
| 73 | offer->currency |
| 74 | = tal_dup_arr(offer, utf8, isocode->name, ISO4217_NAMELEN, 0); |
| 75 | |
| 76 | number = *tok; |
| 77 | number.end -= ISO4217_NAMELEN; |
| 78 | if (!split_tok(buffer, &number, '.', &whole, &frac)) { |
| 79 | whole = number; |
| 80 | cents = 0; |
| 81 | } else { |
| 82 | if (frac.end - frac.start != isocode->minor_unit) |
| 83 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 84 | "Currency %s requires %u minor units", |
| 85 | isocode->name, isocode->minor_unit); |
| 86 | if (!json_to_u64(buffer, &frac, ¢s)) |
| 87 | return command_fail_badparam(cmd, name, buffer, |
| 88 | &number, |
| 89 | "Bad minor units"); |
| 90 | } |
| 91 | |
| 92 | if (!json_to_u64(buffer, &whole, offer->amount)) |
| 93 | return command_fail_badparam(cmd, name, buffer, tok, |
| 94 | "should be 'any', msatoshis or <ISO-4712><amount>[.<amount>]"); |
| 95 | |
| 96 | for (size_t i = 0; i < isocode->minor_unit; i++) { |
| 97 | if (mul_overflows_u64(*offer->amount, 10)) |
| 98 | return command_fail_badparam(cmd, name, buffer, |
nothing calls this directly
no test coverage detected