@ -552,19 +552,24 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
if ( ! EnsureWalletIsAvailable ( fHelp ) )
return NullUniValue ;
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 2 )
if ( fHelp | | params . size ( ) < 1 | | params . size ( ) > 3 )
throw runtime_error (
" z_importkey \" zkey \" ( rescan ) \n "
" z_importkey \" zkey \" ( rescan startHeight ) \n "
" \n Adds a zkey (as returned by z_exportkey) to your wallet. \n "
" \n Arguments: \n "
" 1. \" zkey \" (string, required) The zkey (see z_exportkey) \n "
" 2. rescan (boolean, optional, default=true) Rescan the wallet for transactions \n "
" 2. rescan (boolean or \" whenkeyisnew \" , optional, default= \" whenkeyisnew \" ) Rescan the wallet for transactions \n "
" 3. startHeight (numeric, optional, default=0) Block height to start rescan from \n "
" \n Note: This call can take minutes to complete if rescan is true. \n "
" \n Examples: \n "
" \n Export a zkey \n "
+ HelpExampleCli ( " z_exportkey " , " \" myaddress \" " ) +
" \n Import the zkey with rescan \n "
+ HelpExampleCli ( " z_importkey " , " \" mykey \" " ) +
" \n Import the zkey with partial rescan \n "
+ HelpExampleCli ( " z_importkey " , " \" mykey \" , \" whenkeyisnew \" , 30000 " ) +
" \n Re-import the zkey with longer partial rescan \n "
+ HelpExampleCli ( " z_importkey " , " \" mykey \" , true, 20000 " ) +
" \n As a JSON-RPC call \n "
+ HelpExampleRpc ( " z_importkey " , " \" mykey \" , false " )
) ;
@ -575,8 +580,24 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
// Whether to perform rescan after import
bool fRescan = true ;
if ( params . size ( ) > 1 )
fRescan = params [ 1 ] . get_bool ( ) ;
bool fIgnoreExistingKey = true ;
if ( params . size ( ) > 1 ) {
auto rescan = params [ 1 ] . get_str ( ) ;
if ( rescan . compare ( " whenkeyisnew " ) ! = 0 ) {
fIgnoreExistingKey = false ;
UniValue jVal ;
if ( ! jVal . read ( std : : string ( " [ " ) + rescan + std : : string ( " ] " ) ) | |
! jVal . isArray ( ) | | jVal . size ( ) ! = 1 | | ! jVal [ 0 ] . isBool ( ) ) {
throw JSONRPCError ( RPC_INVALID_PARAMETER , " rescan must be bool or \" whenkeyisnew \" " ) ;
}
fRescan = jVal [ 0 ] . getBool ( ) ;
}
}
// Height to rescan from
int nRescanHeight = 0 ;
if ( params . size ( ) > 2 )
nRescanHeight = params [ 2 ] . get_int ( ) ;
string strSecret = params [ 0 ] . get_str ( ) ;
CZCSpendingKey spendingkey ( strSecret ) ;
@ -585,22 +606,25 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
{
// Don't throw error in case a key is already there
if ( pwalletMain - > HaveSpendingKey ( addr ) )
return NullUniValue ;
pwalletMain - > MarkDirty ( ) ;
if ( pwalletMain - > HaveSpendingKey ( addr ) ) {
if ( fIgnoreExistingKey ) {
return NullUniValue ;
}
} else {
pwalletMain - > MarkDirty ( ) ;
if ( ! pwalletMain - > AddZKey ( key ) )
throw JSONRPCError ( RPC_WALLET_ERROR , " Error adding spending key to wallet " ) ;
if ( ! pwalletMain - > AddZKey ( key ) )
throw JSONRPCError ( RPC_WALLET_ERROR , " Error adding spending key to wallet " ) ;
pwalletMain - > mapZKeyMetadata [ addr ] . nCreateTime = 1 ;
pwalletMain - > mapZKeyMetadata [ addr ] . nCreateTime = 1 ;
}
// whenever a key is imported, we need to scan the whole chain
pwalletMain - > nTimeFirstKey = 1 ; // 0 would be considered 'no value'
// We want to scan for transactions and notes
if ( fRescan ) {
pwalletMain - > ScanForWalletTransactions ( chainActive . Genesis ( ) , true ) ;
pwalletMain - > ScanForWalletTransactions ( chainActive [ nRescanHeight ] , true ) ;
}
}