@@ -21,21 +21,9 @@ static void WalletToolReleaseWallet(CWallet* wallet)
21
21
delete wallet;
22
22
}
23
23
24
- static std::shared_ptr<CWallet> CreateWallet ( const std::string& name, const fs::path& path )
24
+ static void WalletCreate (CWallet* wallet_instance )
25
25
{
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);
32
26
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
- }
39
27
40
28
wallet_instance->SetMinVersion (FEATURE_HD_SPLIT);
41
29
@@ -46,18 +34,26 @@ static std::shared_ptr<CWallet> CreateWallet(const std::string& name, const fs::
46
34
47
35
tfm::format (std::cout, " Topping up keypool...\n " );
48
36
wallet_instance->TopUpKeyPool ();
49
- return wallet_instance;
50
37
}
51
38
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 )
53
40
{
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 );
56
52
return nullptr ;
57
53
}
58
54
59
55
// 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} ;
61
57
DBErrors load_wallet_ret;
62
58
try {
63
59
bool first_run;
@@ -89,6 +85,8 @@ static std::shared_ptr<CWallet> LoadWallet(const std::string& name, const fs::pa
89
85
}
90
86
}
91
87
88
+ if (create) WalletCreate (wallet_instance.get ());
89
+
92
90
return wallet_instance;
93
91
}
94
92
@@ -109,19 +107,14 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
109
107
fs::path path = fs::absolute (name, GetWalletDir ());
110
108
111
109
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 );
113
111
if (wallet_instance) {
114
112
WalletShowInfo (wallet_instance.get ());
115
113
wallet_instance->Close ();
116
114
}
117
115
} 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
-
123
116
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 );
125
118
if (!wallet_instance) return false ;
126
119
WalletShowInfo (wallet_instance.get ());
127
120
wallet_instance->Close ();
0 commit comments