mirror of
https://github.com/aljazceru/lightning.git
synced 2026-02-23 15:04:19 +01:00
common/bolt11: use struct amount_msat
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -433,7 +433,8 @@ static char *decode_r(struct bolt11 *b11,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct bolt11 *new_bolt11(const tal_t *ctx, u64 *msatoshi)
|
||||
struct bolt11 *new_bolt11(const tal_t *ctx,
|
||||
const struct amount_msat *msat TAKES)
|
||||
{
|
||||
struct bolt11 *b11 = tal(ctx, struct bolt11);
|
||||
|
||||
@@ -442,12 +443,12 @@ struct bolt11 *new_bolt11(const tal_t *ctx, u64 *msatoshi)
|
||||
b11->description_hash = NULL;
|
||||
b11->fallbacks = NULL;
|
||||
b11->routes = NULL;
|
||||
b11->msatoshi = NULL;
|
||||
b11->msat = NULL;
|
||||
b11->expiry = DEFAULT_X;
|
||||
b11->min_final_cltv_expiry = DEFAULT_C;
|
||||
|
||||
if (msatoshi)
|
||||
b11->msatoshi = tal_dup(b11, u64, msatoshi);
|
||||
if (msat)
|
||||
b11->msat = tal_dup(b11, struct amount_msat, msat);
|
||||
return b11;
|
||||
}
|
||||
|
||||
@@ -520,7 +521,7 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
|
||||
*
|
||||
* - SHOULD indicate to the payer that amount is unspecified.
|
||||
*/
|
||||
b11->msatoshi = NULL;
|
||||
b11->msat = NULL;
|
||||
} else {
|
||||
u64 m10 = 10;
|
||||
u64 amount;
|
||||
@@ -556,8 +557,8 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
|
||||
* `amount` by the `multiplier` value to derive the
|
||||
* amount required for payment.
|
||||
*/
|
||||
b11->msatoshi = tal(b11, u64);
|
||||
*b11->msatoshi = amount * m10 / 10;
|
||||
b11->msat = tal(b11, struct amount_msat);
|
||||
b11->msat->millisatoshis = amount * m10 / 10;
|
||||
}
|
||||
|
||||
/* BOLT #11:
|
||||
@@ -886,19 +887,20 @@ char *bolt11_encode_(const tal_t *ctx,
|
||||
* - MUST encode `amount` as a positive decimal integer with no leading 0s.
|
||||
* - SHOULD use the shortest representation possible, by using the largest multiplier or omitting the multiplier.
|
||||
*/
|
||||
if (b11->msatoshi) {
|
||||
if (b11->msat) {
|
||||
char postfix;
|
||||
if (*b11->msatoshi % MSAT_PER_BTC == 0) {
|
||||
u64 msat = b11->msat->millisatoshis;
|
||||
if (msat % MSAT_PER_BTC == 0) {
|
||||
postfix = '\0';
|
||||
amount = *b11->msatoshi / MSAT_PER_BTC;
|
||||
amount = msat / MSAT_PER_BTC;
|
||||
} else {
|
||||
size_t i;
|
||||
for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) {
|
||||
if (!(*b11->msatoshi * 10 % multipliers[i].m10))
|
||||
if (!(msat * 10 % multipliers[i].m10))
|
||||
break;
|
||||
}
|
||||
postfix = multipliers[i].letter;
|
||||
amount = *b11->msatoshi * 10 / multipliers[i].m10;
|
||||
amount = msat * 10 / multipliers[i].m10;
|
||||
}
|
||||
hrp = tal_fmt(tmpctx, "ln%s%"PRIu64"%c",
|
||||
b11->chain->bip173_name, amount, postfix);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <bitcoin/short_channel_id.h>
|
||||
#include <ccan/list/list.h>
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <ccan/take/take.h>
|
||||
#include <common/hash_u5.h>
|
||||
#include <secp256k1_recovery.h>
|
||||
|
||||
@@ -37,7 +38,7 @@ struct route_info {
|
||||
struct bolt11 {
|
||||
const struct chainparams *chain;
|
||||
u64 timestamp;
|
||||
u64 *msatoshi; /* NULL if not specified. */
|
||||
struct amount_msat *msat; /* NULL if not specified. */
|
||||
|
||||
struct sha256 payment_hash;
|
||||
struct pubkey receiver_id;
|
||||
@@ -70,7 +71,8 @@ struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str,
|
||||
const char *description, char **fail);
|
||||
|
||||
/* Initialize an empty bolt11 struct with optional amount */
|
||||
struct bolt11 *new_bolt11(const tal_t *ctx, u64 *msatoshi);
|
||||
struct bolt11 *new_bolt11(const tal_t *ctx,
|
||||
const struct amount_msat *msat TAKES);
|
||||
|
||||
/* Encodes and signs, even if it's nonsense. */
|
||||
char *bolt11_encode_(const tal_t *ctx,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "../amount.c"
|
||||
#include "../bech32.c"
|
||||
#include "../bech32_util.c"
|
||||
#include "../bolt11.c"
|
||||
@@ -79,10 +80,10 @@ static void test_b11(const char *b11str,
|
||||
|
||||
assert(b11->chain == expect_b11->chain);
|
||||
assert(b11->timestamp == expect_b11->timestamp);
|
||||
if (!b11->msatoshi)
|
||||
assert(!expect_b11->msatoshi);
|
||||
if (!b11->msat)
|
||||
assert(!expect_b11->msat);
|
||||
else
|
||||
assert(*b11->msatoshi == *expect_b11->msatoshi);
|
||||
assert(amount_msat_eq(*b11->msat, *expect_b11->msat));
|
||||
assert(sha256_eq(&b11->payment_hash, &expect_b11->payment_hash));
|
||||
if (!b11->description)
|
||||
assert(!expect_b11->description);
|
||||
@@ -120,7 +121,7 @@ int main(void)
|
||||
|
||||
struct bolt11 *b11;
|
||||
struct pubkey node;
|
||||
u64 msatoshi;
|
||||
struct amount_msat msatoshi;
|
||||
const char *badstr;
|
||||
|
||||
wally_init(0);
|
||||
@@ -195,7 +196,7 @@ int main(void)
|
||||
* * `aztrnwngzn3kdzw5hydlzf03qdgm2hdq27cqv3agm2awhz5se903vruatfhq77w3ls4evs3ch9zw97j25emudupq63nyw24cg27h2rsp`: signature
|
||||
* * `fj9srp`: Bech32 checksum
|
||||
*/
|
||||
msatoshi = 2500 * (1000ULL * 100000000) / 1000000;
|
||||
msatoshi = AMOUNT_MSAT(2500 * (1000ULL * 100000000) / 1000000);
|
||||
b11 = new_bolt11(tmpctx, &msatoshi);
|
||||
b11->chain = chainparams_for_network("bitcoin");
|
||||
b11->timestamp = 1496314658;
|
||||
@@ -227,7 +228,7 @@ int main(void)
|
||||
* * `cc6gd6ql3jrc5yzme8v4ntcewwz5cnw92tz0pc8qcuufvq7khhr8wpald05e92xw006sq94mg8v2ndf4sefvf9sygkshp5zfem29trqq`: signature
|
||||
* * `2yxxz7`: Bech32 checksum
|
||||
*/
|
||||
msatoshi = 20 * (1000ULL * 100000000) / 1000;
|
||||
msatoshi = AMOUNT_MSAT(20 * (1000ULL * 100000000) / 1000);
|
||||
b11 = new_bolt11(tmpctx, &msatoshi);
|
||||
b11->chain = chainparams_for_network("bitcoin");
|
||||
b11->timestamp = 1496314658;
|
||||
@@ -252,7 +253,7 @@ int main(void)
|
||||
}
|
||||
|
||||
/* ALL UPPERCASE is allowed (useful for QR codes) */
|
||||
msatoshi = 2500 * (1000ULL * 100000000) / 1000000;
|
||||
msatoshi = AMOUNT_MSAT(2500 * (1000ULL * 100000000) / 1000000);
|
||||
b11 = new_bolt11(tmpctx, &msatoshi);
|
||||
b11->chain = chainparams_for_network("bitcoin");
|
||||
b11->timestamp = 1496314658;
|
||||
|
||||
Reference in New Issue
Block a user