Skip to content

Commit 9a5b5ee

Browse files
promagryanofsky
andcommitted
wallet: Replace %w by wallet name in -walletnotify script
Co-authored-by: Russell Yanofsky <[email protected]>
1 parent 6196e93 commit 9a5b5ee

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/util/system.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include <malloc.h>
6464
#endif
6565

66+
#include <boost/algorithm/string/replace.hpp>
6667
#include <thread>
6768
#include <typeinfo>
6869
#include <univalue.h>
@@ -1022,6 +1023,15 @@ fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
10221023
}
10231024
#endif
10241025

1026+
#ifndef WIN32
1027+
std::string ShellEscape(const std::string& arg)
1028+
{
1029+
std::string escaped = arg;
1030+
boost::replace_all(escaped, "'", "'\"'\"'");
1031+
return "'" + escaped + "'";
1032+
}
1033+
#endif
1034+
10251035
#if HAVE_SYSTEM
10261036
void runCommand(const std::string& strCommand)
10271037
{

src/util/system.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ fs::path GetConfigFile(const std::string& confPath);
8181
#ifdef WIN32
8282
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
8383
#endif
84+
#ifndef WIN32
85+
std::string ShellEscape(const std::string& arg);
86+
#endif
8487
#if HAVE_SYSTEM
8588
void runCommand(const std::string& strCommand);
8689
#endif

src/wallet/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void WalletInit::AddWalletOptions() const
6262
gArgs.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
6363
gArgs.AddArg("-walletdir=<dir>", "Specify directory to hold wallets (default: <datadir>/wallets if it exists, otherwise <datadir>)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
6464
#if HAVE_SYSTEM
65-
gArgs.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
65+
gArgs.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes. %s in cmd is replaced by TxID and %w is replaced by wallet name. %w is not currently implemented on windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
6666
#endif
6767
gArgs.AddArg("-walletrbf", strprintf("Send transactions with full-RBF opt-in enabled (RPC only, default: %u)", DEFAULT_WALLET_RBF), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
6868
gArgs.AddArg("-zapwallettxes=<mode>", "Delete all wallet transactions and only recover those parts of the blockchain through -rescan on startup"

src/wallet/wallet.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,14 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
835835
if (!strCmd.empty())
836836
{
837837
boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
838+
#ifndef WIN32
839+
// Substituting the wallet name isn't currently supported on windows
840+
// because windows shell escaping has not been implemented yet:
841+
// https://github.com/bitcoin/bitcoin/pull/13339#issuecomment-537384875
842+
// A few ways it could be implemented in the future are described in:
843+
// https://github.com/bitcoin/bitcoin/pull/13339#issuecomment-461288094
844+
boost::replace_all(strCmd, "%w", ShellEscape(GetName()));
845+
#endif
838846
std::thread t(runCommand, strCmd);
839847
t.detach(); // thread runs free
840848
}

0 commit comments

Comments
 (0)