| 333 | } |
| 334 | |
| 335 | static struct command_result *finish_psbt(struct command *cmd, |
| 336 | struct utxo **utxos, |
| 337 | u32 feerate_per_kw, |
| 338 | size_t weight, |
| 339 | struct amount_sat excess, |
| 340 | u32 reserve, |
| 341 | u32 *locktime, |
| 342 | struct amount_sat change) |
| 343 | { |
| 344 | struct json_stream *response; |
| 345 | struct wally_psbt *psbt; |
| 346 | ssize_t change_outnum; |
| 347 | u32 current_height = get_block_height(cmd->ld->topology); |
| 348 | |
| 349 | if (!locktime) { |
| 350 | locktime = tal(cmd, u32); |
| 351 | *locktime = default_locktime(cmd->ld->topology); |
| 352 | } |
| 353 | |
| 354 | psbt = psbt_using_utxos(cmd, cmd->ld->wallet, utxos, |
| 355 | *locktime, BITCOIN_TX_RBF_SEQUENCE, |
| 356 | NULL); |
| 357 | assert(psbt->version == 2); |
| 358 | |
| 359 | /* Should we add a change output? (Iff it can pay for itself!) */ |
| 360 | change = change_amount(change, feerate_per_kw, weight); |
| 361 | if (amount_sat_greater(change, AMOUNT_SAT(0))) { |
| 362 | s64 keyidx; |
| 363 | u8 *b32script; |
| 364 | enum addrtype type; |
| 365 | |
| 366 | /* FIXME: P2TR for elements! */ |
| 367 | if (chainparams->is_elements) |
| 368 | type = ADDR_BECH32; |
| 369 | else |
| 370 | type = ADDR_P2TR; |
| 371 | |
| 372 | /* Get a change adddress */ |
| 373 | keyidx = wallet_get_newindex(cmd->ld, type); |
| 374 | if (keyidx < 0) |
| 375 | return command_fail(cmd, LIGHTNINGD, |
| 376 | "Failed to generate change address." |
| 377 | " Keys exhausted."); |
| 378 | |
| 379 | if (chainparams->is_elements) { |
| 380 | b32script = p2wpkh_for_keyidx(tmpctx, cmd->ld, keyidx); |
| 381 | } else { |
| 382 | b32script = p2tr_for_keyidx(tmpctx, cmd->ld, keyidx); |
| 383 | } |
| 384 | if (!b32script) { |
| 385 | return command_fail(cmd, LIGHTNINGD, |
| 386 | "Failed to generate change address." |
| 387 | " Keys generation failure"); |
| 388 | } |
| 389 | |
| 390 | change_outnum = psbt->num_outputs; |
| 391 | psbt_append_output(psbt, b32script, change); |
| 392 | /* Add additional weight of output */ |
no test coverage detected