Skip to content

Commit 4a52099

Browse files
Merge dashpay#6066: feat: support descriptor wallets for RPC protx updateregistar
c9a600e fix: linkage error - message signer better to be common code rather than libconsensus (Konstantin Akimov) 8299b3b feat: protxregistar implementation for descriptor wallets (Konstantin Akimov) 6f45432 refactor: removed unused SignSpecialTxPayloadByString (Konstantin Akimov) Pull request description: ## Issue being fixed or feature implemented RPC `protx updateregistar` uses forcely LegacyScriptPubKeyMan instead using CWallet's interface. It causes a failures such as ``` test_framework.authproxy.JSONRPCException: This type of wallet does not support this command (-4) ``` ## What was done? New method `SignSpecialTxPayloadByHash` is implemented in interface instead exporting raw private key for some address. See dashpay/dash-issues#59 to track progress ## How Has This Been Tested? Functional test `feature_dip3_deterministicmns.py` to run by both ways - legacy and descriptor wallets. Run unit and functional tests. Extra test done locally: ```diff --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -242,10 +242,10 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass): if self.options.descriptors is None: # Prefer BDB unless it isn't available - if self.is_bdb_compiled(): - self.options.descriptors = False - elif self.is_sqlite_compiled(): + if self.is_sqlite_compiled(): self.options.descriptors = True + elif self.is_bdb_compiled(): + self.options.descriptors = False ``` to flip flag descriptor wallets/legacy wallets for all functional tests. ## Breaking Changes N/A ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone ACKs for top commit: PastaPastaPasta: utACK c9a600e Tree-SHA512: 00aea1cd9db3537b9a9dcdee096d47ea48337edeea3f52ad54aea91781678b8641ac2dd86b67f61f87e3912945bcb5361a42a3279b6c08bb8d9f096bed8fe842
2 parents fc11cd8 + c9a600e commit 4a52099

File tree

7 files changed

+59
-25
lines changed

7 files changed

+59
-25
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ libbitcoin_server_a_SOURCES = \
480480
masternode/payments.cpp \
481481
masternode/sync.cpp \
482482
masternode/utils.cpp \
483-
messagesigner.cpp \
484483
miner.cpp \
485484
net.cpp \
486485
netfulfilledman.cpp \
@@ -770,6 +769,7 @@ libbitcoin_util_a_SOURCES = \
770769
fs.cpp \
771770
interfaces/handler.cpp \
772771
logging.cpp \
772+
messagesigner.cpp \
773773
random.cpp \
774774
randomenv.cpp \
775775
rpc/request.cpp \

src/rpc/evo.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,25 +294,13 @@ static void UpdateSpecialTxInputsHash(const CMutableTransaction& tx, SpecialTxPa
294294
}
295295

296296
template<typename SpecialTxPayload>
297-
static void SignSpecialTxPayloadByHash(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKey& key)
297+
static void SignSpecialTxPayloadByHash(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKeyID& keyID, const CWallet& wallet)
298298
{
299299
UpdateSpecialTxInputsHash(tx, payload);
300300
payload.vchSig.clear();
301301

302-
uint256 hash = ::SerializeHash(payload);
303-
if (!CHashSigner::SignHash(hash, key, payload.vchSig)) {
304-
throw JSONRPCError(RPC_INTERNAL_ERROR, "failed to sign special tx");
305-
}
306-
}
307-
308-
template<typename SpecialTxPayload>
309-
static void SignSpecialTxPayloadByString(const CMutableTransaction& tx, SpecialTxPayload& payload, const CKey& key)
310-
{
311-
UpdateSpecialTxInputsHash(tx, payload);
312-
payload.vchSig.clear();
313-
314-
std::string m = payload.MakeSignString();
315-
if (!CMessageSigner::SignMessage(m, payload.vchSig, key)) {
302+
const uint256 hash = ::SerializeHash(payload);
303+
if (!wallet.SignSpecialTxPayload(hash, keyID, payload.vchSig)) {
316304
throw JSONRPCError(RPC_INTERNAL_ERROR, "failed to sign special tx");
317305
}
318306
}
@@ -1111,14 +1099,12 @@ static UniValue protx_update_registrar_wrapper(const JSONRPCRequest& request, CC
11111099
ptx.scriptPayout = GetScriptForDestination(payoutDest);
11121100
}
11131101

1114-
LegacyScriptPubKeyMan* spk_man = wallet->GetLegacyScriptPubKeyMan();
1115-
if (!spk_man) {
1116-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
1117-
}
1118-
1119-
CKey keyOwner;
1120-
if (!spk_man->GetKey(dmn->pdmnState->keyIDOwner, keyOwner)) {
1121-
throw std::runtime_error(strprintf("Private key for owner address %s not found in your wallet", EncodeDestination(PKHash(dmn->pdmnState->keyIDOwner))));
1102+
{
1103+
const auto pkhash{PKHash(dmn->pdmnState->keyIDOwner)};
1104+
LOCK(wallet->cs_wallet);
1105+
if (wallet->IsMine(GetScriptForDestination(pkhash)) != isminetype::ISMINE_SPENDABLE) {
1106+
throw std::runtime_error(strprintf("Private key for owner address %s not found in your wallet", EncodeDestination(pkhash)));
1107+
}
11221108
}
11231109

11241110
CMutableTransaction tx;
@@ -1136,7 +1122,7 @@ static UniValue protx_update_registrar_wrapper(const JSONRPCRequest& request, CC
11361122
}
11371123

11381124
FundSpecialTx(wallet.get(), tx, ptx, feeSourceDest);
1139-
SignSpecialTxPayloadByHash(tx, ptx, keyOwner);
1125+
SignSpecialTxPayloadByHash(tx, ptx, dmn->pdmnState->keyIDOwner, *wallet);
11401126
SetTxPayload(tx, ptx);
11411127

11421128
return SignAndSendSpecialTx(request, chain_helper, chainman, tx);

src/wallet/scriptpubkeyman.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <key_io.h>
66
#include <chainparams.h>
77
#include <logging.h>
8+
#include <messagesigner.h>
89
#include <script/descriptor.h>
910
#include <script/sign.h>
1011
#include <shutdown.h>
@@ -726,6 +727,16 @@ SigningResult LegacyScriptPubKeyMan::SignMessage(const std::string& message, con
726727
return SigningResult::SIGNING_FAILED;
727728
}
728729

730+
bool LegacyScriptPubKeyMan::SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const
731+
{
732+
CKey key;
733+
if (!GetKey(keyid, key)) {
734+
return false;
735+
}
736+
737+
return CHashSigner::SignHash(hash, key, vchSig);
738+
}
739+
729740
TransactionError LegacyScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, int sighash_type, bool sign, bool bip32derivs, int* n_signed) const
730741
{
731742
if (n_signed) {
@@ -2196,6 +2207,21 @@ SigningResult DescriptorScriptPubKeyMan::SignMessage(const std::string& message,
21962207
return SigningResult::OK;
21972208
}
21982209

2210+
bool DescriptorScriptPubKeyMan::SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const
2211+
{
2212+
std::unique_ptr<FlatSigningProvider> keys = GetSigningProvider(GetScriptForDestination(PKHash(keyid)), true);
2213+
if (!keys) {
2214+
return false;
2215+
}
2216+
2217+
CKey key;
2218+
if (!keys->GetKey(keyid, key)) {
2219+
return false;
2220+
}
2221+
2222+
return CHashSigner::SignHash(hash, key, vchSig);
2223+
}
2224+
21992225
TransactionError DescriptorScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbtx, int sighash_type, bool sign, bool bip32derivs, int* n_signed) const
22002226
{
22012227
if (n_signed) {

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ class ScriptPubKeyMan
211211
virtual bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const { return false; }
212212
/** Sign a message with the given script */
213213
virtual SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const { return SigningResult::SIGNING_FAILED; };
214+
virtual bool SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const { return false; }
214215
/** Adds script and derivation path information to a PSBT, and optionally signs it. */
215216
virtual TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr) const { return TransactionError::INVALID_PSBT; }
216217

@@ -358,6 +359,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
358359

359360
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const override;
360361
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
362+
bool SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const override;
361363
TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr) const override;
362364

363365
uint256 GetID() const override;
@@ -588,6 +590,7 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
588590

589591
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const override;
590592
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
593+
bool SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const override;
591594
TransactionError FillPSBT(PartiallySignedTransaction& psbt, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr) const override;
592595

593596
uint256 GetID() const override;

src/wallet/wallet.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,19 @@ SigningResult CWallet::SignMessage(const std::string& message, const PKHash& pkh
30703070
return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
30713071
}
30723072

3073+
bool CWallet::SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const
3074+
{
3075+
SignatureData sigdata;
3076+
CScript script_pub_key = GetScriptForDestination(PKHash(keyid));
3077+
for (const auto& spk_man_pair : m_spk_managers) {
3078+
if (spk_man_pair.second->CanProvide(script_pub_key, sigdata)) {
3079+
LOCK(cs_wallet); // DescriptorScriptPubKeyMan calls IsLocked which can lock cs_wallet in a deadlocking order
3080+
return spk_man_pair.second->SignSpecialTxPayload(hash, keyid, vchSig);
3081+
}
3082+
}
3083+
return false;
3084+
}
3085+
30733086
bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl coinControl)
30743087
{
30753088
std::vector<CRecipient> vecSend;

src/wallet/wallet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
11071107
/** Sign the tx given the input coins and sighash. */
11081108
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const;
11091109
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const;
1110+
/** Sign the payload of special transaction.
1111+
* Because wallet is not aware about special transactions entity,
1112+
* but it should work for any its type, we pass there directly a hash of payload.
1113+
*/
1114+
bool SignSpecialTxPayload(const uint256& hash, const CKeyID& keyid, std::vector<unsigned char>& vchSig) const;
11101115

11111116
/**
11121117
* Fills out a PSBT with information from the wallet. Fills in UTXOs if we have

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
# Scripts that are run by default.
9494
# Longest test should go first, to favor running tests in parallel
9595
'feature_dip3_deterministicmns.py --legacy-wallet', # NOTE: needs dash_hash to pass
96+
'feature_dip3_deterministicmns.py --descriptors', # NOTE: needs dash_hash to pass
9697
'feature_llmq_data_recovery.py',
9798
'wallet_hd.py --legacy-wallet',
9899
'wallet_hd.py --descriptors',

0 commit comments

Comments
 (0)