Skip to content

Commit ca05de4

Browse files
G8XSUtnull
authored andcommitted
Add dyn AuthMethod trait
1 parent 5ea1b4a commit ca05de4

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
@@ -46,6 +46,8 @@ use bdk::bitcoin::secp256k1::Secp256k1;
4646
use bdk::blockchain::esplora::EsploraBlockchain;
4747
use bdk::database::SqliteDatabase;
4848
use bdk::template::Bip84;
49+
#[cfg(any(vss, vss_test))]
50+
use vss_client::client::AuthMethod;
4951

5052
use bip39::Mnemonic;
5153

@@ -292,7 +294,9 @@ impl NodeBuilder {
292294
/// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options
293295
/// previously configured.
294296
#[cfg(any(vss, vss_test))]
295-
pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result<Node, BuildError> {
297+
pub fn build_with_vss_store(
298+
&self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static,
299+
) -> Result<Node, BuildError> {
296300
let logger = setup_logger(&self.config)?;
297301

298302
let seed_bytes = seed_bytes_from_config(
@@ -317,7 +321,7 @@ impl NodeBuilder {
317321

318322
let vss_seed_bytes: [u8; 32] = vss_xprv.private_key.secret_bytes();
319323

320-
let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes));
324+
let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes, auth_custom));
321325
build_with_store_internal(
322326
config,
323327
self.chain_data_source_config.as_ref(),
@@ -457,7 +461,9 @@ impl ArcedNodeBuilder {
457461
/// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options
458462
/// previously configured.
459463
#[cfg(any(vss, vss_test))]
460-
pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result<Arc<Node>, BuildError> {
464+
pub fn build_with_vss_store(
465+
&self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static,
466+
) -> Result<Arc<Node>, BuildError> {
461467
self.inner.read().unwrap().build_with_vss_store(url, store_id).map(Arc::new)
462468
}
463469

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)