Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pc/key_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions pc/key_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions pcapps/pyth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -31,7 +35,7 @@ int usage()
cerr << " -r <rpc_host (default " << DEFAULT_RPC_HOST << ")>" << endl;
cerr << " Host name or IP address of solana rpc node in the form "
"host_name[:rpc_port[:ws_port]]\n" << endl;
cerr << " -k <key_store_directory (default " << DEFAULT_KEY_STORE << ")>" << endl;
cerr << " -k <key_store_directory (default " << get_default_key_store() << ")>" << endl;
cerr << " Directory name housing publishing, mapping and program"
" key files\n" << endl;
cerr << " -c <commitment_level (default confirmed)>" << endl;
Expand All @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions pcapps/pyth_admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -39,7 +43,7 @@ int usage()
cerr << " -r <rpc_host (default " << DEFAULT_RPC_HOST << ")>" << endl;
cerr << " Host name or IP address of solana rpc node in the form "
"host_name[:rpc_port[:ws_port]]\n" << endl;
cerr << " -k <key_store_directory (default " << DEFAULT_KEY_STORE << ")>" << endl;
cerr << " -k <key_store_directory (default " << get_default_key_store() << ")>" << endl;
cerr << " Directory name housing publishing, mapping and program"
" key files\n" << endl;
cerr << " -c <commitment_level (default confirmed)>" << endl;
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions pcapps/pythd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -43,7 +42,7 @@ int usage()
<< std::endl;
std::cerr << " -i <interval with which to send notify_price_sched notifications, in milliseconds (default 1000) >" << std::endl;
std::cerr << " -k <key_store_directory (default " << get_key_store()
<< ")>]" << std::endl;
<< ")>" << std::endl;
std::cerr << " Directory name housing publishing, mapping and program"
" key files\n" << std::endl;
std::cerr << " -p <listening_port (default " << get_port() << ">"
Expand Down
20 changes: 20 additions & 0 deletions pctest/test_unit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <pc/key_pair.hpp>
#include <pc/key_store.hpp>
#include <pc/misc.hpp>
#include <pc/log.hpp>
#include <pc/request.hpp>
Expand Down Expand Up @@ -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
Expand Down