Skip to content

Commit

Permalink
refactor: use Musig2 to aggregate public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Feb 15, 2024
1 parent 87b0c78 commit 288691c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ reqwest = { version = "0.11.24", features = ["json"] }
serde_json = "1.0.113"
base64 = "0.21.7"
serde = { version = "1.0.196", features = ["derive"] }
elements = { version = "0.24.1", features = ["serde"] }
elements = { git = "https://github.com/michael1011/rust-elements", branch = "master", features = ["serde"] }
tower-http = { version = "0.5.1", features = ["cors"] }
hex = { version = "0.4.3", features = ["alloc", "serde", "std"] }
serde_with = "3.6.1"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ With these values in the request body:

```JSON
{
"internalKey": "<aggregated public key of the swap>",
"claimPublicKey": "<public key of the user>",
"refundPublicKey": "<public key of Boltz>",
"preimage": "<preimage of the swap>",
"blindingKey": "<blinding key of the lockup address of the swap>",
"address": "<address to which the covenant should be claimed>",
Expand Down
37 changes: 32 additions & 5 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{Extension, Json};
use elements::hashes::Hash;
use elements::secp256k1_zkp::SecretKey;
use elements::secp256k1_zkp::{MusigKeyAggCache, PublicKey, SecretKey};
use elements::{hashes, Address};
use log::debug;
use serde::{Deserialize, Serialize};
Expand All @@ -28,8 +28,12 @@ struct ErrorResponse {
#[derive(Deserialize)]
pub struct CovenantClaimRequest {
#[serde(with = "hex::serde")]
#[serde(rename = "internalKey")]
pub internal_key: Vec<u8>,
#[serde(rename = "claimPublicKey")]
pub claim_public_key: Vec<u8>,

#[serde(with = "hex::serde")]
#[serde(rename = "refundPublicKey")]
pub refund_public_key: Vec<u8>,

#[serde(with = "hex::serde")]
pub preimage: Vec<u8>,
Expand Down Expand Up @@ -102,6 +106,29 @@ pub async fn post_covenant_claim(
}
};

let aggregate = MusigKeyAggCache::new(
&SwapTree::secp(),
&[
match PublicKey::from_slice(body.refund_public_key.as_ref()) {
Ok(res) => res,
Err(err) => {
return CovenantClaimResponse::Error(ErrorResponse {
error: format!("could not parse refundPublicKey: {}", err.to_string()),
})
}
},
match PublicKey::from_slice(body.claim_public_key.as_ref()) {
Ok(res) => res,
Err(err) => {
return CovenantClaimResponse::Error(ErrorResponse {
error: format!("could not parse claimPublicKey: {}", err.to_string()),
})
}
},
],
);
let internal_key = Vec::from(aggregate.agg_pk().serialize());

let preimage_hash: hashes::hash160::Hash = Hash::hash(body.preimage.clone().as_ref());
if Vec::from(preimage_hash.as_byte_array()) != covenant_details.preimage_hash {
return CovenantClaimResponse::Error(ErrorResponse {
Expand All @@ -115,7 +142,7 @@ pub async fn post_covenant_claim(
preimage: body.preimage,
blinding_key: blinding_key.unwrap(),
swap_tree: json!(body.tree).to_string(),
internal_key: body.internal_key.clone(),
internal_key: internal_key.clone(),
status: PendingCovenantStatus::Pending.to_int(),
address: elements::pset::serialize::Serialize::serialize(
&address_script.script_pubkey(),
Expand All @@ -124,7 +151,7 @@ pub async fn post_covenant_claim(
&body
.tree
.clone()
.address(body.internal_key, &state.address_params)
.address(internal_key, &state.address_params)
.script_pubkey(),
),
tx_id: None,
Expand Down

0 comments on commit 288691c

Please sign in to comment.