@@ -103,6 +103,29 @@ static void WalletShowInfo(CWallet* wallet_instance)
103
103
tfm::format (std::cout, " Address Book: %zu\n " , wallet_instance->m_address_book .size ());
104
104
}
105
105
106
+ static bool SalvageWallet (const fs::path& path)
107
+ {
108
+ // Create a Database handle to allow for the db to be initialized before recovery
109
+ std::unique_ptr<WalletDatabase> database = WalletDatabase::Create (path);
110
+
111
+ // Initialize the environment before recovery
112
+ bilingual_str error_string;
113
+ try {
114
+ WalletBatch::VerifyEnvironment (path, error_string);
115
+ } catch (const fs::filesystem_error& e) {
116
+ error_string = Untranslated (strprintf (" Error loading wallet. %s" , fsbridge::get_filesystem_error_message (e)));
117
+ }
118
+ if (!error_string.original .empty ()) {
119
+ tfm::format (std::cerr, " Failed to open wallet for salvage :%s\n " , error_string.original );
120
+ return false ;
121
+ }
122
+
123
+ // Perform the recovery
124
+ CWallet dummy_wallet (nullptr , WalletLocation (), WalletDatabase::CreateDummy ());
125
+ std::string backup_filename;
126
+ return WalletBatch::Recover (path, (void *)&dummy_wallet, WalletBatch::RecoverKeysOnlyFilter, backup_filename);
127
+ }
128
+
106
129
bool ExecuteWalletToolFunc (const std::string& command, const std::string& name)
107
130
{
108
131
fs::path path = fs::absolute (name, GetWalletDir ());
@@ -113,7 +136,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
113
136
WalletShowInfo (wallet_instance.get ());
114
137
wallet_instance->Flush (true );
115
138
}
116
- } else if (command == " info" ) {
139
+ } else if (command == " info" || command == " salvage " ) {
117
140
if (!fs::exists (path)) {
118
141
tfm::format (std::cerr, " Error: no wallet file at %s\n " , name);
119
142
return false ;
@@ -123,10 +146,15 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
123
146
tfm::format (std::cerr, " %s\n Error loading %s. Is wallet being used by other process?\n " , error.original , name);
124
147
return false ;
125
148
}
126
- std::shared_ptr<CWallet> wallet_instance = LoadWallet (name, path);
127
- if (!wallet_instance) return false ;
128
- WalletShowInfo (wallet_instance.get ());
129
- wallet_instance->Flush (true );
149
+
150
+ if (command == " info" ) {
151
+ std::shared_ptr<CWallet> wallet_instance = LoadWallet (name, path);
152
+ if (!wallet_instance) return false ;
153
+ WalletShowInfo (wallet_instance.get ());
154
+ wallet_instance->Flush (true );
155
+ } else if (command == " salvage" ) {
156
+ return SalvageWallet (path);
157
+ }
130
158
} else {
131
159
tfm::format (std::cerr, " Invalid command: %s\n " , command);
132
160
return false ;
0 commit comments