Skip to content

Commit 713497f

Browse files
committed
refactor: switch to secrecy::SecretString for secrets
1 parent 5a64bb7 commit 713497f

File tree

18 files changed

+63
-37
lines changed

18 files changed

+63
-37
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ hmac = "^0.12.1"
2727
sha2 = "^0.10.8"
2828
hex = "^0.4.3"
2929
derive-new = "^0.7.0"
30+
secrecy = "^0.10.3"
3031

3132
#[dev-dependencies]
3233
insta = "1.42.0"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
![Minimum Supported Rust Version](https://img.shields.io/badge/nightly-1.85+-ab6000.svg)
33
[<img alt="crates.io" src="https://img.shields.io/crates/v/v_exchanges.svg?color=fc8d62&logo=rust" height="20" style=flat-square>](https://crates.io/crates/v_exchanges)
44
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs&style=flat-square" height="20">](https://docs.rs/v_exchanges)
5-
![Lines Of Code](https://img.shields.io/badge/LoC-4619-lightblue)
5+
![Lines Of Code](https://img.shields.io/badge/LoC-4631-lightblue)
66
<br>
77
[<img alt="ci errors" src="https://img.shields.io/github/actions/workflow/status/valeratrades/v_exchanges/errors.yml?branch=master&style=for-the-badge&style=flat-square&label=errors&labelColor=420d09" height="20">](https://github.com/valeratrades/v_exchanges/actions?query=branch%3Amaster) <!--NB: Won't find it if repo is private-->
88
[<img alt="ci warnings" src="https://img.shields.io/github/actions/workflow/status/valeratrades/v_exchanges/warnings.yml?branch=master&style=for-the-badge&style=flat-square&label=warnings&labelColor=d16002" height="20">](https://github.com/valeratrades/v_exchanges/actions?query=branch%3Amaster) <!--NB: Won't find it if repo is private-->

examples/binance/market_futures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async fn main() {
2020
dbg!(&klines, price);
2121

2222
if let (Ok(key), Ok(secret)) = (env::var("BINANCE_TIGER_READ_KEY"), env::var("BINANCE_TIGER_READ_SECRET")) {
23-
c.auth(key, secret);
23+
c.auth(key, secret.into());
2424
let balance_usdt = c.asset_balance("USDT".into(), m).await.unwrap();
2525
dbg!(&balance_usdt);
2626
let balances = c.balances(m).await.unwrap();

examples/bybit/market.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async fn main() {
1515
dbg!(&price);
1616

1717
if let (Ok(key), Ok(secret)) = (env::var("BYBIT_TIGER_READ_KEY"), env::var("BYBIT_TIGER_READ_SECRET")) {
18-
c.auth(key, secret);
18+
c.auth(key, secret.into());
1919
private(&*c, m).await;
2020
} else {
2121
eprintln!("BYBIT_TIGER_READ_KEY or BYBIT_TIGER_READ_SECRET is missing, skipping private API methods.");

examples/mexc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ async fn main() {
88

99
let m: AbsMarket = "Mexc/Futures".into();
1010
let mut c = m.client();
11-
c.auth(env::var("MEXC_READ_KEY").unwrap(), env::var("MEXC_READ_SECRET").unwrap());
11+
c.auth(env::var("MEXC_READ_KEY").unwrap(), env::var("MEXC_READ_SECRET").unwrap().into());
1212

1313
let price = c.price(("BTC", "USDT").into(), m).await.unwrap();
1414
dbg!(&price);

v_exchanges/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ v_exchanges_adapters = { version = "^0.4.0", path = "../v_exchanges_adapters/",
4545
reqwest = { version = "^0.12.12", optional = true }
4646

4747
insta.workspace = true #dbg (for some reason is not loading in dev-dependencies rn
48+
secrecy.workspace = true
4849

4950
[dev-dependencies]
5051
color-eyre = "^0.6.3"

v_exchanges/src/binance/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod spot;
66
use adapters::binance::BinanceOption;
77
use derive_more::{Deref, DerefMut};
88
use eyre::Result;
9+
use secrecy::SecretString;
910
use v_exchanges_adapters::Client;
1011
use v_utils::trades::{Asset, Pair, Timeframe};
1112

@@ -26,7 +27,7 @@ impl Exchange for Binance {
2627
self.source_market.unwrap()
2728
}
2829

29-
fn auth(&mut self, key: String, secret: String) {
30+
fn auth(&mut self, key: String, secret: SecretString) {
3031
self.update_default_option(BinanceOption::Key(key));
3132
self.update_default_option(BinanceOption::Secret(secret));
3233
}
@@ -108,6 +109,7 @@ impl crate::core::MarketTrait for Market {
108109
..Default::default()
109110
})
110111
}
112+
111113
fn abs_market(&self) -> AbsMarket {
112114
AbsMarket::Binance(*self)
113115
}

v_exchanges/src/bybit/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use derive_more::{
99
derive::{Deref, DerefMut},
1010
};
1111
use eyre::Result;
12+
use secrecy::SecretString;
1213
use v_exchanges_adapters::Client;
1314
use v_utils::trades::{Asset, Pair, Timeframe};
1415

@@ -32,7 +33,7 @@ impl Exchange for Bybit {
3233
self.source_market.unwrap()
3334
}
3435

35-
fn auth(&mut self, key: String, secret: String) {
36+
fn auth(&mut self, key: String, secret: SecretString) {
3637
self.update_default_option(BybitOption::Key(key));
3738
self.update_default_option(BybitOption::Secret(secret));
3839
}

v_exchanges/src/core.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::{BTreeMap, VecDeque};
33
use chrono::{DateTime, TimeDelta, Utc};
44
use derive_more::{Deref, DerefMut};
55
use eyre::{Report, Result, bail};
6+
use secrecy::SecretString;
67
use serde_json::json;
78
use v_utils::{
89
trades::{Asset, Kline, Pair, Timeframe, Usd},
@@ -17,7 +18,7 @@ pub trait Exchange: std::fmt::Debug + Send {
1718
self.source_market().exchange_name()
1819
}
1920

20-
fn auth(&mut self, key: String, secret: String);
21+
fn auth(&mut self, key: String, secret: SecretString);
2122

2223
async fn exchange_info(&self, m: AbsMarket) -> Result<ExchangeInfo>;
2324

@@ -60,7 +61,7 @@ impl std::fmt::Display for WrongExchangeError {
6061

6162
pub trait MarketTrait {
6263
fn client(&self, source_market: AbsMarket) -> Box<dyn Exchange>;
63-
fn client_authenticated(&self, key: String, secret: String, source_market: AbsMarket) -> Box<dyn Exchange> {
64+
fn client_authenticated(&self, key: String, secret: SecretString, source_market: AbsMarket) -> Box<dyn Exchange> {
6465
let mut client = self.client(source_market);
6566
client.auth(key, secret);
6667
client
@@ -84,7 +85,7 @@ impl AbsMarket {
8485
}
8586

8687
//Q: more I think about it, more this seems redundant / stupid according to Tiger Style
87-
pub fn client_authenticated(&self, key: String, secret: String) -> Box<dyn Exchange> {
88+
pub fn client_authenticated(&self, key: String, secret: SecretString) -> Box<dyn Exchange> {
8889
match self {
8990
Self::Binance(m) => m.client_authenticated(key, secret, *self),
9091
Self::Bybit(m) => m.client_authenticated(key, secret, *self),
@@ -234,16 +235,17 @@ impl RequestRange {
234235
_ => unimplemented!(),
235236
}
236237
}
238+
237239
fn serialize_common(&self) -> serde_json::Value {
238-
filter_nulls(match self {
239-
RequestRange::StartEnd { start, end } => json!({
240-
"startTime": start.timestamp_millis(),
241-
"endTime": end.map(|dt| dt.timestamp_millis()),
242-
}),
243-
RequestRange::Limit(limit) => json!({
244-
"limit": limit,
245-
}),
246-
})
240+
filter_nulls(match self {
241+
RequestRange::StartEnd { start, end } => json!({
242+
"startTime": start.timestamp_millis(),
243+
"endTime": end.map(|dt| dt.timestamp_millis()),
244+
}),
245+
RequestRange::Limit(limit) => json!({
246+
"limit": limit,
247+
}),
248+
})
247249
}
248250
}
249251
impl Default for RequestRange {

0 commit comments

Comments
 (0)