From 78941da5baf6244c7c54e86cf8ce3e09ce60c239 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 27 Jun 2019 21:17:42 -0400 Subject: [PATCH] Optionally allow ImportScripts to set script creation timestamp Behavior changes: * scripts imported in importmulti that are not explicilty scriptPubKeys will have timestamps set for them --- src/wallet/rpcdump.cpp | 6 +++--- src/wallet/wallet.cpp | 10 +++++++++- src/wallet/wallet.h | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 964676e6b..480832c62 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -194,7 +194,7 @@ UniValue importprivkey(const JSONRPCRequest& request) // Add the wpkh script for this key if possible if (pubkey.IsCompressed()) { - pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))}); + pwallet->ImportScripts({GetScriptForDestination(WitnessV0KeyHash(vchAddress))}, 0 /* timestamp */); } } } @@ -316,7 +316,7 @@ UniValue importaddress(const JSONRPCRequest& request) CScript redeem_script(data.begin(), data.end()); std::set scripts = {redeem_script}; - pwallet->ImportScripts(scripts); + pwallet->ImportScripts(scripts, 0 /* timestamp */); if (fP2SH) { scripts.insert(GetScriptForDestination(ScriptHash(CScriptID(redeem_script)))); @@ -1251,7 +1251,7 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con // All good, time to import pwallet->MarkDirty(); - if (!pwallet->ImportScripts(import_data.import_scripts)) { + if (!pwallet->ImportScripts(import_data.import_scripts, timestamp)) { throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet"); } if (!pwallet->ImportPrivKeys(privkey_map, timestamp)) { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 45cd2d0e0..6f080f4b8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1661,7 +1661,7 @@ bool CWallet::DummySignTx(CMutableTransaction &txNew, const std::vector return true; } -bool CWallet::ImportScripts(const std::set scripts) +bool CWallet::ImportScripts(const std::set scripts, int64_t timestamp) { WalletBatch batch(*database); for (const auto& entry : scripts) { @@ -1673,7 +1673,15 @@ bool CWallet::ImportScripts(const std::set scripts) if (!AddCScriptWithDB(batch, entry)) { return false; } + + if (timestamp > 0) { + m_script_metadata[CScriptID(entry)].nCreateTime = timestamp; + } } + if (timestamp > 0) { + UpdateTimeFirstKey(timestamp); + } + return true; } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index b2810246d..1b6a2c99f 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1092,7 +1092,7 @@ public: bool DummySignTx(CMutableTransaction &txNew, const std::vector &txouts, bool use_max_sig = false) const; bool DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig = false) const; - bool ImportScripts(const std::set scripts) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); + bool ImportScripts(const std::set scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool ImportPrivKeys(const std::map& privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool ImportPubKeys(const std::vector& ordered_pubkeys, const std::map& pubkey_map, const std::map>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); bool ImportScriptPubKeys(const std::string& label, const std::set& script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);