From 98410ea8f02290cbe8bf63fc4b8cbaea6e4c4843 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 12 Apr 2024 14:57:41 -0600 Subject: [PATCH] use coinlib address parsing to check address type and not rely specifically on btc address prefixes to validate taproot addresses --- .../intermediate/bip39_hd_currency.dart | 52 ++++--------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart b/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart index 5e6cd60e5..62be12f20 100644 --- a/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart +++ b/lib/wallets/crypto_currency/intermediate/bip39_hd_currency.dart @@ -1,5 +1,3 @@ -import 'package:bech32/bech32.dart'; -import 'package:bs58check/bs58check.dart' as bs58check; import 'package:coinlib_flutter/coinlib_flutter.dart' as coinlib; import 'package:crypto/crypto.dart'; import 'package:flutter/foundation.dart'; @@ -57,47 +55,19 @@ abstract class Bip39HDCurrency extends Bip39Currency { } DerivePathType addressType({required String address}) { - Uint8List? decodeBase58; - Segwit? decodeBech32; - try { - decodeBase58 = bs58check.decode(address); - } catch (err) { - // Base58check decode fail - } - if (decodeBase58 != null) { - if (decodeBase58[0] == networkParams.p2pkhPrefix) { - // P2PKH - return DerivePathType.bip44; - } - if (decodeBase58[0] == networkParams.p2shPrefix) { - // P2SH - return DerivePathType.bip49; - } - throw ArgumentError('Invalid version or Network mismatch'); - } else { - try { - decodeBech32 = segwit.decode(address, networkParams.bech32Hrp); - } catch (err) { - // Bech32 decode fail - } - 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; - } - } + final address2 = coinlib.Address.fromString(address, networkParams); - // P2WPKH + if (address2 is coinlib.P2PKHAddress) { + return DerivePathType.bip44; + } else if (address2 is coinlib.P2SHAddress) { + return DerivePathType.bip49; + } else if (address2 is coinlib.P2WPKHAddress) { return DerivePathType.bip84; + } else if (address2 is coinlib.P2TRAddress) { + return DerivePathType.bip86; + } else { + // TODO: [prio=med] better error handling + throw ArgumentError('Invalid address'); } } }