From af9d763763aaced1f45564bc3ed1f3affcbf7bae Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 18 Aug 2017 14:13:53 +0930 Subject: [PATCH] bitcoin/script: support variants where we only have the ripemd. For space saving, we only keep the ripemd160 for old HTLCs. Signed-off-by: Rusty Russell --- bitcoin/script.c | 58 +++++++++++++++++++++++++++++++++++------------- bitcoin/script.h | 14 ++++++++++++ 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/bitcoin/script.c b/bitcoin/script.c index e93141316..3c0e2f6e6 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -708,11 +708,11 @@ u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx, * OP_ENDIF * OP_ENDIF */ -u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, - const struct pubkey *localkey, - const struct pubkey *remotekey, - const struct sha256 *payment_hash, - const struct pubkey *revocationkey) +u8 *bitcoin_wscript_htlc_offer_ripemd160(const tal_t *ctx, + const struct pubkey *localkey, + const struct pubkey *remotekey, + const struct ripemd160 *payment_ripemd, + const struct pubkey *revocationkey) { u8 *script = tal_arr(ctx, u8, 0); struct ripemd160 ripemd; @@ -739,8 +739,8 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, add_op(&script, OP_CHECKMULTISIG); add_op(&script, OP_ELSE); add_op(&script, OP_HASH160); - ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u)); - add_push_bytes(&script, ripemd.u.u8, sizeof(ripemd.u.u8)); + add_push_bytes(&script, + payment_ripemd->u.u8, sizeof(payment_ripemd->u.u8)); add_op(&script, OP_EQUALVERIFY); add_op(&script, OP_CHECKSIG); add_op(&script, OP_ENDIF); @@ -749,6 +749,19 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, return script; } +u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, + const struct pubkey *localkey, + const struct pubkey *remotekey, + const struct sha256 *payment_hash, + const struct pubkey *revocationkey) +{ + struct ripemd160 ripemd; + + ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u)); + return bitcoin_wscript_htlc_offer_ripemd160(ctx, localkey, remotekey, + &ripemd, revocationkey); +} + /* BOLT #3: * * #### Received HTLC Outputs @@ -775,12 +788,12 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, * OP_ENDIF * OP_ENDIF */ -u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx, - const struct abs_locktime *htlc_abstimeout, - const struct pubkey *localkey, - const struct pubkey *remotekey, - const struct sha256 *payment_hash, - const struct pubkey *revocationkey) +u8 *bitcoin_wscript_htlc_receive_ripemd(const tal_t *ctx, + const struct abs_locktime *htlc_abstimeout, + const struct pubkey *localkey, + const struct pubkey *remotekey, + const struct ripemd160 *payment_ripemd, + const struct pubkey *revocationkey) { u8 *script = tal_arr(ctx, u8, 0); struct ripemd160 ripemd; @@ -800,8 +813,8 @@ u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx, add_op(&script, OP_EQUAL); add_op(&script, OP_IF); add_op(&script, OP_HASH160); - ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u)); - add_push_bytes(&script, ripemd.u.u8, sizeof(ripemd.u.u8)); + add_push_bytes(&script, + payment_ripemd->u.u8, sizeof(payment_ripemd->u.u8)); add_op(&script, OP_EQUALVERIFY); add_number(&script, 2); add_op(&script, OP_SWAP); @@ -820,6 +833,21 @@ u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx, return script; } +u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx, + const struct abs_locktime *htlc_abstimeout, + const struct pubkey *localkey, + const struct pubkey *remotekey, + const struct sha256 *payment_hash, + const struct pubkey *revocationkey) +{ + struct ripemd160 ripemd; + + ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u)); + return bitcoin_wscript_htlc_receive_ripemd(ctx, htlc_abstimeout, + localkey, remotekey, + &ripemd, revocationkey); +} + /* BOLT #3: * * ## HTLC-Timeout and HTLC-Success Transactions diff --git a/bitcoin/script.h b/bitcoin/script.h index 378389685..42b2eebbc 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -11,6 +11,7 @@ struct bitcoin_tx_input; struct preimage; struct pubkey; struct sha256; +struct ripemd160; struct rel_locktime; struct abs_locktime; @@ -141,6 +142,19 @@ u8 **bitcoin_htlc_receive_spend_preimage(const tal_t *ctx, const struct preimage *preimage, const u8 *wscript); +/* Underlying functions for penalties, where we only keep ripemd160 */ +u8 *bitcoin_wscript_htlc_offer_ripemd160(const tal_t *ctx, + const struct pubkey *localkey, + const struct pubkey *remotekey, + const struct ripemd160 *payment_ripemd, + const struct pubkey *revocationkey); +u8 *bitcoin_wscript_htlc_receive_ripemd(const tal_t *ctx, + const struct abs_locktime *htlc_abstimeout, + const struct pubkey *localkey, + const struct pubkey *remotekey, + const struct ripemd160 *payment_ripemd, + const struct pubkey *revocationkey); + /* BOLT #3 HTLC-success/HTLC-timeout output */ u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx, u16 to_self_delay,