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

Function mw_load_outputs

plugins/spender/multiwithdraw.c:565–616  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

mw_after_fundpsbtFunction · 0.85
mw_after_newaddrFunction · 0.85

Calls 10

psbt_insert_outputFunction · 0.85
psbt_input_get_amountFunction · 0.85
psbt_output_get_amountFunction · 0.85
psbt_append_outputFunction · 0.85
mw_sign_and_sendFunction · 0.85
amount_sat_addFunction · 0.50
plugin_errFunction · 0.50
amount_sat_subFunction · 0.50
amount_sat_eqFunction · 0.50

Tested by

no test coverage detected