| 325 | } |
| 326 | |
| 327 | static struct command_result *finish_psbt(struct command *cmd, |
| 328 | struct utxo **utxos, |
| 329 | u32 feerate_per_kw, |
| 330 | size_t weight, |
| 331 | struct amount_sat excess, |
| 332 | u32 reserve, |
| 333 | u32 *locktime, |
| 334 | bool excess_as_change) |
| 335 | { |
| 336 | struct json_stream *response; |
| 337 | struct wally_psbt *psbt; |
| 338 | size_t change_outnum COMPILER_WANTS_INIT("gcc 9.4.0 -Og"); |
| 339 | u32 current_height = get_block_height(cmd->ld->topology); |
| 340 | |
| 341 | /* Setting the locktime to the next block to be mined has multiple |
| 342 | * benefits: |
| 343 | * - anti fee-snipping (even if not yet likely) |
| 344 | * - less distinguishable transactions (with this we create |
| 345 | * general-purpose transactions which looks like bitcoind: |
| 346 | * native segwit, nlocktime set to tip, and sequence set to |
| 347 | * 0xFFFFFFFD by default. Other wallets are likely to implement |
| 348 | * this too). |
| 349 | */ |
| 350 | if (!locktime) { |
| 351 | locktime = tal(cmd, u32); |
| 352 | *locktime = current_height; |
| 353 | |
| 354 | /* Eventually fuzz it too. */ |
| 355 | if (*locktime > 100 && pseudorand(10) == 0) |
| 356 | *locktime -= pseudorand(100); |
| 357 | } |
| 358 | |
| 359 | psbt = psbt_using_utxos(cmd, cmd->ld->wallet, utxos, |
| 360 | cmd->ld->wallet->bip32_base, |
| 361 | *locktime, BITCOIN_TX_RBF_SEQUENCE); |
| 362 | |
| 363 | /* Should we add a change output for the excess? */ |
| 364 | if (excess_as_change) { |
| 365 | struct amount_sat change; |
| 366 | struct pubkey pubkey; |
| 367 | s64 keyidx; |
| 368 | u8 *b32script; |
| 369 | |
| 370 | /* Checks for dust, returns 0sat if below dust */ |
| 371 | change = change_amount(excess, feerate_per_kw, weight); |
| 372 | if (!amount_sat_greater(change, AMOUNT_SAT(0))) { |
| 373 | excess_as_change = false; |
| 374 | goto fee_calc; |
| 375 | } |
| 376 | |
| 377 | /* Get a change adddress */ |
| 378 | keyidx = wallet_get_newindex(cmd->ld); |
| 379 | if (keyidx < 0) |
| 380 | return command_fail(cmd, LIGHTNINGD, |
| 381 | "Failed to generate change address." |
| 382 | " Keys exhausted."); |
| 383 | |
| 384 | if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx)) |
no test coverage detected