| 254 | } |
| 255 | |
| 256 | bool invoices_create(struct invoices *invoices, |
| 257 | struct invoice *pinvoice, |
| 258 | const struct amount_msat *msat TAKES, |
| 259 | const struct json_escape *label TAKES, |
| 260 | u64 expiry, |
| 261 | const char *b11enc, |
| 262 | const char *description, |
| 263 | const u8 *features, |
| 264 | const struct preimage *r, |
| 265 | const struct sha256 *rhash, |
| 266 | const struct sha256 *local_offer_id) |
| 267 | { |
| 268 | struct db_stmt *stmt; |
| 269 | struct invoice dummy; |
| 270 | u64 expiry_time; |
| 271 | u64 now = time_now().ts.tv_sec; |
| 272 | |
| 273 | if (invoices_find_by_label(invoices, &dummy, label)) { |
| 274 | if (taken(msat)) |
| 275 | tal_free(msat); |
| 276 | if (taken(label)) |
| 277 | tal_free(label); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | /* Compute expiration. */ |
| 282 | expiry_time = now + expiry; |
| 283 | |
| 284 | /* Save to database. */ |
| 285 | stmt = db_prepare_v2( |
| 286 | invoices->db, |
| 287 | SQL("INSERT INTO invoices" |
| 288 | " ( payment_hash, payment_key, state" |
| 289 | " , msatoshi, label, expiry_time" |
| 290 | " , pay_index, msatoshi_received" |
| 291 | " , paid_timestamp, bolt11, description, features, local_offer_id)" |
| 292 | " VALUES ( ?, ?, ?" |
| 293 | " , ?, ?, ?" |
| 294 | " , NULL, NULL" |
| 295 | " , NULL, ?, ?, ?, ?);")); |
| 296 | |
| 297 | db_bind_sha256(stmt, 0, rhash); |
| 298 | db_bind_preimage(stmt, 1, r); |
| 299 | db_bind_int(stmt, 2, UNPAID); |
| 300 | if (msat) |
| 301 | db_bind_amount_msat(stmt, 3, msat); |
| 302 | else |
| 303 | db_bind_null(stmt, 3); |
| 304 | db_bind_json_escape(stmt, 4, label); |
| 305 | db_bind_u64(stmt, 5, expiry_time); |
| 306 | db_bind_text(stmt, 6, b11enc); |
| 307 | if (!description) |
| 308 | db_bind_null(stmt, 7); |
| 309 | else |
| 310 | db_bind_text(stmt, 7, description); |
| 311 | db_bind_talarr(stmt, 8, features); |
| 312 | if (local_offer_id) |
| 313 | db_bind_sha256(stmt, 9, local_offer_id); |
no test coverage detected