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 request-response #14

Merged
merged 4 commits into from
Jan 13, 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
36 changes: 30 additions & 6 deletions crates/db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use async_trait::async_trait;
use bitcoin::{OutPoint, XOnlyPublicKey, hashes::sha256};
use libp2p_identity::PeerId;
use libp2p_identity::{PeerId, secp256k1::PublicKey};
use musig2::{PartialSignature, PubNonce};
use serde::{Serialize, de::DeserializeOwned};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::DeserializeOwned};
use snafu::{ResultExt, Snafu};

use crate::states::PeerDepositState;
Expand All @@ -23,15 +23,37 @@ pub enum RepositoryError {
InvalidData { source: Box<dyn std::error::Error> },
}

pub mod public_key_serde {
use super::*;

pub fn serialize<S>(key: &PublicKey, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let key_bytes = key.to_bytes();
serializer.serialize_bytes(&key_bytes)
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<PublicKey, D::Error>
where
D: Deserializer<'de>,
{
let key_bytes: Vec<u8> = Deserialize::deserialize(deserializer)?;
PublicKey::try_from_bytes(&key_bytes).map_err(serde::de::Error::custom)
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct EntryWithSig<T> {
pub struct EntryWithSigAndKey<T> {
pub entry: T,
pub signature: Vec<u8>,
#[serde(with = "public_key_serde")]
pub key: PublicKey,
}

pub type PartialSignaturesEntry = EntryWithSig<Vec<PartialSignature>>;
pub type NoncesEntry = EntryWithSig<Vec<PubNonce>>;
pub type GenesisInfoEntry = EntryWithSig<(OutPoint, Vec<XOnlyPublicKey>)>;
pub type PartialSignaturesEntry = EntryWithSigAndKey<Vec<PartialSignature>>;
pub type NoncesEntry = EntryWithSigAndKey<Vec<PubNonce>>;
pub type GenesisInfoEntry = EntryWithSigAndKey<(OutPoint, Vec<XOnlyPublicKey>)>;

#[async_trait]
pub trait Repository: Send + Sync + 'static {
Expand Down Expand Up @@ -177,4 +199,6 @@ pub struct DepositSetupEntry<DSP: prost::Message + Default> {
#[serde(with = "prost_serde")]
pub payload: DSP,
pub signature: Vec<u8>,
#[serde(with = "public_key_serde")]
pub key: PublicKey,
}
11 changes: 10 additions & 1 deletion crates/db/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod tests {
OutPoint, XOnlyPublicKey,
hashes::{Hash, sha256},
};
use libp2p_identity::PeerId;
use libp2p_identity::{PeerId, secp256k1::PublicKey};
use musig2::{AggNonce, KeyAggContext, SecNonce, sign_partial};
use rand::thread_rng;
use secp256k1::{All, Keypair, Secp256k1};
Expand All @@ -46,6 +46,7 @@ mod tests {
let secp = Secp256k1::new();
let keypair = Keypair::new(&secp, &mut rand::thread_rng());
let message = b"message";
let libp2p_pkey = generate_random_pubkey();

let sec_nonce = SecNonce::generate(
[0u8; 32],
Expand All @@ -62,6 +63,7 @@ mod tests {
let nonces_entry = NoncesEntry {
entry: vec![pub_nonce.clone()],
signature: vec![0x8; 32],
key: libp2p_pkey.clone(),
};

db.set_pub_nonces(operator_id, tx_id, nonces_entry)
Expand All @@ -77,6 +79,7 @@ mod tests {
let sigs_entry = PartialSignaturesEntry {
entry: vec![signature],
signature: vec![],
key: libp2p_pkey.clone(),
};

db.set_partial_signatures(operator_id, tx_id, sigs_entry)
Expand All @@ -96,6 +99,7 @@ mod tests {
let entry = GenesisInfoEntry {
entry: (outpoint, checkpoint_pubkeys.clone()),
signature: vec![],
key: libp2p_pkey.clone(),
};

db.set_genesis_info(operator_id, entry).await.unwrap();
Expand Down Expand Up @@ -123,4 +127,9 @@ mod tests {
let (xonly, _parity) = pubkey.x_only_public_key();
xonly
}

fn generate_random_pubkey() -> PublicKey {
let kp = libp2p_identity::secp256k1::Keypair::generate();
kp.public().clone()
}
}
Loading
Loading