diff --git a/pc/key_store.cpp b/pc/key_store.cpp index a521adbb7..b3f4d0b36 100644 --- a/pc/key_store.cpp +++ b/pc/key_store.cpp @@ -55,6 +55,12 @@ std::string key_store::get_dir() const return dir_; } +std::string key_store::get_default_dir() +{ + const char *home = ::getenv( "HOME" ); + return home ? ( std::string( home ) + "/.pythd/" ) : "./.pythd/"; +} + bool key_store::create() { struct stat fst[1]; diff --git a/pc/key_store.hpp b/pc/key_store.hpp index 27361beb4..4f82aa2bd 100644 --- a/pc/key_store.hpp +++ b/pc/key_store.hpp @@ -22,6 +22,7 @@ namespace pc // directory where to store account keys void set_dir( const std::string& dir_name ); std::string get_dir() const; + static std::string get_default_dir(); // file names std::string get_publish_key_pair_file() const; diff --git a/pcapps/pyth.cpp b/pcapps/pyth.cpp index 935092923..037660234 100644 --- a/pcapps/pyth.cpp +++ b/pcapps/pyth.cpp @@ -12,7 +12,11 @@ using namespace pc; static const std::string DEFAULT_RPC_HOST = "localhost"; static const std::string DEFAULT_TX_HOST = "localhost"; -static const std::string DEFAULT_KEY_STORE = std::string( getenv( "HOME" ) ) + "/.pythd/"; + +static std::string get_default_key_store() +{ + return key_store::get_default_dir(); +} int usage() { @@ -31,7 +35,7 @@ int usage() cerr << " -r " << endl; cerr << " Host name or IP address of solana rpc node in the form " "host_name[:rpc_port[:ws_port]]\n" << endl; - cerr << " -k " << endl; + cerr << " -k " << endl; cerr << " Directory name housing publishing, mapping and program" " key files\n" << endl; cerr << " -c " << endl; @@ -58,7 +62,7 @@ struct pyth_arguments bool invalid_ = false; std::string rpc_host_ = DEFAULT_RPC_HOST; std::string tx_host_ = DEFAULT_TX_HOST; - std::string key_dir_ = DEFAULT_KEY_STORE; + std::string key_dir_ = get_default_key_store(); commitment cmt_ = commitment::e_confirmed; bool do_json_ = false; bool do_tx_ = true; diff --git a/pcapps/pyth_admin.cpp b/pcapps/pyth_admin.cpp index 30a91140f..dad6c6d10 100644 --- a/pcapps/pyth_admin.cpp +++ b/pcapps/pyth_admin.cpp @@ -12,7 +12,11 @@ using namespace pc; static const int DEFAULT_EXPONENT = -5; static const std::string DEFAULT_RPC_HOST = "localhost"; -static const std::string DEFAULT_KEY_STORE = std::string( getenv( "HOME" ) ) + "/.pythd/"; + +static std::string get_default_key_store() +{ + return key_store::get_default_dir(); +} int usage() { @@ -39,7 +43,7 @@ int usage() cerr << " -r " << endl; cerr << " Host name or IP address of solana rpc node in the form " "host_name[:rpc_port[:ws_port]]\n" << endl; - cerr << " -k " << endl; + cerr << " -k " << endl; cerr << " Directory name housing publishing, mapping and program" " key files\n" << endl; cerr << " -c " << endl; @@ -57,7 +61,7 @@ struct pyth_arguments bool invalid_ = false; std::string rpc_host_ = DEFAULT_RPC_HOST; - std::string key_dir_ = DEFAULT_KEY_STORE; + std::string key_dir_ = get_default_key_store(); commitment cmt_ = commitment::e_confirmed; int exponent_ = DEFAULT_EXPONENT; uint8_t min_pub_ = 0; diff --git a/pcapps/pythd.cpp b/pcapps/pythd.cpp index 0151b5fa0..5e4b3a270 100644 --- a/pcapps/pythd.cpp +++ b/pcapps/pythd.cpp @@ -20,8 +20,7 @@ std::string get_tx_host() std::string get_key_store() { - std::string dir = getenv("HOME"); - return dir + "/.pythd/"; + return key_store::get_default_dir(); } int get_port() @@ -43,7 +42,7 @@ int usage() << std::endl; std::cerr << " -i " << std::endl; std::cerr << " -k ]" << std::endl; + << ")>" << std::endl; std::cerr << " Directory name housing publishing, mapping and program" " key files\n" << std::endl; std::cerr << " -p " diff --git a/pctest/test_unit.cpp b/pctest/test_unit.cpp index f020af913..421a055eb 100644 --- a/pctest/test_unit.cpp +++ b/pctest/test_unit.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -178,10 +179,29 @@ void test_request_sub() PC_TEST_CHECK( sub1.check( "r1", p1_3 ) ); } +void test_key_store() +{ + char *orig_home = ::getenv( "HOME" ); + std::string saved_home = orig_home ? orig_home : ""; + + ::unsetenv( "HOME" ); + PC_TEST_CHECK( key_store::get_default_dir() == "./.pythd/" ); + + ::setenv( "HOME", "/test/home", 1 ); + PC_TEST_CHECK( key_store::get_default_dir() == "/test/home/.pythd/" ); + + if ( orig_home ) { + ::setenv( "HOME", saved_home.c_str(), 1 ); + } else { + ::unsetenv( "HOME" ); + } +} + int main(int,char**) { PC_TEST_START test_key(); + test_key_store(); test_log(); test_request_sub(); PC_TEST_END