@@ -87,6 +87,15 @@ static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver,
87
87
}
88
88
}
89
89
90
+ static LegacyScriptPubKeyMan& GetLegacyScriptPubKeyMan (CWallet& wallet)
91
+ {
92
+ LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan ();
93
+ if (!spk_man) {
94
+ throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
95
+ }
96
+ return *spk_man;
97
+ }
98
+
90
99
UniValue importprivkey (const JSONRPCRequest& request)
91
100
{
92
101
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest (request);
@@ -125,10 +134,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
125
134
throw JSONRPCError (RPC_WALLET_ERROR, " Cannot import private keys to a wallet with private keys disabled" );
126
135
}
127
136
128
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
129
- if (!spk_man) {
130
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
131
- }
137
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*wallet);
132
138
133
139
WalletRescanReserver reserver (pwallet);
134
140
bool fRescan = true ;
@@ -256,10 +262,7 @@ UniValue importaddress(const JSONRPCRequest& request)
256
262
},
257
263
}.Check (request);
258
264
259
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
260
- if (!spk_man) {
261
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
262
- }
265
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*pwallet);
263
266
264
267
std::string strLabel;
265
268
if (!request.params [1 ].isNull ())
@@ -462,10 +465,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
462
465
},
463
466
}.Check (request);
464
467
465
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
466
- if (!spk_man) {
467
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
468
- }
468
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*wallet);
469
469
470
470
std::string strLabel;
471
471
if (!request.params [1 ].isNull ())
@@ -549,10 +549,7 @@ UniValue importwallet(const JSONRPCRequest& request)
549
549
},
550
550
}.Check (request);
551
551
552
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
553
- if (!spk_man) {
554
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
555
- }
552
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*wallet);
556
553
557
554
if (pwallet->chain ().havePruned ()) {
558
555
// Exit early and print an error.
@@ -711,10 +708,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
711
708
},
712
709
}.Check (request);
713
710
714
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
715
- if (!spk_man) {
716
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
717
- }
711
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*wallet);
718
712
719
713
auto locked_chain = pwallet->chain ().lock ();
720
714
LOCK (pwallet->cs_wallet );
@@ -726,12 +720,12 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
726
720
if (!IsValidDestination (dest)) {
727
721
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address" );
728
722
}
729
- auto keyid = GetKeyForDestination (* spk_man, dest);
723
+ auto keyid = GetKeyForDestination (spk_man, dest);
730
724
if (keyid.IsNull ()) {
731
725
throw JSONRPCError (RPC_TYPE_ERROR, " Address does not refer to a key" );
732
726
}
733
727
CKey vchSecret;
734
- if (!spk_man-> GetKey (keyid, vchSecret)) {
728
+ if (!spk_man. GetKey (keyid, vchSecret)) {
735
729
throw JSONRPCError (RPC_WALLET_ERROR, " Private key for address " + strAddress + " is not known" );
736
730
}
737
731
return EncodeSecret (vchSecret);
@@ -765,14 +759,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
765
759
},
766
760
}.Check (request);
767
761
768
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
769
- if (!spk_man) {
770
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
771
- }
762
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*wallet);
772
763
773
764
auto locked_chain = pwallet->chain ().lock ();
774
765
LOCK (pwallet->cs_wallet );
775
- AssertLockHeld (spk_man-> cs_wallet );
766
+ AssertLockHeld (spk_man. cs_wallet );
776
767
777
768
EnsureWalletIsUnlocked (pwallet);
778
769
@@ -794,10 +785,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
794
785
throw JSONRPCError (RPC_INVALID_PARAMETER, " Cannot open wallet dump file" );
795
786
796
787
std::map<CKeyID, int64_t > mapKeyBirth;
797
- const std::map<CKeyID, int64_t >& mapKeyPool = spk_man-> GetAllReserveKeys ();
788
+ const std::map<CKeyID, int64_t >& mapKeyPool = spk_man. GetAllReserveKeys ();
798
789
pwallet->GetKeyBirthTimes (*locked_chain, mapKeyBirth);
799
790
800
- std::set<CScriptID> scripts = spk_man-> GetCScripts ();
791
+ std::set<CScriptID> scripts = spk_man. GetCScripts ();
801
792
802
793
// sort time/key pairs
803
794
std::vector<std::pair<int64_t , CKeyID> > vKeyBirth;
@@ -816,11 +807,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
816
807
file << " \n " ;
817
808
818
809
// add the base58check encoded extended master if the wallet uses HD
819
- CKeyID seed_id = spk_man-> GetHDChain ().seed_id ;
810
+ CKeyID seed_id = spk_man. GetHDChain ().seed_id ;
820
811
if (!seed_id.IsNull ())
821
812
{
822
813
CKey seed;
823
- if (spk_man-> GetKey (seed_id, seed)) {
814
+ if (spk_man. GetKey (seed_id, seed)) {
824
815
CExtKey masterKey;
825
816
masterKey.SetSeed (seed.begin (), seed.size ());
826
817
@@ -833,20 +824,20 @@ UniValue dumpwallet(const JSONRPCRequest& request)
833
824
std::string strAddr;
834
825
std::string strLabel;
835
826
CKey key;
836
- if (spk_man-> GetKey (keyid, key)) {
827
+ if (spk_man. GetKey (keyid, key)) {
837
828
file << strprintf (" %s %s " , EncodeSecret (key), strTime);
838
- if (GetWalletAddressesForKey (spk_man, pwallet, keyid, strAddr, strLabel)) {
829
+ if (GetWalletAddressesForKey (& spk_man, pwallet, keyid, strAddr, strLabel)) {
839
830
file << strprintf (" label=%s" , strLabel);
840
831
} else if (keyid == seed_id) {
841
832
file << " hdseed=1" ;
842
833
} else if (mapKeyPool.count (keyid)) {
843
834
file << " reserve=1" ;
844
- } else if (spk_man-> mapKeyMetadata [keyid].hdKeypath == " s" ) {
835
+ } else if (spk_man. mapKeyMetadata [keyid].hdKeypath == " s" ) {
845
836
file << " inactivehdseed=1" ;
846
837
} else {
847
838
file << " change=1" ;
848
839
}
849
- file << strprintf (" # addr=%s%s\n " , strAddr, (spk_man-> mapKeyMetadata [keyid].has_key_origin ? " hdkeypath=" +WriteHDKeypath (spk_man-> mapKeyMetadata [keyid].key_origin .path ) : " " ));
840
+ file << strprintf (" # addr=%s%s\n " , strAddr, (spk_man. mapKeyMetadata [keyid].has_key_origin ? " hdkeypath=" +WriteHDKeypath (spk_man. mapKeyMetadata [keyid].key_origin .path ) : " " ));
850
841
}
851
842
}
852
843
file << " \n " ;
@@ -855,11 +846,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
855
846
std::string create_time = " 0" ;
856
847
std::string address = EncodeDestination (ScriptHash (scriptid));
857
848
// get birth times for scripts with metadata
858
- auto it = spk_man-> m_script_metadata .find (scriptid);
859
- if (it != spk_man-> m_script_metadata .end ()) {
849
+ auto it = spk_man. m_script_metadata .find (scriptid);
850
+ if (it != spk_man. m_script_metadata .end ()) {
860
851
create_time = FormatISO8601DateTime (it->second .nCreateTime );
861
852
}
862
- if (spk_man-> GetCScript (scriptid, script)) {
853
+ if (spk_man. GetCScript (scriptid, script)) {
863
854
file << strprintf (" %s %s script=1" , HexStr (script.begin (), script.end ()), create_time);
864
855
file << strprintf (" # addr=%s\n " , address);
865
856
}
@@ -1355,10 +1346,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
1355
1346
1356
1347
RPCTypeCheck (mainRequest.params , {UniValue::VARR, UniValue::VOBJ});
1357
1348
1358
- LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan ();
1359
- if (!spk_man) {
1360
- throw JSONRPCError (RPC_WALLET_ERROR, " This type of wallet does not support this command" );
1361
- }
1349
+ LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan (*wallet);
1362
1350
1363
1351
const UniValue& requests = mainRequest.params [0 ];
1364
1352
0 commit comments