@@ -146,21 +146,28 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
146
146
return NullUniValue;
147
147
}
148
148
149
- void ImportScript (const CScript& script)
149
+ void ImportAddress (const CBitcoinAddress& address, const string& strLabel);
150
+ void ImportScript (const CScript& script, const string& strLabel, bool isRedeemScript)
150
151
{
151
- if (::IsMine (*pwalletMain, script) == ISMINE_SPENDABLE)
152
+ if (!isRedeemScript && ::IsMine (*pwalletMain, script) == ISMINE_SPENDABLE)
152
153
throw JSONRPCError (RPC_WALLET_ERROR, " The wallet already contains the private key for this address or script" );
153
154
154
155
pwalletMain->MarkDirty ();
155
156
156
157
if (!pwalletMain->HaveWatchOnly (script) && !pwalletMain->AddWatchOnly (script))
157
158
throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
159
+
160
+ if (isRedeemScript) {
161
+ if (!pwalletMain->HaveCScript (script) && !pwalletMain->AddCScript (script))
162
+ throw JSONRPCError (RPC_WALLET_ERROR, " Error adding p2sh redeemScript to wallet" );
163
+ ImportAddress (CBitcoinAddress (CScriptID (script)), strLabel);
164
+ }
158
165
}
159
166
160
167
void ImportAddress (const CBitcoinAddress& address, const string& strLabel)
161
168
{
162
169
CScript script = GetScriptForDestination (address.Get ());
163
- ImportScript (script, false );
170
+ ImportScript (script, strLabel, false );
164
171
// add to address book or update label
165
172
if (address.IsValid ())
166
173
pwalletMain->SetAddressBook (address.Get (), strLabel, " receive" );
@@ -171,14 +178,15 @@ UniValue importaddress(const UniValue& params, bool fHelp)
171
178
if (!EnsureWalletIsAvailable (fHelp ))
172
179
return NullUniValue;
173
180
174
- if (fHelp || params.size () < 1 || params.size () > 3 )
181
+ if (fHelp || params.size () < 1 || params.size () > 4 )
175
182
throw runtime_error (
176
- " importaddress \" address\" ( \" label\" rescan )\n "
183
+ " importaddress \" address\" ( \" label\" rescan p2sh )\n "
177
184
" \n Adds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend.\n "
178
185
" \n Arguments:\n "
179
186
" 1. \" address\" (string, required) The address\n "
180
187
" 2. \" label\" (string, optional, default=\"\" ) An optional label\n "
181
188
" 3. rescan (boolean, optional, default=true) Rescan the wallet for transactions\n "
189
+ " 4. p2sh (boolean, optional, default=false) Add the P2SH version of the script as well\n "
182
190
" \n Note: This call can take minutes to complete if rescan is true.\n "
183
191
" \n Examples:\n "
184
192
" \n Import an address with rescan\n "
@@ -201,15 +209,21 @@ UniValue importaddress(const UniValue& params, bool fHelp)
201
209
if (params.size () > 2 )
202
210
fRescan = params[2 ].get_bool ();
203
211
212
+ // Whether to import a p2sh version, too
213
+ bool fP2SH = false ;
214
+ if (params.size () > 3 )
215
+ fP2SH = params[3 ].get_bool ();
204
216
205
217
LOCK2 (cs_main, pwalletMain->cs_wallet );
206
218
207
219
CBitcoinAddress address (params[0 ].get_str ());
208
220
if (address.IsValid ()) {
221
+ if (fP2SH )
222
+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Cannot use the p2sh flag with an address - use a script instead" );
209
223
ImportAddress (address, strLabel);
210
224
} else if (IsHex (params[0 ].get_str ())) {
211
225
std::vector<unsigned char > data (ParseHex (params[0 ].get_str ()));
212
- ImportScript (CScript (data.begin (), data.end ()));
226
+ ImportScript (CScript (data.begin (), data.end ()), strLabel, fP2SH );
213
227
} else {
214
228
throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Invalid Bitcoin address or script" );
215
229
}
0 commit comments