@@ -21,21 +21,9 @@ static void WalletToolReleaseWallet(CWallet* wallet)
2121 delete wallet;
2222}
2323
24- static std::shared_ptr<CWallet> CreateWallet ( const std::string& name, const fs::path& path )
24+ static void WalletCreate (CWallet* wallet_instance )
2525{
26- if (fs::exists (path)) {
27- tfm::format (std::cerr, " Error: File exists already\n " );
28- return nullptr ;
29- }
30- // dummy chain interface
31- std::shared_ptr<CWallet> wallet_instance (new CWallet (nullptr /* chain */ , name, CreateWalletDatabase (path)), WalletToolReleaseWallet);
3226 LOCK (wallet_instance->cs_wallet );
33- bool first_run = true ;
34- DBErrors load_wallet_ret = wallet_instance->LoadWallet (first_run);
35- if (load_wallet_ret != DBErrors::LOAD_OK) {
36- tfm::format (std::cerr, " Error creating %s" , name);
37- return nullptr ;
38- }
3927
4028 wallet_instance->SetMinVersion (FEATURE_HD_SPLIT);
4129
@@ -46,18 +34,26 @@ static std::shared_ptr<CWallet> CreateWallet(const std::string& name, const fs::
4634
4735 tfm::format (std::cout, " Topping up keypool...\n " );
4836 wallet_instance->TopUpKeyPool ();
49- return wallet_instance;
5037}
5138
52- static std::shared_ptr<CWallet> LoadWallet (const std::string& name, const fs::path& path)
39+ static std::shared_ptr<CWallet> MakeWallet (const std::string& name, const fs::path& path, bool create )
5340{
54- if (!fs::exists (path)) {
55- tfm::format (std::cerr, " Error: Wallet files does not exist\n " );
41+ DatabaseOptions options;
42+ DatabaseStatus status;
43+ if (create) {
44+ options.require_create = true ;
45+ } else {
46+ options.require_existing = true ;
47+ }
48+ bilingual_str error;
49+ std::unique_ptr<WalletDatabase> database = MakeDatabase (path, options, status, error);
50+ if (!database) {
51+ tfm::format (std::cerr, " %s\n " , error.original );
5652 return nullptr ;
5753 }
5854
5955 // dummy chain interface
60- std::shared_ptr<CWallet> wallet_instance ( new CWallet (nullptr /* chain */ , name, CreateWalletDatabase (path )), WalletToolReleaseWallet) ;
56+ std::shared_ptr<CWallet> wallet_instance{ new CWallet (nullptr /* chain */ , name, std::move (database )), WalletToolReleaseWallet} ;
6157 DBErrors load_wallet_ret;
6258 try {
6359 bool first_run;
@@ -89,6 +85,8 @@ static std::shared_ptr<CWallet> LoadWallet(const std::string& name, const fs::pa
8985 }
9086 }
9187
88+ if (create) WalletCreate (wallet_instance.get ());
89+
9290 return wallet_instance;
9391}
9492
@@ -109,19 +107,14 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
109107 fs::path path = fs::absolute (name, GetWalletDir ());
110108
111109 if (command == " create" ) {
112- std::shared_ptr<CWallet> wallet_instance = CreateWallet (name, path);
110+ std::shared_ptr<CWallet> wallet_instance = MakeWallet (name, path, /* create= */ true );
113111 if (wallet_instance) {
114112 WalletShowInfo (wallet_instance.get ());
115113 wallet_instance->Close ();
116114 }
117115 } else if (command == " info" || command == " salvage" ) {
118- if (!fs::exists (path)) {
119- tfm::format (std::cerr, " Error: no wallet file at %s\n " , name);
120- return false ;
121- }
122-
123116 if (command == " info" ) {
124- std::shared_ptr<CWallet> wallet_instance = LoadWallet (name, path);
117+ std::shared_ptr<CWallet> wallet_instance = MakeWallet (name, path, /* create= */ false );
125118 if (!wallet_instance) return false ;
126119 WalletShowInfo (wallet_instance.get ());
127120 wallet_instance->Close ();
0 commit comments