mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 03:52:49 +01:00
26fe9b990995f9cb5eee21d40b4daaad19f7181f Add support for descriptors to utxoupdatepsbt (Pieter Wuille) 3135c1a2d2e2fb31bc362c848bd2456d576e408b Abstract out UpdatePSBTOutput from FillPSBT (Pieter Wuille) fb90ec3c33e824f5abb6a68452c683d6ce8b3e4a Abstract out EvalDescriptorStringOrObject from scantxoutset (Pieter Wuille) eaf4f887348a08c620732125ad4430e1a133d434 Abstract out IsSegWitOutput from utxoupdatepsbt (Pieter Wuille) Pull request description: This adds a descriptors argument to the `utxoupdatepsbt` RPC. This means: * Input and output scripts and keys will be filled in when known. * P2SH-witness inputs will be filled in from the UTXO set when a descriptor is provided that shows they're spending segwit outputs. This also moves some (newly) shared code to separate functions: `UpdatePSBTOutput` (an analogue to `SignPSBTInput`), `IsSegWitOutput`, and `EvalDescriptorStringOrObject` (implementing the string or object notation parsing used in `scantxoutset`). ACKs for top commit: jnewbery: utACK 26fe9b990995f9cb5eee21d40b4daaad19f7181f laanwj: utACK 26fe9b990995f9cb5eee21d40b4daaad19f7181f (will hold merging until response to promag's comments) promag: ACK 26fe9b9, checked refactors and tests look comprehensive. Still missing a release note but can be added later. Tree-SHA512: 1d833b7351b59d6c5ded6da399ff371a8a2a6ad04c0a8f90e6e46105dc737fa6f2740b1e5340280d59e01f42896c40b720c042f44417e38dfbee6477b894b245 Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
This commit is contained in:
parent
ef3f738f6f
commit
6df949628f
19
src/psbt.cpp
19
src/psbt.cpp
@ -163,6 +163,25 @@ void PSBTOutput::Merge(const PSBTOutput& output)
|
||||
if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
|
||||
}
|
||||
|
||||
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
|
||||
{
|
||||
const CTxOut& out = psbt.tx->vout.at(index);
|
||||
PSBTOutput& psbt_out = psbt.outputs.at(index);
|
||||
|
||||
// Fill a SignatureData with output info
|
||||
SignatureData sigdata;
|
||||
psbt_out.FillSignatureData(sigdata);
|
||||
|
||||
// Construct a would-be spend of this output, to update sigdata with.
|
||||
// Note that ProduceSignature is used to fill in metadata (not actual signatures),
|
||||
// so provider does not need to provide any private keys (it can be a HidingSigningProvider).
|
||||
MutableTransactionSignatureCreator creator(psbt.tx.get_ptr(), /* index */ 0, out.nValue, SIGHASH_ALL);
|
||||
ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
|
||||
|
||||
// Put redeem_script, key paths, into PSBTOutput.
|
||||
psbt_out.FromSignatureData(sigdata);
|
||||
}
|
||||
|
||||
bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, int index, int sighash)
|
||||
{
|
||||
// if this input has a final scriptsig, don't do anything with it
|
||||
|
@ -485,6 +485,12 @@ struct PartiallySignedTransaction
|
||||
/** Signs a PSBTInput, verifying that all provided data matches what is being signed. */
|
||||
bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, int index, int sighash = SIGHASH_ALL);
|
||||
|
||||
/** Updates a PSBTOutput with information from provider.
|
||||
*
|
||||
* This fills in the redeem_script, and hd_keypaths where possible.
|
||||
*/
|
||||
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index);
|
||||
|
||||
/**
|
||||
* Finalizes a PSBT if possible, combining partial signatures.
|
||||
*
|
||||
|
@ -2532,42 +2532,12 @@ UniValue scantxoutset(const JSONRPCRequest& request)
|
||||
|
||||
// loop through the scan objects
|
||||
for (const UniValue& scanobject : request.params[1].get_array().getValues()) {
|
||||
std::string desc_str;
|
||||
std::pair<int64_t, int64_t> range = {0, 1000};
|
||||
if (scanobject.isStr()) {
|
||||
desc_str = scanobject.get_str();
|
||||
} else if (scanobject.isObject()) {
|
||||
UniValue desc_uni = find_value(scanobject, "desc");
|
||||
if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
|
||||
desc_str = desc_uni.get_str();
|
||||
UniValue range_uni = find_value(scanobject, "range");
|
||||
if (!range_uni.isNull()) {
|
||||
range = ParseDescriptorRange(range_uni);
|
||||
}
|
||||
} else {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
|
||||
}
|
||||
|
||||
FlatSigningProvider provider;
|
||||
std::string error;
|
||||
auto desc = Parse(desc_str, provider, error);
|
||||
if (!desc) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
|
||||
}
|
||||
if (!desc->IsRange()) {
|
||||
range.first = 0;
|
||||
range.second = 0;
|
||||
}
|
||||
for (int i = range.first; i <= range.second; ++i) {
|
||||
std::vector<CScript> scripts;
|
||||
if (!desc->Expand(i, provider, scripts, provider)) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
|
||||
}
|
||||
for (const auto& script : scripts) {
|
||||
std::string inferred = InferDescriptor(script, provider)->ToString();
|
||||
needles.emplace(script);
|
||||
descriptors.emplace(std::move(script), std::move(inferred));
|
||||
}
|
||||
auto scripts = EvalDescriptorStringOrObject(scanobject, provider);
|
||||
for (const auto& script : scripts) {
|
||||
std::string inferred = InferDescriptor(script, provider)->ToString();
|
||||
needles.emplace(script);
|
||||
descriptors.emplace(std::move(script), std::move(inferred));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1421,10 +1421,18 @@ UniValue converttopsbt(const JSONRPCRequest& request)
|
||||
|
||||
UniValue utxoupdatepsbt(const JSONRPCRequest& request)
|
||||
{
|
||||
|
||||
RPCHelpMan{"utxoupdatepsbt",
|
||||
"\nUpdates a PSBT with UTXOs retrieved from the UTXO set or the mempool.\n",
|
||||
"\nUpdates a PSBT with data from output descriptors, UTXOs retrieved from the UTXO set or the mempool.\n",
|
||||
{
|
||||
{"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"}
|
||||
{"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"},
|
||||
{"descriptors", RPCArg::Type::ARR, RPCArg::Optional::OMITTED_NAMED_ARG, "An array of either strings or objects", {
|
||||
{"", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "An output descriptor"},
|
||||
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with an output descriptor and extra information", {
|
||||
{"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
|
||||
{"range", RPCArg::Type::RANGE, "1000", "Up to what index HD chains should be explored (either end or [begin,end])"},
|
||||
}},
|
||||
}},
|
||||
},
|
||||
RPCResult {
|
||||
RPCResult::Type::STR, "", "The base64-encoded partially signed transaction with inputs updated"
|
||||
@ -1432,8 +1440,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
|
||||
RPCExamples {
|
||||
HelpExampleCli("utxoupdatepsbt", "\"psbt\"")
|
||||
}}.Check(request);
|
||||
|
||||
RPCTypeCheck(request.params, {UniValue::VSTR}, true);
|
||||
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VARR}, true);
|
||||
|
||||
// Unserialize the transactions
|
||||
PartiallySignedTransaction psbtx;
|
||||
@ -1442,6 +1449,17 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("TX decode failed %s", error));
|
||||
}
|
||||
|
||||
// Parse descriptors, if any.
|
||||
FlatSigningProvider provider;
|
||||
if (!request.params[1].isNull()) {
|
||||
auto descs = request.params[1].get_array();
|
||||
for (size_t i = 0; i < descs.size(); ++i) {
|
||||
EvalDescriptorStringOrObject(descs[i], provider);
|
||||
}
|
||||
}
|
||||
// We don't actually need private keys further on; hide them as a precaution.
|
||||
HidingSigningProvider public_provider(&provider, /* nosign */ true, /* nobip32derivs */ false);
|
||||
|
||||
// Fetch previous transactions (inputs):
|
||||
CCoinsView viewDummy;
|
||||
CCoinsViewCache view(&viewDummy);
|
||||
@ -1467,10 +1485,15 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
|
||||
continue;
|
||||
}
|
||||
|
||||
const Coin& coin = view.AccessCoin(psbtx.tx->vin[i].prevout);
|
||||
// Update script/keypath information using descriptor data.
|
||||
// Note that SignPSBTInput does a lot more than just constructing ECDSA signatures
|
||||
// we don't actually care about those here, in fact.
|
||||
SignPSBTInput(public_provider, *psbtx.tx, input, i, /* sighash_type */ 1);
|
||||
}
|
||||
|
||||
std::vector<std::vector<unsigned char>> solutions_data;
|
||||
Solver(coin.out.scriptPubKey, solutions_data);
|
||||
// Update script/keypath information using descriptor data.
|
||||
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
|
||||
UpdatePSBTOutput(public_provider, psbtx, i);
|
||||
}
|
||||
|
||||
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <keystore.h>
|
||||
#include <pubkey.h>
|
||||
#include <rpc/util.h>
|
||||
#include <script/descriptor.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/system.h>
|
||||
#include <util/strencodings.h>
|
||||
@ -828,3 +829,41 @@ UniValue GetServicesNames(ServiceFlags services)
|
||||
|
||||
return servicesNames;
|
||||
}
|
||||
|
||||
std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider)
|
||||
{
|
||||
std::string desc_str;
|
||||
std::pair<int64_t, int64_t> range = {0, 1000};
|
||||
if (scanobject.isStr()) {
|
||||
desc_str = scanobject.get_str();
|
||||
} else if (scanobject.isObject()) {
|
||||
UniValue desc_uni = find_value(scanobject, "desc");
|
||||
if (desc_uni.isNull()) throw JSONRPCError(RPC_INVALID_PARAMETER, "Descriptor needs to be provided in scan object");
|
||||
desc_str = desc_uni.get_str();
|
||||
UniValue range_uni = find_value(scanobject, "range");
|
||||
if (!range_uni.isNull()) {
|
||||
range = ParseDescriptorRange(range_uni);
|
||||
}
|
||||
} else {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
|
||||
}
|
||||
|
||||
std::string error;
|
||||
auto desc = Parse(desc_str, provider, error);
|
||||
if (!desc) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
|
||||
}
|
||||
if (!desc->IsRange()) {
|
||||
range.first = 0;
|
||||
range.second = 0;
|
||||
}
|
||||
std::vector<CScript> ret;
|
||||
for (int i = range.first; i <= range.second; ++i) {
|
||||
std::vector<CScript> scripts;
|
||||
if (!desc->Expand(i, provider, scripts, provider)) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));
|
||||
}
|
||||
std::move(scripts.begin(), scripts.end(), std::back_inserter(ret));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -11,6 +11,8 @@
|
||||
#include <pubkey.h>
|
||||
#include <rpc/protocol.h>
|
||||
#include <rpc/request.h>
|
||||
#include <script/script.h>
|
||||
#include <script/sign.h>
|
||||
#include <script/standard.h>
|
||||
#include <univalue.h>
|
||||
#include <util/check.h>
|
||||
@ -104,6 +106,8 @@ enum class OuterType {
|
||||
OBJ,
|
||||
NONE, // Only set on first recursion
|
||||
};
|
||||
/** Evaluate a descriptor given as a string, or as a {"desc":...,"range":...} object, with default range of 1000. */
|
||||
std::vector<CScript> EvalDescriptorStringOrObject(const UniValue& scanobject, FlatSigningProvider& provider);
|
||||
|
||||
struct RPCArg {
|
||||
enum class Type {
|
||||
|
@ -32,16 +32,7 @@ TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& ps
|
||||
|
||||
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
|
||||
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
|
||||
const CTxOut& out = psbtx.tx->vout.at(i);
|
||||
PSBTOutput& psbt_out = psbtx.outputs.at(i);
|
||||
|
||||
// Fill a SignatureData with output info
|
||||
SignatureData sigdata;
|
||||
psbt_out.FillSignatureData(sigdata);
|
||||
|
||||
MutableTransactionSignatureCreator creator(psbtx.tx.get_ptr(), 0, out.nValue, 1);
|
||||
ProduceSignature(HidingSigningProvider(pwallet, true, !bip32derivs), creator, out.scriptPubKey, sigdata);
|
||||
psbt_out.FromSignatureData(sigdata);
|
||||
UpdatePSBTOutput(HidingSigningProvider(pwallet, true, !bip32derivs), psbtx, i);
|
||||
}
|
||||
|
||||
return TransactionError::OK;
|
||||
|
@ -219,18 +219,31 @@ class PSBTTest(BitcoinTestFramework):
|
||||
vout3 = find_output(self.nodes[0], txid3, 11)
|
||||
self.sync_all()
|
||||
|
||||
# Update a PSBT with UTXOs from the node
|
||||
# Inputs should not be filled because they are non-witness
|
||||
def test_psbt_input_keys(psbt_input, keys):
|
||||
"""Check that the psbt input has only the expected keys."""
|
||||
assert_equal(set(keys), set(psbt_input.keys()))
|
||||
|
||||
# Create a PSBT. None of the inputs are filled initially
|
||||
psbt = self.nodes[1].createpsbt([{"txid":txid1, "vout":vout1},{"txid":txid2, "vout":vout2},{"txid":txid3, "vout":vout3}], {self.nodes[0].getnewaddress():32.999})
|
||||
decoded = self.nodes[1].decodepsbt(psbt)
|
||||
assert "witness_utxo" not in decoded['inputs'][0] and "non_witness_utxo" not in decoded['inputs'][0]
|
||||
assert "witness_utxo" not in decoded['inputs'][1] and "non_witness_utxo" not in decoded['inputs'][1]
|
||||
assert "witness_utxo" not in decoded['inputs'][2] and "non_witness_utxo" not in decoded['inputs'][2]
|
||||
test_psbt_input_keys(decoded['inputs'][0], [])
|
||||
test_psbt_input_keys(decoded['inputs'][1], [])
|
||||
test_psbt_input_keys(decoded['inputs'][2], [])
|
||||
|
||||
# Update a PSBT with UTXOs from the node
|
||||
# No inputs should be filled because they are non-witness
|
||||
updated = self.nodes[1].utxoupdatepsbt(psbt)
|
||||
decoded = self.nodes[1].decodepsbt(updated)
|
||||
assert "witness_utxo" not in decoded['inputs'][0] and "non_witness_utxo" not in decoded['inputs'][0]
|
||||
assert "witness_utxo" not in decoded['inputs'][1] and "non_witness_utxo" not in decoded['inputs'][1]
|
||||
assert "witness_utxo" not in decoded['inputs'][2] and "non_witness_utxo" not in decoded['inputs'][2]
|
||||
test_psbt_input_keys(decoded['inputs'][1], [])
|
||||
test_psbt_input_keys(decoded['inputs'][2], [])
|
||||
|
||||
# Try again, now while providing descriptors
|
||||
descs = [self.nodes[1].getaddressinfo(addr)['desc'] for addr in [addr1,addr2,addr3]]
|
||||
updated = self.nodes[1].utxoupdatepsbt(psbt, descs)
|
||||
decoded = self.nodes[1].decodepsbt(updated)
|
||||
test_psbt_input_keys(decoded['inputs'][0], [])
|
||||
test_psbt_input_keys(decoded['inputs'][1], [])
|
||||
test_psbt_input_keys(decoded['inputs'][2], [])
|
||||
|
||||
# Two PSBTs with a common input should not be joinable
|
||||
psbt1 = self.nodes[1].createpsbt([{"txid":txid1, "vout":vout1}], {self.nodes[0].getnewaddress():Decimal('10.999')})
|
||||
|
Loading…
Reference in New Issue
Block a user