From b50985aec7e4df44147c8294f40d28deb6a01d93 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Tue, 26 Mar 2024 16:05:16 -0500 Subject: [PATCH] detect p2tr outputs --- .../intermediate/bip39_hd_currency.dart | 20 ++++++++++++++----- .../electrumx_interface.dart | 14 +++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart b/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart index fa5d5c796..5e6cd60e5 100644 --- a/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart +++ b/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart @@ -80,12 +80,22 @@ abstract class Bip39HDCurrency extends Bip39Currency { } catch (err) { // Bech32 decode fail } - if (networkParams.bech32Hrp != decodeBech32!.hrp) { - throw ArgumentError('Invalid prefix or Network mismatch'); - } - if (decodeBech32.version != 0) { - throw ArgumentError('Invalid address version'); + if (decodeBech32?.hrp != null) { + // P2TR Bech32m addresses have null hrp. + if (networkParams.bech32Hrp != decodeBech32!.hrp) { + throw ArgumentError('Invalid prefix or Network mismatch'); + } + if (decodeBech32.version != 0) { + throw ArgumentError('Invalid address version'); + } + } else { + // P2TR. + if (address.startsWith('bc1p') || address.startsWith('tb1p')) { + // P2TR (Taproot). + return DerivePathType.bip86; + } } + // P2WPKH return DerivePathType.bip84; } diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart index aa738c6ec..78847397e 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/electrumx_interface.dart @@ -682,6 +682,20 @@ mixin ElectrumXInterface on Bip39HDWallet { // TODO: use coinlib + // Check if any txData.recipients are taproot/P2TR outputs. + bool hasTaprootOutput = false; + for (final recipient in txData.recipients!) { + if (cryptoCurrency.addressType(address: recipient.address) == + DerivePathType.bip86) { + hasTaprootOutput = true; + } + } + + if (hasTaprootOutput) { + // Use Coinlib to construct taproot transaction. + // TODO [prio=high]: Implement taproot transaction construction. + } + final txb = bitcoindart.TransactionBuilder( network: bitcoindart.NetworkType( messagePrefix: cryptoCurrency.networkParams.messagePrefix,