From fbb4a59457e51fa0a6c75835c9a269b3093e903b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Tue, 10 Dec 2024 17:37:06 +0300 Subject: [PATCH] verifier_rpc: Implement set_watchtower. --- core/src/database/common.rs | 10 ++++++---- core/src/rpc/verifier.rs | 31 +++++++++++++++++++++++++++++-- scripts/schema.sql | 2 +- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/core/src/database/common.rs b/core/src/database/common.rs index dd314b8c..a5ee078c 100644 --- a/core/src/database/common.rs +++ b/core/src/database/common.rs @@ -935,9 +935,11 @@ impl Database { .collect(); let wpk = borsh::to_vec(&wpk)?; - let query = sqlx::query("INSERT INTO winternitz_public_keys (watchtower_id, winternitz_public_key) VALUES ($1, $2);") - .bind(watchtower_id as i64) - .bind(wpk); + let query = sqlx::query( + "INSERT INTO winternitz_public_keys (watchtower_id, public_keys) VALUES ($1, $2);", + ) + .bind(watchtower_id as i64) + .bind(wpk); match tx { Some(tx) => query.execute(&mut **tx).await, @@ -955,7 +957,7 @@ impl Database { watchtower_id: u32, ) -> Result, BridgeError> { let query = sqlx::query_as( - "SELECT winternitz_public_key FROM winternitz_public_keys WHERE watchtower_id = $1;", + "SELECT public_keys FROM winternitz_public_keys WHERE watchtower_id = $1;", ) .bind(watchtower_id as i64); diff --git a/core/src/rpc/verifier.rs b/core/src/rpc/verifier.rs index 286c63be..1570d5a3 100644 --- a/core/src/rpc/verifier.rs +++ b/core/src/rpc/verifier.rs @@ -12,6 +12,9 @@ use crate::{ ByteArray32, ByteArray66, EVMAddress, }; use bitcoin::{hashes::Hash, Amount, TapSighash, Txid}; +use bitvm::{ + bridge::transactions::signing_winternitz::WinternitzPublicKey, signatures::winternitz, +}; use futures::StreamExt; use secp256k1::{schnorr, Message}; use std::{pin::pin, str::FromStr}; @@ -145,9 +148,33 @@ impl ClementineVerifier for Verifier { #[allow(clippy::blocks_in_conditions)] async fn set_watchtower( &self, - _request: Request, + request: Request, ) -> Result, Status> { - todo!() + let watchtower_params = request.into_inner(); + + let winternitz_public_keys = watchtower_params + .winternitz_pubkeys + .into_iter() + .map(|wpk| { + let wpk = borsh::to_vec(&wpk.digit_pubkey)?; + let public_key: winternitz::PublicKey = borsh::from_slice(&wpk)?; + + Ok(WinternitzPublicKey { + public_key, + parameters: winternitz::Parameters::new(0, 4), + }) + }) + .collect::, BridgeError>>()?; + + self.db + .save_winternitz_public_key( + None, + watchtower_params.watchtower_id, + winternitz_public_keys, + ) + .await?; + + Ok(Response::new(Empty {})) } #[tracing::instrument(skip(self), err(level = tracing::Level::ERROR), ret(level = tracing::Level::TRACE))] diff --git a/scripts/schema.sql b/scripts/schema.sql index dce763d2..2d3e983d 100644 --- a/scripts/schema.sql +++ b/scripts/schema.sql @@ -133,7 +133,7 @@ create table if not exists header_chain_proofs ( -- Verifier table of Winternitz public keys for every operator and time_tx pair create table if not exists winternitz_public_keys ( watchtower_id int primary key not null, - winternitz_public_key bytea not null + public_keys bytea not null ); COMMIT;