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;
|
|
|
|
use bdk::descriptor::HDKeyPaths;
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
impl AddressValidator for DummyValidator {
|
|
|
|
fn validate(
|
|
|
|
&self,
|
2020-12-14 17:14:24 +01:00
|
|
|
keychain: KeychainKind,
|
2020-08-15 23:21:13 +02:00
|
|
|
hd_keypaths: &HDKeyPaths,
|
|
|
|
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)))";
|
2020-12-23 13:48:17 +11:00
|
|
|
let mut wallet =
|
2020-08-15 23:21:13 +02:00
|
|
|
Wallet::new_offline(descriptor, None, Network::Regtest, MemoryDatabase::new())?;
|
|
|
|
|
2020-11-03 16:03:04 +11:00
|
|
|
wallet.add_address_validator(Arc::new(DummyValidator));
|
2020-08-15 23:21:13 +02:00
|
|
|
|
|
|
|
wallet.get_new_address()?;
|
|
|
|
wallet.get_new_address()?;
|
|
|
|
wallet.get_new_address()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|