From 64e668f99e83a2b321e78ee08739212a7c90e5f4 Mon Sep 17 00:00:00 2001 From: "Jamil Lambert, PhD" Date: Wed, 28 Aug 2024 12:00:13 +0100 Subject: [PATCH] Change from_str(s) to parse::() in Examples `s.parse` is more idiomatic and produces more helpful error messages. This has been changed in examples. --- bitcoin/examples/ecdsa-psbt-simple.rs | 6 +++--- bitcoin/examples/ecdsa-psbt.rs | 10 +++++----- bitcoin/examples/taproot-psbt-simple.rs | 8 ++++---- bitcoin/examples/taproot-psbt.rs | 26 ++++++++++++------------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/bitcoin/examples/ecdsa-psbt-simple.rs b/bitcoin/examples/ecdsa-psbt-simple.rs index 8d3d9977b..0a4c0f436 100644 --- a/bitcoin/examples/ecdsa-psbt-simple.rs +++ b/bitcoin/examples/ecdsa-psbt-simple.rs @@ -184,13 +184,13 @@ fn main() { // information to the PSBT. let ty = EcdsaSighashType::All.into(); let derivation_paths = [ - DerivationPath::from_str("m/84'/0'/0'/0/0").expect("valid derivation path"), - DerivationPath::from_str("m/84'/0'/0'/1/0").expect("valid derivation path"), + "m/84'/0'/0'/0/0".parse::().expect("valid derivation path"), + "m/84'/0'/0'/1/0".parse::().expect("valid derivation path"), ]; let mut bip32_derivations = Vec::new(); for (idx, pk) in pk_inputs.iter().enumerate() { let mut map = BTreeMap::new(); - let fingerprint = Fingerprint::from_str(MASTER_FINGERPRINT).expect("valid fingerprint"); + let fingerprint = MASTER_FINGERPRINT.parse::().expect("valid fingerprint"); map.insert(pk.0, (fingerprint, derivation_paths[idx].clone())); bip32_derivations.push(map); } diff --git a/bitcoin/examples/ecdsa-psbt.rs b/bitcoin/examples/ecdsa-psbt.rs index 943e0dab9..c616ef258 100644 --- a/bitcoin/examples/ecdsa-psbt.rs +++ b/bitcoin/examples/ecdsa-psbt.rs @@ -112,7 +112,7 @@ impl ColdStorage { /// /// The newly created signer along with the data needed to configure a watch-only wallet. fn new(secp: &Secp256k1, xpriv: &str) -> Result { - let master_xpriv = Xpriv::from_str(xpriv)?; + let master_xpriv = xpriv.parse::()?; let master_xpub = Xpub::from_priv(secp, &master_xpriv); // Hardened children require secret data to derive. @@ -176,10 +176,10 @@ impl WatchOnly { /// Creates the PSBT, in BIP174 parlance this is the 'Creator'. fn create_psbt(&self, secp: &Secp256k1) -> Result { let to_address = Address::from_str(RECEIVE_ADDRESS)?.require_network(Network::Regtest)?; - let to_amount = Amount::from_str(OUTPUT_AMOUNT_BTC)?; + let to_amount = OUTPUT_AMOUNT_BTC.parse::()?; let (_, change_address, _) = self.change_address(secp)?; - let change_amount = Amount::from_str(CHANGE_AMOUNT_BTC)?; + let change_amount = CHANGE_AMOUNT_BTC.parse::()?; let tx = Transaction { version: transaction::Version::TWO, @@ -217,7 +217,7 @@ impl WatchOnly { map.insert(pk.0, (fingerprint, path)); input.bip32_derivation = map; - let ty = PsbtSighashType::from_str("SIGHASH_ALL")?; + let ty = "SIGHASH_ALL".parse::()?; input.sighash_type = Some(ty); psbt.inputs = vec![input]; @@ -276,7 +276,7 @@ fn input_derivation_path() -> Result { fn previous_output() -> TxOut { let script_pubkey = ScriptBuf::from_hex(INPUT_UTXO_SCRIPT_PUBKEY) .expect("failed to parse input utxo scriptPubkey"); - let amount = Amount::from_str(INPUT_UTXO_VALUE).expect("failed to parse input utxo value"); + let amount = INPUT_UTXO_VALUE.parse::().expect("failed to parse input utxo value"); TxOut { value: amount, script_pubkey } } diff --git a/bitcoin/examples/taproot-psbt-simple.rs b/bitcoin/examples/taproot-psbt-simple.rs index f827c5670..09d202fcf 100644 --- a/bitcoin/examples/taproot-psbt-simple.rs +++ b/bitcoin/examples/taproot-psbt-simple.rs @@ -151,13 +151,13 @@ fn main() { // Map of tap root X-only keys to origin info and leaf hashes contained in it. let origin_input_1 = get_tap_key_origin( pk_input_1, - Fingerprint::from_str(MASTER_FINGERPRINT).unwrap(), - DerivationPath::from_str("m/86'/0'/0'/0/0").unwrap(), + MASTER_FINGERPRINT.parse::().unwrap(), + "m/86'/0'/0'/0/0".parse::().unwrap(), ); let origin_input_2 = get_tap_key_origin( pk_input_2, - Fingerprint::from_str(MASTER_FINGERPRINT).unwrap(), - DerivationPath::from_str("m/86'/0'/0'/1/0").unwrap(), + MASTER_FINGERPRINT.parse::().unwrap(), + "m/86'/0'/0'/1/0".parse::().unwrap(), ); let origins = [origin_input_1, origin_input_2]; diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index be43816c6..486211ab1 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -116,7 +116,7 @@ fn main() -> Result<(), Box> { let tx_hex_string = encode::serialize_hex(&generate_bip86_key_spend_tx( &secp, // The master extended private key from the descriptor in step 4 - Xpriv::from_str(BENEFACTOR_XPRIV_STR)?, + BENEFACTOR_XPRIV_STR.parse::()?, // Set these fields with valid data for the UTXO from step 5 above UTXO_1, vec![ @@ -135,10 +135,10 @@ fn main() -> Result<(), Box> { println!("START EXAMPLE 2 - Script path spending of inheritance UTXO\n"); { - let beneficiary = BeneficiaryWallet::new(Xpriv::from_str(BENEFICIARY_XPRIV_STR)?)?; + let beneficiary = BeneficiaryWallet::new(BENEFICIARY_XPRIV_STR.parse::()?)?; let mut benefactor = BenefactorWallet::new( - Xpriv::from_str(BENEFACTOR_XPRIV_STR)?, + BENEFACTOR_XPRIV_STR.parse::()?, beneficiary.master_xpub(), )?; let (tx, psbt) = benefactor.create_inheritance_funding_tx( @@ -173,10 +173,10 @@ fn main() -> Result<(), Box> { println!("START EXAMPLE 3 - Key path spending of inheritance UTXO\n"); { - let beneficiary = BeneficiaryWallet::new(Xpriv::from_str(BENEFICIARY_XPRIV_STR)?)?; + let beneficiary = BeneficiaryWallet::new(BENEFICIARY_XPRIV_STR.parse::()?)?; let mut benefactor = BenefactorWallet::new( - Xpriv::from_str(BENEFACTOR_XPRIV_STR)?, + BENEFACTOR_XPRIV_STR.parse::()?, beneficiary.master_xpub(), )?; let (tx, _) = benefactor.create_inheritance_funding_tx( @@ -227,7 +227,7 @@ fn generate_bip86_key_spend_tx( outputs: Vec, ) -> Result> { let from_amount = input_utxo.amount_in_sats; - let input_pubkey = XOnlyPublicKey::from_str(input_utxo.pubkey)?; + let input_pubkey = input_utxo.pubkey.parse::()?; // CREATOR + UPDATER let tx1 = Transaction { @@ -249,8 +249,8 @@ fn generate_bip86_key_spend_tx( ( vec![], ( - Fingerprint::from_str(input_utxo.master_fingerprint)?, - DerivationPath::from_str(input_utxo.derivation_path)?, + input_utxo.master_fingerprint.parse::()?, + input_utxo.derivation_path.parse::()?, ), ), ); @@ -264,7 +264,7 @@ fn generate_bip86_key_spend_tx( tap_key_origins: origins, ..Default::default() }; - let ty = PsbtSighashType::from_str("SIGHASH_ALL")?; + let ty = "SIGHASH_ALL".parse::()?; input.sighash_type = Some(ty); input.tap_internal_key = Some(input_pubkey); psbt.inputs = vec![input]; @@ -391,7 +391,7 @@ impl BenefactorWallet { } // We use some other derivation path in this example for our inheritance protocol. The important thing is to ensure // that we use an unhardened path so we can make use of xpubs. - let derivation_path = DerivationPath::from_str(&format!("101/1/0/0/{}", self.next))?; + let derivation_path = format!("101/1/0/0/{}", self.next).parse::()?; let internal_keypair = self.master_xpriv.derive_priv(&self.secp, &derivation_path).to_keypair(&self.secp); let beneficiary_key = @@ -443,7 +443,7 @@ impl BenefactorWallet { internal_keypair.x_only_public_key().0, (vec![], (self.master_xpriv.fingerprint(&self.secp), derivation_path)), ); - let ty = PsbtSighashType::from_str("SIGHASH_ALL")?; + let ty = "SIGHASH_ALL".parse::()?; let mut tap_scripts = BTreeMap::new(); tap_scripts.insert( taproot_spend_info.control_block(&(script.clone(), LeafVersion::TapScript)).unwrap(), @@ -480,7 +480,7 @@ impl BenefactorWallet { // We use some other derivation path in this example for our inheritance protocol. The important thing is to ensure // that we use an unhardened path so we can make use of xpubs. let new_derivation_path = - DerivationPath::from_str(&format!("101/1/0/0/{}", self.next))?; + format!("101/1/0/0/{}", self.next).parse::()?; let new_internal_keypair = self .master_xpriv .derive_priv(&self.secp, &new_derivation_path) @@ -582,7 +582,7 @@ impl BenefactorWallet { beneficiary_key, (vec![leaf_hash], (self.beneficiary_xpub.fingerprint(), new_derivation_path)), ); - let ty = PsbtSighashType::from_str("SIGHASH_ALL")?; + let ty = "SIGHASH_ALL".parse::()?; let mut tap_scripts = BTreeMap::new(); tap_scripts.insert( taproot_spend_info