use crate::{PaymentInfo, Bip353Error};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct WalletPaymentInfo {
pub payment_info: PaymentInfo,
pub display_name: String,
pub metadata: WalletMetadata,
}
#[derive(Debug, Clone)]
pub struct WalletMetadata {
pub original_address: String,
pub dnssec_proof: Option<Vec<u8>>,
pub suggested_label: String,
pub wallet_specific: HashMap<String, String>,
}
pub struct WalletIntegrationHelper;
impl WalletIntegrationHelper {
pub fn prepare_for_wallet(
payment_info: PaymentInfo,
original_address: &str,
wallet_type: WalletType,
) -> WalletPaymentInfo {
let mut metadata = WalletMetadata {
original_address: original_address.to_string(),
dnssec_proof: None,
suggested_label: format!("BIP-353: {}", original_address),
wallet_specific: HashMap::new(),
};
match wallet_type {
WalletType::Sparrow => {
metadata.wallet_specific.insert(
"sparrow_memo".to_string(),
format!("Paid to {}", original_address),
);
},
WalletType::Electrum => {
metadata.wallet_specific.insert(
"electrum_description".to_string(),
format!("BIP-353 payment to {}", original_address),
);
},
WalletType::BitcoinCore => {
metadata.wallet_specific.insert(
"core_comment".to_string(),
format!("BIP-353: {}", original_address),
);
},
WalletType::BDK => {
},
}
WalletPaymentInfo {
payment_info,
display_name: Self::create_display_name(original_address),
metadata,
}
}
fn create_display_name(address: &str) -> String {
let addr = address.strip_prefix("₿").unwrap_or(address);
format!("₿{}", addr)
}
pub fn extract_bip21_params(wallet_info: &WalletPaymentInfo) -> HashMap<String, String> {
let mut params = wallet_info.payment_info.parameters.clone();
params.insert("label".to_string(), wallet_info.metadata.suggested_label.clone());
params.insert("message".to_string(), format!("Payment to {}", wallet_info.metadata.original_address));
params
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WalletType {
Sparrow,
Electrum,
BitcoinCore,
BDK,
}
impl std::fmt::Display for WalletType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WalletType::Sparrow => write!(f, "sparrow"),
WalletType::Electrum => write!(f, "electrum"),
WalletType::BitcoinCore => write!(f, "bitcoin-core"),
WalletType::BDK => write!(f, "bdk"),
}
}
}
pub trait WalletIntegration {
type Error: std::error::Error + Send + Sync + 'static;
type TransactionOutput;
async fn create_bip353_transaction(
&self,
wallet_info: WalletPaymentInfo,
amount: bitcoin::Amount,
) -> Result<Self::TransactionOutput, Self::Error>;
}