refactor: rename inner field names
This commit is contained in:
		
							parent
							
								
									c3e8469686
								
							
						
					
					
						commit
						7717ebb097
					
				| @ -17,7 +17,7 @@ use std::path::PathBuf; | ||||
| use std::sync::{Arc, Mutex, MutexGuard}; | ||||
| 
 | ||||
| pub(crate) struct Blockchain { | ||||
|     blockchain_mutex: Mutex<AnyBlockchain>, | ||||
|     inner_mutex: Mutex<AnyBlockchain>, | ||||
| } | ||||
| 
 | ||||
| impl Blockchain { | ||||
| @ -52,16 +52,16 @@ impl Blockchain { | ||||
|         }; | ||||
|         let blockchain = AnyBlockchain::from_config(&any_blockchain_config)?; | ||||
|         Ok(Self { | ||||
|             blockchain_mutex: Mutex::new(blockchain), | ||||
|             inner_mutex: Mutex::new(blockchain), | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn get_blockchain(&self) -> MutexGuard<AnyBlockchain> { | ||||
|         self.blockchain_mutex.lock().expect("blockchain") | ||||
|         self.inner_mutex.lock().expect("blockchain") | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn broadcast(&self, transaction: &Transaction) -> Result<(), BdkError> { | ||||
|         let tx = &transaction.internal; | ||||
|         let tx = &transaction.inner; | ||||
|         self.get_blockchain().broadcast(tx) | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -36,7 +36,7 @@ impl Descriptor { | ||||
|         keychain_kind: KeychainKind, | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = secret_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
| @ -61,7 +61,7 @@ impl Descriptor { | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap(); | ||||
|         let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = public_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorPublicKey::XPub(descriptor_x_key) => { | ||||
| @ -87,7 +87,7 @@ impl Descriptor { | ||||
|         keychain_kind: KeychainKind, | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = secret_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
| @ -112,7 +112,7 @@ impl Descriptor { | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap(); | ||||
|         let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = public_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorPublicKey::XPub(descriptor_x_key) => { | ||||
| @ -138,7 +138,7 @@ impl Descriptor { | ||||
|         keychain_kind: KeychainKind, | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = secret_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
| @ -163,7 +163,7 @@ impl Descriptor { | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap(); | ||||
|         let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = public_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorPublicKey::XPub(descriptor_x_key) => { | ||||
| @ -189,7 +189,7 @@ impl Descriptor { | ||||
|         keychain_kind: KeychainKind, | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = secret_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
| @ -214,7 +214,7 @@ impl Descriptor { | ||||
|         network: Network, | ||||
|     ) -> Self { | ||||
|         let fingerprint = Fingerprint::from_str(fingerprint.as_str()).unwrap(); | ||||
|         let derivable_key = public_key.descriptor_public_key_mutex.lock().unwrap(); | ||||
|         let derivable_key = public_key.inner_mutex.lock().unwrap(); | ||||
| 
 | ||||
|         match derivable_key.deref() { | ||||
|             BdkDescriptorPublicKey::XPub(descriptor_x_key) => { | ||||
|  | ||||
| @ -17,7 +17,7 @@ use std::sync::{Arc, Mutex}; | ||||
| /// Mnemonic phrases are a human-readable version of the private keys.
 | ||||
| /// Supported number of words are 12, 15, 18, 21 and 24.
 | ||||
| pub(crate) struct Mnemonic { | ||||
|     internal: BdkMnemonic, | ||||
|     inner: BdkMnemonic, | ||||
| } | ||||
| 
 | ||||
| impl Mnemonic { | ||||
| @ -26,13 +26,13 @@ impl Mnemonic { | ||||
|         let generated_key: GeneratedKey<_, BareCtx> = | ||||
|             BdkMnemonic::generate((word_count, Language::English)).unwrap(); | ||||
|         let mnemonic = BdkMnemonic::parse_in(Language::English, generated_key.to_string()).unwrap(); | ||||
|         Mnemonic { internal: mnemonic } | ||||
|         Mnemonic { inner: mnemonic } | ||||
|     } | ||||
| 
 | ||||
|     /// Parse a Mnemonic with given string
 | ||||
|     pub(crate) fn from_string(mnemonic: String) -> Result<Self, BdkError> { | ||||
|         BdkMnemonic::from_str(&mnemonic) | ||||
|             .map(|m| Mnemonic { internal: m }) | ||||
|             .map(|m| Mnemonic { inner: m }) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string())) | ||||
|     } | ||||
| 
 | ||||
| @ -40,25 +40,25 @@ impl Mnemonic { | ||||
|     /// Entropy must be a multiple of 32 bits (4 bytes) and 128-256 bits in length.
 | ||||
|     pub(crate) fn from_entropy(entropy: Vec<u8>) -> Result<Self, BdkError> { | ||||
|         BdkMnemonic::from_entropy(entropy.as_slice()) | ||||
|             .map(|m| Mnemonic { internal: m }) | ||||
|             .map(|m| Mnemonic { inner: m }) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string())) | ||||
|     } | ||||
| 
 | ||||
|     /// Returns Mnemonic as string
 | ||||
|     pub(crate) fn as_string(&self) -> String { | ||||
|         self.internal.to_string() | ||||
|         self.inner.to_string() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| pub(crate) struct DerivationPath { | ||||
|     derivation_path_mutex: Mutex<BdkDerivationPath>, | ||||
|     inner_mutex: Mutex<BdkDerivationPath>, | ||||
| } | ||||
| 
 | ||||
| impl DerivationPath { | ||||
|     pub(crate) fn new(path: String) -> Result<Self, BdkError> { | ||||
|         BdkDerivationPath::from_str(&path) | ||||
|             .map(|x| DerivationPath { | ||||
|                 derivation_path_mutex: Mutex::new(x), | ||||
|                 inner_mutex: Mutex::new(x), | ||||
|             }) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string())) | ||||
|     } | ||||
| @ -66,36 +66,36 @@ impl DerivationPath { | ||||
| 
 | ||||
| #[derive(Debug)] | ||||
| pub(crate) struct DescriptorSecretKey { | ||||
|     pub(crate) descriptor_secret_key_mutex: Mutex<BdkDescriptorSecretKey>, | ||||
|     pub(crate) inner_mutex: Mutex<BdkDescriptorSecretKey>, | ||||
| } | ||||
| 
 | ||||
| impl DescriptorSecretKey { | ||||
|     pub(crate) fn new(network: Network, mnemonic: Arc<Mnemonic>, password: Option<String>) -> Self { | ||||
|         let mnemonic = mnemonic.internal.clone(); | ||||
|         let mnemonic = mnemonic.inner.clone(); | ||||
|         let xkey: ExtendedKey = (mnemonic, password).into_extended_key().unwrap(); | ||||
|         let descriptor_secret_key = BdkDescriptorSecretKey::XPrv(DescriptorXKey { | ||||
|         let inner = BdkDescriptorSecretKey::XPrv(DescriptorXKey { | ||||
|             origin: None, | ||||
|             xkey: xkey.into_xprv(network).unwrap(), | ||||
|             derivation_path: BdkDerivationPath::master(), | ||||
|             wildcard: bdk::descriptor::Wildcard::Unhardened, | ||||
|         }); | ||||
|         Self { | ||||
|             descriptor_secret_key_mutex: Mutex::new(descriptor_secret_key), | ||||
|             inner_mutex: Mutex::new(inner), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn from_string(private_key: String) -> Result<Self, BdkError> { | ||||
|         let descriptor_secret_key = BdkDescriptorSecretKey::from_str(private_key.as_str()) | ||||
|         let inner = BdkDescriptorSecretKey::from_str(private_key.as_str()) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string()))?; | ||||
|         Ok(Self { | ||||
|             descriptor_secret_key_mutex: Mutex::new(descriptor_secret_key), | ||||
|             inner_mutex: Mutex::new(inner), | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn derive(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> { | ||||
|         let secp = Secp256k1::new(); | ||||
|         let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let path = path.derivation_path_mutex.lock().unwrap().deref().clone(); | ||||
|         let descriptor_secret_key = self.inner_mutex.lock().unwrap(); | ||||
|         let path = path.inner_mutex.lock().unwrap().deref().clone(); | ||||
|         match descriptor_secret_key.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
|                 let derived_xprv = descriptor_x_key.xkey.derive_priv(&secp, &path)?; | ||||
| @ -110,7 +110,7 @@ impl DescriptorSecretKey { | ||||
|                     wildcard: descriptor_x_key.wildcard, | ||||
|                 }); | ||||
|                 Ok(Arc::new(Self { | ||||
|                     descriptor_secret_key_mutex: Mutex::new(derived_descriptor_secret_key), | ||||
|                     inner_mutex: Mutex::new(derived_descriptor_secret_key), | ||||
|                 })) | ||||
|             } | ||||
|             BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic( | ||||
| @ -120,19 +120,19 @@ impl DescriptorSecretKey { | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn extend(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> { | ||||
|         let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let path = path.derivation_path_mutex.lock().unwrap().deref().clone(); | ||||
|         match descriptor_secret_key.deref() { | ||||
|         let inner = self.inner_mutex.lock().unwrap(); | ||||
|         let path = path.inner_mutex.lock().unwrap().deref().clone(); | ||||
|         match inner.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
|                 let extended_path = descriptor_x_key.derivation_path.extend(path); | ||||
|                 let extended_descriptor_secret_key = BdkDescriptorSecretKey::XPrv(DescriptorXKey { | ||||
|                 let extended_inner = BdkDescriptorSecretKey::XPrv(DescriptorXKey { | ||||
|                     origin: descriptor_x_key.origin.clone(), | ||||
|                     xkey: descriptor_x_key.xkey, | ||||
|                     derivation_path: extended_path, | ||||
|                     wildcard: descriptor_x_key.wildcard, | ||||
|                 }); | ||||
|                 Ok(Arc::new(Self { | ||||
|                     descriptor_secret_key_mutex: Mutex::new(extended_descriptor_secret_key), | ||||
|                     inner_mutex: Mutex::new(extended_inner), | ||||
|                 })) | ||||
|             } | ||||
|             BdkDescriptorSecretKey::Single(_) => Err(BdkError::Generic( | ||||
| @ -143,21 +143,16 @@ impl DescriptorSecretKey { | ||||
| 
 | ||||
|     pub(crate) fn as_public(&self) -> Arc<DescriptorPublicKey> { | ||||
|         let secp = Secp256k1::new(); | ||||
|         let descriptor_public_key = self | ||||
|             .descriptor_secret_key_mutex | ||||
|             .lock() | ||||
|             .unwrap() | ||||
|             .to_public(&secp) | ||||
|             .unwrap(); | ||||
|         let descriptor_public_key = self.inner_mutex.lock().unwrap().to_public(&secp).unwrap(); | ||||
|         Arc::new(DescriptorPublicKey { | ||||
|             descriptor_public_key_mutex: Mutex::new(descriptor_public_key), | ||||
|             inner_mutex: Mutex::new(descriptor_public_key), | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     /// Get the private key as bytes.
 | ||||
|     pub(crate) fn secret_bytes(&self) -> Vec<u8> { | ||||
|         let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap(); | ||||
|         let secret_bytes: Vec<u8> = match descriptor_secret_key.deref() { | ||||
|         let inner = self.inner_mutex.lock().unwrap(); | ||||
|         let secret_bytes: Vec<u8> = match inner.deref() { | ||||
|             BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { | ||||
|                 descriptor_x_key.xkey.private_key.secret_bytes().to_vec() | ||||
|             } | ||||
| @ -170,13 +165,13 @@ impl DescriptorSecretKey { | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn as_string(&self) -> String { | ||||
|         self.descriptor_secret_key_mutex.lock().unwrap().to_string() | ||||
|         self.inner_mutex.lock().unwrap().to_string() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug)] | ||||
| pub(crate) struct DescriptorPublicKey { | ||||
|     pub(crate) descriptor_public_key_mutex: Mutex<BdkDescriptorPublicKey>, | ||||
|     pub(crate) inner_mutex: Mutex<BdkDescriptorPublicKey>, | ||||
| } | ||||
| 
 | ||||
| impl DescriptorPublicKey { | ||||
| @ -184,14 +179,14 @@ impl DescriptorPublicKey { | ||||
|         let descriptor_public_key = BdkDescriptorPublicKey::from_str(public_key.as_str()) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string()))?; | ||||
|         Ok(Self { | ||||
|             descriptor_public_key_mutex: Mutex::new(descriptor_public_key), | ||||
|             inner_mutex: Mutex::new(descriptor_public_key), | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn derive(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> { | ||||
|         let secp = Secp256k1::new(); | ||||
|         let descriptor_public_key = self.descriptor_public_key_mutex.lock().unwrap(); | ||||
|         let path = path.derivation_path_mutex.lock().unwrap().deref().clone(); | ||||
|         let descriptor_public_key = self.inner_mutex.lock().unwrap(); | ||||
|         let path = path.inner_mutex.lock().unwrap().deref().clone(); | ||||
| 
 | ||||
|         match descriptor_public_key.deref() { | ||||
|             BdkDescriptorPublicKey::XPub(descriptor_x_key) => { | ||||
| @ -207,7 +202,7 @@ impl DescriptorPublicKey { | ||||
|                     wildcard: descriptor_x_key.wildcard, | ||||
|                 }); | ||||
|                 Ok(Arc::new(Self { | ||||
|                     descriptor_public_key_mutex: Mutex::new(derived_descriptor_public_key), | ||||
|                     inner_mutex: Mutex::new(derived_descriptor_public_key), | ||||
|                 })) | ||||
|             } | ||||
|             BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic( | ||||
| @ -217,8 +212,8 @@ impl DescriptorPublicKey { | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn extend(&self, path: Arc<DerivationPath>) -> Result<Arc<Self>, BdkError> { | ||||
|         let descriptor_public_key = self.descriptor_public_key_mutex.lock().unwrap(); | ||||
|         let path = path.derivation_path_mutex.lock().unwrap().deref().clone(); | ||||
|         let descriptor_public_key = self.inner_mutex.lock().unwrap(); | ||||
|         let path = path.inner_mutex.lock().unwrap().deref().clone(); | ||||
|         match descriptor_public_key.deref() { | ||||
|             BdkDescriptorPublicKey::XPub(descriptor_x_key) => { | ||||
|                 let extended_path = descriptor_x_key.derivation_path.extend(path); | ||||
| @ -229,7 +224,7 @@ impl DescriptorPublicKey { | ||||
|                     wildcard: descriptor_x_key.wildcard, | ||||
|                 }); | ||||
|                 Ok(Arc::new(Self { | ||||
|                     descriptor_public_key_mutex: Mutex::new(extended_descriptor_public_key), | ||||
|                     inner_mutex: Mutex::new(extended_descriptor_public_key), | ||||
|                 })) | ||||
|             } | ||||
|             BdkDescriptorPublicKey::Single(_) => Err(BdkError::Generic( | ||||
| @ -239,7 +234,7 @@ impl DescriptorPublicKey { | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn as_string(&self) -> String { | ||||
|         self.descriptor_public_key_mutex.lock().unwrap().to_string() | ||||
|         self.inner_mutex.lock().unwrap().to_string() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -254,7 +249,7 @@ mod test { | ||||
|     use bdk::bitcoin::Network; | ||||
|     use std::sync::Arc; | ||||
| 
 | ||||
|     fn get_descriptor_secret_key() -> DescriptorSecretKey { | ||||
|     fn get_inner() -> DescriptorSecretKey { | ||||
|         let mnemonic = Mnemonic::from_string("chaos fabric time speed sponsor all flat solution wisdom trophy crack object robot pave observe combine where aware bench orient secret primary cable detect".to_string()).unwrap(); | ||||
|         DescriptorSecretKey::new(Network::Testnet, Arc::new(mnemonic), None) | ||||
|     } | ||||
| @ -293,14 +288,14 @@ mod test { | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_generate_descriptor_secret_key() { | ||||
|         let master_dsk = get_descriptor_secret_key(); | ||||
|         let master_dsk = get_inner(); | ||||
|         assert_eq!(master_dsk.as_string(), "tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/*"); | ||||
|         assert_eq!(master_dsk.as_public().as_string(), "tpubD6NzVbkrYhZ4WywdEfYbbd62yuvqLjAZuPsNyvzCNV85JekAEMbKHWSHLF9h3j45SxewXDcLv328B1SEZrxg4iwGfmdt1pDFjZiTkGiFqGa/*"); | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_derive_self() { | ||||
|         let master_dsk = get_descriptor_secret_key(); | ||||
|         let master_dsk = get_inner(); | ||||
|         let derived_dsk: &DescriptorSecretKey = &derive_dsk(&master_dsk, "m").unwrap(); | ||||
|         assert_eq!(derived_dsk.as_string(), "[d1d04177]tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/*"); | ||||
|         let master_dpk: &DescriptorPublicKey = &master_dsk.as_public(); | ||||
| @ -310,7 +305,7 @@ mod test { | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_derive_descriptors_keys() { | ||||
|         let master_dsk = get_descriptor_secret_key(); | ||||
|         let master_dsk = get_inner(); | ||||
|         let derived_dsk: &DescriptorSecretKey = &derive_dsk(&master_dsk, "m/0").unwrap(); | ||||
|         assert_eq!(derived_dsk.as_string(), "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/*"); | ||||
|         let master_dpk: &DescriptorPublicKey = &master_dsk.as_public(); | ||||
| @ -320,7 +315,7 @@ mod test { | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_extend_descriptor_keys() { | ||||
|         let master_dsk = get_descriptor_secret_key(); | ||||
|         let master_dsk = get_inner(); | ||||
|         let extended_dsk: &DescriptorSecretKey = &extend_dsk(&master_dsk, "m/0").unwrap(); | ||||
|         assert_eq!(extended_dsk.as_string(), "tprv8ZgxMBicQKsPdWuqM1t1CDRvQtQuBPyfL6GbhQwtxDKgUAVPbxmj71pRA8raTqLrec5LyTs5TqCxdABcZr77bt2KyWA5bizJHnC4g4ysm4h/0/*"); | ||||
|         let master_dpk: &DescriptorPublicKey = &master_dsk.as_public(); | ||||
| @ -334,7 +329,7 @@ mod test { | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_from_str_descriptor_secret_key() { | ||||
|     fn test_from_str_inner() { | ||||
|         let key1 = "L2wTu6hQrnDMiFNWA5na6jB12ErGQqtXwqpSL7aWquJaZG8Ai3ch"; | ||||
|         let key2 = "tprv8ZgxMBicQKsPcwcD4gSnMti126ZiETsuX7qwrtMypr6FBwAP65puFn4v6c3jrN9VwtMRMph6nyT63NrfUL4C3nBzPcduzVSuHD7zbX2JKVc/1/1/1/*"; | ||||
|         let private_descriptor_key1 = DescriptorSecretKey::from_string(key1.to_string()).unwrap(); | ||||
| @ -347,8 +342,8 @@ mod test { | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_derive_and_extend_descriptor_secret_key() { | ||||
|         let master_dsk = get_descriptor_secret_key(); | ||||
|     fn test_derive_and_extend_inner() { | ||||
|         let master_dsk = get_inner(); | ||||
|         // derive DescriptorSecretKey with path "m/0" from master
 | ||||
|         let derived_dsk: &DescriptorSecretKey = &derive_dsk(&master_dsk, "m/0").unwrap(); | ||||
|         assert_eq!(derived_dsk.as_string(), "[d1d04177/0]tprv8d7Y4JLmD25jkKbyDZXcdoPHu1YtMHuH21qeN7mFpjfumtSU7eZimFYUCSa3MYzkEYfSNRBV34GEr2QXwZCMYRZ7M1g6PUtiLhbJhBZEGYJ/*"); | ||||
| @ -359,14 +354,14 @@ mod test { | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_derive_hardened_path_using_public() { | ||||
|         let master_dpk = get_descriptor_secret_key().as_public(); | ||||
|         let master_dpk = get_inner().as_public(); | ||||
|         let derived_dpk = &derive_dpk(&master_dpk, "m/84h/1h/0h"); | ||||
|         assert!(derived_dpk.is_err()); | ||||
|     } | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_retrieve_master_secret_key() { | ||||
|         let master_dpk = get_descriptor_secret_key(); | ||||
|         let master_dpk = get_inner(); | ||||
|         let master_private_key = master_dpk.secret_bytes().to_hex(); | ||||
|         assert_eq!( | ||||
|             master_private_key, | ||||
|  | ||||
| @ -203,7 +203,7 @@ impl From<&BdkTxOut> for TxOut { | ||||
|         TxOut { | ||||
|             value: tx_out.value, | ||||
|             script_pubkey: Arc::new(Script { | ||||
|                 script: tx_out.script_pubkey.clone(), | ||||
|                 inner: tx_out.script_pubkey.clone(), | ||||
|             }), | ||||
|         } | ||||
|     } | ||||
| @ -226,7 +226,7 @@ impl From<BdkLocalUtxo> for LocalUtxo { | ||||
|             txout: TxOut { | ||||
|                 value: local_utxo.txout.value, | ||||
|                 script_pubkey: Arc::new(Script { | ||||
|                     script: local_utxo.txout.script_pubkey, | ||||
|                     inner: local_utxo.txout.script_pubkey, | ||||
|                 }), | ||||
|             }, | ||||
|             keychain: local_utxo.keychain, | ||||
| @ -275,7 +275,7 @@ impl From<&BdkTxIn> for TxIn { | ||||
|                 vout: tx_in.previous_output.vout, | ||||
|             }, | ||||
|             script_sig: Arc::new(Script { | ||||
|                 script: tx_in.script_sig.clone(), | ||||
|                 inner: tx_in.script_sig.clone(), | ||||
|             }), | ||||
|             sequence: tx_in.sequence.0, | ||||
|             witness: tx_in.witness.to_vec(), | ||||
| @ -286,93 +286,93 @@ impl From<&BdkTxIn> for TxIn { | ||||
| /// A Bitcoin transaction.
 | ||||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||||
| pub struct Transaction { | ||||
|     internal: BdkTransaction, | ||||
|     inner: BdkTransaction, | ||||
| } | ||||
| 
 | ||||
| impl Transaction { | ||||
|     fn new(transaction_bytes: Vec<u8>) -> Result<Self, BdkError> { | ||||
|         let mut decoder = Cursor::new(transaction_bytes); | ||||
|         let tx: BdkTransaction = BdkTransaction::consensus_decode(&mut decoder)?; | ||||
|         Ok(Transaction { internal: tx }) | ||||
|         Ok(Transaction { inner: tx }) | ||||
|     } | ||||
| 
 | ||||
|     fn txid(&self) -> String { | ||||
|         self.internal.txid().to_string() | ||||
|         self.inner.txid().to_string() | ||||
|     } | ||||
| 
 | ||||
|     fn weight(&self) -> u64 { | ||||
|         self.internal.weight() as u64 | ||||
|         self.inner.weight() as u64 | ||||
|     } | ||||
| 
 | ||||
|     fn size(&self) -> u64 { | ||||
|         self.internal.size() as u64 | ||||
|         self.inner.size() as u64 | ||||
|     } | ||||
| 
 | ||||
|     fn vsize(&self) -> u64 { | ||||
|         self.internal.vsize() as u64 | ||||
|         self.inner.vsize() as u64 | ||||
|     } | ||||
| 
 | ||||
|     fn serialize(&self) -> Vec<u8> { | ||||
|         self.internal.serialize() | ||||
|         self.inner.serialize() | ||||
|     } | ||||
| 
 | ||||
|     fn is_coin_base(&self) -> bool { | ||||
|         self.internal.is_coin_base() | ||||
|         self.inner.is_coin_base() | ||||
|     } | ||||
| 
 | ||||
|     fn is_explicitly_rbf(&self) -> bool { | ||||
|         self.internal.is_explicitly_rbf() | ||||
|         self.inner.is_explicitly_rbf() | ||||
|     } | ||||
| 
 | ||||
|     fn is_lock_time_enabled(&self) -> bool { | ||||
|         self.internal.is_lock_time_enabled() | ||||
|         self.inner.is_lock_time_enabled() | ||||
|     } | ||||
| 
 | ||||
|     fn version(&self) -> i32 { | ||||
|         self.internal.version | ||||
|         self.inner.version | ||||
|     } | ||||
| 
 | ||||
|     fn lock_time(&self) -> u32 { | ||||
|         self.internal.lock_time.0 | ||||
|         self.inner.lock_time.0 | ||||
|     } | ||||
| 
 | ||||
|     fn input(&self) -> Vec<TxIn> { | ||||
|         self.internal.input.iter().map(|x| x.into()).collect() | ||||
|         self.inner.input.iter().map(|x| x.into()).collect() | ||||
|     } | ||||
| 
 | ||||
|     fn output(&self) -> Vec<TxOut> { | ||||
|         self.internal.output.iter().map(|x| x.into()).collect() | ||||
|         self.inner.output.iter().map(|x| x.into()).collect() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<BdkTransaction> for Transaction { | ||||
|     fn from(tx: BdkTransaction) -> Self { | ||||
|         Transaction { internal: tx } | ||||
|         Transaction { inner: tx } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// A Bitcoin address.
 | ||||
| #[derive(Debug, PartialEq, Eq)] | ||||
| pub struct Address { | ||||
|     address: BdkAddress, | ||||
|     inner: BdkAddress, | ||||
| } | ||||
| 
 | ||||
| impl Address { | ||||
|     fn new(address: String) -> Result<Self, BdkError> { | ||||
|         BdkAddress::from_str(address.as_str()) | ||||
|             .map(|a| Address { address: a }) | ||||
|             .map(|a| Address { inner: a }) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string())) | ||||
|     } | ||||
| 
 | ||||
|     /// alternative constructor
 | ||||
|     fn from_script(script: Arc<Script>, network: Network) -> Result<Self, BdkError> { | ||||
|         BdkAddress::from_script(&script.script, network) | ||||
|             .map(|a| Address { address: a }) | ||||
|         BdkAddress::from_script(&script.inner, network) | ||||
|             .map(|a| Address { inner: a }) | ||||
|             .map_err(|e| BdkError::Generic(e.to_string())) | ||||
|     } | ||||
| 
 | ||||
|     fn payload(&self) -> Payload { | ||||
|         match &self.address.payload.clone() { | ||||
|         match &self.inner.payload.clone() { | ||||
|             BdkPayload::PubkeyHash(pubkey_hash) => Payload::PubkeyHash { | ||||
|                 pubkey_hash: pubkey_hash.to_vec(), | ||||
|             }, | ||||
| @ -387,27 +387,27 @@ impl Address { | ||||
|     } | ||||
| 
 | ||||
|     fn network(&self) -> Network { | ||||
|         self.address.network | ||||
|         self.inner.network | ||||
|     } | ||||
| 
 | ||||
|     fn script_pubkey(&self) -> Arc<Script> { | ||||
|         Arc::new(Script { | ||||
|             script: self.address.script_pubkey(), | ||||
|             inner: self.inner.script_pubkey(), | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     fn to_qr_uri(&self) -> String { | ||||
|         self.address.to_qr_uri() | ||||
|         self.inner.to_qr_uri() | ||||
|     } | ||||
| 
 | ||||
|     fn as_string(&self) -> String { | ||||
|         self.address.to_string() | ||||
|         self.inner.to_string() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<BdkAddress> for Address { | ||||
|     fn from(address: BdkAddress) -> Self { | ||||
|         Address { address } | ||||
|         Address { inner: address } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -430,23 +430,23 @@ pub enum Payload { | ||||
| /// A Bitcoin script.
 | ||||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||||
| pub struct Script { | ||||
|     script: BdkScript, | ||||
|     inner: BdkScript, | ||||
| } | ||||
| 
 | ||||
| impl Script { | ||||
|     fn new(raw_output_script: Vec<u8>) -> Self { | ||||
|         let script: BdkScript = BdkScript::from(raw_output_script); | ||||
|         Script { script } | ||||
|         Script { inner: script } | ||||
|     } | ||||
| 
 | ||||
|     fn to_bytes(&self) -> Vec<u8> { | ||||
|         self.script.to_bytes() | ||||
|         self.inner.to_bytes() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl From<BdkScript> for Script { | ||||
|     fn from(bdk_script: BdkScript) -> Self { | ||||
|         Script { script: bdk_script } | ||||
|         Script { inner: bdk_script } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -10,7 +10,7 @@ use crate::{BdkError, FeeRate, Transaction}; | ||||
| 
 | ||||
| #[derive(Debug)] | ||||
| pub(crate) struct PartiallySignedTransaction { | ||||
|     pub(crate) internal: Mutex<BdkPartiallySignedTransaction>, | ||||
|     pub(crate) inner: Mutex<BdkPartiallySignedTransaction>, | ||||
| } | ||||
| 
 | ||||
| impl PartiallySignedTransaction { | ||||
| @ -18,24 +18,24 @@ impl PartiallySignedTransaction { | ||||
|         let psbt: BdkPartiallySignedTransaction = | ||||
|             BdkPartiallySignedTransaction::from_str(&psbt_base64)?; | ||||
|         Ok(PartiallySignedTransaction { | ||||
|             internal: Mutex::new(psbt), | ||||
|             inner: Mutex::new(psbt), | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn serialize(&self) -> String { | ||||
|         let psbt = self.internal.lock().unwrap().clone(); | ||||
|         let psbt = self.inner.lock().unwrap().clone(); | ||||
|         psbt.to_string() | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn txid(&self) -> String { | ||||
|         let tx = self.internal.lock().unwrap().clone().extract_tx(); | ||||
|         let tx = self.inner.lock().unwrap().clone().extract_tx(); | ||||
|         let txid = tx.txid(); | ||||
|         txid.to_hex() | ||||
|     } | ||||
| 
 | ||||
|     /// Return the transaction.
 | ||||
|     pub(crate) fn extract_tx(&self) -> Arc<Transaction> { | ||||
|         let tx = self.internal.lock().unwrap().clone().extract_tx(); | ||||
|         let tx = self.inner.lock().unwrap().clone().extract_tx(); | ||||
|         Arc::new(tx.into()) | ||||
|     } | ||||
| 
 | ||||
| @ -46,19 +46,19 @@ impl PartiallySignedTransaction { | ||||
|         &self, | ||||
|         other: Arc<PartiallySignedTransaction>, | ||||
|     ) -> Result<Arc<PartiallySignedTransaction>, BdkError> { | ||||
|         let other_psbt = other.internal.lock().unwrap().clone(); | ||||
|         let mut original_psbt = self.internal.lock().unwrap().clone(); | ||||
|         let other_psbt = other.inner.lock().unwrap().clone(); | ||||
|         let mut original_psbt = self.inner.lock().unwrap().clone(); | ||||
| 
 | ||||
|         original_psbt.combine(other_psbt)?; | ||||
|         Ok(Arc::new(PartiallySignedTransaction { | ||||
|             internal: Mutex::new(original_psbt), | ||||
|             inner: Mutex::new(original_psbt), | ||||
|         })) | ||||
|     } | ||||
| 
 | ||||
|     /// The total transaction fee amount, sum of input amounts minus sum of output amounts, in Sats.
 | ||||
|     /// If the PSBT is missing a TxOut for an input returns None.
 | ||||
|     pub(crate) fn fee_amount(&self) -> Option<u64> { | ||||
|         self.internal.lock().unwrap().fee_amount() | ||||
|         self.inner.lock().unwrap().fee_amount() | ||||
|     } | ||||
| 
 | ||||
|     /// The transaction's fee rate. This value will only be accurate if calculated AFTER the
 | ||||
| @ -66,12 +66,12 @@ impl PartiallySignedTransaction { | ||||
|     /// transaction.
 | ||||
|     /// If the PSBT is missing a TxOut for an input returns None.
 | ||||
|     pub(crate) fn fee_rate(&self) -> Option<Arc<FeeRate>> { | ||||
|         self.internal.lock().unwrap().fee_rate().map(Arc::new) | ||||
|         self.inner.lock().unwrap().fee_rate().map(Arc::new) | ||||
|     } | ||||
| 
 | ||||
|     /// Serialize the PSBT data structure as a String of JSON.
 | ||||
|     pub(crate) fn json_serialize(&self) -> String { | ||||
|         let psbt = self.internal.lock().unwrap(); | ||||
|         let psbt = self.inner.lock().unwrap(); | ||||
|         serde_json::to_string(psbt.deref()).unwrap() | ||||
|     } | ||||
| } | ||||
| @ -90,7 +90,7 @@ mod test { | ||||
|         let test_wpkh = "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"; | ||||
|         let (funded_wallet, _, _) = get_funded_wallet(test_wpkh); | ||||
|         let test_wallet = Wallet { | ||||
|             wallet_mutex: Mutex::new(funded_wallet), | ||||
|             inner_mutex: Mutex::new(funded_wallet), | ||||
|         }; | ||||
|         let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string(); | ||||
|         let drain_to_script = crate::Address::new(drain_to_address) | ||||
| @ -103,7 +103,7 @@ mod test { | ||||
|             .drain_to(drain_to_script.clone()); | ||||
|         //dbg!(&tx_builder);
 | ||||
|         assert!(tx_builder.drain_wallet); | ||||
|         assert_eq!(tx_builder.drain_to, Some(drain_to_script.script.clone())); | ||||
|         assert_eq!(tx_builder.drain_to, Some(drain_to_script.inner.clone())); | ||||
| 
 | ||||
|         let tx_builder_result = tx_builder.finish(&test_wallet).unwrap(); | ||||
| 
 | ||||
|  | ||||
| @ -23,7 +23,7 @@ use crate::{ | ||||
| 
 | ||||
| #[derive(Debug)] | ||||
| pub(crate) struct Wallet { | ||||
|     pub(crate) wallet_mutex: Mutex<BdkWallet<AnyDatabase>>, | ||||
|     pub(crate) inner_mutex: Mutex<BdkWallet<AnyDatabase>>, | ||||
| } | ||||
| 
 | ||||
| /// A Bitcoin wallet.
 | ||||
| @ -53,11 +53,13 @@ impl Wallet { | ||||
|             network, | ||||
|             database, | ||||
|         )?); | ||||
|         Ok(Wallet { wallet_mutex }) | ||||
|         Ok(Wallet { | ||||
|             inner_mutex: wallet_mutex, | ||||
|         }) | ||||
|     } | ||||
| 
 | ||||
|     pub(crate) fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyDatabase>> { | ||||
|         self.wallet_mutex.lock().expect("wallet") | ||||
|         self.inner_mutex.lock().expect("wallet") | ||||
|     } | ||||
| 
 | ||||
|     /// Get the Bitcoin network the wallet is using.
 | ||||
| @ -67,7 +69,7 @@ impl Wallet { | ||||
| 
 | ||||
|     /// Return whether or not a script is part of this wallet (either internal or external).
 | ||||
|     pub(crate) fn is_mine(&self, script: Arc<Script>) -> Result<bool, BdkError> { | ||||
|         self.get_wallet().is_mine(&script.script) | ||||
|         self.get_wallet().is_mine(&script.inner) | ||||
|     } | ||||
| 
 | ||||
|     /// Sync the internal database with the blockchain.
 | ||||
| @ -131,7 +133,7 @@ impl Wallet { | ||||
|         psbt: &PartiallySignedTransaction, | ||||
|         sign_options: Option<SignOptions>, | ||||
|     ) -> Result<bool, BdkError> { | ||||
|         let mut psbt = psbt.internal.lock().unwrap(); | ||||
|         let mut psbt = psbt.inner.lock().unwrap(); | ||||
|         self.get_wallet().sign( | ||||
|             &mut psbt, | ||||
|             sign_options.map(SignOptions::into).unwrap_or_default(), | ||||
| @ -271,7 +273,7 @@ impl TxBuilder { | ||||
|     /// Add a recipient to the internal list.
 | ||||
|     pub(crate) fn add_recipient(&self, script: Arc<Script>, amount: u64) -> Arc<Self> { | ||||
|         let mut recipients: Vec<(BdkScript, u64)> = self.recipients.clone(); | ||||
|         recipients.append(&mut vec![(script.script.clone(), amount)]); | ||||
|         recipients.append(&mut vec![(script.inner.clone(), amount)]); | ||||
|         Arc::new(TxBuilder { | ||||
|             recipients, | ||||
|             ..self.clone() | ||||
| @ -281,7 +283,7 @@ impl TxBuilder { | ||||
|     pub(crate) fn set_recipients(&self, recipients: Vec<ScriptAmount>) -> Arc<Self> { | ||||
|         let recipients = recipients | ||||
|             .iter() | ||||
|             .map(|script_amount| (script_amount.script.script.clone(), script_amount.amount)) | ||||
|             .map(|script_amount| (script_amount.script.inner.clone(), script_amount.amount)) | ||||
|             .collect(); | ||||
|         Arc::new(TxBuilder { | ||||
|             recipients, | ||||
| @ -386,7 +388,7 @@ impl TxBuilder { | ||||
|     /// to allow this output to be reduced to pay for the extra fees.
 | ||||
|     pub(crate) fn drain_to(&self, script: Arc<Script>) -> Arc<Self> { | ||||
|         Arc::new(TxBuilder { | ||||
|             drain_to: Some(script.script.clone()), | ||||
|             drain_to: Some(script.inner.clone()), | ||||
|             ..self.clone() | ||||
|         }) | ||||
|     } | ||||
| @ -468,7 +470,7 @@ impl TxBuilder { | ||||
|             .finish() | ||||
|             .map(|(psbt, tx_details)| TxBuilderResult { | ||||
|                 psbt: Arc::new(PartiallySignedTransaction { | ||||
|                     internal: Mutex::new(psbt), | ||||
|                     inner: Mutex::new(psbt), | ||||
|                 }), | ||||
|                 transaction_details: TransactionDetails::from(tx_details), | ||||
|             }) | ||||
| @ -552,7 +554,7 @@ impl BumpFeeTxBuilder { | ||||
|         tx_builder | ||||
|             .finish() | ||||
|             .map(|(psbt, _)| PartiallySignedTransaction { | ||||
|                 internal: Mutex::new(psbt), | ||||
|                 inner: Mutex::new(psbt), | ||||
|             }) | ||||
|             .map(Arc::new) | ||||
|     } | ||||
| @ -580,7 +582,7 @@ mod test { | ||||
|         let test_wpkh = "wpkh(cVpPVruEDdmutPzisEsYvtST1usBR3ntr8pXSyt6D2YYqXRyPcFW)"; | ||||
|         let (funded_wallet, _, _) = get_funded_wallet(test_wpkh); | ||||
|         let test_wallet = Wallet { | ||||
|             wallet_mutex: Mutex::new(funded_wallet), | ||||
|             inner_mutex: Mutex::new(funded_wallet), | ||||
|         }; | ||||
|         let drain_to_address = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt".to_string(); | ||||
|         let drain_to_script = crate::Address::new(drain_to_address) | ||||
| @ -590,10 +592,10 @@ mod test { | ||||
|             .drain_wallet() | ||||
|             .drain_to(drain_to_script.clone()); | ||||
|         assert!(tx_builder.drain_wallet); | ||||
|         assert_eq!(tx_builder.drain_to, Some(drain_to_script.script.clone())); | ||||
|         assert_eq!(tx_builder.drain_to, Some(drain_to_script.inner.clone())); | ||||
| 
 | ||||
|         let tx_builder_result = tx_builder.finish(&test_wallet).unwrap(); | ||||
|         let psbt = tx_builder_result.psbt.internal.lock().unwrap().clone(); | ||||
|         let psbt = tx_builder_result.psbt.inner.lock().unwrap().clone(); | ||||
|         let tx_details = tx_builder_result.transaction_details; | ||||
| 
 | ||||
|         // confirm one input with 50,000 sats
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user