diff options
author | login <login@tilde.team> | 2021-06-30 16:18:51 +0000 |
---|---|---|
committer | login <login@tilde.team> | 2021-06-30 16:18:51 +0000 |
commit | bc268e30f1e622f5cfa67c8a52a2d3c7c1c1520e (patch) | |
tree | 824fa4401d0af9cd20e95b037c047e656eaf6805 /pcoin.cpp | |
parent | 2e22b08ff724c1fcdf7daad1ee201bcf944bcbc2 (diff) | |
download | tcoin-bc268e30f1e622f5cfa67c8a52a2d3c7c1c1520e.tar.gz |
Update the way pcoin_keys works to use a folder/directory instead of a single file
This is preparing tcoin/pcoin for the automatic registration of program accounts. Eventually, I want program accounts to be tradeable by there being a way to regenerate a new key. So, send the current key to whomever you want the program account to be used by, then the new owner regenerates the key to complete the transfer. The old owner will no longer be able to use the old key to operate the new account. The key is long and the key generation random enough for key collisions to be virtually impossible.
Diffstat (limited to 'pcoin.cpp')
-rw-r--r-- | pcoin.cpp | 60 |
1 files changed, 40 insertions, 20 deletions
diff --git a/pcoin.cpp b/pcoin.cpp index b6f2f8f..2d5a490 100644 --- a/pcoin.cpp +++ b/pcoin.cpp @@ -28,7 +28,8 @@ #define TCOIN_PASS_PATH "/home/login/tcoin/passwords/" #define TCOIN_PROG_ACT_PATH "/home/login/tcoin/program_accounting/" #define PROG_ACT_W_SLASH "program_accounting/" -#define PCOIN_KEY_PATH "/home/login/bin/pcoin_keys" +#define PCOIN_KEY_PATH_W_SLASH "/home/login/bin/pcoin_keys/" +#define LS_PCOIN_KEY_CMD "/bin/ls /home/login/bin/pcoin_keys" #define TCOIN_CODEZ_PATH "/home/login/bin/tcoin_codez" #define TCOIN_BIN_PATH_W_SPACE "/home/login/bin/tcoin " @@ -556,21 +557,30 @@ void show_tsv_messages_tail(const char* username, int lineCount) bool program_exists(const char* username) { - std::ifstream fin(PCOIN_KEY_PATH); - //first word is program username, second word is key - std::string word1; - std::string word2; - while(fin >> word1) + char *program_key_path = new char[strlen(username) + sizeof(PCOIN_KEY_PATH_W_SLASH) + 4]; //sizeof counts NULL char at the end too + std::strcpy(program_key_path, PCOIN_KEY_PATH_W_SLASH); + std::strcat(program_key_path, username); + std::strcat(program_key_path, ".txt"); + + std::ifstream fin(program_key_path); + bool return_value = false; + + if(!fin) //file doesn't exist { - if(!word1.compare(username)) - { - fin.close(); - return true; - } - fin >> word2; //to get rid of the second word (which is the key) + fin.close(); + delete[] program_key_path; + + return_value = false; //program account not found } - fin.close(); - return false; //if program not found + else + { + fin.close(); + delete[] program_key_path; + + return_value = true; //program account found + } + + return return_value; } bool username_exists(const char* username) @@ -1463,22 +1473,32 @@ bool is_number(const char* test_string) std::string get_username_from_key(std::string &key) { - std::ifstream fin(PCOIN_KEY_PATH); + const static std::string all_usernames_dot_txt = exec(LS_PCOIN_KEY_CMD); + std::istringstream iss(all_usernames_dot_txt); + std::string word1, word2; - bool should_return = false; - //first word is program username, second word is key - while(fin >> word1) + //first word is program username with .txt on the end, second word is key + while(iss >> word1) { + char *program_key_path = new char[strlen(word1.c_str()) + sizeof(PCOIN_KEY_PATH_W_SLASH)]; //sizeof counts NULL char at the end too + std::strcpy(program_key_path, PCOIN_KEY_PATH_W_SLASH); + std::strcat(program_key_path, word1.c_str()); + + std::ifstream fin(program_key_path); + fin >> word2; + if(!strctcmp(word2.c_str(), key.c_str())) { fin.close(); - return word1; + delete[] program_key_path; + return word1.substr(0, word1.size()-4); //removing .txt from the username returned } + fin.close(); + delete[] program_key_path; } word1.assign("n/a"); - fin.close(); return word1; } |