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

Function mw_load_outputs

plugins/spender/multiwithdraw.c:563–614  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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