|
14 | 14 | // You should have received a copy of the GNU General Public License
|
15 | 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 | 16 |
|
| 17 | +use std::fmt::Display; |
17 | 18 | use std::fs;
|
18 | 19 | use std::net::{SocketAddr, ToSocketAddrs};
|
19 | 20 | use std::path::PathBuf;
|
@@ -329,6 +330,39 @@ impl GlobalConfig {
|
329 | 330 | pub fn load_from_file(path: &str) -> Result<Self, ConfigError> {
|
330 | 331 | Self::try_from(&PathBuf::from(path))
|
331 | 332 | }
|
| 333 | + |
| 334 | + /// Return a string with non-sensitive configuration |
| 335 | + /// information for logging purposes |
| 336 | + pub fn config_to_log_string(&self) -> String { |
| 337 | + let tx_fee = match self.tx_fee_ustx { |
| 338 | + 0 => "default".to_string(), |
| 339 | + _ => (self.tx_fee_ustx as f64 / 1_000_000.0).to_string(), |
| 340 | + }; |
| 341 | + format!( |
| 342 | + r#" |
| 343 | +Stacks node host: {node_host} |
| 344 | +Signer endpoint: {endpoint} |
| 345 | +Stacks address: {stacks_address} |
| 346 | +Public key: {public_key} |
| 347 | +Network: {network} |
| 348 | +Database path: {db_path} |
| 349 | +DKG transaction fee: {tx_fee} uSTX |
| 350 | +"#, |
| 351 | + node_host = self.node_host, |
| 352 | + endpoint = self.endpoint, |
| 353 | + stacks_address = self.stacks_address.to_string(), |
| 354 | + public_key = StacksPublicKey::from_private(&self.stacks_private_key).to_hex(), |
| 355 | + network = self.network, |
| 356 | + db_path = self.db_path.to_str().unwrap_or_default(), |
| 357 | + tx_fee = tx_fee |
| 358 | + ) |
| 359 | + } |
| 360 | +} |
| 361 | + |
| 362 | +impl Display for GlobalConfig { |
| 363 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 364 | + write!(f, "{}", self.config_to_log_string()) |
| 365 | + } |
332 | 366 | }
|
333 | 367 |
|
334 | 368 | /// Helper function for building a signer config for each provided signer private key
|
@@ -412,4 +446,24 @@ mod tests {
|
412 | 446 |
|
413 | 447 | assert_eq!(config.auth_password, "melon");
|
414 | 448 | }
|
| 449 | + |
| 450 | + #[test] |
| 451 | + fn test_config_to_string() { |
| 452 | + let config = GlobalConfig::load_from_file("./src/tests/conf/signer-0.toml").unwrap(); |
| 453 | + let config_str = config.config_to_log_string(); |
| 454 | + assert_eq!( |
| 455 | + config_str, |
| 456 | + format!( |
| 457 | + r#" |
| 458 | +Stacks node host: 127.0.0.1:20443 |
| 459 | +Signer endpoint: [::1]:30000 |
| 460 | +Stacks address: ST3FPN8KBZ3YPBP0ZJGAAHTVFMQDTJCR5QPS7VTNJ |
| 461 | +Public key: 03bc489f27da3701d9f9e577c88de5567cf4023111b7577042d55cde4d823a3505 |
| 462 | +Network: testnet |
| 463 | +Database path: :memory: |
| 464 | +DKG transaction fee: 0.01 uSTX |
| 465 | +"# |
| 466 | + ) |
| 467 | + ); |
| 468 | + } |
415 | 469 | }
|
0 commit comments