protocol: add output script to close_clearing message.

We just use a p2sh to a single address for the moment, but that's simply for
non-segwit wallets; we'll pay to whatever the other side specifies.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-04-24 20:01:52 +09:30
parent f24b73124a
commit 43729c6856
8 changed files with 61 additions and 20 deletions

View File

@ -1,6 +1,4 @@
#include "bitcoin/pubkey.h"
#include "bitcoin/script.h"
#include "bitcoin/shadouble.h"
#include "bitcoin/tx.h"
#include "close_tx.h"
#include "permute_tx.h"
@ -8,15 +6,14 @@
struct bitcoin_tx *create_close_tx(secp256k1_context *secpctx,
const tal_t *ctx,
const struct pubkey *our_final,
const struct pubkey *their_final,
const u8 *our_script,
const u8 *their_script,
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);
@ -28,14 +25,15 @@ struct bitcoin_tx *create_close_tx(secp256k1_context *secpctx,
/* 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 = tal_dup_arr(tx, u8,
our_script, tal_count(our_script), 0);
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 = tal_dup_arr(tx, u8,
their_script, tal_count(their_script),
0);
tx->output[1].script_length = tal_count(tx->output[1].script);
assert(tx->output[0].amount + tx->output[1].amount <= anchor_satoshis);

View File

@ -12,8 +12,8 @@ struct pubkey;
* input scriptsig. */
struct bitcoin_tx *create_close_tx(secp256k1_context *secpctx,
const tal_t *ctx,
const struct pubkey *our_final,
const struct pubkey *their_final,
const u8 *our_script,
const u8 *their_script,
const struct sha256_double *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,

View File

@ -388,9 +388,18 @@ void queue_pkt_err(struct peer *peer, Pkt *err)
void queue_pkt_close_clearing(struct peer *peer)
{
u8 *redeemscript;
CloseClearing *c = tal(peer, CloseClearing);
close_clearing__init(c);
redeemscript = bitcoin_redeem_single(c, &peer->us.finalkey);
peer->closing.our_script = scriptpubkey_p2sh(peer, redeemscript);
c->scriptpubkey.data = tal_dup_arr(c, u8,
peer->closing.our_script,
tal_count(peer->closing.our_script),
0);
c->scriptpubkey.len = tal_count(c->scriptpubkey.data);
queue_pkt(peer, PKT__PKT_CLOSE_CLEARING, c);
}
@ -748,7 +757,13 @@ Pkt *accept_pkt_revocation(struct peer *peer, const Pkt *pkt)
Pkt *accept_pkt_close_clearing(struct peer *peer, const Pkt *pkt)
{
/* FIXME: Reject unknown odd fields? */
const CloseClearing *c = pkt->close_clearing;
/* FIXME: Filter for non-standardness? */
peer->closing.their_script = tal_dup_arr(peer, u8,
c->scriptpubkey.data,
c->scriptpubkey.len, 0);
return NULL;
}

View File

@ -948,8 +948,8 @@ struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee)
cstate.b.pay_msat / 1000);
return create_close_tx(peer->dstate->secpctx, peer,
&peer->us.finalkey,
&peer->them.finalkey,
peer->closing.our_script,
peer->closing.their_script,
&peer->anchor.txid,
peer->anchor.index,
peer->anchor.satoshis,

View File

@ -170,6 +170,8 @@ struct peer {
struct bitcoin_signature *their_sig;
/* If their_sig is non-NULL, this is the fee. */
u64 their_fee;
/* scriptPubKey we/they want for closing. */
u8 *our_script, *their_script;
} closing;
/* If not INPUT_NONE, send this when we have no more HTLCs. */

View File

@ -2017,9 +2017,29 @@ const ProtobufCMessageDescriptor update_revocation__descriptor =
(ProtobufCMessageInit) update_revocation__init,
NULL,NULL,NULL /* reserved[123] */
};
#define close_clearing__field_descriptors NULL
#define close_clearing__field_indices_by_name NULL
#define close_clearing__number_ranges NULL
static const ProtobufCFieldDescriptor close_clearing__field_descriptors[1] =
{
{
"scriptPubkey",
1,
PROTOBUF_C_LABEL_REQUIRED,
PROTOBUF_C_TYPE_BYTES,
0, /* quantifier_offset */
offsetof(CloseClearing, scriptpubkey),
NULL,
NULL,
0, /* flags */
0,NULL,NULL /* reserved1,reserved2, etc */
},
};
static const unsigned close_clearing__field_indices_by_name[] = {
0, /* field[0] = scriptPubkey */
};
static const ProtobufCIntRange close_clearing__number_ranges[1 + 1] =
{
{ 1, 0 },
{ 0, 1 }
};
const ProtobufCMessageDescriptor close_clearing__descriptor =
{
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
@ -2028,10 +2048,10 @@ const ProtobufCMessageDescriptor close_clearing__descriptor =
"CloseClearing",
"",
sizeof(CloseClearing),
0,
1,
close_clearing__field_descriptors,
close_clearing__field_indices_by_name,
0, close_clearing__number_ranges,
1, close_clearing__number_ranges,
(ProtobufCMessageInit) close_clearing__init,
NULL,NULL,NULL /* reserved[123] */
};

View File

@ -417,10 +417,14 @@ struct _UpdateRevocation
struct _CloseClearing
{
ProtobufCMessage base;
/*
* Output script for mutual close tx.
*/
ProtobufCBinaryData scriptpubkey;
};
#define CLOSE_CLEARING__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&close_clearing__descriptor) \
}
, {0,NULL} }
struct _CloseSignature

View File

@ -173,6 +173,8 @@ message update_revocation {
// Start clearing out the channel HTLCs so we can close it
message close_clearing {
// Output script for mutual close tx.
required bytes scriptPubkey = 1;
}
message close_signature {