| 803 | } |
| 804 | |
| 805 | static struct command_result *json_signpsbt(struct command *cmd, |
| 806 | const char *buffer, |
| 807 | const jsmntok_t *obj UNNEEDED, |
| 808 | const jsmntok_t *params) |
| 809 | { |
| 810 | struct command_result *res; |
| 811 | struct json_stream *response; |
| 812 | struct wally_psbt *psbt, *signed_psbt; |
| 813 | struct utxo **utxos; |
| 814 | const struct hsm_utxo **hsm_utxos; |
| 815 | u32 *input_nums; |
| 816 | u32 psbt_version; |
| 817 | |
| 818 | if (!param_check(cmd, buffer, params, |
| 819 | p_req("psbt", param_psbt, &psbt), |
| 820 | p_opt("signonly", param_input_numbers, &input_nums), |
| 821 | NULL)) |
| 822 | return command_param_failed(); |
| 823 | |
| 824 | /* We internally deal with v2 only but we want to return V2 if given */ |
| 825 | psbt_version = psbt->version; |
| 826 | if (!psbt_set_version(psbt, 2)) { |
| 827 | return command_fail(cmd, LIGHTNINGD, |
| 828 | "Could not set PSBT version: %s", |
| 829 | fmt_wally_psbt(tmpctx, psbt)); |
| 830 | } |
| 831 | |
| 832 | /* Sanity check! */ |
| 833 | for (size_t i = 0; i < tal_count(input_nums); i++) { |
| 834 | if (input_nums[i] >= psbt->num_inputs) |
| 835 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 836 | "signonly[%zu]: %u out of range", |
| 837 | i, input_nums[i]); |
| 838 | } |
| 839 | |
| 840 | /* We have to find/locate the utxos that are ours on this PSBT, |
| 841 | * so that the HSM knows how/what to sign for (it's possible some of |
| 842 | * our utxos require more complicated data to sign for e.g. |
| 843 | * closeinfo outputs */ |
| 844 | res = match_psbt_inputs_to_utxos(cmd, psbt, input_nums, &utxos); |
| 845 | if (res) |
| 846 | return res; |
| 847 | |
| 848 | if (tal_count(utxos) == 0) |
| 849 | return command_fail(cmd, LIGHTNINGD, |
| 850 | "No wallet inputs to sign. Are you sure you" |
| 851 | " added inputs to this PSBT? If not, then" |
| 852 | " there is no need to sign it."); |
| 853 | |
| 854 | if (command_check_only(cmd)) |
| 855 | return command_check_done(cmd); |
| 856 | |
| 857 | /* Update the keypaths on any outputs that are in our wallet (change addresses). */ |
| 858 | if (!match_psbt_outputs_to_wallet(psbt, cmd->ld->wallet)) |
| 859 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 860 | "Could not add keypaths to PSBT?"); |
| 861 | |
| 862 | /* FIXME: hsm will sign almost anything, but it should really |
nothing calls this directly
no test coverage detected