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

Function mw_load_outputs

plugins/spender/multiwithdraw.c:564–615  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

562mw_sign_and_send(struct multiwithdraw_command *mw);
563
564static struct command_result *
565mw_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/*-----------------------------------------------------------------------------
618Sign and Send PSBT

Callers 2

mw_after_fundpsbtFunction · 0.85
mw_after_newaddrFunction · 0.85

Calls 11

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

Tested by

no test coverage detected