-
Notifications
You must be signed in to change notification settings - Fork 2
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 b7a835c
Showing
12 changed files
with
686 additions
and
10 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,29 @@ | ||
#include "joinstr.h" | ||
|
||
auto errorToString(JoinstrError e) -> std::string { | ||
switch (e) { | ||
case JoinstrError::None: | ||
return "None"; | ||
case JoinstrError::Tokio: | ||
return "Tokio"; | ||
case JoinstrError::CastString: | ||
return "CastString"; | ||
case JoinstrError::JsonError: | ||
return "Json"; | ||
case JoinstrError::CString: | ||
return "CString"; | ||
case JoinstrError::ListPools: | ||
return "ListPools"; | ||
case ListCoins: | ||
return "ListCoins"; | ||
case InitiateConjoin: | ||
return "InitiateCoinjoin"; | ||
case SerdeJson: | ||
return "SerdeJson"; | ||
case PoolConfig: | ||
return "PoolConfig"; | ||
case PeerConfig: | ||
return "PeerConfig"; | ||
} | ||
return "Unknown"; | ||
} |
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,90 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <stdint.h> | ||
#include <string> | ||
using f64_t = double; | ||
|
||
extern "C" { | ||
|
||
enum Network { // NOLINT(performance-enum-size) | ||
Bitcoin = 0, | ||
Testnet, | ||
Signet, | ||
Regtest, | ||
}; | ||
|
||
enum JoinstrError { // NOLINT(performance-enum-size) | ||
None = 0, | ||
Tokio, | ||
CastString, | ||
JsonError, | ||
CString, | ||
ListPools, | ||
ListCoins, | ||
InitiateConjoin, | ||
SerdeJson, | ||
PoolConfig, | ||
PeerConfig, | ||
}; | ||
|
||
struct PoolConfig { | ||
f64_t denomination; | ||
uint32_t fee; | ||
uint64_t max_duration; | ||
uint8_t peers; | ||
Network network; | ||
}; | ||
|
||
struct PeerConfig { | ||
const char* electrum_address; | ||
uint16_t electrum_port; | ||
const char* mnemonics; | ||
const char* input; | ||
const char* output; | ||
const char* relay; | ||
}; | ||
|
||
struct Pools { | ||
const char* pools; | ||
JoinstrError error; | ||
}; | ||
|
||
struct Coins { | ||
const char* coins; | ||
JoinstrError error; | ||
}; | ||
|
||
struct Txid { | ||
const char* txid; | ||
JoinstrError error; | ||
}; | ||
|
||
auto list_pools( // NOLINT(readability-identifier-naming) | ||
uint64_t back, | ||
uint64_t timeout, | ||
const char* relay | ||
) -> Pools; | ||
|
||
auto list_coins( // NOLINT(readability-identifier-naming) | ||
const char* mnemonics, | ||
const char* addr, | ||
uint16_t port, | ||
Network network, | ||
uint32_t index_min, | ||
uint32_t index_max | ||
) -> Coins; | ||
|
||
auto initiate_coinjoin( // NOLINT(readability-identifier-naming) | ||
struct PoolConfig config, | ||
struct PeerConfig peer | ||
) -> Txid; | ||
|
||
auto join_coinjoin( // NOLINT(readability-identifier-naming) | ||
const char* pool, | ||
struct PeerConfig peer | ||
) -> Txid; | ||
|
||
} | ||
|
||
auto errorToString(JoinstrError e) -> std::string; |
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,228 @@ | ||
use std::{fmt::Display, time::Duration}; | ||
|
||
use bitcoin::{address::NetworkUnchecked, Address, Network}; | ||
use nostr_sdk::Keys; | ||
use tokio::time::sleep; | ||
|
||
use crate::{ | ||
electrum::Client, | ||
joinstr::Joinstr, | ||
nostr::{client::NostrClient, Pool}, | ||
signer::{Coin, CoinPath, WpkhHotSigner}, | ||
utils::now, | ||
}; | ||
|
||
pub enum Error { | ||
Unknown, | ||
NostrClient(crate::nostr::client::Error), | ||
SerdeJson(serde_json::Error), | ||
Joinstr(crate::joinstr::Error), | ||
Signer(crate::signer::Error), | ||
Electrum(crate::electrum::Error), | ||
} | ||
|
||
impl From<crate::nostr::client::Error> for Error { | ||
fn from(value: crate::nostr::client::Error) -> Self { | ||
Self::NostrClient(value) | ||
} | ||
} | ||
|
||
impl From<crate::joinstr::Error> for Error { | ||
fn from(value: crate::joinstr::Error) -> Self { | ||
Self::Joinstr(value) | ||
} | ||
} | ||
|
||
impl From<crate::signer::Error> for Error { | ||
fn from(value: crate::signer::Error) -> Self { | ||
Self::Signer(value) | ||
} | ||
} | ||
|
||
impl From<crate::electrum::Error> for Error { | ||
fn from(value: crate::electrum::Error) -> Self { | ||
Self::Electrum(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), | ||
Error::Joinstr(e) => write!(f, "Joinstr error: {:?}", e), | ||
Error::Signer(e) => write!(f, "Signer error: {:?}", e), | ||
Error::Electrum(e) => write!(f, "Electrum error: {:?}", e), | ||
} | ||
} | ||
} | ||
|
||
pub struct PoolConfig { | ||
pub denomination: f64, | ||
pub fee: u32, | ||
pub max_duration: u64, | ||
pub peers: usize, | ||
pub network: Network, | ||
} | ||
|
||
pub struct PeerConfig { | ||
pub mnemonics: String, | ||
pub electrum_address: String, | ||
pub electrum_port: u16, | ||
pub input: String, | ||
pub output: String, | ||
pub relay: String, | ||
} | ||
|
||
/// List available coins | ||
// FIXME: this function is a ugly+ineficient hack, we should use | ||
// the electrum notification mechanism and let consumer poll our | ||
// static/cached state | ||
pub async fn list_coins( | ||
mnemonics: String, | ||
electrum_address: String, | ||
electrum_port: u16, | ||
range: (u32, u32), | ||
network: Network, | ||
) -> Result<Vec<Coin>, Error> { | ||
let mut signer = WpkhHotSigner::new_from_mnemonics(network, &mnemonics)?; | ||
let client = Client::new(&electrum_address, electrum_port)?; | ||
signer.set_client(client); | ||
|
||
for i in range.0..range.1 { | ||
let recv = CoinPath::new(0, i); | ||
let change = CoinPath::new(1, i); | ||
let _ = signer.get_coins_at(recv); | ||
let _ = signer.get_coins_at(change); | ||
} | ||
|
||
let coins = signer.list_coins().into_iter().map(|c| c.1).collect(); | ||
|
||
Ok(coins) | ||
} | ||
|
||
/// Initiate and participate to a coinjoin | ||
/// | ||
/// # Arguments | ||
/// * `config` - configuration of the pool to initiate | ||
/// * `peer` - information about the peer | ||
/// | ||
pub async fn initiate_coinjoin( | ||
config: PoolConfig, | ||
peer: PeerConfig, | ||
) -> Result<String /* Txid */, Error> { | ||
let relays = vec![peer.relay.clone()]; | ||
let (url, port) = (peer.electrum_address, peer.electrum_port); | ||
let mut initiator = Joinstr::new_initiator( | ||
Keys::generate(), | ||
&relays, | ||
(&url, port), | ||
config.network, | ||
"initiator", | ||
) | ||
.await? | ||
.denomination(config.denomination)? | ||
.fee(config.fee)? | ||
.simple_timeout(now() + config.max_duration)? | ||
.min_peers(config.peers)?; | ||
|
||
let mut signer = WpkhHotSigner::new_from_mnemonics(config.network, &peer.mnemonics)?; | ||
let client = Client::new(&url, port)?; | ||
signer.set_client(client); | ||
|
||
let addr: Address<NetworkUnchecked> = serde_json::from_str(&peer.output)?; | ||
let coin: Coin = serde_json::from_str(&peer.input)?; | ||
|
||
initiator.set_coin(coin)?; | ||
initiator.set_address(addr)?; | ||
|
||
initiator.start_coinjoin(None, Some(&signer)).await?; | ||
|
||
let txid = initiator | ||
.final_tx() | ||
.expect("coinjoin success") | ||
.compute_txid() | ||
.to_string(); | ||
|
||
Ok(txid) | ||
} | ||
|
||
/// 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 async fn join_coinjoin( | ||
pool: String, /* Pool */ | ||
peer: PeerConfig, | ||
) -> Result<String /* Txid */, Error> { | ||
let pool: Pool = serde_json::from_str(&pool)?; | ||
let relays = vec![peer.relay.clone()]; | ||
let (url, port) = (peer.electrum_address, peer.electrum_port); | ||
let addr: Address<NetworkUnchecked> = serde_json::from_str(&peer.output)?; | ||
let coin: Coin = serde_json::from_str(&peer.input)?; | ||
let mut joinstr_peer = Joinstr::new_peer_with_electrum( | ||
&relays, | ||
&pool, | ||
(&url, port), | ||
coin, | ||
addr, | ||
pool.network, | ||
"peer", | ||
) | ||
.await?; | ||
|
||
let mut signer = WpkhHotSigner::new_from_mnemonics(pool.network, &peer.mnemonics)?; | ||
let client = Client::new(&url, port)?; | ||
signer.set_client(client); | ||
|
||
joinstr_peer.start_coinjoin(None, Some(&signer)).await?; | ||
|
||
let txid = joinstr_peer | ||
.final_tx() | ||
.expect("coinjoin success") | ||
.compute_txid() | ||
.to_string(); | ||
|
||
Ok(txid) | ||
} |
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
Oops, something went wrong.