Module bdk::wallet::address_validator[][src]

Expand description

Address validation callbacks

The typical usage of those callbacks is for displaying the newly-generated address on a hardware wallet, so that the user can cross-check its correctness.

More generally speaking though, these callbacks can also be used to “do something” every time an address is generated, without necessarily checking or validating it.

An address validator can be attached to a Wallet by using the Wallet::add_address_validator method, and whenever a new address is generated (either explicitly by the user with Wallet::get_address or internally to create a change address) all the attached validators will be polled, in sequence. All of them must complete successfully to continue.

Example

#[derive(Debug)]
struct PrintAddressAndContinue;

impl AddressValidator for PrintAddressAndContinue {
    fn validate(
        &self,
        keychain: KeychainKind,
        hd_keypaths: &HdKeyPaths,
        script: &Script
    ) -> Result<(), AddressValidatorError> {
        let address = Address::from_script(script, Network::Testnet)
            .as_ref()
            .map(Address::to_string)
            .unwrap_or(script.to_string());
        println!("New address of type {:?}: {}", keychain, address);
        println!("HD keypaths: {:#?}", hd_keypaths);

        Ok(())
    }
}

let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
let mut wallet = Wallet::new_offline(descriptor, None, Network::Testnet, MemoryDatabase::default())?;
wallet.add_address_validator(Arc::new(PrintAddressAndContinue));

let address = wallet.get_address(New)?;
println!("Address: {}", address);

Enums

AddressValidatorError

Errors that can be returned to fail the validation of an address

Traits

AddressValidator

Trait to build address validators