From a49503402b6bc21e3878e151c07529941d36aed0 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 15 May 2019 00:21:11 -0400 Subject: [PATCH] Make and get the multisig redeemscript and destination in one function instead of two Instead of creating a redeemScript with CreateMultisigRedeemscript and then getting the destination with AddAndGetDestinationForScript, do both in the same function. CreateMultisigRedeemscript is changed to AddAndGetMultisigDestination. It creates the redeemScript and returns it via an output parameter. Then it calls AddAndGetDestinationForScript to add the destination to the keystore and get the proper destination. This allows us to inspect the public keys in the redeemScript before creating the destination so that the correct destination is used when uncompressed pubkeys are in the multisig. --- src/rpc/misc.cpp | 4 ++-- src/rpc/util.cpp | 24 +++++++++++++++------ src/rpc/util.h | 3 ++- src/wallet/rpcwallet.cpp | 4 ++-- test/functional/rpc_createmultisig.py | 30 ++++++++++++++++++++++++++- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 7008a8314..5ecf73421 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -130,9 +130,9 @@ static UniValue createmultisig(const JSONRPCRequest& request) } // Construct using pay-to-script-hash: - const CScript inner = CreateMultisigRedeemscript(required, pubkeys); CBasicKeyStore keystore; - const CTxDestination dest = AddAndGetDestinationForScript(keystore, inner, output_type); + CScript inner; + const CTxDestination dest = AddAndGetMultisigDestination(required, pubkeys, output_type, keystore, inner); UniValue result(UniValue::VOBJ); result.pushKV("address", EncodeDestination(dest)); diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 9cdb22001..4642cf16b 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -150,8 +151,8 @@ CPubKey AddrToPubKey(CKeyStore* const keystore, const std::string& addr_in) return vchPubKey; } -// Creates a multisig redeemscript from a given list of public keys and number required. -CScript CreateMultisigRedeemscript(const int required, const std::vector& pubkeys) +// Creates a multisig address from a given list of public keys, number of signatures required, and the address type +CTxDestination AddAndGetMultisigDestination(const int required, const std::vector& pubkeys, OutputType type, CKeyStore& keystore, CScript& script_out) { // Gather public keys if (required < 1) { @@ -164,13 +165,24 @@ CScript CreateMultisigRedeemscript(const int required, const std::vector 16\nReduce the number"); } - CScript result = GetScriptForMultisig(required, pubkeys); + script_out = GetScriptForMultisig(required, pubkeys); - if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) { - throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE))); + if (script_out.size() > MAX_SCRIPT_ELEMENT_SIZE) { + throw JSONRPCError(RPC_INVALID_PARAMETER, (strprintf("redeemScript exceeds size limit: %d > %d", script_out.size(), MAX_SCRIPT_ELEMENT_SIZE))); } - return result; + // Check if any keys are uncompressed. If so, the type is legacy + for (const CPubKey& pk : pubkeys) { + if (!pk.IsCompressed()) { + type = OutputType::LEGACY; + break; + } + } + + // Make the address + CTxDestination dest = AddAndGetDestinationForScript(keystore, script_out, type); + + return dest; } class DescribeAddressVisitor : public boost::static_visitor diff --git a/src/rpc/util.h b/src/rpc/util.h index e4fa8fc3d..0eb2fef5c 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -6,6 +6,7 @@ #define BITCOIN_RPC_UTIL_H #include +#include #include #include #include