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