From c4ad6eaf08edd96aa9169a2c03964c77e2a67008 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 7 Feb 2024 14:13:49 +0100 Subject: [PATCH 1/5] Expose `VssStore` in bindings --- bindings/ldk_node.udl | 2 ++ src/builder.rs | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 58fab0d52..490afe8a4 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -37,6 +37,8 @@ interface Builder { Node build(); [Throws=BuildError] Node build_with_fs_store(); + [Throws=BuildError] + Node build_with_vss_store(string url, string store_id); }; interface Node { diff --git a/src/builder.rs b/src/builder.rs index 6d3db420f..e118b2111 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -512,6 +512,13 @@ impl ArcedNodeBuilder { self.inner.read().unwrap().build_with_fs_store().map(Arc::new) } + /// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options + /// previously configured. + #[cfg(any(vss, vss_test))] + pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result, BuildError> { + self.inner.read().unwrap().build_with_vss_store(url, store_id).map(Arc::new) + } + /// Builds a [`Node`] instance according to the options previously configured. pub fn build_with_store(&self, kv_store: Arc) -> Result, BuildError> { self.inner.read().unwrap().build_with_store(kv_store).map(Arc::new) From 36265202568db4102d6bd69eb475867c86cb1bec Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 7 Feb 2024 14:19:59 +0100 Subject: [PATCH 2/5] FIXME: Update `Cargo.toml` to use `VssStore` with `AuthMethod` --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 4c4422461..c3caa120c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,8 @@ libc = "0.2" uniffi = { version = "0.26.0", features = ["build"], optional = true } [target.'cfg(vss)'.dependencies] -vss-client = "0.2" +#vss-client = "0.2" +vss-client = { git = "https://github.com/g8xsu/vss-rust-client", rev="ceaa00db43b10992e7e327661ffb398ca2178f6d"} prost = { version = "0.11.6", default-features = false} [target.'cfg(windows)'.dependencies] From 36c60acd90d75f92e276e0ba1249b47ecedb940e Mon Sep 17 00:00:00 2001 From: Gursharan Singh <3442979+G8XSU@users.noreply.github.com> Date: Mon, 5 Feb 2024 11:03:30 -0800 Subject: [PATCH 3/5] Add dyn AuthMethod trait --- src/builder.rs | 12 +++++++++--- src/io/vss_store.rs | 36 +++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index e118b2111..7f2d3efb5 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -52,6 +52,8 @@ use bdk::bitcoin::secp256k1::Secp256k1; use bdk::blockchain::esplora::EsploraBlockchain; use bdk::database::SqliteDatabase; use bdk::template::Bip84; +#[cfg(any(vss, vss_test))] +use vss_client::client::AuthMethod; use bip39::Mnemonic; @@ -335,7 +337,9 @@ impl NodeBuilder { /// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options /// previously configured. #[cfg(any(vss, vss_test))] - pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result { + pub fn build_with_vss_store( + &self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static, + ) -> Result { let logger = setup_logger(&self.config)?; let seed_bytes = seed_bytes_from_config( @@ -360,7 +364,7 @@ impl NodeBuilder { let vss_seed_bytes: [u8; 32] = vss_xprv.private_key.secret_bytes(); - let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes)); + let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes, auth_custom)); build_with_store_internal( config, self.chain_data_source_config.as_ref(), @@ -515,7 +519,9 @@ impl ArcedNodeBuilder { /// Builds a [`Node`] instance with a [`VssStore`] backend and according to the options /// previously configured. #[cfg(any(vss, vss_test))] - pub fn build_with_vss_store(&self, url: String, store_id: String) -> Result, BuildError> { + pub fn build_with_vss_store( + &self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static, + ) -> Result, BuildError> { self.inner.read().unwrap().build_with_vss_store(url, store_id).map(Arc::new) } diff --git a/src/io/vss_store.rs b/src/io/vss_store.rs index 426af1fbb..0b0ef3d13 100644 --- a/src/io/vss_store.rs +++ b/src/io/vss_store.rs @@ -3,6 +3,7 @@ use std::io; use std::io::ErrorKind; #[cfg(test)] use std::panic::RefUnwindSafe; +use std::sync::Arc; use std::time::Duration; use crate::io::utils::check_namespace_key_validity; @@ -10,7 +11,7 @@ use lightning::util::persist::KVStore; use prost::Message; use rand::RngCore; use tokio::runtime::Runtime; -use vss_client::client::VssClient; +use vss_client::client::{AuthMethod, VssClient}; use vss_client::error::VssError; use vss_client::types::{ DeleteObjectRequest, GetObjectRequest, KeyValue, ListKeyVersionsRequest, PutObjectRequest, @@ -35,10 +36,14 @@ pub struct VssStore { store_id: String, runtime: Runtime, storable_builder: StorableBuilder, + auth_custom: Arc, } impl VssStore { - pub(crate) fn new(base_url: String, store_id: String, data_encryption_key: [u8; 32]) -> Self { + pub(crate) fn new( + base_url: String, store_id: String, data_encryption_key: [u8; 32], + auth_custom: impl AuthMethod + 'static, + ) -> Self { let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap(); let storable_builder = StorableBuilder::new(data_encryption_key, RandEntropySource); let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100)) @@ -55,7 +60,7 @@ impl VssStore { }) as _); let client = VssClient::new(&base_url, retry_policy); - Self { client, store_id, runtime, storable_builder } + Self { client, store_id, runtime, storable_builder, auth_custom: Arc::new(auth_custom) } } fn build_key( @@ -118,18 +123,19 @@ impl KVStore for VssStore { key: self.build_key(primary_namespace, secondary_namespace, key)?, }; - let resp = - tokio::task::block_in_place(|| self.runtime.block_on(self.client.get_object(&request))) - .map_err(|e| { - let msg = format!( - "Failed to read from key {}/{}/{}: {}", - primary_namespace, secondary_namespace, key, e - ); - match e { - VssError::NoSuchKeyError(..) => Error::new(ErrorKind::NotFound, msg), - _ => Error::new(ErrorKind::Other, msg), - } - })?; + let resp = tokio::task::block_in_place(|| { + self.runtime.block_on(self.client.get_object(&request, self.auth_custom.clone())) + }) + .map_err(|e| { + let msg = format!( + "Failed to read from key {}/{}/{}: {}", + primary_namespace, secondary_namespace, key, e + ); + match e { + VssError::NoSuchKeyError(..) => Error::new(ErrorKind::NotFound, msg), + _ => Error::new(ErrorKind::Other, msg), + } + })?; // unwrap safety: resp.value must be always present for a non-erroneous VSS response, otherwise // it is an API-violation which is converted to [`VssError::InternalServerError`] in [`VssClient`] let storable = Storable::decode(&resp.value.unwrap().value[..])?; From dbf44f3af9e706cc15f95e43685fd51949f3f27c Mon Sep 17 00:00:00 2001 From: Gursharan Singh <3442979+G8XSU@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:22:40 -0800 Subject: [PATCH 4/5] with arc --- src/builder.rs | 6 +++--- src/io/vss_store.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 7f2d3efb5..357842595 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -338,7 +338,7 @@ impl NodeBuilder { /// previously configured. #[cfg(any(vss, vss_test))] pub fn build_with_vss_store( - &self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static, + &self, url: String, store_id: String, auth_custom: Arc, ) -> Result { let logger = setup_logger(&self.config)?; @@ -364,7 +364,7 @@ impl NodeBuilder { let vss_seed_bytes: [u8; 32] = vss_xprv.private_key.secret_bytes(); - let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes, auth_custom)); + let vss_store = Arc::new(VssStore::new(url, store_id, vss_seed_bytes, auth_custom.clone())); build_with_store_internal( config, self.chain_data_source_config.as_ref(), @@ -520,7 +520,7 @@ impl ArcedNodeBuilder { /// previously configured. #[cfg(any(vss, vss_test))] pub fn build_with_vss_store( - &self, url: String, store_id: String, auth_custom: impl AuthMethod + 'static, + &self, url: String, store_id: String, auth_custom: Arc, ) -> Result, BuildError> { self.inner.read().unwrap().build_with_vss_store(url, store_id).map(Arc::new) } diff --git a/src/io/vss_store.rs b/src/io/vss_store.rs index 0b0ef3d13..283350200 100644 --- a/src/io/vss_store.rs +++ b/src/io/vss_store.rs @@ -42,7 +42,7 @@ pub struct VssStore { impl VssStore { pub(crate) fn new( base_url: String, store_id: String, data_encryption_key: [u8; 32], - auth_custom: impl AuthMethod + 'static, + auth_custom: Arc, ) -> Self { let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap(); let storable_builder = StorableBuilder::new(data_encryption_key, RandEntropySource); @@ -60,7 +60,7 @@ impl VssStore { }) as _); let client = VssClient::new(&base_url, retry_policy); - Self { client, store_id, runtime, storable_builder, auth_custom: Arc::new(auth_custom) } + Self { client, store_id, runtime, storable_builder, auth_custom } } fn build_key( From 3f3bc4adb2ae872c481e84e87e02a51ece395a15 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 7 Feb 2024 14:39:43 +0100 Subject: [PATCH 5/5] WIP Expose custom `AuthMethod` in bindings --- bindings/ldk_node.udl | 7 ++++++- src/builder.rs | 2 +- src/lib.rs | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 490afe8a4..6eff0d594 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -38,7 +38,7 @@ interface Builder { [Throws=BuildError] Node build_with_fs_store(); [Throws=BuildError] - Node build_with_vss_store(string url, string store_id); + Node build_with_vss_store(string url, string store_id, AuthMethod auth_custom); }; interface Node { @@ -355,6 +355,11 @@ enum LogLevel { "Error", }; +[Trait] +interface AuthMethod { + record get([ByRef]sequence request_body); +}; + [Custom] typedef string Txid; diff --git a/src/builder.rs b/src/builder.rs index 357842595..89f34955d 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -522,7 +522,7 @@ impl ArcedNodeBuilder { pub fn build_with_vss_store( &self, url: String, store_id: String, auth_custom: Arc, ) -> Result, BuildError> { - self.inner.read().unwrap().build_with_vss_store(url, store_id).map(Arc::new) + self.inner.read().unwrap().build_with_vss_store(url, store_id, auth_custom).map(Arc::new) } /// Builds a [`Node`] instance according to the options previously configured. diff --git a/src/lib.rs b/src/lib.rs index 3d619cebb..cdd3e8fde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,6 +101,8 @@ pub use bip39; pub use bitcoin; pub use lightning; pub use lightning_invoice; +#[cfg(vss)] +pub use vss_client; pub use balance::{BalanceDetails, LightningBalance, PendingSweepBalance}; pub use config::{default_config, Config}; @@ -112,6 +114,8 @@ pub use types::ChannelConfig; pub use io::utils::generate_entropy_mnemonic; +#[cfg(all(vss, feature = "uniffi"))] +pub use vss_client::client::AuthMethod; #[cfg(feature = "uniffi")] use uniffi_types::*;