-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bindings:implement a generic interface
- Loading branch information
1 parent
761ece3
commit d308f82
Showing
9 changed files
with
280 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdint.h> | ||
|
||
typedef enum { | ||
None = 0, | ||
Tokio, | ||
CastString, | ||
Json, | ||
CString, | ||
ListPools | ||
} Error; | ||
|
||
typedef struct Pools { | ||
const char* pools; | ||
Error error; | ||
} Pools; | ||
|
||
Pools list_pools(uint64_t back, uint64_t timeout, const char* relay); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use std::{fmt::Display, time::Duration}; | ||
|
||
use bitcoin::Network; | ||
use nostr_sdk::Keys; | ||
use tokio::time::sleep; | ||
|
||
use crate::nostr::client::NostrClient; | ||
|
||
pub enum Error { | ||
Unknown, | ||
NostrClient(crate::nostr::client::Error), | ||
SerdeJson(serde_json::Error), | ||
} | ||
|
||
impl From<crate::nostr::client::Error> for Error { | ||
fn from(value: crate::nostr::client::Error) -> Self { | ||
Self::NostrClient(value) | ||
} | ||
} | ||
|
||
impl From<serde_json::Error> for Error { | ||
fn from(value: serde_json::Error) -> Self { | ||
Self::SerdeJson(value) | ||
} | ||
} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Error::Unknown => write!(f, "Unknown error!"), | ||
Error::NostrClient(e) => write!(f, "NostrClient error: {:?}", e), | ||
Error::SerdeJson(e) => write!(f, "serde_json error: {:?}", e), | ||
} | ||
} | ||
} | ||
|
||
pub struct PoolConfig { | ||
pub denomination: f64, | ||
pub fee: u32, | ||
pub max_duration: u32, | ||
pub peers: u8, | ||
pub network: Network, | ||
} | ||
|
||
pub struct PeerConfig { | ||
pub outpoint: String, | ||
pub electrum: String, | ||
pub mnemonics: String, | ||
pub address: String, | ||
pub relay: String, | ||
} | ||
|
||
/// Initiate and participate to a coinjoin | ||
/// | ||
/// # Arguments | ||
/// * `config` - configuration of the pool to initiate | ||
/// * `peer` - information about the peer | ||
/// | ||
pub fn initiate_coinjoin( | ||
_config: PoolConfig, | ||
_peer: PeerConfig, | ||
) -> Result<String /* Txid */, Error> { | ||
// TODO: | ||
Ok(String::new()) | ||
} | ||
|
||
/// List available pools | ||
/// | ||
/// # Arguments | ||
/// * `back` - how many second back look in the past | ||
/// * `timeout` - how many microseconds we will wait before fetching relay notifications | ||
/// * `relay` - the relay url, must start w/ `wss://` or `ws://` | ||
/// | ||
/// # Returns a [`Vec`] of [`String`] containing a json serialization of a [`Pool`] | ||
pub async fn list_pools( | ||
back: u64, | ||
timeout: u64, | ||
relay: String, | ||
) -> Result<Vec<String /* Pool */>, Error> { | ||
let mut pools = Vec::new(); | ||
let relays = vec![relay]; | ||
let mut pool_listener = NostrClient::new("pool_listener") | ||
.relays(&relays)? | ||
.keys(Keys::generate())?; | ||
pool_listener.connect_nostr().await.unwrap(); | ||
// subscribe to 2020 event up to 1 day back in time | ||
pool_listener.subscribe_pools(back).await.unwrap(); | ||
|
||
sleep(Duration::from_micros(timeout)).await; | ||
|
||
while let Some(pool) = pool_listener.receive_pool_notification()? { | ||
let str = serde_json::to_string(&pool)?; | ||
pools.push(str) | ||
} | ||
|
||
Ok(pools) | ||
} | ||
|
||
/// Try to join an already initiated coinjoin | ||
/// | ||
/// # Arguments | ||
/// * `pool` - [`String`] containing a json serialization of a [`Pool`] | ||
/// * `peer` - information about the peer | ||
/// | ||
pub fn join_coinjoin( | ||
_pool: String, /* Pool */ | ||
_peer: PeerConfig, | ||
) -> Result<String /* Txid */, Error> { | ||
// TODO: | ||
Ok(String::new()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,135 @@ | ||
#![allow(dead_code)] | ||
pub mod coinjoin; | ||
pub mod electrum; | ||
pub mod interface; | ||
pub mod joinstr; | ||
pub mod nostr; | ||
pub mod signer; | ||
pub mod utils; | ||
|
||
use lazy_static::lazy_static; | ||
use serde::Serialize; | ||
use std::{ | ||
ffi::{c_char, CStr, CString}, | ||
ptr::null, | ||
sync::Mutex, | ||
}; | ||
use tokio::runtime::Runtime; | ||
|
||
lazy_static! { | ||
static ref RT: Mutex<Runtime> = Mutex::new(Runtime::new().unwrap()); | ||
} | ||
|
||
fn cast_to_cstring<T>(value: T) -> Result<CString, Error> | ||
where | ||
T: Serialize, | ||
{ | ||
match serde_json::to_string(&value) { | ||
Ok(v) => match CString::new(v) { | ||
Ok(s) => Ok(s), | ||
Err(_) => { | ||
log::error!("fail to convert json string to C string!"); | ||
Err(Error::Json) | ||
} | ||
}, | ||
Err(_) => { | ||
log::error!("fail to convert pool list to it json string representation!"); | ||
Err(Error::CString) | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy)] | ||
pub enum Network { | ||
/// Mainnet Bitcoin. | ||
Bitcoin, | ||
/// Bitcoin's testnet network. | ||
Testnet, | ||
/// Bitcoin's signet network. | ||
Signet, | ||
/// Bitcoin's regtest network. | ||
Regtest, | ||
} | ||
|
||
#[repr(C)] | ||
pub struct PoolConfig { | ||
pub denomination: f64, | ||
pub fee: u32, | ||
pub max_duration: u32, | ||
pub peers: u8, | ||
pub network: Network, | ||
} | ||
|
||
#[repr(C)] | ||
pub struct PeerConfig { | ||
pub outpoint: *mut c_char, | ||
pub electrum: *mut c_char, | ||
pub mnemonics: *mut c_char, | ||
pub address: *mut c_char, | ||
pub relay: *mut c_char, | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy)] | ||
pub enum Error { | ||
None, | ||
Tokio, | ||
CastString, | ||
Json, | ||
CString, | ||
ListPools, | ||
} | ||
|
||
|
||
impl Pools { | ||
pub fn ok(pools: CString) -> Self { | ||
Pools { | ||
pools: pools.into_raw(), | ||
error: Error::None, | ||
} | ||
} | ||
pub fn error(e: Error) -> Self { | ||
Pools { | ||
pools: null(), | ||
error: e, | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct Pools { | ||
pools: *const c_char, | ||
error: Error, | ||
} | ||
|
||
#[no_mangle] | ||
#[allow(clippy::not_unsafe_ptr_arg_deref)] | ||
pub extern "C" fn list_pools(back: u64, timeout: u64, relay: *const c_char) -> Pools { | ||
let relay = unsafe { CStr::from_ptr(relay) }; | ||
let relay = if let Ok(relay) = relay.to_str() { | ||
relay.to_owned() | ||
} else { | ||
log::error!("list_pool(): fail to cast `relay` arg!"); | ||
return Pools::error(Error::CastString); | ||
}; | ||
let future = async { | ||
match interface::list_pools(back, timeout, relay).await { | ||
Ok(p) => cast_to_cstring(p), | ||
Err(e) => { | ||
log::error!("list_pools() fail: {}", e); | ||
Err(Error::ListPools) | ||
} | ||
} | ||
}; | ||
|
||
if let Ok(runtime) = RT.lock() { | ||
match runtime.block_on(future) { | ||
Ok(p) => Pools::ok(p), | ||
Err(e) => Pools::error(e), | ||
} | ||
} else { | ||
log::error!("list_pools(): fail to get a lock on tokio runtime!"); | ||
Pools::error(Error::Tokio) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters