~ The PSBT we are holding currently has no outputs (except an optional change output). We now proceed to filling in those outputs now that we know what the funding scriptpubkeys are. First thing we do is to shuffle the outputs. This is needed in order to decorrelate the transaction outputs from the parameters passed into the command. We should assume that the caller of the command might inadver
| 844 | of its arguments, so we shuffle the outputs. |
| 845 | */ |
| 846 | static struct command_result * |
| 847 | perform_funding_tx_finalize(struct multifundchannel_command *mfc) |
| 848 | { |
| 849 | struct multifundchannel_destination **deck; |
| 850 | char *content = tal_strdup(tmpctx, ""); |
| 851 | size_t v1_dest_count = dest_count(mfc, FUND_CHANNEL); |
| 852 | size_t v2_dest_count = dest_count(mfc, OPEN_CHANNEL); |
| 853 | size_t i, deck_i; |
| 854 | u32 psbt_version = mfc->psbt->version; |
| 855 | |
| 856 | plugin_log(mfc->cmd->plugin, LOG_DBG, |
| 857 | "mfc %"PRIu64": Creating funding tx.", |
| 858 | mfc->id); |
| 859 | |
| 860 | /* We operate over PSBTv2 only */ |
| 861 | if (!psbt_set_version(mfc->psbt, 2)) { |
| 862 | /* It should be well-formed? */ |
| 863 | plugin_err(mfc->cmd->plugin, |
| 864 | "mfc: could not set PSBT version: %s", |
| 865 | fmt_wally_psbt(tmpctx, mfc->psbt)); |
| 866 | } |
| 867 | |
| 868 | /* Construct a deck of destinations. */ |
| 869 | deck = tal_arr(tmpctx, struct multifundchannel_destination *, |
| 870 | v1_dest_count); |
| 871 | |
| 872 | deck_i = 0; |
| 873 | for (i = 0; i < tal_count(mfc->destinations); i++) { |
| 874 | if (is_v2(&mfc->destinations[i])) |
| 875 | continue; |
| 876 | |
| 877 | assert(deck_i < tal_count(deck)); |
| 878 | deck[deck_i++] = &mfc->destinations[i]; |
| 879 | } |
| 880 | |
| 881 | /* Fisher-Yates shuffle. */ |
| 882 | for (i = tal_count(deck); i > 1; --i) { |
| 883 | size_t j = pseudorand(i); |
| 884 | if (j == i - 1) |
| 885 | continue; |
| 886 | struct multifundchannel_destination *tmp; |
| 887 | tmp = deck[j]; |
| 888 | deck[j] = deck[i - 1]; |
| 889 | deck[i - 1] = tmp; |
| 890 | } |
| 891 | |
| 892 | /* Now that we have our outputs shuffled, add outputs to the PSBT. */ |
| 893 | for (unsigned int outnum = 0; outnum < tal_count(deck); ++outnum) { |
| 894 | struct multifundchannel_destination *dest; |
| 895 | |
| 896 | if (outnum != 0) |
| 897 | tal_append_fmt(&content, ", "); |
| 898 | |
| 899 | /* Funding outpoint. */ |
| 900 | dest = deck[outnum]; |
| 901 | (void) psbt_insert_output(mfc->psbt, |
| 902 | dest->funding_script, |
| 903 | dest->amount, |
no test coverage detected