@@ -151,6 +151,27 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
151151 return NullUniValue;
152152}
153153
154+ void ImportScript (const CScript& script)
155+ {
156+ if (::IsMine (*pwalletMain, script) == ISMINE_SPENDABLE)
157+ throw JSONRPCError (RPC_WALLET_ERROR, " The wallet already contains the private key for this address or script" );
158+
159+ pwalletMain->MarkDirty ();
160+
161+ if (!pwalletMain->HaveWatchOnly (script) && !pwalletMain->AddWatchOnly (script))
162+ throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
163+ }
164+
165+ void ImportAddress (const CTxDestination& dest, const std::string& strLabel, const std::string& strPurpose)
166+ {
167+ CScript script = GetScriptForDestination (dest);
168+ ImportScript (script);
169+ // add to address book or update label
170+ if (IsValidDestination (dest)) {
171+ pwalletMain->SetAddressBook (dest, strLabel, strPurpose);
172+ }
173+ }
174+
154175UniValue importaddress (const UniValue& params, bool fHelp )
155176{
156177 if (fHelp || params.size () < 1 || params.size () > 3 )
@@ -173,22 +194,6 @@ UniValue importaddress(const UniValue& params, bool fHelp)
173194 " \n As a JSON-RPC call\n " +
174195 HelpExampleRpc (" importaddress" , " \" myaddress\" , \" testing\" , false" ));
175196
176- LOCK2 (cs_main, pwalletMain->cs_wallet );
177-
178- CScript script;
179-
180- bool isStakingAddress = false ;
181- CTxDestination dest = DecodeDestination (params[0 ].get_str (), isStakingAddress);
182- bool isAddressValid = IsValidDestination (dest);
183- if (isAddressValid) {
184- script = GetScriptForDestination (dest);
185- } else if (IsHex (params[0 ].get_str ())) {
186- std::vector<unsigned char > data (ParseHex (params[0 ].get_str ()));
187- script = CScript (data.begin (), data.end ());
188- } else {
189- throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid PIVX address or script" );
190- }
191-
192197 std::string strLabel = " " ;
193198 if (params.size () > 1 )
194199 strLabel = params[1 ].get_str ();
@@ -198,31 +203,27 @@ UniValue importaddress(const UniValue& params, bool fHelp)
198203 if (params.size () > 2 )
199204 fRescan = params[2 ].get_bool ();
200205
201- {
202- if (::IsMine (*pwalletMain, script) & ISMINE_SPENDABLE_ALL)
203- throw JSONRPCError (RPC_WALLET_ERROR, " The wallet already contains the private key for this address or script" );
204-
205- // add to address book or update label
206- if (isAddressValid) {
207- pwalletMain->SetAddressBook (dest, strLabel,
208- (isStakingAddress ?
209- AddressBook::AddressBookPurpose::COLD_STAKING :
210- AddressBook::AddressBookPurpose::RECEIVE));
211- }
206+ LOCK2 (cs_main, pwalletMain->cs_wallet );
212207
213- // Don't throw error in case an address is already there
214- if (pwalletMain->HaveWatchOnly (script))
215- return NullUniValue;
208+ bool isStakingAddress = false ;
209+ CTxDestination dest = DecodeDestination (params[0 ].get_str (), isStakingAddress);
216210
217- pwalletMain->MarkDirty ();
211+ if (IsValidDestination (dest)) {
212+ ImportAddress (dest, strLabel, isStakingAddress ?
213+ AddressBook::AddressBookPurpose::COLD_STAKING :
214+ AddressBook::AddressBookPurpose::RECEIVE);
218215
219- if (!pwalletMain->AddWatchOnly (script))
220- throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
216+ } else if (IsHex (params[0 ].get_str ())) {
217+ std::vector<unsigned char > data (ParseHex (params[0 ].get_str ()));
218+ ImportScript (CScript (data.begin (), data.end ()));
221219
222- if (fRescan ) {
223- pwalletMain->ScanForWalletTransactions (chainActive.Genesis (), true );
224- pwalletMain->ReacceptWalletTransactions ();
225- }
220+ } else {
221+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid PIVX address or script" );
222+ }
223+
224+ if (fRescan ) {
225+ pwalletMain->ScanForWalletTransactions (chainActive.Genesis (), true );
226+ pwalletMain->ReacceptWalletTransactions ();
226227 }
227228
228229 return NullUniValue;
0 commit comments