Skip to content
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
18 changes: 7 additions & 11 deletions crates/client/src/chain/goat_adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use crate::chain::goat_adaptor::IGateway::IGatewayInstance;
use alloy::primitives::TxHash;
use alloy::{
eips::BlockNumberOrTag,
network::{
Ethereum, EthereumWallet, NetworkWallet, TransactionBuilder, TxSigner, TxSignerSync,
eip2718::Encodable2718,
},
network::{Ethereum, EthereumWallet, NetworkWallet, eip2718::Encodable2718},
primitives::{Address as EvmAddress, Bytes, ChainId, FixedBytes, U256},
providers::{Provider, ProviderBuilder, RootProvider},
rpc::types::TransactionRequest,
signers::{Signer, local::PrivateKeySigner},
sol,
sol_types::SolEvent,
transports::http::{Client, Http, reqwest::Url},
};
use anyhow::format_err;
Expand Down Expand Up @@ -112,10 +108,10 @@ pub struct GoatInitConfig {
}
pub struct GoatAdaptor {
chain_id: ChainId,
gateway_address: EvmAddress,
gateway_creation_block: u64,
_gateway_address: EvmAddress,
_gateway_creation_block: u64,
provider: RootProvider<Http<Client>>,
to_block: Option<BlockNumberOrTag>,
_to_block: Option<BlockNumberOrTag>,
gate_way: IGatewayInstance<Http<Client>, RootProvider<Http<Client>>>,
signer: EthereumWallet,
}
Expand Down Expand Up @@ -555,10 +551,10 @@ impl GoatAdaptor {
};
let provider = ProviderBuilder::new().on_http(config.rpc_url);
Self {
gateway_address: config.gateway_address,
gateway_creation_block: config.gateway_creation_block,
_gateway_address: config.gateway_address,
_gateway_creation_block: config.gateway_creation_block,
provider: provider.clone(),
to_block: config.to_block,
_to_block: config.to_block,
gate_way: IGateway::new(config.gateway_address, provider),
signer: EthereumWallet::new(signer),
chain_id,
Expand Down
72 changes: 71 additions & 1 deletion crates/client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::chain::chain::Chain;
use crate::chain::chain_adaptor::{
ChainAdaptor, GoatNetwork, OperatorData, PeginData, get_chain_adaptor,
BitcoinTx, GoatNetwork, OperatorData, PeginData, get_chain_adaptor,
};
use crate::chain::goat_adaptor::GoatInitConfig;
use crate::esplora::get_esplora_url;
Expand Down Expand Up @@ -102,4 +102,74 @@ impl BitVM2Client {
pub async fn get_operator_data(&self, graph_id: Uuid) -> anyhow::Result<OperatorData> {
self.chain_service.adaptor.get_operator_data(graph_id).await
}

pub async fn finish_withdraw_happy_path(
&self,
graph_id: &Uuid,
tx: &bitcoin::Transaction,
) -> anyhow::Result<()> {
let raw_take1_tx = self.tx_reconstruct(tx);
let (_root, proof, _leaf, height, index) =
self.get_btc_tx_proof_info(&tx.compute_txid()).await?;
Ok(self
.chain_service
.adaptor
.finish_withdraw_happy_path(graph_id, &raw_take1_tx, height, &proof, index)
.await?)
}

pub async fn finish_withdraw_unhappy_path(
&self,
graph_id: &Uuid,
tx: &bitcoin::Transaction,
) -> anyhow::Result<()> {
let raw_take2_tx = self.tx_reconstruct(tx);
let (_root, proof, _leaf, height, index) =
self.get_btc_tx_proof_info(&tx.compute_txid()).await?;
Ok(self
.chain_service
.adaptor
.finish_withdraw_unhappy_path(graph_id, &raw_take2_tx, height, &proof, index)
.await?)
}

pub async fn finish_withdraw_disproved(
&self,
graph_id: &Uuid,
tx: &bitcoin::Transaction,
) -> anyhow::Result<()> {
let raw_disprove_tx = self.tx_reconstruct(tx);
let (_root, proof, _leaf, height, index) =
self.get_btc_tx_proof_info(&tx.compute_txid()).await?;
Ok(self
.chain_service
.adaptor
.finish_withdraw_disproved(graph_id, &raw_disprove_tx, height, &proof, index)
.await?)
}

pub async fn get_btc_tx_proof_info(
&self,
tx_id: &Txid,
) -> anyhow::Result<([u8; 32], Vec<[u8; 32]>, [u8; 32], u64, u64)> {
let (root, proof_info) = self.get_bitc_merkle_proof(tx_id).await?;
let proof: Vec<[u8; 32]> = proof_info.merkle.iter().map(|v| v.to_byte_array()).collect();
let leaf = tx_id.to_byte_array();
Ok((
root.to_byte_array(),
proof,
leaf,
proof_info.block_height as u64,
proof_info.pos as u64,
))
}

fn tx_reconstruct(&self, tx: &bitcoin::Transaction) -> BitcoinTx {
BitcoinTx {
version: tx.version.0 as u32,
lock_time: tx.lock_time.to_consensus_u32(),
input_vector: bitcoin::consensus::serialize(&tx.input),
output_vector: bitcoin::consensus::serialize(&tx.output),
}
}
}
2 changes: 1 addition & 1 deletion crates/identity/src/musig2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl MuSig2StateMachine {
// all key pair can be represented by identity::keypair
pub fn generate_musig2_key() -> secp256k1::Keypair {
let secp = Secp256k1::new();
let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
let (secret_key, _) = secp.generate_keypair(&mut rand::thread_rng());
secp256k1::Keypair::from_secret_key(&secp, &secret_key)
}

Expand Down
8 changes: 5 additions & 3 deletions crates/store/src/localdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl<'a> StorageProcessor<'a> {
graph_id: Uuid,
nonces: &[[String; COMMITTEE_PRE_SIGN_NUM]],
committee_pubkey: String,
partial_sigs: &[String],
partial_sigs: &[[String; COMMITTEE_PRE_SIGN_NUM]],
) -> anyhow::Result<()> {
let nonce_collect = sqlx::query_as!(
NonceCollect ,
Expand All @@ -597,7 +597,8 @@ impl<'a> StorageProcessor<'a> {
if let Some(nonce_collect) = nonce_collect {
let mut stored_nonces: Vec<[String; COMMITTEE_PRE_SIGN_NUM]> =
serde_json::from_str(&nonce_collect.nonces)?;
let mut stored_sigs: Vec<String> = serde_json::from_str(&nonce_collect.partial_sigs)?;
let mut stored_sigs: Vec<[String; COMMITTEE_PRE_SIGN_NUM]> =
serde_json::from_str(&nonce_collect.partial_sigs)?;
nonces.append(&mut stored_nonces);
partial_sigs.append(&mut stored_sigs);
created_at = nonce_collect.created_at;
Expand Down Expand Up @@ -633,7 +634,8 @@ impl<'a> StorageProcessor<'a> {
Some(nonce_collect) => {
let stored_nonces: Vec<[String; COMMITTEE_PRE_SIGN_NUM]> =
serde_json::from_str(&nonce_collect.nonces)?;
let stored_sigs: Vec<String> = serde_json::from_str(&nonce_collect.partial_sigs)?;
let stored_sigs: Vec<[String; COMMITTEE_PRE_SIGN_NUM]> =
serde_json::from_str(&nonce_collect.partial_sigs)?;
Ok(Some(NonceCollectMetaData {
instance_id,
graph_id,
Expand Down
2 changes: 1 addition & 1 deletion crates/store/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub struct NonceCollectMetaData {
pub graph_id: Uuid,
pub nonces: Vec<[String; COMMITTEE_PRE_SIGN_NUM]>,
pub committee_pubkey: String,
pub partial_sigs: Vec<String>,
pub partial_sigs: Vec<[String; COMMITTEE_PRE_SIGN_NUM]>,
pub updated_at: i64,
pub created_at: i64,
}
Loading
Loading