Browse Source

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>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
725512fb03
  1. 28
      daemon/json.c
  2. 4
      daemon/json.h

28
daemon/json.c

@ -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)

4
daemon/json.h

@ -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);

Loading…
Cancel
Save