@ -2,6 +2,8 @@
# include <assert.h>
# include <bitcoin/script.h>
# include <bitcoin/tx.h>
# include <ccan/ptrint/ptrint.h>
# include <lightningd/utxo.h>
# include <permute_tx.h>
# ifndef SUPERVERBOSE
@ -9,9 +11,8 @@
# endif
struct bitcoin_tx * funding_tx ( const tal_t * ctx ,
const struct sha256_double * input_txid ,
unsigned int input_txout ,
u64 input_satoshis ,
u32 * outnum ,
const struct utxo * utxos ,
u64 funding_satoshis ,
const struct pubkey * local_fundingkey ,
const struct pubkey * remote_fundingkey ,
@ -19,13 +20,17 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx,
u64 feerate_per_kw ,
u64 dust_limit_satoshis )
{
struct bitcoin_tx * tx = bitcoin_tx ( ctx , 1 , 2 ) ;
struct bitcoin_tx * tx = bitcoin_tx ( ctx , tal_count ( utxos ) , 2 ) ;
u8 * wscript ;
u64 fee , weight ;
tx - > input [ 0 ] . txid = * input_txid ;
tx - > input [ 0 ] . index = input_txout ;
tx - > input [ 0 ] . amount = tal_dup ( tx , u64 , & input_satoshis ) ;
u64 fee , weight , input_satoshis = 0 ;
size_t i ;
for ( i = 0 ; i < tal_count ( utxos ) ; i + + ) {
tx - > input [ i ] . txid = utxos [ i ] . txid ;
tx - > input [ i ] . index = utxos [ i ] . outnum ;
tx - > input [ i ] . amount = tal_dup ( tx , u64 , & utxos [ i ] . amount ) ;
input_satoshis + = utxos [ i ] . amount ;
}
tx - > output [ 0 ] . amount = funding_satoshis ;
wscript = bitcoin_redeem_2of2 ( tx , local_fundingkey , remote_fundingkey ) ;
@ -42,26 +47,17 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx,
fee = weight * feerate_per_kw / 1000 ;
/* Too small an output after fee? Drop it. */
if ( input_satoshis - funding_satoshis < dust_limit_satoshis + fee )
if ( input_satoshis - funding_satoshis < dust_limit_satoshis + fee ) {
tal_resize ( & tx - > output , 1 ) ;
else {
* outnum = 0 ;
} else {
const void * map [ 2 ] ;
map [ 0 ] = int2ptr ( 0 ) ;
map [ 1 ] = int2ptr ( 1 ) ;
tx - > output [ 1 ] . amount = input_satoshis - funding_satoshis - fee ;
permute_outputs ( tx - > output , tal_count ( tx - > output ) , NULL ) ;
permute_outputs ( tx - > output , tal_count ( tx - > output ) , map ) ;
* outnum = ( map [ 0 ] = = int2ptr ( 0 ) ? 0 : 1 ) ;
}
return tx ;
}
void sign_funding_tx ( struct bitcoin_tx * funding ,
const struct pubkey * inputkey ,
const struct privkey * input_privkey )
{
secp256k1_ecdsa_signature sig ;
u8 * subscript = scriptpubkey_p2pkh ( funding , inputkey ) ;
sign_tx_input ( funding , 0 , subscript , NULL , input_privkey , inputkey ,
& sig ) ;
tal_free ( subscript ) ;
funding - > input [ 0 ] . script = bitcoin_redeem_p2pkh ( funding , inputkey , & sig ) ;
}