Skip to content

Commit 5eaa41b

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(wasm-utxo): move bitgo_psbt to fixed_script_wallet module
Move BitGo PSBT functionality from root module to fixed_script_wallet where it logically belongs. Update imports and paths across files to maintain functionality. Also move psbt_wallet_input.rs into the bitgo_psbt module. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent b05f397 commit 5eaa41b

File tree

11 files changed

+42
-44
lines changed

11 files changed

+42
-44
lines changed

packages/wasm-utxo/cli/src/parse/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bitcoin::consensus::Decodable;
33
use bitcoin::hashes::Hash;
44
use bitcoin::psbt::Psbt;
55
use bitcoin::{Network, ScriptBuf, Transaction};
6-
use wasm_utxo::bitgo_psbt::{
6+
use wasm_utxo::fixed_script_wallet::bitgo_psbt::{
77
p2tr_musig2_input::{Musig2PartialSig, Musig2Participants, Musig2PubNonce},
88
BitGoKeyValue, ProprietaryKeySubtype, BITGO,
99
};
@@ -56,7 +56,7 @@ fn musig2_participants_to_node(participants: &Musig2Participants) -> Node {
5656

5757
let mut participants_node = Node::new("participant_pub_keys", Primitive::U64(2));
5858
for (i, pub_key) in participants.participant_pub_keys.iter().enumerate() {
59-
let pub_key_vec: Vec<u8> = pub_key.to_bytes().to_vec();
59+
let pub_key_vec: Vec<u8> = pub_key.to_bytes().as_slice().to_vec();
6060
participants_node.add_child(Node::new(
6161
format!("participant_{}", i),
6262
Primitive::Buffer(pub_key_vec),

packages/wasm-utxo/src/bitgo_psbt/mod.rs renamed to packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ pub mod p2tr_musig2_input;
77
#[cfg(test)]
88
mod p2tr_musig2_input_utxolib;
99
mod propkv;
10+
mod psbt_wallet_input;
1011
mod sighash;
1112
mod zcash_psbt;
1213

13-
use crate::{bitgo_psbt::zcash_psbt::ZcashPsbt, networks::Network};
14-
14+
use crate::Network;
1515
use miniscript::bitcoin::{psbt::Psbt, secp256k1, CompressedPublicKey};
1616
pub use propkv::{BitGoKeyValue, ProprietaryKeySubtype, BITGO};
1717
pub use sighash::validate_sighash_type;
18+
use zcash_psbt::ZcashPsbt;
1819

1920
#[derive(Debug)]
2021
pub enum DeserializeError {

packages/wasm-utxo/src/bitgo_psbt/p2tr_musig2_input.rs renamed to packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/p2tr_musig2_input.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
//! key-values in PSBTs, following the format specified in:
55
//! https://gist.github.com/sanket1729/4b525c6049f4d9e034d27368c49f28a6
66
7-
use crate::bitgo_psbt::propkv::{find_kv, is_musig2_key, BitGoKeyValue};
8-
use crate::fixed_script_wallet::bitgo_musig::key_agg_p2tr_musig2;
9-
10-
use super::propkv::ProprietaryKeySubtype;
7+
use super::propkv::{find_kv, is_musig2_key, BitGoKeyValue, ProprietaryKeySubtype};
118
use crate::bitcoin::{key::UntweakedPublicKey, CompressedPublicKey};
9+
use crate::fixed_script_wallet::wallet_scripts::bitgo_musig::key_agg_p2tr_musig2;
1210
use miniscript::bitcoin::hashes::{hex, Hash};
1311
use miniscript::bitcoin::{
1412
bip32::{KeySource, Xpriv, Xpub},
@@ -17,6 +15,11 @@ use miniscript::bitcoin::{
1715
};
1816
use musig2::PubNonce;
1917

18+
#[cfg(test)]
19+
use super::BitGoPsbt;
20+
#[cfg(test)]
21+
use crate::fixed_script_wallet::test_utils::fixtures::XprvTriple;
22+
2023
pub type TapKeyOrigins = std::collections::BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>;
2124

2225
pub fn derive_xpriv_for_input_tap(
@@ -1030,9 +1033,9 @@ impl Musig2Input {
10301033
/// * `input_index` - Index of the MuSig2 input
10311034
#[cfg(test)]
10321035
pub fn assert_set_nonce_and_sign_musig2_keypath(
1033-
xpriv_triple: &crate::fixed_script_wallet::test_utils::fixtures::XprvTriple,
1034-
unsigned_bitgo_psbt: &mut crate::bitgo_psbt::BitGoPsbt,
1035-
halfsigned_bitgo_psbt: &crate::bitgo_psbt::BitGoPsbt,
1036+
xpriv_triple: &XprvTriple,
1037+
unsigned_bitgo_psbt: &mut BitGoPsbt,
1038+
halfsigned_bitgo_psbt: &BitGoPsbt,
10361039
input_index: usize,
10371040
) -> Result<(), String> {
10381041
// Test 1: Functional API (utxolib-compatible, fixture-validated)
@@ -1074,8 +1077,8 @@ pub fn assert_set_nonce_and_sign_musig2_keypath(
10741077
/// * `input_index` - Index of the MuSig2 input
10751078
#[cfg(test)]
10761079
pub fn assert_set_nonce_and_sign_musig2_keypath_state_machine(
1077-
xpriv_triple: &crate::fixed_script_wallet::test_utils::fixtures::XprvTriple,
1078-
unsigned_bitgo_psbt: &mut crate::bitgo_psbt::BitGoPsbt,
1080+
xpriv_triple: &XprvTriple,
1081+
unsigned_bitgo_psbt: &mut BitGoPsbt,
10791082
input_index: usize,
10801083
) -> Result<(), String> {
10811084
// Verify this is actually a MuSig2 input

packages/wasm-utxo/src/bitgo_psbt/p2tr_musig2_input_utxolib.rs renamed to packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/p2tr_musig2_input_utxolib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
//! For production use, prefer the State-Machine API in the parent module which
88
//! provides better protection against nonce reuse.
99
10-
use super::p2tr_musig2_input::{
11-
collect_prevouts, derive_xpriv_for_input_tap, derive_xpub_for_input_tap, Musig2Context,
12-
Musig2Error, Musig2Input, Musig2PubNonce,
10+
use super::{
11+
p2tr_musig2_input::{
12+
collect_prevouts, derive_xpriv_for_input_tap, derive_xpub_for_input_tap, Musig2Context,
13+
Musig2Error, Musig2Input, Musig2PubNonce,
14+
},
15+
BitGoPsbt,
1316
};
1417
use crate::bitcoin::{
1518
bip32::Xpriv,
@@ -19,7 +22,6 @@ use crate::bitcoin::{
1922
sighash::TapSighash,
2023
taproot::TapNodeHash,
2124
};
22-
use crate::bitgo_psbt::BitGoPsbt;
2325
use crate::fixed_script_wallet::RootWalletKeys;
2426
use musig2::{secp::Point, PubNonce};
2527

File renamed without changes.

packages/wasm-utxo/src/fixed_script_wallet/psbt_wallet_input.rs renamed to packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/psbt_wallet_input.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ pub fn validate_psbt_wallet_inputs(
376376
#[cfg(test)]
377377
pub mod test_helpers {
378378
use super::*;
379+
use crate::fixed_script_wallet::{RootWalletKeys, XpubTriple};
380+
use crate::test_utils::fixtures;
379381

380382
/// Checks if a specific input in a PSBT is protected by replay protection
381383
pub fn is_replay_protected_input(
@@ -545,13 +547,6 @@ pub mod test_helpers {
545547
}
546548
}
547549
}
548-
}
549-
550-
#[cfg(test)]
551-
mod tests {
552-
use super::*;
553-
use crate::fixed_script_wallet::{RootWalletKeys, XpubTriple};
554-
use crate::test_utils::fixtures;
555550

556551
fn get_reversed_wallet_keys(wallet_keys: &RootWalletKeys) -> RootWalletKeys {
557552
let triple: XpubTriple = wallet_keys
@@ -565,8 +560,6 @@ mod tests {
565560
}
566561

567562
crate::test_psbt_fixtures!(test_validate_psbt_wallet_inputs, network, format, {
568-
use crate::fixed_script_wallet::psbt_wallet_input::test_helpers::*;
569-
570563
let replay_protection = ReplayProtection::new(vec![
571564
ScriptBuf::from_hex("a91420b37094d82a513451ff0ccd9db23aba05bc5ef387")
572565
.expect("Failed to parse replay protection output script"),

packages/wasm-utxo/src/bitgo_psbt/sighash.rs renamed to packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/sighash.rs

File renamed without changes.

packages/wasm-utxo/src/bitgo_psbt/zcash_psbt.rs renamed to packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/zcash_psbt.rs

File renamed without changes.

packages/wasm-utxo/src/fixed_script_wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// This module contains code for the BitGo Fixed Script Wallets.
22
/// These are not based on descriptors.
3-
pub mod psbt_wallet_input;
3+
pub mod bitgo_psbt;
44
mod wallet_keys;
55
pub mod wallet_scripts;
66

packages/wasm-utxo/src/fixed_script_wallet/test_utils/fixtures.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@
4141
use std::str::FromStr;
4242

4343
use crate::{
44-
bitcoin::bip32::Xpriv, bitgo_psbt::p2tr_musig2_input, fixed_script_wallet::RootWalletKeys,
44+
bitcoin::bip32::Xpriv,
45+
fixed_script_wallet::{
46+
bitgo_psbt::{p2tr_musig2_input, validate_sighash_type, BitGoPsbt},
47+
RootWalletKeys,
48+
},
49+
Network,
4550
};
4651
use miniscript::bitcoin::bip32::Xpub;
4752
use serde::{Deserialize, Serialize};
4853

49-
use crate::Network;
50-
5154
#[derive(Debug, Clone, PartialEq, Eq)]
5255
pub struct XprvTriple([Xpriv; 3]);
5356

@@ -205,7 +208,7 @@ impl Musig2Participants {
205208
}
206209

207210
for (i, parsed_key) in parsed.participant_pub_keys.iter().enumerate() {
208-
let parsed_key_hex = hex::encode(parsed_key.to_bytes());
211+
let parsed_key_hex = hex::encode(parsed_key.to_bytes().as_slice());
209212
assert_hex_eq(
210213
&parsed_key_hex,
211214
&self.participant_pub_keys[i],
@@ -659,11 +662,8 @@ impl PsbtFixture {
659662
Ok(BASE64_STANDARD.decode(&self.psbt_base64)?)
660663
}
661664

662-
pub fn to_bitgo_psbt(
663-
&self,
664-
network: Network,
665-
) -> Result<crate::bitgo_psbt::BitGoPsbt, Box<dyn std::error::Error>> {
666-
let psbt = crate::bitgo_psbt::BitGoPsbt::deserialize(&self.to_psbt_bytes()?, network)?;
665+
pub fn to_bitgo_psbt(&self, network: Network) -> Result<BitGoPsbt, Box<dyn std::error::Error>> {
666+
let psbt = BitGoPsbt::deserialize(&self.to_psbt_bytes()?, network)?;
667667
Ok(psbt)
668668
}
669669

@@ -880,8 +880,8 @@ pub fn assert_hex_eq(generated: &str, expected: &str, field_name: &str) -> Resul
880880
}
881881

882882
/// Validates sighash type for the given network
883-
fn validate_sighash_type(sighash_type: u32, network: Network) -> Result<(), String> {
884-
crate::bitgo_psbt::validate_sighash_type(sighash_type, network)
883+
fn validate_sighash_type_fixture(sighash_type: u32, network: Network) -> Result<(), String> {
884+
validate_sighash_type(sighash_type, network)
885885
}
886886

887887
/// Validates output script from witness UTXO against generated script
@@ -926,7 +926,7 @@ impl P2shInput {
926926
let redeem_script_hex = scripts.redeem_script.to_hex_string();
927927
assert_hex_eq(&redeem_script_hex, &self.redeem_script, "Redeem script")?;
928928

929-
validate_sighash_type(self.sighash_type, network)
929+
validate_sighash_type_fixture(self.sighash_type, network)
930930
}
931931
}
932932

@@ -950,7 +950,7 @@ impl P2shP2wshInput {
950950
let witness_script_hex = scripts.witness_script.to_hex_string();
951951
assert_hex_eq(&witness_script_hex, &self.witness_script, "Witness script")?;
952952

953-
validate_sighash_type(self.sighash_type, network)
953+
validate_sighash_type_fixture(self.sighash_type, network)
954954
}
955955
}
956956

@@ -970,7 +970,7 @@ impl P2wshInput {
970970
let witness_script_hex = scripts.witness_script.to_hex_string();
971971
assert_hex_eq(&witness_script_hex, &self.witness_script, "Witness script")?;
972972

973-
validate_sighash_type(self.sighash_type, network)
973+
validate_sighash_type_fixture(self.sighash_type, network)
974974
}
975975
}
976976

@@ -1017,7 +1017,7 @@ impl P2trScriptPathInput {
10171017
}
10181018
}
10191019

1020-
validate_sighash_type(self.sighash_type, network)
1020+
validate_sighash_type_fixture(self.sighash_type, network)
10211021
}
10221022

10231023
/// Validates that the generated WalletScripts matches this fixture
@@ -1052,7 +1052,7 @@ impl P2trMusig2KeyPathInput {
10521052
let merkle_root_hex = hex::encode(merkle_root_bytes);
10531053
assert_hex_eq(&merkle_root_hex, &self.tap_merkle_root, "Merkle root")?;
10541054

1055-
validate_sighash_type(self.sighash_type, network)
1055+
validate_sighash_type_fixture(self.sighash_type, network)
10561056
}
10571057

10581058
/// Validates that the generated WalletScripts matches this fixture

0 commit comments

Comments
 (0)