diff --git a/Cargo.toml b/Cargo.toml index eed208f9..cf32400e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ anyhow = { version = "1.0.98", optional = true } bdk_file_store = { version = "0.21.1", optional = true } bip39 = { version = "2.0", optional = true } tempfile = { version = "3.20.0", optional = true } +bitcoin-payment-instructions = { version = "0.5.0", optional = true} [features] default = ["std"] @@ -39,6 +40,7 @@ keys-bip39 = ["bip39"] rusqlite = ["bdk_chain/rusqlite"] file_store = ["bdk_file_store"] test-utils = ["std", "anyhow", "tempfile"] +bip353 = ["bitcoin-payment-instructions"] [dev-dependencies] anyhow = "1" diff --git a/src/wallet/dns_tx_builder.rs b/src/wallet/dns_tx_builder.rs new file mode 100644 index 00000000..92ec2827 --- /dev/null +++ b/src/wallet/dns_tx_builder.rs @@ -0,0 +1,58 @@ +// Bitcoin Dev Kit +// Written in 2020 by Alekos Filini +// +// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +use core::ops::{Deref, DerefMut}; + +use bitcoin::Amount; +use bitcoin_payment_instructions::{hrn_resolution::HrnResolver, PaymentInstructions}; + +use crate::tx_builder::TxBuilder; + +/// A transaction builder with BIP 353 DNS payment instructions support +#[derive(Debug)] +pub struct TxBuilderDns<'a, Cs, R> { + pub(crate) tx_builder: TxBuilder<'a, Cs>, + pub(crate) resolver: R, +} + +impl<'a, Cs, R> Deref for TxBuilderDns<'a, Cs, R> { + type Target = TxBuilder<'a, Cs>; + fn deref(&self) -> &Self::Target { + &self.tx_builder + } +} + +impl<'a, Cs, R> DerefMut for TxBuilderDns<'a, Cs, R> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.tx_builder + } +} + +impl<'a, Cs, R: HrnResolver> TxBuilderDns<'a, Cs, R> { + /// Chose the resolver to use + pub fn resolver(self, resolver: Rs) -> TxBuilderDns<'a, Cs, Rs> { + TxBuilderDns { + tx_builder: self.tx_builder, + resolver, + } + } + + // Add a recipient with human_readable_name to the internal list + // The human readable name is in the form â‚¿user@domain or user@domain + pub fn add_recipient( + &mut self, + human_readable_name: &str, + amount: Amount, + ) -> Result<&mut Self, &str> { + + Ok(self) + } +} diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index bc814589..3a6cca1d 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -57,9 +57,19 @@ mod params; mod persisted; pub mod signer; pub mod tx_builder; + +#[cfg(feature = "bip353")] +pub mod dns_tx_builder; +#[cfg(feature = "bip353")] +use core::{net::SocketAddr, str::FromStr}; +#[cfg(feature = "bip353")] +use bitcoin_payment_instructions::dns_resolver::DNSHrnResolver; + pub(crate) mod utils; -use crate::collections::{BTreeMap, HashMap, HashSet}; +#[cfg(feature = "bip353")] +use crate::dns_tx_builder::TxBuilderDns; +use crate::{collections::{BTreeMap, HashMap, HashSet}}; use crate::descriptor::{ check_wallet_descriptor, error::Error as DescriptorError, policy::BuildSatisfaction, DerivedDescriptor, DescriptorMeta, ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, @@ -1342,6 +1352,15 @@ impl Wallet { } } + #[cfg(feature = "bip353")] + /// Build tx with default dns resolver + pub fn build_tx_with_dns(&mut self) -> TxBuilderDns<'_, DefaultCoinSelectionAlgorithm, DNSHrnResolver> { + TxBuilderDns { + tx_builder: self.build_tx(), + resolver: DNSHrnResolver(SocketAddr::from_str("8.8.8.8:53").unwrap()) + } + } + pub(crate) fn create_tx( &mut self, coin_selection: Cs, diff --git a/src/wallet/tx_builder.rs b/src/wallet/tx_builder.rs index f3bfe3f9..0e84d71e 100644 --- a/src/wallet/tx_builder.rs +++ b/src/wallet/tx_builder.rs @@ -141,6 +141,8 @@ pub(crate) struct TxParams { pub(crate) bumping_fee: Option, pub(crate) current_height: Option, pub(crate) allow_dust: bool, + #[cfg(feature = "bip353")] + pub(crate) supports_proof_of_payment_callbacks: bool } #[derive(Clone, Copy, Debug)]