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