| 562 | mw_sign_and_send(struct multiwithdraw_command *mw); |
| 563 | |
| 564 | static struct command_result * |
| 565 | mw_load_outputs(struct multiwithdraw_command *mw) |
| 566 | { |
| 567 | /* Insert outputs at random locations. */ |
| 568 | for (size_t i = 0; i < tal_count(mw->outputs); ++i) { |
| 569 | struct wally_psbt_output *out; |
| 570 | /* There are already `i` outputs at this point, |
| 571 | * select from 0 to `i` inclusive, with 0 meaning |
| 572 | * "before first output" and `i` meaning "after |
| 573 | * last output". */ |
| 574 | size_t point = pseudorand(i + 1); |
| 575 | out = psbt_insert_output(mw->psbt, |
| 576 | mw->outputs[i].script, |
| 577 | mw->outputs[i].amount, |
| 578 | point); |
| 579 | if (mw->outputs[i].is_to_external) |
| 580 | psbt_output_mark_as_external(mw->psbt, out); |
| 581 | } |
| 582 | |
| 583 | if (chainparams->is_elements) { |
| 584 | struct amount_sat sum = AMOUNT_SAT(0); |
| 585 | /* Elements transactions have a fee output. |
| 586 | * Bitcoin assumes fee = inputs - outputs, so just |
| 587 | * do that here. */ |
| 588 | for (size_t i = 0; i < mw->psbt->num_inputs; ++i) |
| 589 | if (!amount_sat_add(&sum, sum, |
| 590 | psbt_input_get_amount(mw->psbt, |
| 591 | i))) |
| 592 | plugin_err(mw->cmd->plugin, |
| 593 | "Overflow in summing inputs."); |
| 594 | for (size_t i = 0; i < mw->psbt->num_outputs; ++i) |
| 595 | if (!amount_sat_sub(&sum, sum, |
| 596 | psbt_output_get_amount(mw->psbt, |
| 597 | i))) { |
| 598 | /* Not enough already. */ |
| 599 | sum = AMOUNT_SAT(0); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | /* We always add this at the end. |
| 604 | * The fee output is fairly obvious --- it is |
| 605 | * the one without a SCRIPT --- so it is actually |
| 606 | * pointless to shuffle it with the rest of the |
| 607 | * outputs, since it will never fool chain analysis. */ |
| 608 | if (!amount_sat_eq(sum, AMOUNT_SAT(0))) |
| 609 | psbt_append_output(mw->psbt, |
| 610 | NULL, |
| 611 | sum); |
| 612 | } |
| 613 | |
| 614 | return mw_sign_and_send(mw); |
| 615 | } |
| 616 | |
| 617 | /*----------------------------------------------------------------------------- |
| 618 | Sign and Send PSBT |
no test coverage detected