MCPcopy Create free account
hub / github.com/ElementsProject/lightning / bolt11_encode_

Function bolt11_encode_

common/bolt11.c:1275–1428  ·  view source on GitHub ↗

Encodes, even if it's nonsense. */

Source from the content-addressed store, hash-verified

1273
1274/* Encodes, even if it's nonsense. */
1275char *bolt11_encode_(const tal_t *ctx,
1276 const struct bolt11 *b11, bool n_field,
1277 bool (*sign)(const u5 *u5bytes,
1278 const u8 *hrpu8,
1279 secp256k1_ecdsa_recoverable_signature *rsig,
1280 void *arg),
1281 void *arg)
1282{
1283 u5 *data = tal_arr(tmpctx, u5, 0);
1284 char *hrp, *output;
1285 u64 amount;
1286 struct bolt11_field *extra;
1287 secp256k1_ecdsa_recoverable_signature rsig;
1288 u8 sig_and_recid[65];
1289 u8 *hrpu8;
1290 int recid;
1291
1292 /* BOLT #11:
1293 *
1294 * A writer:
1295 * - MUST encode `prefix` using the currency required for successful payment.
1296 * - if a specific minimum `amount` is required for successful payment:
1297 * - MUST include that `amount`.
1298 * - MUST encode `amount` as a positive decimal integer with no leading 0s.
1299 * - If the `p` multiplier is used the last decimal of `amount` MUST be `0`.
1300 * - SHOULD use the shortest representation possible, by using the largest multiplier or omitting the multiplier.
1301 */
1302 if (b11->msat) {
1303 char postfix;
1304 u64 msat = b11->msat->millisatoshis; /* Raw: best-multiplier calc */
1305 if (msat % MSAT_PER_BTC == 0) {
1306 postfix = '\0';
1307 amount = msat / MSAT_PER_BTC;
1308 } else {
1309 size_t i;
1310 for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) {
1311 if (!(msat * 10 % multipliers[i].m10))
1312 break;
1313 }
1314 postfix = multipliers[i].letter;
1315 amount = msat * 10 / multipliers[i].m10;
1316 }
1317 hrp = tal_fmt(tmpctx, "ln%s%"PRIu64"%c",
1318 b11->chain->lightning_hrp, amount, postfix);
1319 } else
1320 hrp = tal_fmt(tmpctx, "ln%s", b11->chain->lightning_hrp);
1321
1322 /* BOLT #11:
1323 *
1324 * 1. `timestamp`: seconds-since-1970 (35 bits, big-endian)
1325 * 1. zero or more tagged parts
1326 * 1. `signature`: compact ECDSA/secp256k1 signature of the above
1327 * (520 bits: 64-byte R||S + 1-byte recovery id)
1328 */
1329 push_varlen_uint(&data, b11->timestamp, 35);
1330
1331 /* This is a hack to match the test vectors, *some* of which
1332 * order differently! */

Callers

nothing calls this directly

Calls 15

push_varlen_uintFunction · 0.85
encode_sFunction · 0.85
encode_pFunction · 0.85
encode_hFunction · 0.85
encode_dFunction · 0.85
encode_mFunction · 0.85
encode_nFunction · 0.85
encode_xFunction · 0.85
encode_cFunction · 0.85
encode_fFunction · 0.85
encode_rFunction · 0.85
maybe_encode_9Function · 0.85

Tested by

no test coverage detected