Skip to content

Commit

Permalink
chore: mock RotateKeysTransactions with consistent addresses (#833)
Browse files Browse the repository at this point in the history
* mock RotateKeysTransactions with consistent addresses

* randomize test aggregate_key
  • Loading branch information
Jiloc authored Nov 14, 2024
1 parent a93ab6d commit ec2002f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
19 changes: 16 additions & 3 deletions signer/src/testing/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ use bitcoin::TxOut;
use bitvec::array::BitArray;
use blockstack_lib::burnchains::Txid as StacksTxid;
use blockstack_lib::chainstate::{nakamoto, stacks};
use clarity::util::secp256k1::Secp256k1PublicKey;
use fake::Fake;
use rand::seq::IteratorRandom as _;
use rand::Rng;
use sbtc::deposits::DepositScriptInputs;
use sbtc::deposits::ReclaimScriptInputs;
use secp256k1::ecdsa::RecoverableSignature;
use secp256k1::SECP256K1;
use stacks_common::address::AddressHashMode;
use stacks_common::address::C32_ADDRESS_VERSION_TESTNET_MULTISIG;
use stacks_common::types::chainstate::StacksAddress;

use crate::keys::PrivateKey;
Expand Down Expand Up @@ -362,14 +365,24 @@ impl fake::Dummy<SignerSetConfig> for RotateKeysTransaction {
let signer_set: Vec<PublicKey> = std::iter::repeat_with(|| fake::Faker.fake_with_rng(rng))
.take(config.num_keys as usize)
.collect();
let aggregate_key = PublicKey::combine_keys(signer_set.iter()).unwrap();

let address = StacksPrincipal::from(clarity::vm::types::PrincipalData::from(
StacksAddress::p2pkh(false, &aggregate_key.into()),
StacksAddress::from_public_keys(
C32_ADDRESS_VERSION_TESTNET_MULTISIG,
&AddressHashMode::SerializeP2SH,
config.signatures_required as usize,
&signer_set
.iter()
.map(Secp256k1PublicKey::from)
.collect::<Vec<_>>(),
)
.expect("failed to create StacksAddress"),
));

RotateKeysTransaction {
txid: fake::Faker.fake_with_rng(rng),
address,
aggregate_key,
aggregate_key: fake::Faker.fake_with_rng(rng),
signer_set,
signatures_required: config.signatures_required,
}
Expand Down
44 changes: 28 additions & 16 deletions signer/src/testing/wsts.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
//! Test utilities for running a wsts signer and coordinator.
use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::time::Duration;

use clarity::util::secp256k1::Secp256k1PublicKey;
use clarity::vm::types::PrincipalData;
use fake::Fake;
use stacks_common::address::AddressHashMode;
use stacks_common::address::C32_ADDRESS_VERSION_TESTNET_MULTISIG;
use stacks_common::types::chainstate::StacksAddress;
use wsts::net::SignatureType;
use wsts::state_machine::coordinator;
use wsts::state_machine::coordinator::frost;
use wsts::state_machine::coordinator::Coordinator as _;
use wsts::state_machine::StateMachine as _;

use crate::ecdsa::SignEcdsa as _;
use crate::keys::PrivateKey;
use crate::keys::PublicKey;
use crate::message;
use crate::network;
use crate::network::MessageTransfer as _;
use crate::storage;
use crate::storage::model;
use crate::storage::model::EncryptedDkgShares;
use crate::storage::model::StacksPrincipal;
use crate::wsts_state_machine;

use fake::Fake;
use wsts::state_machine::coordinator;
use wsts::state_machine::coordinator::frost;

use crate::ecdsa::SignEcdsa as _;
use crate::network::MessageTransfer as _;

use stacks_common::types::chainstate::StacksAddress;
use wsts::net::SignatureType;
use wsts::state_machine::coordinator::Coordinator as _;
use wsts::state_machine::StateMachine as _;

/// Signer info
#[derive(Debug, Clone)]
pub struct SignerInfo {
Expand Down Expand Up @@ -547,10 +550,19 @@ impl SignerSet {
tx_type: model::TransactionType::RotateKeys,
block_hash: stacks_chain_tip.block_hash.to_bytes(),
};
let address = StacksPrincipal::from(clarity::vm::types::PrincipalData::from(
StacksAddress::p2pkh(false, &shares.aggregate_key.into()),
let address = StacksPrincipal::from(PrincipalData::from(
StacksAddress::from_public_keys(
C32_ADDRESS_VERSION_TESTNET_MULTISIG,
&AddressHashMode::SerializeP2SH,
self.signers.len(),
&self
.signer_keys()
.iter()
.map(Secp256k1PublicKey::from)
.collect::<Vec<_>>(),
)
.expect("failed to create StacksAddress"),
));

let rotate_keys_tx = model::RotateKeysTransaction {
aggregate_key: shares.aggregate_key,
address,
Expand Down
2 changes: 1 addition & 1 deletion signer/tests/integration/rotate_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl TestRotateKeySetup {

let aggregate_key: PublicKey = self.aggregate_key();
let address = StacksPrincipal::from(clarity::vm::types::PrincipalData::from(
StacksAddress::p2pkh(false, &aggregate_key.into()),
self.wallet.address().clone(),
));
let rotate_key_tx = RotateKeysTransaction {
address: address,
Expand Down

0 comments on commit ec2002f

Please sign in to comment.