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
| 1468 | */ |
| 1469 | /* Master makes sure we only get told preimages once other node is committed. */ |
| 1470 | static void handle_preimage(struct tracked_output **outs, |
| 1471 | const struct preimage *preimage) |
| 1472 | { |
| 1473 | size_t i; |
| 1474 | struct sha256 sha; |
| 1475 | struct ripemd160 ripemd; |
| 1476 | |
| 1477 | sha256(&sha, preimage, sizeof(*preimage)); |
| 1478 | ripemd160(&ripemd, &sha, sizeof(sha)); |
| 1479 | |
| 1480 | for (i = 0; i < tal_count(outs); i++) { |
| 1481 | const u8 *msg; |
| 1482 | |
| 1483 | if (outs[i]->output_type != THEIR_HTLC) |
| 1484 | continue; |
| 1485 | |
| 1486 | if (!ripemd160_eq(&outs[i]->htlc.ripemd, &ripemd)) |
| 1487 | continue; |
| 1488 | |
| 1489 | /* If HTLC has timed out, we will already have |
| 1490 | * proposed a "ignore this, it's their problem". But |
| 1491 | * now try this proposal instead! */ |
| 1492 | if (outs[i]->resolved) { |
| 1493 | if (outs[i]->resolved->tx_type != SELF) { |
| 1494 | status_broken("HTLC already resolved by %s" |
| 1495 | " when we found preimage", |
| 1496 | tx_type_name(outs[i]->resolved->tx_type)); |
| 1497 | return; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | /* stash the payment_hash so we can track this coin movement */ |
| 1502 | outs[i]->payment_hash = sha; |
| 1503 | |
| 1504 | /* Discard any previous resolution. Could be a timeout, |
| 1505 | * could be due to multiple identical rhashes in tx. */ |
| 1506 | outs[i]->proposal = tal_free(outs[i]->proposal); |
| 1507 | |
| 1508 | /* BOLT #5: |
| 1509 | * |
| 1510 | * A local node: |
| 1511 | * - if it receives (or already possesses) a payment preimage |
| 1512 | * for an unresolved HTLC output that it has been offered |
| 1513 | * AND for which it has committed to an outgoing HTLC: |
| 1514 | * - MUST *resolve* the output by spending it, using the |
| 1515 | * HTLC-success transaction. |
| 1516 | */ |
| 1517 | if (outs[i]->remote_htlc_sig) { |
| 1518 | struct amount_sat fee; |
| 1519 | const u8 *htlc_wscript; |
| 1520 | |
| 1521 | /* FIXME: lightningd could derive this itself? */ |
| 1522 | htlc_wscript = bitcoin_wscript_htlc_tx(tmpctx, |
| 1523 | to_self_delay[LOCAL], |
| 1524 | &keyset->self_revocation_key, |
| 1525 | &keyset->self_delayed_payment_key); |
| 1526 | |
| 1527 | fee = get_htlc_success_fee(outs[i]); |
no test coverage detected