| 
									
										
										
										
											2021-03-03 13:22:05 -08:00
										 |  |  | // Bitcoin Dev Kit
 | 
					
						
							|  |  |  | // Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
 | 
					
						
							| 
									
										
										
										
											2020-08-31 11:26:36 +02:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2021-03-03 13:22:05 -08:00
										 |  |  | // Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
 | 
					
						
							| 
									
										
										
										
											2020-08-31 11:26:36 +02:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2021-03-03 13:22:05 -08:00
										 |  |  | // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
 | 
					
						
							|  |  |  | // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 | 
					
						
							|  |  |  | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
 | 
					
						
							|  |  |  | // You may not use this file except in accordance with one or both of these
 | 
					
						
							|  |  |  | // licenses.
 | 
					
						
							| 
									
										
										
										
											2020-08-31 11:26:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | use std::sync::Arc;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 14:25:38 +02:00
										 |  |  | use bdk::bitcoin;
 | 
					
						
							|  |  |  | use bdk::database::MemoryDatabase;
 | 
					
						
							| 
									
										
										
										
											2021-03-30 16:33:07 +02:00
										 |  |  | use bdk::descriptor::HdKeyPaths;
 | 
					
						
							| 
									
										
										
										
											2022-07-11 16:37:41 +08:00
										 |  |  | #[allow(deprecated)]
 | 
					
						
							| 
									
										
										
										
											2020-09-14 14:25:38 +02:00
										 |  |  | use bdk::wallet::address_validator::{AddressValidator, AddressValidatorError};
 | 
					
						
							| 
									
										
										
										
											2020-12-14 17:14:24 +01:00
										 |  |  | use bdk::KeychainKind;
 | 
					
						
							| 
									
										
										
										
											2020-12-23 13:48:17 +11:00
										 |  |  | use bdk::Wallet;
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 16:17:10 -08:00
										 |  |  | use bdk::wallet::AddressIndex::New;
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | use bitcoin::hashes::hex::FromHex;
 | 
					
						
							|  |  |  | use bitcoin::util::bip32::Fingerprint;
 | 
					
						
							|  |  |  | use bitcoin::{Network, Script};
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 14:11:29 +11:00
										 |  |  | #[derive(Debug)]
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | struct DummyValidator;
 | 
					
						
							| 
									
										
										
										
											2022-07-11 16:37:41 +08:00
										 |  |  | #[allow(deprecated)]
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | impl AddressValidator for DummyValidator {
 | 
					
						
							|  |  |  |     fn validate(
 | 
					
						
							|  |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2020-12-14 17:14:24 +01:00
										 |  |  |         keychain: KeychainKind,
 | 
					
						
							| 
									
										
										
										
											2021-03-30 16:33:07 +02:00
										 |  |  |         hd_keypaths: &HdKeyPaths,
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  |         script: &Script,
 | 
					
						
							|  |  |  |     ) -> Result<(), AddressValidatorError> {
 | 
					
						
							|  |  |  |         let (_, path) = hd_keypaths
 | 
					
						
							|  |  |  |             .values()
 | 
					
						
							|  |  |  |             .find(|(fing, _)| fing == &Fingerprint::from_hex("bc123c3e").unwrap())
 | 
					
						
							|  |  |  |             .ok_or(AddressValidatorError::InvalidScript)?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         println!(
 | 
					
						
							|  |  |  |             "Validating `{:?}` {} address, script: {}",
 | 
					
						
							| 
									
										
										
										
											2020-12-14 17:14:24 +01:00
										 |  |  |             keychain, path, script
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  |         );
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Ok(())
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 14:25:38 +02:00
										 |  |  | fn main() -> Result<(), bdk::Error> {
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  |     let descriptor = "sh(and_v(v:pk(tpubDDpWvmUrPZrhSPmUzCMBHffvC3HyMAPnWDSAQNBTnj1iZeJa7BZQEttFiP4DS4GCcXQHezdXhn86Hj6LHX5EDstXPWrMaSneRWM8yUf6NFd/*),after(630000)))";
 | 
					
						
							| 
									
										
										
										
											2022-01-26 15:17:48 +11:00
										 |  |  |     let mut wallet = Wallet::new(descriptor, None, Network::Regtest, MemoryDatabase::new())?;
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 16:37:41 +08:00
										 |  |  |     #[allow(deprecated)]
 | 
					
						
							| 
									
										
										
										
											2020-11-03 16:03:04 +11:00
										 |  |  |     wallet.add_address_validator(Arc::new(DummyValidator));
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 16:17:10 -08:00
										 |  |  |     wallet.get_address(New)?;
 | 
					
						
							|  |  |  |     wallet.get_address(New)?;
 | 
					
						
							|  |  |  |     wallet.get_address(New)?;
 | 
					
						
							| 
									
										
										
										
											2020-08-15 23:21:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 |