@@ -1592,6 +1592,36 @@ static void sign_all_inputs(struct bitcoin_tx *tx, struct utxo **utxos)
15921592 }
15931593}
15941594
1595+ /* For dual-funded transactions, we don't sign every input. Instead,
1596+ * we find the inputs that correspond with the utxos we have and sign
1597+ * only those
1598+ * */
1599+ static void sign_our_inputs (struct bitcoin_tx * tx , struct utxo * * utxos ,
1600+ const void * * map , size_t map_len )
1601+ {
1602+ size_t i , j ;
1603+ int input_index ;
1604+
1605+ assert (tx -> wtx -> num_inputs >= tal_count (utxos ));
1606+ assert (tal_count (utxos ) <= map_len );
1607+
1608+ /* We add utxos to the tx first, and then any other
1609+ * inputs. Thus, we can iterate through the map from zero upward
1610+ * to find the correct utxo placement to sign */
1611+ for (i = 0 ; i < tal_count (utxos ); i ++ ) {
1612+ struct pubkey inkey ;
1613+ struct bitcoin_signature sig ;
1614+
1615+ for (j = 0 ; j < map_len ; j ++ ) {
1616+ if (ptr2int (map [j ]) == i ) {
1617+ input_index = j ;
1618+ break ;
1619+ }
1620+ }
1621+ sign_input (tx , utxos [i ], & inkey , & sig , input_index );
1622+ }
1623+ }
1624+
15951625/*~ lightningd asks us to sign the transaction to fund a channel; it feeds us
15961626 * the set of inputs and the local and remote pubkeys, and we sign it. */
15971627static struct io_plan * handle_sign_funding_tx (struct io_conn * conn ,
@@ -1642,17 +1672,45 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn,
16421672{
16431673 struct utxo * * utxos ;
16441674 struct bitcoin_tx * tx ;
1675+ struct bitcoin_tx_input * * inputs ;
16451676 struct bitcoin_tx_output * * outputs ;
1677+ size_t input_count , i , j ;
1678+ int input_index ;
16461679
16471680 if (!fromwire_hsm_sign_withdrawal (tmpctx , msg_in ,
1648- & outputs , & utxos ))
1681+ & inputs , & outputs ,
1682+ & utxos ))
16491683 return bad_req (conn , c , msg_in );
16501684
1685+ input_count = tal_count (utxos ) + tal_count (inputs );
1686+ const void * map [input_count ];
1687+ for (i = 0 ; i < input_count ; i ++ )
1688+ map [i ] = int2ptr (i );
1689+
16511690 tx = withdraw_tx (tmpctx , c -> chainparams ,
16521691 cast_const2 (const struct utxo * * , utxos ),
1653- outputs , NULL );
1654-
1655- sign_all_inputs (tx , utxos );
1692+ inputs , outputs , NULL ,
1693+ (const void * * )& map );
1694+
1695+ /* Put our signatures on the transaction */
1696+ sign_our_inputs (tx , utxos , (const void * * )& map , input_count );
1697+
1698+ /* Add any 3rd party signatures */
1699+ for (i = 0 ; i < tal_count (inputs ); i ++ ) {
1700+ if (inputs [i ]-> witness ) {
1701+ size_t offset = tal_count (utxos ) + i ;
1702+
1703+ /* Find the index */
1704+ for (j = 0 ; j < input_count ; j ++ ) {
1705+ if (ptr2int (map [j ]) == offset ) {
1706+ input_index = j ;
1707+ break ;
1708+ }
1709+ }
1710+ bitcoin_tx_input_set_witness (tx , input_index ,
1711+ inputs [i ]-> witness );
1712+ }
1713+ }
16561714
16571715 return req_reply (conn , c ,
16581716 take (towire_hsm_sign_withdrawal_reply (NULL , tx )));
0 commit comments