Skip to content

Commit

Permalink
Return the aggregate key in the will sign query
Browse files Browse the repository at this point in the history
  • Loading branch information
djordon committed Jan 10, 2025
1 parent fcebd40 commit a339f62
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions signer/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,13 @@ impl super::DbRead for SharedStore {
async fn will_sign_bitcoin_tx_sighash(
&self,
sighash: &model::SigHash,
) -> Result<Option<bool>, Error> {
) -> Result<Option<(bool, PublicKeyXOnly)>, Error> {
Ok(self
.lock()
.await
.bitcoin_sighashes
.get(sighash)
.map(|s| s.will_sign))
.map(|s| (s.will_sign, s.aggregate_key)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion signer/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub trait DbRead {
fn will_sign_bitcoin_tx_sighash(
&self,
sighash: &model::SigHash,
) -> impl Future<Output = Result<Option<bool>, Error>> + Send;
) -> impl Future<Output = Result<Option<(bool, PublicKeyXOnly)>, Error>> + Send;
}

/// Represents the ability to write data to the signer storage.
Expand Down
8 changes: 5 additions & 3 deletions signer/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1896,10 +1896,12 @@ impl super::DbRead for PgStore {
async fn will_sign_bitcoin_tx_sighash(
&self,
sighash: &model::SigHash,
) -> Result<Option<bool>, Error> {
sqlx::query_scalar::<_, bool>(
) -> Result<Option<(bool, PublicKeyXOnly)>, Error> {
sqlx::query_as::<_, (bool, PublicKeyXOnly)>(
r#"
SELECT will_sign
SELECT
will_sign
, x_only_public_key
FROM sbtc_signer.bitcoin_tx_sighashes
WHERE sighash = $1
"#,
Expand Down
21 changes: 9 additions & 12 deletions signer/src/transaction_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::ecdsa::SignEcdsa as _;
use crate::error::Error;
use crate::keys::PrivateKey;
use crate::keys::PublicKey;
use crate::keys::PublicKeyXOnly;
use crate::message;
use crate::message::BitcoinPreSignAck;
use crate::message::StacksTransactionSignRequest;
Expand Down Expand Up @@ -608,10 +609,10 @@ where

let db = self.context.get_storage();
let sig_hash = &request.message;
let validation_outcome = Self::validate_bitcoin_sign_request(&db, sig_hash).await;
let locking_aggregate_key = Self::validate_bitcoin_sign_request(&db, sig_hash).await;

let validation_status = match &validation_outcome {
Ok(()) => "success",
let validation_status = match &locking_aggregate_key {
Ok(_) => "success",
Err(Error::SigHashConversion(_)) => "improper-sighash",
Err(Error::UnknownSigHash(_)) => "unknown-sighash",
Err(Error::InvalidSigHash(_)) => "invalid-sighash",
Expand All @@ -627,13 +628,9 @@ where
.increment(1);

if !self.wsts_state_machines.contains_key(&msg.txid) {
let (maybe_aggregate_key, _) = self
.get_signer_set_and_aggregate_key(bitcoin_chain_tip)
.await?;

let state_machine = SignerStateMachine::load(
&db,
maybe_aggregate_key.ok_or(Error::NoDkgShares)?,
locking_aggregate_key?,
self.threshold,
self.signer_private_key,
)
Expand Down Expand Up @@ -721,17 +718,17 @@ where

/// Check whether we will sign the message, which is supposed to be a
/// bitcoin sighash
async fn validate_bitcoin_sign_request<D>(db: &D, message: &[u8]) -> Result<(), Error>
async fn validate_bitcoin_sign_request<D>(db: &D, msg: &[u8]) -> Result<PublicKeyXOnly, Error>
where
D: DbRead,
{
let sighash = TapSighash::from_slice(message)
let sighash = TapSighash::from_slice(msg)
.map_err(Error::SigHashConversion)?
.into();

match db.will_sign_bitcoin_tx_sighash(&sighash).await? {
Some(true) => Ok(()),
Some(false) => Err(Error::InvalidSigHash(sighash)),
Some((true, public_key)) => Ok(public_key),
Some((false, _)) => Err(Error::InvalidSigHash(sighash)),
None => Err(Error::UnknownSigHash(sighash)),
}
}
Expand Down
4 changes: 2 additions & 2 deletions signer/src/wsts_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ impl SignerStateMachine {
/// Create a state machine from loaded DKG shares for the given aggregate key
pub async fn load<S>(
storage: &S,
aggregate_key: PublicKey,
aggregate_key: PublicKeyXOnly,
threshold: u32,
signer_private_key: PrivateKey,
) -> Result<Self, error::Error>
where
S: storage::DbRead,
{
let encrypted_shares = storage
.get_encrypted_dkg_shares(&aggregate_key)
.get_encrypted_dkg_shares(aggregate_key)
.await?
.ok_or(error::Error::MissingDkgShares(aggregate_key.into()))?;

Expand Down
2 changes: 1 addition & 1 deletion signer/tests/integration/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2919,7 +2919,7 @@ async fn can_write_and_get_multiple_bitcoin_txs_sighashes() {
let results = join_all(withdrawal_outputs_futures).await;

for (output, result) in sighashes.iter().zip(results) {
let result = result.unwrap().unwrap();
let (result, _) = result.unwrap().unwrap();
assert_eq!(result, output.will_sign);
}
signer::testing::storage::drop_db(db).await;
Expand Down
4 changes: 2 additions & 2 deletions signer/tests/integration/transaction_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,14 @@ pub async fn assert_should_be_able_to_handle_sbtc_requests() {

// Check that the intentions to sign the requests sighashes
// are stored in the database
let will_sign = db
let (will_sign, _) = db
.will_sign_bitcoin_tx_sighash(&signer_digest.sighash.into())
.await
.expect("query to check if signer sighash is stored failed")
.expect("signer sighash not stored");

assert!(will_sign);
let will_sign = db
let (will_sign, _) = db
.will_sign_bitcoin_tx_sighash(&deposit_digest.sighash.into())
.await
.expect("query to check if deposit sighash is stored failed")
Expand Down

0 comments on commit a339f62

Please sign in to comment.