From 43729c6856a632300d1600d20c9d7bb1327ac734 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 24 Apr 2016 20:01:52 +0930 Subject: [PATCH] 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 --- close_tx.c | 16 +++++++--------- close_tx.h | 4 ++-- daemon/packets.c | 17 ++++++++++++++++- daemon/peer.c | 4 ++-- daemon/peer.h | 2 ++ lightning.pb-c.c | 30 +++++++++++++++++++++++++----- lightning.pb-c.h | 6 +++++- lightning.proto | 2 ++ 8 files changed, 61 insertions(+), 20 deletions(-) diff --git a/close_tx.c b/close_tx.c index f20b3c34a..b2bfedfa7 100644 --- a/close_tx.c +++ b/close_tx.c @@ -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); diff --git a/close_tx.h b/close_tx.h index e52349e83..8d7a79a0d 100644 --- a/close_tx.h +++ b/close_tx.h @@ -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, diff --git a/daemon/packets.c b/daemon/packets.c index 302f6cc66..e0f46690a 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -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; } diff --git a/daemon/peer.c b/daemon/peer.c index 99369039f..2c12e1f5b 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -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, diff --git a/daemon/peer.h b/daemon/peer.h index 265ce6acf..2c0b3180b 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -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. */ diff --git a/lightning.pb-c.c b/lightning.pb-c.c index 54f6b4ccf..035ce86c0 100644 --- a/lightning.pb-c.c +++ b/lightning.pb-c.c @@ -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] */ }; diff --git a/lightning.pb-c.h b/lightning.pb-c.h index 58d8b9d3f..2649d0d2b 100644 --- a/lightning.pb-c.h +++ b/lightning.pb-c.h @@ -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 diff --git a/lightning.proto b/lightning.proto index 99c63cae0..fd13dff9c 100644 --- a/lightning.proto +++ b/lightning.proto @@ -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 {