11#include "bitcoin/feerate.h"
22#include <bitcoin/privkey.h>
33#include <bitcoin/script.h>
4+ #include <ccan/cast/cast.h>
45#include <ccan/tal/str/str.h>
56#include <common/addr.h>
67#include <common/channel_config.h>
78#include <common/features.h>
89#include <common/funding_tx.h>
910#include <common/json_command.h>
11+ #include <common/json_helpers.h>
1012#include <common/jsonrpc_errors.h>
1113#include <common/key_derive.h>
1214#include <common/param.h>
@@ -679,20 +681,23 @@ static void opening_fundee_finished(struct subd *openingd,
679681 const int * fds ,
680682 struct uncommitted_channel * uc )
681683{
682- u8 * funding_signed ;
684+ u8 * final_peer_msg ;
683685 struct channel_info channel_info ;
684686 struct bitcoin_signature remote_commit_sig ;
685687 struct bitcoin_tx * remote_commit ;
686688 struct lightningd * ld = openingd -> ld ;
687689 struct bitcoin_txid funding_txid ;
688690 u16 funding_outnum ;
689- struct amount_sat funding ;
691+ struct amount_sat opener_funding ;
692+ struct amount_sat accepter_funding ;
693+ struct amount_sat total_funding ;
690694 struct amount_msat push ;
691695 u32 feerate ;
692696 u8 channel_flags ;
693697 struct channel * channel ;
694698 u8 * remote_upfront_shutdown_script ;
695699 struct per_peer_state * pps ;
700+ struct utxo * * utxos ;
696701
697702 log_debug (uc -> log , "Got opening_fundee_finish_response" );
698703
@@ -712,12 +717,12 @@ static void opening_fundee_finished(struct subd *openingd,
712717 & channel_info .remote_fundingkey ,
713718 & funding_txid ,
714719 & funding_outnum ,
715- & funding ,
716- & AMOUNT_SAT ( 0 ) ,
720+ & opener_funding ,
721+ & accepter_funding ,
717722 & push ,
718723 & channel_flags ,
719724 & feerate ,
720- & funding_signed ,
725+ & final_peer_msg ,
721726 & uc -> our_config .channel_reserve ,
722727 & remote_upfront_shutdown_script )) {
723728 log_broken (uc -> log , "bad OPENING_FUNDEE_REPLY %s" ,
@@ -736,14 +741,23 @@ static void opening_fundee_finished(struct subd *openingd,
736741 goto failed ;
737742 }
738743
744+ if (!amount_sat_add (& total_funding , opener_funding , accepter_funding ))
745+ abort ();
746+
747+ if (uc -> pf )
748+ /* We steal here, because otherwise gets eaten below */
749+ utxos = tal_steal (tmpctx , uc -> pf -> utxos );
750+ else
751+ utxos = NULL ;
752+
739753 /* Consumes uc */
740754 channel = wallet_commit_channel (ld , uc ,
741755 remote_commit ,
742756 & remote_commit_sig ,
743757 & funding_txid ,
744758 funding_outnum ,
745- funding ,
746- AMOUNT_SAT ( 0 ) ,
759+ total_funding ,
760+ accepter_funding ,
747761 push ,
748762 REMOTE ,
749763 channel_flags ,
@@ -759,14 +773,19 @@ static void opening_fundee_finished(struct subd *openingd,
759773 type_to_string (reply , struct bitcoin_txid ,
760774 & channel -> funding_txid ));
761775
776+ if (utxos ) {
777+ // TODO: mark utxos as shared
778+ tal_free (utxos );
779+ }
780+
762781 channel_watch_funding (ld , channel );
763782
764783 /* Tell plugins about the success */
765784 notify_channel_opened (ld , & channel -> peer -> id , & channel -> funding ,
766785 & channel -> funding_txid , & channel -> remote_funding_locked );
767786
768787 /* On to normal operation! */
769- peer_start_channeld (channel , pps , funding_signed , false);
788+ peer_start_channeld (channel , pps , final_peer_msg , false);
770789
771790 subd_release_channel (openingd , uc );
772791 uc -> openingd = NULL ;
@@ -778,6 +797,7 @@ static void opening_fundee_finished(struct subd *openingd,
778797 close (fds [1 ]);
779798 close (fds [3 ]);
780799 tal_free (uc );
800+ tal_free (utxos );
781801}
782802
783803static void opening_funder_failed (struct subd * openingd , const u8 * msg ,
@@ -940,9 +960,9 @@ static void channel_config(struct lightningd *ld,
940960
941961struct openchannel_hook_payload {
942962 struct subd * openingd ;
943- struct amount_sat funding_satoshis ;
944963 /* Is this a v2 openchannel call? */
945964 bool is_v2 ;
965+ struct amount_sat opener_satoshis ;
946966 struct amount_msat push_msat ;
947967 struct amount_sat dust_limit_satoshis ;
948968 struct amount_msat max_htlc_value_in_flight_msat ;
@@ -965,7 +985,7 @@ openchannel_hook_serialize(struct openchannel_hook_payload *payload,
965985 json_object_start (stream , "openchannel" );
966986 json_add_node_id (stream , "id" , & uc -> peer -> id );
967987 json_add_amount_sat_only (stream , "funding_satoshis" ,
968- payload -> funding_satoshis );
988+ payload -> opener_satoshis );
969989 json_add_amount_msat_only (stream , "push_msat" , payload -> push_msat );
970990 json_add_amount_sat_only (stream , "dust_limit_satoshis" ,
971991 payload -> dust_limit_satoshis );
@@ -994,11 +1014,16 @@ static void openchannel_payload_remove_openingd(struct subd *openingd,
9941014}
9951015
9961016static void openchannel_hook_cb (struct openchannel_hook_payload * payload ,
997- const char * buffer ,
998- const jsmntok_t * toks )
1017+ const char * buffer ,
1018+ const jsmntok_t * toks )
9991019{
10001020 struct subd * openingd = payload -> openingd ;
1021+ struct uncommitted_channel * uc = openingd -> channel ;
1022+ struct peer_funding * pf = uc -> pf ;
10011023 const char * errmsg = NULL ;
1024+ struct amount_sat change ;
1025+ struct bitcoin_tx_output * * outputs ;
1026+ bool has_change ;
10021027
10031028 /* We want to free this, whatever happens. */
10041029 tal_steal (tmpctx , payload );
@@ -1009,6 +1034,7 @@ static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
10091034
10101035 tal_del_destructor2 (openingd , openchannel_payload_remove_openingd , payload );
10111036
1037+ pf -> accepter_funding = AMOUNT_SAT (0 );
10121038 /* If we had a hook, check what it says */
10131039 if (buffer ) {
10141040 const jsmntok_t * t = json_get_member (buffer , toks , "result" );
@@ -1031,10 +1057,107 @@ static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
10311057 fatal ("Plugin returned an invalid result for the "
10321058 "openchannel hook: %.*s" ,
10331059 t -> end - t -> start , buffer + t -> start );
1060+
1061+ if (payload -> is_v2 && !errmsg ) {
1062+ const jsmntok_t * funding_sats = json_get_member (buffer ,
1063+ toks ,
1064+ "funding_sats" );
1065+ if (funding_sats )
1066+ json_to_sat (buffer , funding_sats , & pf -> accepter_funding );
1067+ }
1068+ }
1069+
1070+ if (!payload -> is_v2 || errmsg ) {
1071+ subd_send_msg (openingd ,
1072+ take (towire_opening_got_offer_reply (NULL , errmsg )));
1073+ return ;
1074+ }
1075+
1076+ /* Find utxos for funding this */
1077+ if (amount_sat_greater (pf -> accepter_funding , AMOUNT_SAT (0 ))) {
1078+ /* Print a warning if we're trying to fund a channel with more
1079+ * than we said that we'd be allowed to */
1080+ if (amount_sat_greater (pf -> accepter_funding , payload -> available_funds ))
1081+ log_info (openingd -> log ,
1082+ "Attempting to fund channel for %s when max was set to %s" ,
1083+ type_to_string (
1084+ tmpctx , struct amount_sat , & pf -> accepter_funding ),
1085+ type_to_string (
1086+ tmpctx , struct amount_sat , & payload -> available_funds ));
1087+
1088+ struct amount_sat fee_estimate UNUSED ;
1089+ pf -> utxos = (struct utxo * * )wallet_select_coins (pf , openingd -> ld -> wallet ,
1090+ true, pf -> accepter_funding ,
1091+ 0 , 0 ,
1092+ UINT32_MAX , /* minconf 1 */
1093+ & fee_estimate , & change );
1094+
1095+ /* Verify that we're still under the max remote that is allowed */
1096+ if (tal_count (pf -> utxos ) > REMOTE_CONTRIB_LIMIT ) {
1097+ log_info (openingd -> log ,
1098+ "Too many utxos selected (%ld), only %" PRIu16 " allowed" ,
1099+ tal_count (pf -> utxos ), REMOTE_CONTRIB_LIMIT - 1 );
1100+ pf -> utxos = tal_free (pf -> utxos );
1101+ change = AMOUNT_SAT (0 );
1102+ }
1103+ } else {
1104+ change = AMOUNT_SAT (0 );
1105+ pf -> utxos = NULL ;
1106+ }
1107+
1108+ /* Either no utxos were found, or we weren't funding it anyway */
1109+ if (!pf -> utxos ) {
1110+ if (amount_sat_greater (pf -> accepter_funding , AMOUNT_SAT (0 )))
1111+ /* FIXME: send notification to the plugin that
1112+ * we weren't able to fund this channel as they
1113+ * requested; utxo set has changed since hook was called */
1114+ log_unusual (openingd -> log ,
1115+ "Unable to fund channel with %s; utxos unavailable" ,
1116+ type_to_string (tmpctx , struct amount_sat ,
1117+ & pf -> accepter_funding ));
1118+
1119+ pf -> accepter_funding = AMOUNT_SAT (0 );
1120+ change = AMOUNT_SAT (0 );
1121+ }
1122+
1123+ has_change = amount_sat_greater (change , AMOUNT_SAT (0 ));
1124+ outputs = tal_arr (tmpctx , struct bitcoin_tx_output * , has_change ? 1 : 0 );
1125+ if (has_change ) {
1126+ struct bitcoin_tx_output * output ;
1127+ struct pubkey * changekey ;
1128+ s64 change_keyindex ;
1129+
1130+ output = tal (outputs , struct bitcoin_tx_output );
1131+ output -> amount = change ;
1132+
1133+ change_keyindex = wallet_get_newindex (openingd -> ld );
1134+ changekey = tal (tmpctx , struct pubkey );
1135+ if (!bip32_pubkey (openingd -> ld -> wallet -> bip32_base ,
1136+ changekey , change_keyindex )) {
1137+ fatal ("Error deriving change key %lu" , change_keyindex );
1138+ }
1139+ output -> script = scriptpubkey_p2wpkh (output , changekey );
1140+ outputs [0 ] = output ;
1141+ }
1142+
1143+ /* We need to supply the scriptSig to our peer, for P2SH wrapped inputs */
1144+ for (size_t i = 0 ; i < tal_count (pf -> utxos ); i ++ ) {
1145+ if (pf -> utxos [i ]-> is_p2sh )
1146+ pf -> utxos [i ]-> scriptSig =
1147+ derive_redeem_scriptsig (pf -> utxos [i ],
1148+ openingd -> ld -> wallet ,
1149+ pf -> utxos [i ]-> keyindex );
1150+ else
1151+ pf -> utxos [i ]-> scriptSig = NULL ;
1152+
10341153 }
10351154
10361155 subd_send_msg (openingd ,
1037- take (towire_opening_got_offer_reply (NULL , errmsg )));
1156+ take (towire_opening_got_offer_reply_fund (
1157+ NULL , pf -> accepter_funding ,
1158+ cast_const2 (const struct utxo * * , pf -> utxos ),
1159+ cast_const2 (const struct bitcoin_tx_output * * ,
1160+ outputs ))));
10381161}
10391162
10401163REGISTER_PLUGIN_HOOK (openchannel ,
@@ -1061,7 +1184,7 @@ static void opening_got_offer(struct subd *openingd,
10611184 payload -> openingd = openingd ;
10621185 if (!fromwire_opening_got_offer (payload , msg ,
10631186 & payload -> is_v2 ,
1064- & payload -> funding_satoshis ,
1187+ & payload -> opener_satoshis ,
10651188 & payload -> push_msat ,
10661189 & payload -> dust_limit_satoshis ,
10671190 & payload -> max_htlc_value_in_flight_msat ,
@@ -1157,6 +1280,7 @@ static unsigned int openingd_msg(struct subd *openingd,
11571280 case WIRE_OPENING_FUNDER_COMPLETE :
11581281 case WIRE_OPENING_FUNDER_CANCEL :
11591282 case WIRE_OPENING_GOT_OFFER_REPLY :
1283+ case WIRE_OPENING_GOT_OFFER_REPLY_FUND :
11601284 case WIRE_OPENING_DEV_MEMLEAK :
11611285 /* Replies never get here */
11621286 case WIRE_OPENING_DEV_MEMLEAK_REPLY :
0 commit comments