Skip to content

Commit

Permalink
[multisign_account] Add assert to ensure threshold <= public keys len (
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar authored Sep 11, 2024
1 parent 99c3fb4 commit cb4ee71
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/rooch-types/src/bitcoin/multisign_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::address::BitcoinAddress;
use crate::addresses::BITCOIN_MOVE_ADDRESS;
use anyhow::Result;
use anyhow::{ensure, Result};
use bitcoin::bip32::{DerivationPath, Fingerprint};
use bitcoin::key::constants::SCHNORR_PUBLIC_KEY_SIZE;
use bitcoin::key::Secp256k1;
Expand Down Expand Up @@ -97,6 +97,11 @@ pub fn generate_multisign_address(
threshold: usize,
public_keys: Vec<Vec<u8>>,
) -> Result<BitcoinAddress> {
ensure!(
threshold > 0 && threshold <= public_keys.len(),
"Invalid threshold: {}",
threshold
);
let mut x_only_public_keys = public_keys
.into_iter()
.map(|pk| {
Expand Down Expand Up @@ -132,6 +137,7 @@ pub fn generate_multisign_address(

/// Create a multisig script, the caller should ensure the public keys are sorted
fn create_multisig_script(threshold: usize, public_keys: &Vec<XOnlyPublicKey>) -> ScriptBuf {
debug_assert!(threshold <= public_keys.len());
let mut builder = bitcoin::script::Builder::new();

for pubkey in public_keys {
Expand Down
1 change: 1 addition & 0 deletions frameworks/bitcoin-move/sources/multisign_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module bitcoin_move::multisign_account{
}

public fun generate_multisign_address(threshold: u64, public_keys: vector<vector<u8>>): BitcoinAddress{
assert!(vector::length(&public_keys) >= threshold, ErrorInvalidThreshold);
let to_x_only_public_keys = to_x_only_public_keys(public_keys);
//We need to sort the public keys to generate the same multisign address
//And we sort the x_only_public_keys, not the original public keys
Expand Down

0 comments on commit cb4ee71

Please sign in to comment.