Helper: load environment variables from a .env file
| 126 | |
| 127 | // Helper: load environment variables from a .env file |
| 128 | void loadEnvFromFile(const std::string &env_path) { |
| 129 | std::ifstream env_file(env_path); |
| 130 | if (!env_file.is_open()) { |
| 131 | std::cerr << "Warning: .env file not found at " << env_path << std::endl; |
| 132 | return; |
| 133 | } |
| 134 | std::string line; |
| 135 | while (std::getline(env_file, line)) { |
| 136 | // Ignore comments and empty lines |
| 137 | if (line.empty() || line[0] == '#') |
| 138 | continue; |
| 139 | size_t eq_pos = line.find('='); |
| 140 | if (eq_pos == std::string::npos) |
| 141 | continue; |
| 142 | std::string key = line.substr(0, eq_pos); |
| 143 | std::string value = line.substr(eq_pos + 1); |
| 144 | // Remove possible trailing % or whitespace |
| 145 | if (!value.empty() && value.back() == '%') |
| 146 | value.pop_back(); |
| 147 | // Remove leading/trailing whitespace |
| 148 | key.erase(0, key.find_first_not_of(" \t\n\r")); |
| 149 | key.erase(key.find_last_not_of(" \t\n\r") + 1); |
| 150 | value.erase(0, value.find_first_not_of(" \t\n\r")); |
| 151 | value.erase(value.find_last_not_of(" \t\n\r") + 1); |
| 152 | setenv(key.c_str(), value.c_str(), 0); // do not overwrite existing |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | int main(int argc, char *argv[]) { |
| 157 | // If PRIVATE_KEY is not set, try to load from ./ethereum-sdk/.env |