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

feat: add verify inclusion proof to cli #95

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion src/bitcoind_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ pub enum BitcoindRequest {
CheckRpcCall,
GenerateTxInclusionProof { txid: String, respond_to: oneshot::Sender<Option<String>> },
CheckMerkleProof { request_id: u64, proof: Proof },
VerifyInclusionProof { inclusion_proof: InclusionProof, respond_to: oneshot::Sender<Option<String>> },
}

#[derive(Debug)]
pub enum BitcoindResult {
ProofValid { request_id: u64, valid: bool }
ProofValid { request_id: u64, valid: bool },
}

pub struct BitcoindClient {
Expand Down Expand Up @@ -159,6 +160,12 @@ impl BitcoindHandler {
_ => { validation_result.push(BitcoindResult::ProofValid { request_id, valid: false }); }
}
},
BitcoindRequest::VerifyInclusionProof { inclusion_proof, respond_to } => {
println!("[CIVKITD] - BITCOIND CLIENT: Received rpc call - Verify inclusion proof");

let res = BitcoindClient::verifytxoutproof(inclusion_proof).await;
respond_to.send(Some(res.to_string()));
}
_ => {},
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use adminctrl::admin_ctrl_client::AdminCtrlClient;
//TODO: simplify by using prefix
use adminctrl::{PingRequest, PongRequest, ShutdownRequest, ShutdownReply, SendNote, ReceivedNote, ListClientRequest, ListSubscriptionRequest, PeerConnectionRequest, DisconnectClientRequest, SendNotice, SendOffer, SendInvoice, ListDbEventsRequest, ListDbClientsRequest, ListDbClientsReply, CheckChainStateRequest, CheckChainStateReply, GenerateTxInclusionProofRequest, GenerateTxInclusionProofReply};
use adminctrl::{PingRequest, PongRequest, ShutdownRequest, ShutdownReply, SendNote, ReceivedNote, ListClientRequest, ListSubscriptionRequest, PeerConnectionRequest, DisconnectClientRequest, SendNotice, SendOffer, SendInvoice, ListDbEventsRequest, ListDbClientsRequest, ListDbClientsReply, CheckChainStateRequest, CheckChainStateReply, GenerateTxInclusionProofRequest, GenerateTxInclusionProofReply, VerifyInclusionProofRequest, VerifyInclusionProofReply};

use std::env;
use std::process;
Expand Down Expand Up @@ -83,7 +83,8 @@ enum Command {
/// Generate a merkle block (header + merkle branch) for the target txid
GenerateTxInclusionProof {
txid: String,
}
},
VerifyInclusionProof,
}

#[tokio::main]
Expand Down Expand Up @@ -222,6 +223,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("tx inclusion proof: {}", response.into_inner().merkle_block);
}
}
Command::VerifyInclusionProof => {
let request = tonic::Request::new(VerifyInclusionProofRequest {});

if let Ok(response) = client.verify_inclusion_proof(request).await {
println!("verified: {:?}", response.into_inner().verified);
}
}
}
Ok(())
}
2 changes: 2 additions & 0 deletions src/inclusionproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::mainstay::{get_proof};
use crate::config::Config;
use crate::nostr_db::{write_new_inclusion_proof_db};

#[derive(Debug, Clone)]
pub struct InclusionProof {
pub txid: Arc<Mutex<String>>,
pub commitment: Arc<Mutex<String>>,
Expand All @@ -25,6 +26,7 @@ pub struct InclusionProof {
pub config: Config,
}

#[derive(Debug)]
pub struct Ops {
pub append: bool,
pub commitment: String,
Expand Down
8 changes: 8 additions & 0 deletions src/proto/adminctrl.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ service AdminCtrl {
rpc ListDbClients (ListDbClientsRequest) returns (ListDbClientsReply);
rpc CheckChainState (CheckChainStateRequest) returns (CheckChainStateReply);
rpc GenerateTxInclusionProof (GenerateTxInclusionProofRequest) returns (GenerateTxInclusionProofReply);
rpc VerifyInclusionProof (VerifyInclusionProofRequest) returns (VerifyInclusionProofReply);
}

message PingRequest {
Expand Down Expand Up @@ -143,3 +144,10 @@ message GenerateTxInclusionProofRequest {
message GenerateTxInclusionProofReply {
string merkle_block = 1;
}

message VerifyInclusionProofRequest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good if we can move those methods on the civkitservice.proto interface. That way they can be reused by notaryd in the future.

}

message VerifyInclusionProofReply {
string verified = 1;
}
20 changes: 17 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ impl AdminCtrl for std::sync::Arc<ServiceManager> {
Ok(Response::new(adminctrl::GenerateTxInclusionProofReply { merkle_block: response } ))
} else { Ok(Response::new(adminctrl::GenerateTxInclusionProofReply { merkle_block: String::new() })) }
}

async fn verify_inclusion_proof(&self, request: Request<adminctrl::VerifyInclusionProofRequest>) -> Result<Response<adminctrl::VerifyInclusionProofReply>, Status> {

println!("[CIVKITD] - CONTROL: verify inclusion proof !");

let (send, recv) = oneshot::channel::<Option<String>>();
{
let mut send_bitcoind_request_lock = self.send_bitcoind_request.lock().unwrap();
send_bitcoind_request_lock.send(BitcoindRequest::VerifyInclusionProof { inclusion_proof: (*self.inclusion_proof).clone(), respond_to: send });
}
if let Some(response) = recv.await.expect("BitcoindHandler has been killed") {
Ok(Response::new(adminctrl::VerifyInclusionProofReply { verified: response } ))
} else { Ok(Response::new(adminctrl::VerifyInclusionProofReply { verified: false.to_string() })) }
}
}


Expand Down Expand Up @@ -435,12 +449,12 @@ fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

let mut bitcoind_handler = BitcoindHandler::new(config.clone(), receive_bitcoind_request, receive_bitcoind_request_handler, send_bitcoind_result_gateway);

// Main handler of services provision.
let service_manager_arc = Arc::new(ServiceManager::new(node_signer, anchor_manager, service_mngr_events_send, service_mngr_peer_send, manager_send_dbrequests, manager_send_bitcoind_request, send_events_gateway, config.clone()));

// We initialize the inclusion proof with txid, commitment and merkle proof as empty strings.
let mut inclusion_proof = InclusionProof::new("".to_string(), "".to_string(), "".to_string(), Vec::new(), config.clone());

// Main handler of services provision.
let service_manager_arc = Arc::new(ServiceManager::new(node_signer, anchor_manager, service_mngr_events_send, service_mngr_peer_send, manager_send_dbrequests, manager_send_bitcoind_request, send_events_gateway, Arc::new(inclusion_proof.clone()), config.clone()));

let addr = format!("[::1]:{}", cli.cli_port).parse()?;

let service_mngr_svc = Server::builder()
Expand Down
5 changes: 4 additions & 1 deletion src/servicemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use civkit::nodesigner::NodeSigner;
use civkit::peerhandler::PeerInfo;
use civkit::bitcoind_client::BitcoindRequest;
use civkit::config::Config;
use civkit::inclusionproof::InclusionProof;

// use lock from futures::lock
use std::sync::Mutex;
Expand All @@ -52,13 +53,14 @@ pub struct ServiceManager
pub send_events_gateway: Mutex<mpsc::UnboundedSender<ClientEvents>>,

our_service_pubkey: PublicKey,
pub inclusion_proof: Arc<InclusionProof>,
config: Config,
secp_ctx: Secp256k1<secp256k1::All>,
}

impl ServiceManager
{
pub fn new(node_signer: Arc<NodeSigner>, anchor_manager: Arc<AnchorManager>, board_events_send: mpsc::UnboundedSender<ClientEvents>, board_peers_send: mpsc::UnboundedSender<PeerInfo>, send_db_request: mpsc::UnboundedSender<DbRequest>, send_bitcoind_request: mpsc::UnboundedSender<BitcoindRequest>, send_gateway_events: mpsc::UnboundedSender<ClientEvents>, our_config: Config) -> Self {
pub fn new(node_signer: Arc<NodeSigner>, anchor_manager: Arc<AnchorManager>, board_events_send: mpsc::UnboundedSender<ClientEvents>, board_peers_send: mpsc::UnboundedSender<PeerInfo>, send_db_request: mpsc::UnboundedSender<DbRequest>, send_bitcoind_request: mpsc::UnboundedSender<BitcoindRequest>, send_gateway_events: mpsc::UnboundedSender<ClientEvents>, inclusion_proof: Arc<InclusionProof>, our_config: Config) -> Self {
let secp_ctx = Secp256k1::new();
let pubkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42;32]).unwrap());
ServiceManager {
Expand All @@ -71,6 +73,7 @@ impl ServiceManager
send_bitcoind_request: Mutex::new(send_bitcoind_request),
send_events_gateway: Mutex::new(send_gateway_events),
our_service_pubkey: pubkey,
inclusion_proof: inclusion_proof,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to integrate it like this, when the mainstay module is more encapsulated, it can be moved in notaryd.

config: our_config,
secp_ctx,
}
Expand Down
Loading