Skip to content

Commit

Permalink
verifier_rpc: Implement set_watchtower.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Dec 10, 2024
1 parent cca72ff commit 7edb9f9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
10 changes: 6 additions & 4 deletions core/src/database/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -955,7 +957,7 @@ impl Database {
watchtower_id: u32,
) -> Result<Vec<winternitz::PublicKey>, 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);

Expand Down
31 changes: 29 additions & 2 deletions core/src/rpc/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -145,9 +148,33 @@ impl ClementineVerifier for Verifier {
#[allow(clippy::blocks_in_conditions)]
async fn set_watchtower(
&self,
_request: Request<WatchtowerParams>,
request: Request<WatchtowerParams>,
) -> Result<Response<Empty>, 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).unwrap();
let public_key: winternitz::PublicKey = borsh::from_slice(&wpk).unwrap();

WinternitzPublicKey {
public_key,
parameters: winternitz::Parameters::new(0, 4),
}
})
.collect::<Vec<_>>();

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))]
Expand Down
2 changes: 1 addition & 1 deletion scripts/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 7edb9f9

Please sign in to comment.