json: parse bitcoind-style bitcoin amount.

Always of form d*.dddddddd; we turn that into satoshis, because we're
sane.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-01-22 06:41:48 +10:30
parent 06a25887da
commit 725512fb03
2 changed files with 32 additions and 0 deletions

View File

@@ -78,6 +78,34 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
return true;
}
bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi)
{
char *end;
unsigned long btc, sat;
btc = strtoul(buffer + tok->start, &end, 0);
if (btc == ULONG_MAX && errno == ERANGE)
return false;
if (end != buffer + tok->end) {
/* Expect always 8 decimal places. */
if (*end != '.' || buffer + tok->start - end != 9)
return false;
sat = strtoul(end+1, &end, 0);
if (sat == ULONG_MAX && errno == ERANGE)
return false;
if (end != buffer + tok->end)
return false;
} else
sat = 0;
*satoshi = btc * (uint64_t)100000000 + sat;
if (*satoshi != btc * (uint64_t)100000000 + sat)
return false;
return true;
}
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok)
{
if (tok->type != JSMN_PRIMITIVE)

View File

@@ -28,6 +28,10 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
bool json_tok_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num);
/* Extract satoshis from this (may be a string, or a decimal number literal) */
bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi);
/* Is this the null primitive? */
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);