Skip to content

WIP: Expose VssStore in bindings #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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, AuthMethod auth_custom);
};

interface Node {
Expand Down Expand Up @@ -353,6 +355,11 @@ enum LogLevel {
"Error",
};

[Trait]
interface AuthMethod {
record<string, string> get([ByRef]sequence<u8> request_body);
};

[Custom]
typedef string Txid;

Expand Down
17 changes: 15 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Node, BuildError> {
pub fn build_with_vss_store(
&self, url: String, store_id: String, auth_custom: Arc<dyn AuthMethod>,
) -> Result<Node, BuildError> {
let logger = setup_logger(&self.config)?;

let seed_bytes = seed_bytes_from_config(
Expand All @@ -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.clone()));
build_with_store_internal(
config,
self.chain_data_source_config.as_ref(),
Expand Down Expand Up @@ -512,6 +516,15 @@ 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, auth_custom: Arc<dyn AuthMethod>,
) -> Result<Arc<Node>, BuildError> {
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.
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Arc<Node>, BuildError> {
self.inner.read().unwrap().build_with_store(kv_store).map(Arc::new)
Expand Down
36 changes: 21 additions & 15 deletions src/io/vss_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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;
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,
Expand All @@ -35,10 +36,14 @@ pub struct VssStore {
store_id: String,
runtime: Runtime,
storable_builder: StorableBuilder<RandEntropySource>,
auth_custom: Arc<dyn AuthMethod>,
}

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: Arc<dyn AuthMethod>,
) -> 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))
Expand All @@ -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 }
}

fn build_key(
Expand Down Expand Up @@ -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[..])?;
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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::*;

Expand Down