BOLT #5: * * A local node: * - if it receives (or already possesses) a payment preimage for an unresolved * HTLC output that it has been offered AND for which it has committed to an * outgoing HTLC: * - MUST *resolve* the output by spending it, using the HTLC-success * transaction. * - MUST NOT reveal its own preimage when it's not the final recipient... * - MUST res
| 1984 | */ |
| 1985 | /* Master makes sure we only get told preimages once other node is committed. */ |
| 1986 | static void handle_preimage(struct tracked_output **outs, |
| 1987 | const struct preimage *preimage) |
| 1988 | { |
| 1989 | size_t i; |
| 1990 | struct sha256 sha; |
| 1991 | struct ripemd160 ripemd; |
| 1992 | u8 **witness; |
| 1993 | |
| 1994 | sha256(&sha, preimage, sizeof(*preimage)); |
| 1995 | ripemd160(&ripemd, &sha, sizeof(sha)); |
| 1996 | |
| 1997 | for (i = 0; i < tal_count(outs); i++) { |
| 1998 | struct bitcoin_tx *tx; |
| 1999 | struct bitcoin_signature sig; |
| 2000 | |
| 2001 | if (outs[i]->output_type != THEIR_HTLC) |
| 2002 | continue; |
| 2003 | |
| 2004 | if (!ripemd160_eq(&outs[i]->htlc.ripemd, &ripemd)) |
| 2005 | continue; |
| 2006 | |
| 2007 | /* Too late? */ |
| 2008 | if (outs[i]->resolved) { |
| 2009 | status_broken("HTLC already resolved by %s" |
| 2010 | " when we found preimage", |
| 2011 | tx_type_name(outs[i]->resolved->tx_type)); |
| 2012 | return; |
| 2013 | } |
| 2014 | |
| 2015 | /* stash the payment_hash so we can track this coin movement */ |
| 2016 | outs[i]->payment_hash = sha; |
| 2017 | |
| 2018 | /* Discard any previous resolution. Could be a timeout, |
| 2019 | * could be due to multiple identical rhashes in tx. */ |
| 2020 | outs[i]->proposal = tal_free(outs[i]->proposal); |
| 2021 | |
| 2022 | /* BOLT #5: |
| 2023 | * |
| 2024 | * |
| 2025 | * ## HTLC Output Handling: Local Commitment, Remote Offers |
| 2026 | *... |
| 2027 | * A local node: |
| 2028 | * - if it receives (or already possesses) a payment preimage |
| 2029 | * for an unresolved HTLC output that it has been offered |
| 2030 | * AND for which it has committed to an outgoing HTLC: |
| 2031 | * - MUST *resolve* the output by spending it, using the |
| 2032 | * HTLC-success transaction. |
| 2033 | */ |
| 2034 | if (outs[i]->remote_htlc_sig) { |
| 2035 | struct amount_msat htlc_amount; |
| 2036 | if (!amount_sat_to_msat(&htlc_amount, outs[i]->sat)) |
| 2037 | status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 2038 | "Overflow in output %zu %s", |
| 2039 | i, |
| 2040 | type_to_string(tmpctx, |
| 2041 | struct amount_sat, |
| 2042 | &outs[i]->sat)); |
| 2043 | tx = htlc_success_tx(outs[i], chainparams, |
no test coverage detected