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

Function bolt11_encode_

common/bolt11.c:1123–1260  ·  view source on GitHub ↗

Encodes, even if it's nonsense. */

Source from the content-addressed store, hash-verified

1121
1122/* Encodes, even if it's nonsense. */
1123char *bolt11_encode_(const tal_t *ctx,
1124 const struct bolt11 *b11, bool n_field,
1125 bool (*sign)(const u5 *u5bytes,
1126 const u8 *hrpu8,
1127 secp256k1_ecdsa_recoverable_signature *rsig,
1128 void *arg),
1129 void *arg)
1130{
1131 u5 *data = tal_arr(tmpctx, u5, 0);
1132 char *hrp, *output;
1133 u64 amount;
1134 struct bolt11_field *extra;
1135 secp256k1_ecdsa_recoverable_signature rsig;
1136 u8 sig_and_recid[65];
1137 u8 *hrpu8;
1138 int recid;
1139
1140 /* BOLT #11:
1141 *
1142 * A writer:
1143 * - MUST encode `prefix` using the currency required for successful payment.
1144 * - if a specific minimum `amount` is required for successful payment:
1145 * - MUST include that `amount`.
1146 * - MUST encode `amount` as a positive decimal integer with no leading 0s.
1147 * - If the `p` multiplier is used the last decimal of `amount` MUST be `0`.
1148 * - SHOULD use the shortest representation possible, by using the largest multiplier or omitting the multiplier.
1149 */
1150 if (b11->msat) {
1151 char postfix;
1152 u64 msat = b11->msat->millisatoshis; /* Raw: best-multiplier calc */
1153 if (msat % MSAT_PER_BTC == 0) {
1154 postfix = '\0';
1155 amount = msat / MSAT_PER_BTC;
1156 } else {
1157 size_t i;
1158 for (i = 0; i < ARRAY_SIZE(multipliers)-1; i++) {
1159 if (!(msat * 10 % multipliers[i].m10))
1160 break;
1161 }
1162 postfix = multipliers[i].letter;
1163 amount = msat * 10 / multipliers[i].m10;
1164 }
1165 hrp = tal_fmt(tmpctx, "ln%s%"PRIu64"%c",
1166 b11->chain->lightning_hrp, amount, postfix);
1167 } else
1168 hrp = tal_fmt(tmpctx, "ln%s", b11->chain->lightning_hrp);
1169
1170 /* BOLT #11:
1171 *
1172 * 1. `timestamp`: seconds-since-1970 (35 bits, big-endian)
1173 * 1. zero or more tagged parts
1174 * 1. `signature`: Bitcoin-style signature of above (520 bits)
1175 */
1176 push_varlen_uint(&data, b11->timestamp, 35);
1177
1178 /* This is a hack to match the test vectors, *some* of which
1179 * order differently! */
1180 if (IFDEV(modern_order(b11), true)) {

Callers

nothing calls this directly

Calls 15

push_varlen_uintFunction · 0.85
modern_orderFunction · 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

Tested by

no test coverage detected