| 2014 | } |
| 2015 | |
| 2016 | static RPCHelpMan finalizepsbt() |
| 2017 | { |
| 2018 | return RPCHelpMan{"finalizepsbt", |
| 2019 | "Finalize the inputs of a PSBT. If the transaction is fully signed, it will produce a\n" |
| 2020 | "network serialized transaction which can be broadcast with sendrawtransaction. Otherwise a PSBT will be\n" |
| 2021 | "created which has the final_scriptSig and final_scriptWitness fields filled for inputs that are complete.\n" |
| 2022 | "Implements the Finalizer and Extractor roles.\n", |
| 2023 | { |
| 2024 | {"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"}, |
| 2025 | {"extract", RPCArg::Type::BOOL, RPCArg::Default{true}, "If true and the transaction is complete,\n" |
| 2026 | " extract and return the complete transaction in normal network serialization instead of the PSBT."}, |
| 2027 | }, |
| 2028 | RPCResult{ |
| 2029 | RPCResult::Type::OBJ, "", "", |
| 2030 | { |
| 2031 | {RPCResult::Type::STR, "psbt", /*optional=*/true, "The base64-encoded partially signed transaction if not extracted"}, |
| 2032 | {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "The hex-encoded network transaction if extracted"}, |
| 2033 | {RPCResult::Type::BOOL, "complete", "If the transaction has a complete set of signatures"}, |
| 2034 | } |
| 2035 | }, |
| 2036 | RPCExamples{ |
| 2037 | HelpExampleCli("finalizepsbt", "\"psbt\"") |
| 2038 | }, |
| 2039 | [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 2040 | { |
| 2041 | if (!g_con_elementsmode) |
| 2042 | throw std::runtime_error("PSBT operations are disabled when not in elementsmode.\n"); |
| 2043 | |
| 2044 | RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VBOOL}, true); |
| 2045 | |
| 2046 | // Unserialize the transactions |
| 2047 | PartiallySignedTransaction psbtx; |
| 2048 | std::string error; |
| 2049 | if (!DecodeBase64PSBT(psbtx, request.params[0].get_str(), error)) { |
| 2050 | throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error)); |
| 2051 | } |
| 2052 | |
| 2053 | bool extract = request.params[1].isNull() || (!request.params[1].isNull() && request.params[1].get_bool()); |
| 2054 | |
| 2055 | CMutableTransaction mtx; |
| 2056 | bool complete = FinalizeAndExtractPSBT(psbtx, mtx); |
| 2057 | |
| 2058 | UniValue result(UniValue::VOBJ); |
| 2059 | CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); |
| 2060 | std::string result_str; |
| 2061 | |
| 2062 | if (complete && extract) { |
| 2063 | ssTx << mtx; |
| 2064 | result_str = HexStr(ssTx); |
| 2065 | result.pushKV("hex", result_str); |
| 2066 | } else { |
| 2067 | ssTx << psbtx; |
| 2068 | result_str = EncodeBase64(ssTx.str()); |
| 2069 | result.pushKV("psbt", result_str); |
| 2070 | } |
| 2071 | result.pushKV("complete", complete); |
| 2072 | return result; |
| 2073 | }, |
nothing calls this directly
no test coverage detected