Skip to content

Commit

Permalink
Merge pull request #495 from breez/signer
Browse files Browse the repository at this point in the history
Support user implemented signer.
  • Loading branch information
roeierez authored Oct 20, 2024
2 parents cbe835b + ef28bec commit e928c3d
Show file tree
Hide file tree
Showing 27 changed files with 934 additions and 138 deletions.
3 changes: 2 additions & 1 deletion cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ typedef struct wire_cst_config {
} wire_cst_config;

typedef struct wire_cst_connect_request {
struct wire_cst_list_prim_u_8_strict *mnemonic;
struct wire_cst_config config;
struct wire_cst_list_prim_u_8_strict *mnemonic;
} wire_cst_connect_request;

typedef struct wire_cst_aes_success_action_data_decrypted {
Expand Down
7 changes: 6 additions & 1 deletion lib/bindings/langs/react-native/src/gen_kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ pub use uniffi_bindgen::bindings::kotlin::gen_kotlin::*;
use crate::generator::RNConfig;

static IGNORED_FUNCTIONS: Lazy<HashSet<String>> = Lazy::new(|| {
let list: Vec<&str> = vec!["connect", "add_event_listener", "set_logger"];
let list: Vec<&str> = vec![
"connect",
"add_event_listener",
"set_logger",
"connect_with_signer",
];
HashSet::from_iter(list.into_iter().map(|s| s.to_string()))
});

Expand Down
7 changes: 6 additions & 1 deletion lib/bindings/langs/react-native/src/gen_swift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ use crate::generator::RNConfig;
pub use uniffi_bindgen::bindings::swift::gen_swift::*;

static IGNORED_FUNCTIONS: Lazy<HashSet<String>> = Lazy::new(|| {
let list: Vec<&str> = vec!["connect", "add_event_listener", "set_logger"];
let list: Vec<&str> = vec![
"connect",
"add_event_listener",
"set_logger",
"connect_with_signer",
];
HashSet::from_iter(list.into_iter().map(|s| s.to_string()))
});

Expand Down
7 changes: 6 additions & 1 deletion lib/bindings/langs/react-native/src/gen_typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ static KEYWORDS: Lazy<HashSet<String>> = Lazy::new(|| {
});

static IGNORED_FUNCTIONS: Lazy<HashSet<String>> = Lazy::new(|| {
let list: Vec<&str> = vec!["connect", "add_event_listener", "set_logger"];
let list: Vec<&str> = vec![
"connect",
"add_event_listener",
"set_logger",
"connect_with_signer",
];
HashSet::from_iter(list.into_iter().map(|s| s.to_string()))
});

Expand Down
34 changes: 33 additions & 1 deletion lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,14 @@ enum LiquidNetwork {
};

dictionary ConnectRequest {
Config config;
Config config;
string mnemonic;
};

dictionary ConnectWithSignerRequest {
Config config;
};

dictionary GetInfoResponse {
u64 balance_sat;
u64 pending_send_sat;
Expand Down Expand Up @@ -601,6 +605,9 @@ namespace breez_sdk_liquid {
[Throws=SdkError]
BindingLiquidSdk connect(ConnectRequest req);

[Throws=SdkError]
BindingLiquidSdk connect_with_signer(ConnectWithSignerRequest req, Signer signer);

[Throws=SdkError]
void set_logger(Logger logger);

Expand All @@ -614,6 +621,31 @@ namespace breez_sdk_liquid {
LNInvoice parse_invoice(string input);
};

[Error]
interface SignerError {
Generic(string err);
};

callback interface Signer {
[Throws=SignerError]
sequence<u8> xpub();

[Throws=SignerError]
sequence<u8> derive_xpub(string derivation_path);

[Throws=SignerError]
sequence<u8> sign_ecdsa(sequence<u8> msg, string derivation_path);

[Throws=SignerError]
sequence<u8> sign_ecdsa_recoverable(sequence<u8> msg);

[Throws=SignerError]
sequence<u8> slip77_master_blinding_key();

[Throws=SignerError]
sequence<u8> hmac_sha256(sequence<u8> msg, string derivation_path);
};

interface BindingLiquidSdk {
[Throws=SdkError]
string add_event_listener(EventListener listener);
Expand Down
10 changes: 10 additions & 0 deletions lib/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ pub fn connect(req: ConnectRequest) -> Result<Arc<BindingLiquidSdk>, SdkError> {
})
}

pub fn connect_with_signer(
req: ConnectWithSignerRequest,
signer: Box<dyn Signer>,
) -> Result<Arc<BindingLiquidSdk>, SdkError> {
rt().block_on(async {
let sdk = LiquidSdk::connect_with_signer(req, signer).await?;
Ok(Arc::from(BindingLiquidSdk { sdk }))
})
}

pub fn default_config(
network: LiquidNetwork,
breez_api_key: Option<String>,
Expand Down
5 changes: 2 additions & 3 deletions lib/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ lwk_wollet = { git = "https://github.com/dangeross/lwk", branch = "savage-try-he
#lwk_wollet = "0.7.0"
rusqlite = { version = "0.31", features = ["backup", "bundled"] }
rusqlite_migration = "1.0"
sdk-common = { git = "https://github.com/breez/breez-sdk", branch = "main", features = [
"liquid",
] }
sdk-common = { git = "https://github.com/breez/breez-sdk", rev = "387e37f8ce48ee841762b945137e2c6b4b4b5cfa", features = ["liquid"]}
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.116"
strum = "0.25"
Expand All @@ -50,6 +48,7 @@ reqwest = { version = "=0.11.20", features = ["json"] }
electrum-client = { version = "0.19.0" }
zbase32 = "0.1.2"
x509-parser = { version = "0.16.0" }
tempfile = "3"

[dev-dependencies]
lazy_static = "1.5.0"
Expand Down
14 changes: 7 additions & 7 deletions lib/core/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2305,11 +2305,11 @@ impl SseDecode for crate::model::Config {
impl SseDecode for crate::model::ConnectRequest {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut var_mnemonic = <String>::sse_decode(deserializer);
let mut var_config = <crate::model::Config>::sse_decode(deserializer);
let mut var_mnemonic = <String>::sse_decode(deserializer);
return crate::model::ConnectRequest {
mnemonic: var_mnemonic,
config: var_config,
mnemonic: var_mnemonic,
};
}
}
Expand Down Expand Up @@ -4334,8 +4334,8 @@ impl flutter_rust_bridge::IntoIntoDart<crate::model::Config> for crate::model::C
impl flutter_rust_bridge::IntoDart for crate::model::ConnectRequest {
fn into_dart(self) -> flutter_rust_bridge::for_generated::DartAbi {
[
self.mnemonic.into_into_dart().into_dart(),
self.config.into_into_dart().into_dart(),
self.mnemonic.into_into_dart().into_dart(),
]
.into_dart()
}
Expand Down Expand Up @@ -6298,8 +6298,8 @@ impl SseEncode for crate::model::Config {
impl SseEncode for crate::model::ConnectRequest {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<String>::sse_encode(self.mnemonic, serializer);
<crate::model::Config>::sse_encode(self.config, serializer);
<String>::sse_encode(self.mnemonic, serializer);
}
}

Expand Down Expand Up @@ -8225,8 +8225,8 @@ mod io {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> crate::model::ConnectRequest {
crate::model::ConnectRequest {
mnemonic: self.mnemonic.cst_decode(),
config: self.config.cst_decode(),
mnemonic: self.mnemonic.cst_decode(),
}
}
}
Expand Down Expand Up @@ -9586,8 +9586,8 @@ mod io {
impl NewWithNullPtr for wire_cst_connect_request {
fn new_with_null_ptr() -> Self {
Self {
mnemonic: core::ptr::null_mut(),
config: Default::default(),
mnemonic: core::ptr::null_mut(),
}
}
}
Expand Down Expand Up @@ -11475,8 +11475,8 @@ mod io {
#[repr(C)]
#[derive(Clone, Copy)]
pub struct wire_cst_connect_request {
mnemonic: *mut wire_cst_list_prim_u_8_strict,
config: wire_cst_config,
mnemonic: *mut wire_cst_list_prim_u_8_strict,
}
#[repr(C)]
#[derive(Clone, Copy)]
Expand Down
3 changes: 3 additions & 0 deletions lib/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ pub mod error;
pub(crate) mod event;
#[cfg(feature = "frb")]
pub(crate) mod frb_generated;
pub(crate) mod lnurl;
pub mod logger;
pub mod model;
pub mod persist;
Expand All @@ -179,6 +180,7 @@ pub(crate) mod receive_swap;
mod restore;
pub mod sdk;
pub(crate) mod send_swap;
pub(crate) mod signer;
pub(crate) mod swapper;
pub(crate) mod test_utils;
pub(crate) mod utils;
Expand All @@ -192,4 +194,5 @@ pub mod prelude {
pub use crate::*;
pub use crate::model::*;
pub use crate::sdk::*;
pub use crate::signer::SdkSigner;
}
47 changes: 47 additions & 0 deletions lib/core/src/lnurl/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::sync::Arc;

use sdk_common::{
bitcoin::util::bip32::{ChildNumber, DerivationPath},
prelude::{LnUrlResult, LnurlAuthSigner},
};

use crate::model::Signer;

pub(crate) struct SdkLnurlAuthSigner {
signer: Arc<Box<dyn Signer>>,
}

impl SdkLnurlAuthSigner {
pub fn new(signer: Arc<Box<dyn Signer>>) -> Self {
Self { signer }
}
}

impl LnurlAuthSigner for SdkLnurlAuthSigner {
fn derive_bip32_pub_key(&self, derivation_path: &[ChildNumber]) -> LnUrlResult<Vec<u8>> {
let derivation: DerivationPath = derivation_path.to_vec().into();
self.signer
.derive_xpub(derivation.to_string())
.map_err(|e| sdk_common::prelude::LnUrlError::Generic(e.to_string()))
.map(|xpub| xpub.to_vec())
}

fn sign_ecdsa(&self, msg: &[u8], derivation_path: &[ChildNumber]) -> LnUrlResult<Vec<u8>> {
let derivation: DerivationPath = derivation_path.to_vec().into();
self.signer
.sign_ecdsa(msg.to_vec(), derivation.to_string())
.map_err(|e| sdk_common::prelude::LnUrlError::Generic(e.to_string()))
.map(|s: Vec<u8>| s.to_vec())
}

fn hmac_sha256(
&self,
key_derivation_path: &[ChildNumber],
input: &[u8],
) -> LnUrlResult<Vec<u8>> {
let derivation: DerivationPath = key_derivation_path.to_vec().into();
self.signer
.hmac_sha256(input.to_vec(), derivation.to_string())
.map_err(|e| sdk_common::prelude::LnUrlError::Generic(e.to_string()))
}
}
1 change: 1 addition & 0 deletions lib/core/src/lnurl/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod auth;
Loading

0 comments on commit e928c3d

Please sign in to comment.