Skip to content
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

Lightning & Nostr transport v2 #71

Merged
merged 6 commits into from
Jan 3, 2025
Merged
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
15 changes: 14 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ jobs:
override: true
- name: Check format
run: cargo fmt --check
check:
name: check
runs-on: ubuntu-latest
steps:
- name: Install Protoc
run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: cargo check
run: cargo check --all-features
integration_tests_prepare:
runs-on: ubuntu-latest
timeout-minutes: 30
Expand All @@ -57,7 +70,7 @@ jobs:
key: test-cache-${{ github.run_id }}-${{ github.run_number }}
- uses: actions/checkout@v4
- id: set-matrix
run: cargo test --no-run && echo "::set-output name=matrix::$(testconfig/scripts/get_test_list.sh manager_execution manager_tests contract_updater)"
run: cargo test --no-run --all-features && echo "::set-output name=matrix::$(testconfig/scripts/get_test_list.sh manager_execution manager_tests contract_updater)"
integration_tests:
name: integration tests
needs: integration_tests_prepare
Expand Down
6 changes: 3 additions & 3 deletions ddk-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ repository = "https://github.com/bennyhodl/dlcdevkit"
edition = "2021"

[dependencies]
ddk = { path = "../ddk", features = ["lightning", "marketplace", "sled", "kormir"] }
ddk-manager = { path = "../ddk-manager", features = ["use-serde"] }
ddk-payouts = { path = "../payouts" }
ddk = { version = "0.0.13", path = "../ddk", features = ["lightning", "marketplace", "sled", "kormir"] }
ddk-manager = { version = "0.7.2", path = "../ddk-manager", features = ["use-serde"] }
ddk-payouts = { version = "0.0.13", path = "../payouts" }

dlc = { version = "0.7.1", features = ["use-serde"] }
dlc-messages = { version = "0.7.1", features = [ "use-serde"] }
Expand Down
2 changes: 2 additions & 0 deletions ddk-node/src/cli_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub enum WalletCommand {
/// Fee rate in sats/vbyte
fee_rate: u64,
},
#[command(about = "Sync the on-chain wallet.")]
Sync,
}

#[derive(Clone, Debug, Subcommand)]
Expand Down
14 changes: 7 additions & 7 deletions ddk-node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ddkrpc::{
AcceptOfferRequest, ConnectRequest, GetWalletTransactionsRequest, InfoRequest,
ListContractsRequest, ListOffersRequest, ListOraclesRequest, ListPeersRequest,
ListUtxosRequest, NewAddressRequest, OracleAnnouncementsRequest, SendOfferRequest, SendRequest,
WalletBalanceRequest,
WalletBalanceRequest, WalletSyncRequest,
};
use anyhow::anyhow;
use bitcoin::Transaction;
Expand Down Expand Up @@ -172,8 +172,6 @@ pub async fn cli_command(
})
.await?
.into_inner();
let offer_hex = hex::encode(&offer.offer_dlc);
println!("{}", offer_hex);
let offer_dlc: OfferDlc = serde_json::from_slice(&offer.offer_dlc)?;
let offer = serde_json::to_string_pretty(&offer_dlc)?;
print!("{}", offer);
Expand All @@ -195,8 +193,6 @@ pub async fn cli_command(
.await?
.into_inner();
let accept_dlc: AcceptDlc = serde_json::from_slice(&accept.accept_dlc)?;
let accept_hex = hex::encode(&accept.accept_dlc);
println!("{}", accept_hex);
let accept_dlc = serde_json::to_string_pretty(&accept_dlc)?;
print!("{}", accept_dlc)
}
Expand Down Expand Up @@ -230,7 +226,7 @@ pub async fn cli_command(
.await?
.into_inner();
let pretty_string = serde_json::to_string_pretty(&address)?;
println!("{}", pretty_string);
print!("{}", pretty_string);
}
WalletCommand::Transactions => {
let transactions = client
Expand Down Expand Up @@ -273,6 +269,10 @@ pub async fn cli_command(
.into_inner();
print!("{}", serde_json::to_string_pretty(&txid)?)
}
WalletCommand::Sync => {
let _ = client.wallet_sync(WalletSyncRequest {}).await?.into_inner();
println!("Wallet synced.")
}
},
CliCommand::Oracle(command) => match command {
OracleCommand::Announcements => {
Expand Down Expand Up @@ -303,7 +303,7 @@ pub async fn cli_command(
host: parts[1].to_string(),
})
.await?;
println!("Connected to {}", parts[0])
print!("Connected to {}", parts[0])
}
}

Expand Down
81 changes: 81 additions & 0 deletions ddk-node/src/ddkrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ pub struct OracleAnnouncementsResponse {
#[prost(bytes = "vec", repeated, tag = "1")]
pub announcements: ::prost::alloc::vec::Vec<::prost::alloc::vec::Vec<u8>>,
}
#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct WalletSyncRequest {}
#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct WalletSyncResponse {}
/// Generated client implementations.
pub mod ddk_rpc_client {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
Expand Down Expand Up @@ -414,6 +422,28 @@ pub mod ddk_rpc_client {
.insert(GrpcMethod::new("ddkrpc.DdkRpc", "WalletBalance"));
self.inner.unary(req, path, codec).await
}
pub async fn wallet_sync(
&mut self,
request: impl tonic::IntoRequest<super::WalletSyncRequest>,
) -> std::result::Result<
tonic::Response<super::WalletSyncResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static("/ddkrpc.DdkRpc/WalletSync");
let mut req = request.into_request();
req.extensions_mut().insert(GrpcMethod::new("ddkrpc.DdkRpc", "WalletSync"));
self.inner.unary(req, path, codec).await
}
pub async fn get_wallet_transactions(
&mut self,
request: impl tonic::IntoRequest<super::GetWalletTransactionsRequest>,
Expand Down Expand Up @@ -648,6 +678,13 @@ pub mod ddk_rpc_server {
tonic::Response<super::WalletBalanceResponse>,
tonic::Status,
>;
async fn wallet_sync(
&self,
request: tonic::Request<super::WalletSyncRequest>,
) -> std::result::Result<
tonic::Response<super::WalletSyncResponse>,
tonic::Status,
>;
async fn get_wallet_transactions(
&self,
request: tonic::Request<super::GetWalletTransactionsRequest>,
Expand Down Expand Up @@ -1046,6 +1083,50 @@ pub mod ddk_rpc_server {
};
Box::pin(fut)
}
"/ddkrpc.DdkRpc/WalletSync" => {
#[allow(non_camel_case_types)]
struct WalletSyncSvc<T: DdkRpc>(pub Arc<T>);
impl<T: DdkRpc> tonic::server::UnaryService<super::WalletSyncRequest>
for WalletSyncSvc<T> {
type Response = super::WalletSyncResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::WalletSyncRequest>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as DdkRpc>::wallet_sync(&inner, request).await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = WalletSyncSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/ddkrpc.DdkRpc/GetWalletTransactions" => {
#[allow(non_camel_case_types)]
struct GetWalletTransactionsSvc<T: DdkRpc>(pub Arc<T>);
Expand Down
28 changes: 22 additions & 6 deletions ddk-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ddkrpc::{
ListOraclesResponse, ListPeersRequest, ListPeersResponse, ListUtxosRequest, ListUtxosResponse,
NewAddressRequest, NewAddressResponse, OracleAnnouncementsRequest, OracleAnnouncementsResponse,
Peer, SendOfferRequest, SendOfferResponse, SendRequest, SendResponse, WalletBalanceRequest,
WalletBalanceResponse,
WalletBalanceResponse, WalletSyncRequest, WalletSyncResponse,
};
use ddkrpc::{InfoRequest, InfoResponse};
use opts::NodeOpts;
Expand Down Expand Up @@ -90,13 +90,18 @@ impl DdkNode {
let ddk: Ddk = builder.finish().await?;

ddk.start()?;

let node = DdkNode::new(ddk);

Server::builder()
let node_stop = node.node.clone();
let server = Server::builder()
.add_service(DdkRpcServer::new(node))
.serve(opts.grpc_host.parse()?)
.await?;
.serve_with_shutdown(opts.grpc_host.parse()?, async {
tokio::signal::ctrl_c()
.await
.expect("Failed to install Ctrl+C signal handler");
let _ = node_stop.stop();
});

server.await?;

Ok(())
}
Expand Down Expand Up @@ -355,4 +360,15 @@ impl DdkRpc for DdkNode {
.collect();
Ok(Response::new(OracleAnnouncementsResponse { announcements }))
}

async fn wallet_sync(
&self,
_request: Request<WalletSyncRequest>,
) -> Result<Response<WalletSyncResponse>, Status> {
self.node
.wallet
.sync()
.map_err(|_| Status::new(Code::Aborted, "Did not sync wallet."))?;
Ok(Response::new(WalletSyncResponse {}))
}
}
4 changes: 4 additions & 0 deletions ddk-node/src/proto/ddkrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ service DdkRpc {
rpc ListOffers (ListOffersRequest) returns (ListOffersResponse);
rpc NewAddress (NewAddressRequest) returns (NewAddressResponse);
rpc WalletBalance (WalletBalanceRequest) returns (WalletBalanceResponse);
rpc WalletSync (WalletSyncRequest) returns (WalletSyncResponse);
rpc GetWalletTransactions (GetWalletTransactionsRequest) returns (GetWalletTransactionsResponse);
rpc ListUtxos (ListUtxosRequest) returns (ListUtxosResponse);
rpc ListPeers (ListPeersRequest) returns (ListPeersResponse);
Expand Down Expand Up @@ -124,3 +125,6 @@ message OracleAnnouncementsRequest {}
message OracleAnnouncementsResponse {
repeated bytes announcements = 1;
}

message WalletSyncRequest {}
message WalletSyncResponse {}
7 changes: 6 additions & 1 deletion ddk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,10 @@ bitcoincore-rpc = "0.19.0"

[[example]]
name = "lighnting"
path = "examples/ddk.rs"
path = "examples/lightning.rs"
required-features = ["lightning", "kormir", "sled"]

[[example]]
name = "nostr"
path = "examples/nostr.rs"
required-features = ["nostr"]
File renamed without changes.
33 changes: 33 additions & 0 deletions ddk/examples/nostr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::Result;
use bitcoin::key::rand::Fill;
use bitcoin::Network;
use ddk::builder::Builder;
use ddk::oracle::memory::MemoryOracle;
use ddk::storage::memory::MemoryStorage;
use ddk::transport::nostr::NostrDlc;
use std::sync::Arc;

type NostrDdk = ddk::DlcDevKit<NostrDlc, MemoryStorage, MemoryOracle>;

#[tokio::main]
async fn main() -> Result<()> {
let mut seed_bytes = [0u8; 32];
seed_bytes.try_fill(&mut bitcoin::key::rand::thread_rng())?;

let transport =
Arc::new(NostrDlc::new(&seed_bytes, "wss://nostr.dlcdevkit.com", Network::Regtest).await?);
let storage = Arc::new(MemoryStorage::new());
let oracle_client = Arc::new(MemoryOracle::default());

let mut builder = Builder::new();
builder.set_seed_bytes(seed_bytes);
builder.set_transport(transport.clone());
builder.set_storage(storage.clone());
builder.set_oracle(oracle_client.clone());

let ddk: NostrDdk = builder.finish().await?;

ddk.start().expect("couldn't start ddk");

loop {}
}
3 changes: 3 additions & 0 deletions ddk/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl<T: Transport, S: Storage, O: Oracle> Builder<T, S, O> {
let esplora_client = Arc::new(EsploraClient::new(&self.esplora_host, self.network)?);

let (sender, receiver) = unbounded::<DlcManagerMessage>();
let (stop_signal_sender, stop_signal) = tokio::sync::watch::channel(false);

let manager = Arc::new(
Manager::new(
Expand All @@ -177,6 +178,8 @@ impl<T: Transport, S: Storage, O: Oracle> Builder<T, S, O> {
storage,
oracle,
network: self.network,
stop_signal,
stop_signal_sender,
})
}
}
Loading
Loading