Skip to content

Commit

Permalink
bitcoin/script: support variants where we only have the ripemd.
Browse files Browse the repository at this point in the history
For space saving, we only keep the ripemd160 for old HTLCs.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Aug 20, 2017
1 parent edd27d2 commit af9d763
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
58 changes: 43 additions & 15 deletions bitcoin/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions bitcoin/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct bitcoin_tx_input;
struct preimage;
struct pubkey;
struct sha256;
struct ripemd160;
struct rel_locktime;
struct abs_locktime;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit af9d763

Please sign in to comment.