Browse Source

bitcoin/script: update scripts to the lightningnetwork/lightning-rfc#123 version

aka "BOLT 3: Use revocation key hash rather than revocation key",
which builds on top of lightningnetwork/lightning-rfc#105 "BOLT 2,3,5:
Make htlc outputs of the commitment tx spendable with revocation key".

This affects callers, since they now need to hand us the revocation
pubkey, but commit_tx has that already anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
29d6004efc
  1. 55
      bitcoin/script.c
  2. 6
      bitcoin/script.h
  3. 25
      lightningd/commit_tx.c
  4. 8
      lightningd/htlc_tx.c
  5. 4
      lightningd/htlc_tx.h
  6. 4
      lightningd/opening/opening.c
  7. 10
      lightningd/test/run-commit_tx.c

55
bitcoin/script.c

@ -705,12 +705,18 @@ u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx,
/* BOLT #3: /* BOLT #3:
* *
* This output sends funds to a HTLC-timeout transaction after the * #### Offered HTLC Outputs
* HTLC timeout, or to the remote peer on successful payment preimage.
* The output is a P2WSH, with a witness script:
* *
* <remotekey> OP_SWAP * This output sends funds to a HTLC-timeout transaction after the HTLC
* OP_SIZE 32 OP_EQUAL * timeout, or to the remote peer using the payment preimage or the revocation
* key. The output is a P2WSH, with a witness script:
*
* # To you with revocation key
* OP_DUP OP_HASH160 <revocationkey-hash> OP_EQUAL
* OP_IF
* OP_CHECKSIG
* OP_ELSE
* <remotekey> OP_SWAP OP_SIZE 32 OP_EQUAL
* OP_NOTIF * OP_NOTIF
* # To me via HTLC-timeout transaction (timelocked). * # To me via HTLC-timeout transaction (timelocked).
* OP_DROP 2 OP_SWAP <localkey> 2 OP_CHECKMULTISIG * OP_DROP 2 OP_SWAP <localkey> 2 OP_CHECKMULTISIG
@ -719,15 +725,25 @@ u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx,
* OP_HASH160 <ripemd-of-payment-hash> OP_EQUALVERIFY * OP_HASH160 <ripemd-of-payment-hash> OP_EQUALVERIFY
* OP_CHECKSIG * OP_CHECKSIG
* OP_ENDIF * OP_ENDIF
* OP_ENDIF
*/ */
u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
const struct pubkey *localkey, const struct pubkey *localkey,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const struct sha256 *payment_hash) const struct sha256 *payment_hash,
const struct pubkey *revocationkey)
{ {
u8 *script = tal_arr(ctx, u8, 0); u8 *script = tal_arr(ctx, u8, 0);
struct ripemd160 ripemd; struct ripemd160 ripemd;
add_op(&script, OP_DUP);
add_op(&script, OP_HASH160);
hash160_key(&ripemd, revocationkey);
add_push_bytes(&script, &ripemd, sizeof(ripemd));
add_op(&script, OP_EQUAL);
add_op(&script, OP_IF);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ELSE);
add_push_key(&script, remotekey); add_push_key(&script, remotekey);
add_op(&script, OP_SWAP); add_op(&script, OP_SWAP);
add_op(&script, OP_SIZE); add_op(&script, OP_SIZE);
@ -747,16 +763,24 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
add_op(&script, OP_EQUALVERIFY); add_op(&script, OP_EQUALVERIFY);
add_op(&script, OP_CHECKSIG); add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ENDIF); add_op(&script, OP_ENDIF);
add_op(&script, OP_ENDIF);
return script; return script;
} }
/* BOLT #3: /* BOLT #3:
* *
* This output sends funds to the remote peer after the HTLC timeout, * #### Received HTLC Outputs
* or to an HTLC-success transaction with a successful payment *
* preimage. The output is a P2WSH, with a witness script: * This output sends funds to the remote peer after the HTLC timeout or using
* the revocation key, or to an HTLC-success transaction with a successful
* payment preimage. The output is a P2WSH, with a witness script:
* *
* # To you with revocation key
* OP_DUP OP_HASH160 <revocationkey-hash> OP_EQUAL
* OP_IF
* OP_CHECKSIG
* OP_ELSE
* <remotekey> OP_SWAP * <remotekey> OP_SWAP
* OP_SIZE 32 OP_EQUAL * OP_SIZE 32 OP_EQUAL
* OP_IF * OP_IF
@ -768,16 +792,26 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
* OP_DROP <locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP * OP_DROP <locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP
* OP_CHECKSIG * OP_CHECKSIG
* OP_ENDIF * OP_ENDIF
* OP_ENDIF
*/ */
u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx, u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx,
const struct abs_locktime *htlc_abstimeout, const struct abs_locktime *htlc_abstimeout,
const struct pubkey *localkey, const struct pubkey *localkey,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const struct sha256 *payment_hash) const struct sha256 *payment_hash,
const struct pubkey *revocationkey)
{ {
u8 *script = tal_arr(ctx, u8, 0); u8 *script = tal_arr(ctx, u8, 0);
struct ripemd160 ripemd; struct ripemd160 ripemd;
add_op(&script, OP_DUP);
add_op(&script, OP_HASH160);
hash160_key(&ripemd, revocationkey);
add_push_bytes(&script, &ripemd, sizeof(ripemd));
add_op(&script, OP_EQUAL);
add_op(&script, OP_IF);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ELSE);
add_push_key(&script, remotekey); add_push_key(&script, remotekey);
add_op(&script, OP_SWAP); add_op(&script, OP_SWAP);
add_op(&script, OP_SIZE); add_op(&script, OP_SIZE);
@ -800,6 +834,7 @@ u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx,
add_op(&script, OP_DROP); add_op(&script, OP_DROP);
add_op(&script, OP_CHECKSIG); add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ENDIF); add_op(&script, OP_ENDIF);
add_op(&script, OP_ENDIF);
return script; return script;
} }

6
bitcoin/script.h

@ -123,7 +123,8 @@ u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx,
u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
const struct pubkey *localkey, const struct pubkey *localkey,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const struct sha256 *payment_hash); const struct sha256 *payment_hash,
const struct pubkey *revocationkey);
u8 **bitcoin_htlc_offer_spend_timeout(const tal_t *ctx, u8 **bitcoin_htlc_offer_spend_timeout(const tal_t *ctx,
const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *localsig,
const secp256k1_ecdsa_signature *remotesig, const secp256k1_ecdsa_signature *remotesig,
@ -132,7 +133,8 @@ u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx,
const struct abs_locktime *htlc_abstimeout, const struct abs_locktime *htlc_abstimeout,
const struct pubkey *localkey, const struct pubkey *localkey,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const struct sha256 *payment_hash); const struct sha256 *payment_hash,
const struct pubkey *revocationkey);
u8 **bitcoin_htlc_receive_spend_preimage(const tal_t *ctx, u8 **bitcoin_htlc_receive_spend_preimage(const tal_t *ctx,
const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *localsig,
const secp256k1_ecdsa_signature *remotesig, const secp256k1_ecdsa_signature *remotesig,

25
lightningd/commit_tx.c

@ -55,10 +55,10 @@ u64 htlc_timeout_fee(u64 feerate_per_kw)
* *
* The fee for an HTLC-timeout transaction MUST BE calculated to match: * The fee for an HTLC-timeout transaction MUST BE calculated to match:
* *
* 1. Multiply `feerate-per-kw` by 635 and divide by 1000 (rounding * 1. Multiply `feerate-per-kw` by 663 and divide by 1000 (rounding
* down). * down).
*/ */
return feerate_per_kw * 635 / 1000; return feerate_per_kw * 663 / 1000;
} }
u64 htlc_success_fee(u64 feerate_per_kw) u64 htlc_success_fee(u64 feerate_per_kw)
@ -67,10 +67,10 @@ u64 htlc_success_fee(u64 feerate_per_kw)
* *
* The fee for an HTLC-success transaction MUST BE calculated to match: * The fee for an HTLC-success transaction MUST BE calculated to match:
* *
* 1. Multiply `feerate-per-kw` by 673 and divide by 1000 (rounding * 1. Multiply `feerate-per-kw` by 703 and divide by 1000 (rounding
* down). * down).
*/ */
return feerate_per_kw * 673 / 1000; return feerate_per_kw * 703 / 1000;
} }
static bool trim(const struct htlc *htlc, static bool trim(const struct htlc *htlc,
@ -147,11 +147,13 @@ u64 commit_tx_base_fee(u64 feerate_per_kw, size_t num_untrimmed_htlcs)
static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n, static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
const struct htlc *htlc, const struct htlc *htlc,
const struct pubkey *selfkey, const struct pubkey *selfkey,
const struct pubkey *otherkey) const struct pubkey *otherkey,
const struct pubkey *revocationkey)
{ {
u8 *wscript = bitcoin_wscript_htlc_offer(tx, u8 *wscript = bitcoin_wscript_htlc_offer(tx,
selfkey, otherkey, selfkey, otherkey,
&htlc->rhash); &htlc->rhash,
revocationkey);
tx->output[n].amount = htlc->msatoshi / 1000; tx->output[n].amount = htlc->msatoshi / 1000;
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n", SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n",
@ -162,12 +164,13 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n, static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
const struct htlc *htlc, const struct htlc *htlc,
const struct pubkey *selfkey, const struct pubkey *selfkey,
const struct pubkey *otherkey) const struct pubkey *otherkey,
const struct pubkey *revocationkey)
{ {
u8 *wscript = bitcoin_wscript_htlc_receive(tx, u8 *wscript = bitcoin_wscript_htlc_receive(tx,
&htlc->expiry, &htlc->expiry,
selfkey, otherkey, selfkey, otherkey,
&htlc->rhash); &htlc->rhash, revocationkey);
tx->output[n].amount = htlc->msatoshi / 1000; tx->output[n].amount = htlc->msatoshi / 1000;
tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript); tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript);
SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n", SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n",
@ -250,7 +253,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
continue; continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side)) if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
continue; continue;
add_offered_htlc_out(tx, n, htlcs[i], selfkey, otherkey); add_offered_htlc_out(tx, n, htlcs[i], selfkey, otherkey,
revocation_pubkey);
if (htlcmap) if (htlcmap)
(*htlcmap)[n++] = htlcs[i]; (*htlcmap)[n++] = htlcs[i];
} }
@ -265,7 +269,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
continue; continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side)) if (trim(htlcs[i], feerate_per_kw, dust_limit_satoshis, side))
continue; continue;
add_received_htlc_out(tx, n, htlcs[i],selfkey, otherkey); add_received_htlc_out(tx, n, htlcs[i],selfkey, otherkey,
revocation_pubkey);
if (htlcmap) if (htlcmap)
(*htlcmap)[n++] = htlcs[i]; (*htlcmap)[n++] = htlcs[i];
} }

8
lightningd/htlc_tx.c

@ -95,7 +95,8 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *localsig,
const secp256k1_ecdsa_signature *remotesig, const secp256k1_ecdsa_signature *remotesig,
const struct preimage *payment_preimage) const struct preimage *payment_preimage,
const struct pubkey *revocationkey)
{ {
struct sha256 hash; struct sha256 hash;
u8 *wscript; u8 *wscript;
@ -104,7 +105,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
wscript = bitcoin_wscript_htlc_receive(htlc_success, wscript = bitcoin_wscript_htlc_receive(htlc_success,
htlc_abstimeout, htlc_abstimeout,
localkey, remotekey, localkey, remotekey,
&hash); &hash, revocationkey);
htlc_success->input[0].witness htlc_success->input[0].witness
= bitcoin_htlc_receive_spend_preimage(htlc_success->input, = bitcoin_htlc_receive_spend_preimage(htlc_success->input,
@ -137,12 +138,13 @@ void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout,
const struct pubkey *localkey, const struct pubkey *localkey,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
const struct pubkey *revocationkey,
const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *localsig,
const secp256k1_ecdsa_signature *remotesig) const secp256k1_ecdsa_signature *remotesig)
{ {
u8 *wscript = bitcoin_wscript_htlc_offer(htlc_timeout, u8 *wscript = bitcoin_wscript_htlc_offer(htlc_timeout,
localkey, remotekey, localkey, remotekey,
payment_hash); payment_hash, revocationkey);
htlc_timeout->input[0].witness htlc_timeout->input[0].witness
= bitcoin_htlc_offer_spend_timeout(htlc_timeout->input, = bitcoin_htlc_offer_spend_timeout(htlc_timeout->input,

4
lightningd/htlc_tx.h

@ -25,7 +25,8 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *localsig,
const secp256k1_ecdsa_signature *remotesig, const secp256k1_ecdsa_signature *remotesig,
const struct preimage *payment_preimage); const struct preimage *payment_preimage,
const struct pubkey *revocationkey);
/* Create HTLC-timeout tx to spend an offered HTLC commitment tx /* Create HTLC-timeout tx to spend an offered HTLC commitment tx
* output; doesn't fill in input witness. */ * output; doesn't fill in input witness. */
@ -43,6 +44,7 @@ void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout,
const struct pubkey *localkey, const struct pubkey *localkey,
const struct pubkey *remotekey, const struct pubkey *remotekey,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
const struct pubkey *revocationkey,
const secp256k1_ecdsa_signature *localsig, const secp256k1_ecdsa_signature *localsig,
const secp256k1_ecdsa_signature *remotesig); const secp256k1_ecdsa_signature *remotesig);

4
lightningd/opening/opening.c

@ -136,9 +136,9 @@ static void check_config_bounds(struct state *state,
/* BOLT #2: /* BOLT #2:
* *
* It MUST fail the channel if `max-accepted-htlcs` is greater * It MUST fail the channel if `max-accepted-htlcs` is greater
* than 511. * than 483.
*/ */
if (remoteconf->max_accepted_htlcs > 511) if (remoteconf->max_accepted_htlcs > 483)
peer_failed(PEER_FD, &state->cs, NULL, peer_failed(PEER_FD, &state->cs, NULL,
WIRE_OPENING_PEER_BAD_CONFIG, WIRE_OPENING_PEER_BAD_CONFIG,
"max_accepted_htlcs %u too large", "max_accepted_htlcs %u too large",

10
lightningd/test/run-commit_tx.c

@ -227,7 +227,8 @@ static void report_htlcs(const struct bitcoin_tx *tx,
wscript[i] = bitcoin_wscript_htlc_offer(tmpctx, wscript[i] = bitcoin_wscript_htlc_offer(tmpctx,
localkey, localkey,
remotekey, remotekey,
&htlc->rhash); &htlc->rhash,
local_revocation_key);
} else { } else {
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i, htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
htlc, to_self_delay, htlc, to_self_delay,
@ -238,7 +239,8 @@ static void report_htlcs(const struct bitcoin_tx *tx,
&htlc->expiry, &htlc->expiry,
localkey, localkey,
remotekey, remotekey,
&htlc->rhash); &htlc->rhash,
local_revocation_key);
} }
sign_tx_input(htlc_tx[i], 0, sign_tx_input(htlc_tx[i], 0,
NULL, NULL,
@ -271,13 +273,15 @@ static void report_htlcs(const struct bitcoin_tx *tx,
htlc_timeout_tx_add_witness(htlc_tx[i], htlc_timeout_tx_add_witness(htlc_tx[i],
localkey, remotekey, localkey, remotekey,
&htlc->rhash, &htlc->rhash,
local_revocation_key,
&localsig, &remotesig[i]); &localsig, &remotesig[i]);
} else { } else {
htlc_success_tx_add_witness(htlc_tx[i], htlc_success_tx_add_witness(htlc_tx[i],
&htlc->expiry, &htlc->expiry,
localkey, remotekey, localkey, remotekey,
&localsig, &remotesig[i], &localsig, &remotesig[i],
htlc->r); htlc->r,
local_revocation_key);
} }
printf("output htlc_%s_tx %"PRIu64": %s\n", printf("output htlc_%s_tx %"PRIu64": %s\n",
htlc_owner(htlc) == LOCAL ? "timeout" : "success", htlc_owner(htlc) == LOCAL ? "timeout" : "success",

Loading…
Cancel
Save