From 3e1ae177fb1a71108c850be14d801f056ce9d6ff Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Feb 2017 12:14:22 +1030 Subject: [PATCH] bitcoin/script: BOLT 3 htlc transaction support. Signed-off-by: Rusty Russell --- bitcoin/script.c | 35 +++++++++++++++++++++++++++++++++++ bitcoin/script.h | 8 +++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/bitcoin/script.c b/bitcoin/script.c index 63daca606..ae2c5b732 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -814,6 +814,41 @@ u8 **bitcoin_htlc_receive_spend_preimage(const tal_t *ctx, return witness; } +u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx, + u16 to_self_delay, + const struct pubkey *revocation_pubkey, + const struct pubkey *local_delayedkey) +{ + u8 *script = tal_arr(ctx, u8, 0); + + /* BOLT #3: + * + * The witness script for the output is: + * + * OP_IF + * # Penalty transaction + * + * OP_ELSE + * `to-self-delay` + * OP_CSV + * OP_DROP + * + * OP_ENDIF + * OP_CHECKSIG + */ + add_op(&script, OP_IF); + add_push_key(&script, revocation_pubkey); + add_op(&script, OP_ELSE); + add_number(&script, to_self_delay); + add_op(&script, OP_CHECKSEQUENCEVERIFY); + add_op(&script, OP_DROP); + add_push_key(&script, local_delayedkey); + add_op(&script, OP_ENDIF); + add_op(&script, OP_CHECKSIG); + + return script; +} + bool scripteq(const tal_t *s1, const tal_t *s2) { memcheck(s1, tal_len(s1)); diff --git a/bitcoin/script.h b/bitcoin/script.h index e1e412bbb..5dafe5098 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -129,7 +129,13 @@ u8 **bitcoin_htlc_receive_spend_preimage(const tal_t *ctx, const struct preimage *preimage, const u8 *wscript); -/* Is this a pay to pubkeu hash? */ +/* BOLT #3 HTLC-success/HTLC-timeout output */ +u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx, + u16 to_self_delay, + const struct pubkey *revocation_pubkey, + const struct pubkey *local_delayedkey); + +/* Is this a pay to pubkey hash? */ bool is_p2pkh(const u8 *script); /* Is this a pay to script hash? */