Skip to content

Commit 36c60ac

Browse files
G8XSUtnull
authored andcommitted
Add dyn AuthMethod trait
1 parent 3626520 commit 36c60ac

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/builder.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ use bdk::bitcoin::secp256k1::Secp256k1;
5252
use bdk::blockchain::esplora::EsploraBlockchain;
5353
use bdk::database::SqliteDatabase;
5454
use bdk::template::Bip84;
55+
#[cfg(any(vss, vss_test))]
56+
use vss_client::client::AuthMethod;
5557

5658
use bip39::Mnemonic;
5759

@@ -335,7 +337,9 @@ impl NodeBuilder {
335337
/// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options
336338
/// previously configured.
337339
#[cfg(any(vss, vss_test))]
338-
pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result<Node, BuildError> {
340+
pub fn build_with_vss_store(
341+
&self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static,
342+
) -> Result<Node, BuildError> {
339343
let logger = setup_logger(&self.config)?;
340344

341345
let seed_bytes = seed_bytes_from_config(
@@ -360,7 +364,7 @@ impl NodeBuilder {
360364

361365
let vss_seed_bytes: [u8; 32] = vss_xprv.private_key.secret_bytes();
362366

363-
let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes));
367+
let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes, auth_custom));
364368
build_with_store_internal(
365369
config,
366370
self.chain_data_source_config.as_ref(),
@@ -515,7 +519,9 @@ impl ArcedNodeBuilder {
515519
/// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options
516520
/// previously configured.
517521
#[cfg(any(vss, vss_test))]
518-
pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result<Arc<Node>, BuildError> {
522+
pub fn build_with_vss_store(
523+
&self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static,
524+
) -> Result<Arc<Node>, BuildError> {
519525
self.inner.read().unwrap().build_with_vss_store(url, store_id).map(Arc::new)
520526
}
521527

src/io/vss_store.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use std::io;
33
use std::io::ErrorKind;
44
#[cfg(test)]
55
use std::panic::RefUnwindSafe;
6+
use std::sync::Arc;
67
use std::time::Duration;
78

89
use crate::io::utils::check_namespace_key_validity;
910
use lightning::util::persist::KVStore;
1011
use prost::Message;
1112
use rand::RngCore;
1213
use tokio::runtime::Runtime;
13-
use vss_client::client::VssClient;
14+
use vss_client::client::{AuthMethod, VssClient};
1415
use vss_client::error::VssError;
1516
use vss_client::types::{
1617
DeleteObjectRequest, GetObjectRequest, KeyValue, ListKeyVersionsRequest, PutObjectRequest,
@@ -35,10 +36,14 @@ pub struct VssStore {
3536
store_id: String,
3637
runtime: Runtime,
3738
storable_builder: StorableBuilder<RandEntropySource>,
39+
auth_custom: Arc<dyn AuthMethod>,
3840
}
3941

4042
impl VssStore {
41-
pub(crate) fn new(base_url: String, store_id: String, data_encryption_key: [u8; 32]) -> Self {
43+
pub(crate) fn new(
44+
base_url: String, store_id: String, data_encryption_key: [u8; 32],
45+
auth_custom: impl AuthMethod + 'static,
46+
) -> Self {
4247
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();
4348
let storable_builder = StorableBuilder::new(data_encryption_key, RandEntropySource);
4449
let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100))
@@ -55,7 +60,7 @@ impl VssStore {
5560
}) as _);
5661

5762
let client = VssClient::new(&base_url, retry_policy);
58-
Self { client, store_id, runtime, storable_builder }
63+
Self { client, store_id, runtime, storable_builder, auth_custom: Arc::new(auth_custom) }
5964
}
6065

6166
fn build_key(
@@ -118,18 +123,19 @@ impl KVStore for VssStore {
118123
key: self.build_key(primary_namespace, secondary_namespace, key)?,
119124
};
120125

121-
let resp =
122-
tokio::task::block_in_place(|| self.runtime.block_on(self.client.get_object(&request)))
123-
.map_err(|e| {
124-
let msg = format!(
125-
"Failed to read from key {}/{}/{}: {}",
126-
primary_namespace, secondary_namespace, key, e
127-
);
128-
match e {
129-
VssError::NoSuchKeyError(..) => Error::new(ErrorKind::NotFound, msg),
130-
_ => Error::new(ErrorKind::Other, msg),
131-
}
132-
})?;
126+
let resp = tokio::task::block_in_place(|| {
127+
self.runtime.block_on(self.client.get_object(&request, self.auth_custom.clone()))
128+
})
129+
.map_err(|e| {
130+
let msg = format!(
131+
"Failed to read from key {}/{}/{}: {}",
132+
primary_namespace, secondary_namespace, key, e
133+
);
134+
match e {
135+
VssError::NoSuchKeyError(..) => Error::new(ErrorKind::NotFound, msg),
136+
_ => Error::new(ErrorKind::Other, msg),
137+
}
138+
})?;
133139
// unwrap safety: resp.value must be always present for a non-erroneous VSS response, otherwise
134140
// it is an API-violation which is converted to [`VssError::InternalServerError`] in [`VssClient`]
135141
let storable = Storable::decode(&resp.value.unwrap().value[..])?;

0 commit comments

Comments
 (0)