By policy we don't trust our subdaemons (much): before we store a * closing transaction as the channel's last tx and broadcast it, check it * really is a well-formed close of this channel. It must spend exactly our * funding outpoint, be shaped like a close rather than a commitment, and * every output must go to a known shutdown script. */
| 334 | * funding outpoint, be shaped like a close rather than a commitment, and |
| 335 | * every output must go to a known shutdown script. */ |
| 336 | const char *close_tx_check(const tal_t *ctx, |
| 337 | const struct channel *channel, |
| 338 | const struct bitcoin_tx *tx) |
| 339 | { |
| 340 | bool local_matched = false, remote_matched = false; |
| 341 | |
| 342 | if (tx->wtx->num_inputs != 1) |
| 343 | return tal_fmt(ctx, "expected 1 input, got %zu", |
| 344 | tx->wtx->num_inputs); |
| 345 | |
| 346 | if (!wally_tx_input_spends(&tx->wtx->inputs[0], &channel->funding)) |
| 347 | return tal_fmt(ctx, "does not spend funding outpoint %s", |
| 348 | fmt_bitcoin_outpoint(ctx, &channel->funding)); |
| 349 | |
| 350 | /* A closing transaction has a standard nLockTime/nSequence (locktime |
| 351 | * 0 or the negotiated value, sequence 0xFFFFFFF[DF]). Anything shaped |
| 352 | * like the commitment transactions we hand out - upper locktime byte |
| 353 | * 0x20 and upper sequence byte 0x80 - is not a close, no |
| 354 | * matter how its outputs happen to look. */ |
| 355 | if ((tx->wtx->locktime >> 24) == 0x20 |
| 356 | && (tx->wtx->inputs[0].sequence >> 24) == 0x80) |
| 357 | return tal_fmt(ctx, "is not shaped like a closing transaction"); |
| 358 | |
| 359 | for (size_t i = 0; i < tx->wtx->num_outputs; i++) { |
| 360 | const struct wally_tx_output *out = &tx->wtx->outputs[i]; |
| 361 | /* Elements has an explicit fee output with no script. */ |
| 362 | if (out->script_len == 0) { |
| 363 | if (chainparams->is_elements) |
| 364 | continue; |
| 365 | return tal_fmt(ctx, "output %zu has no script", i); |
| 366 | } |
| 367 | const u8 *script = tal_dup_arr(ctx, u8, |
| 368 | out->script, out->script_len, 0); |
| 369 | /* Only let each side's output match once. */ |
| 370 | if (scripteq(script, channel->shutdown_scriptpubkey[LOCAL]) |
| 371 | && !local_matched) { |
| 372 | local_matched = true; |
| 373 | continue; |
| 374 | } |
| 375 | if (scripteq(script, channel->shutdown_scriptpubkey[REMOTE]) |
| 376 | && !remote_matched) { |
| 377 | remote_matched = true; |
| 378 | continue; |
| 379 | } |
| 380 | return tal_fmt(ctx, |
| 381 | "output %zu goes to unknown script %s", |
| 382 | i, tal_hex(ctx, script)); |
| 383 | } |
| 384 | return NULL; |
| 385 | } |
| 386 | |
| 387 | static void peer_received_closing_signature(struct channel *channel, |
| 388 | const u8 *msg) |
no test coverage detected