Find our inputs by the pubkey associated with the inputs, and * add a partial sig for each */
| 553 | /* Find our inputs by the pubkey associated with the inputs, and |
| 554 | * add a partial sig for each */ |
| 555 | static void sign_our_inputs(struct hsm_utxo **utxos, struct wally_psbt *psbt) |
| 556 | { |
| 557 | bool is_cache_enabled = false; |
| 558 | for (size_t i = 0; i < tal_count(utxos); i++) { |
| 559 | struct hsm_utxo *utxo = utxos[i]; |
| 560 | for (size_t j = 0; j < psbt->num_inputs; j++) { |
| 561 | struct privkey privkey; |
| 562 | struct pubkey pubkey; |
| 563 | bool needed_sig; |
| 564 | |
| 565 | if (!wally_psbt_input_spends(&psbt->inputs[j], |
| 566 | &utxo->outpoint)) |
| 567 | continue; |
| 568 | |
| 569 | hsm_key_for_utxo(&privkey, &pubkey, utxo); |
| 570 | |
| 571 | /* This line is basically the entire reason we have |
| 572 | * to iterate through to match the psbt input |
| 573 | * to the UTXO -- otherwise we would just |
| 574 | * call wally_psbt_sign for every utxo privkey |
| 575 | * and be done with it. We can't do that though |
| 576 | * because any UTXO that's derived from channel_info |
| 577 | * requires the HSM to find the pubkey, and we |
| 578 | * skip doing that until now as a bit of a reduction |
| 579 | * of complexity in the calling code */ |
| 580 | const size_t script_len = tal_bytelen(utxo->scriptPubkey); |
| 581 | psbt_input_add_pubkey(psbt, j, &pubkey, |
| 582 | is_p2tr(utxo->scriptPubkey, script_len, NULL)); |
| 583 | |
| 584 | /* It's actually a P2WSH in this case. */ |
| 585 | if (utxo->close_info && utxo->close_info->option_anchors) { |
| 586 | const u8 *wscript |
| 587 | = bitcoin_wscript_to_remote_anchored(tmpctx, |
| 588 | &pubkey, |
| 589 | utxo->close_info->csv); |
| 590 | psbt_input_set_witscript(psbt, j, wscript); |
| 591 | psbt_input_set_wit_utxo(psbt, j, |
| 592 | scriptpubkey_p2wsh(psbt, wscript), |
| 593 | utxo->amount); |
| 594 | } |
| 595 | tal_wally_start(); |
| 596 | if (!is_cache_enabled) { |
| 597 | /* Enable caching signature sub-hashes */ |
| 598 | wally_psbt_signing_cache_enable(psbt, 0); |
| 599 | is_cache_enabled = true; |
| 600 | } |
| 601 | |
| 602 | /* We watch for pre-taproot variable-length sigs */ |
| 603 | needed_sig = (psbt->inputs[j].signatures.num_items == 0); |
| 604 | |
| 605 | if (wally_psbt_sign(psbt, privkey.secret.data, |
| 606 | sizeof(privkey.secret.data), |
| 607 | EC_FLAG_GRIND_R) != WALLY_OK) { |
| 608 | tal_wally_end(psbt); |
| 609 | /* Converting to v0 for log consumption */ |
| 610 | psbt_set_version(psbt, 0); |
| 611 | hsmd_status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 612 | "Received wally_err attempting to " |
no test coverage detected