core-lightning/close_tx.c
Rusty Russell 58b14292ad bitcoin/tx: (optional) input amount.
We need this for signing segwitness txs.  Unfortunately, we don't have it
for transactions we received as hex, only ones we created; to make this safe
we use a pointer which is NULL if we don't know, and those will crash if
we try to sign or check their sigs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:43:53 +09:30

46 lines
1.4 KiB
C

#include "bitcoin/pubkey.h"
#include "bitcoin/script.h"
#include "bitcoin/shadouble.h"
#include "bitcoin/tx.h"
#include "close_tx.h"
#include "permute_tx.h"
#include "protobuf_convert.h"
struct bitcoin_tx *create_close_tx(secp256k1_context *secpctx,
const tal_t *ctx,
const struct pubkey *our_final,
const struct pubkey *their_final,
const struct sha256_double *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them)
{
struct bitcoin_tx *tx;
const u8 *redeemscript;
/* Now create close tx: one input, two outputs. */
tx = bitcoin_tx(ctx, 1, 2);
/* Our input spends the anchor tx output. */
tx->input[0].txid = *anchor_txid;
tx->input[0].index = anchor_index;
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
/* One output is to us. */
tx->output[0].amount = to_us;
redeemscript = bitcoin_redeem_single(tx, our_final);
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);
tx->output[0].script_length = tal_count(tx->output[0].script);
/* Other output is to them. */
tx->output[1].amount = to_them;
redeemscript = bitcoin_redeem_single(tx, their_final);
tx->output[1].script = scriptpubkey_p2sh(tx, redeemscript);
tx->output[1].script_length = tal_count(tx->output[1].script);
assert(tx->output[0].amount + tx->output[1].amount <= anchor_satoshis);
permute_outputs(tx->output, 2, NULL);
return tx;
}