diff --git a/src/components/btc-wallet/XpubPage.tsx b/src/components/btc-wallet/XpubPage.tsx index 093e511..82b3c62 100644 --- a/src/components/btc-wallet/XpubPage.tsx +++ b/src/components/btc-wallet/XpubPage.tsx @@ -5,21 +5,30 @@ import React, { useContext, useEffect, useState } from "react"; import { AppContext } from "../../store/AppContext"; import { useFeature } from "../../hooks/useFeature"; import { PageContent } from "../index"; +import { constants, getBtcPurpose } from "../../util/helpers"; + const { Search, TextArea } = Input; const SEARCH_ID = "xpub-data"; +const WALLET_TYPE_LABELS = { + [constants.BTC_PURPOSE_LEGACY]: "Legacy", + [constants.BTC_PURPOSE_WRAPPED_SEGWIT]: "Wrapped SegWit", + [constants.BTC_PURPOSE_SEGWIT]: "Native SegWit", +} as const; + interface XpubPageProps { session: any; } const XpubPage: React.FC = ({ session }) => { - const context = useContext(AppContext); + const { isMobile } = useContext(AppContext); const { SUPPORTS_XPUB } = useFeature(); const [xpub, setXpub] = useState(null); const [windowWidth, setWindowWidth] = useState( document.getElementById("main-content-inner")?.offsetWidth ); const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); useEffect(() => { updateXpubAddress(); @@ -34,17 +43,31 @@ const XpubPage: React.FC = ({ session }) => { const updateXpubAddress = async () => { if (!session) return; + // Get the active BTC purpose (wallet type) + const btcPurpose = getBtcPurpose(); + if (btcPurpose === constants.BTC_PURPOSE_NONE) { + setError("BTC Wallet is hidden. Please enable it in settings to view XPUB."); + return; + } + setLoading(true); + setError(null); + try { + // Use the active wallet's BTC purpose as the derivation path const addresses = await session.client?.getAddresses({ - startPath: [44 + 0x80000000, 0 + 0x80000000, 0 + 0x80000000], - flag: 6, + startPath: [btcPurpose, constants.BTC_COIN, 0 + constants.HARDENED_OFFSET], + flag: 6, // LatticeGetAddressesFlag.secp256k1Xpub }); + if (addresses && addresses.length > 0) { setXpub(addresses[0]); + } else { + setError("No XPUB returned from device"); } } catch (err) { console.error("Error fetching XPUB:", err); + setError("Failed to fetch XPUB from device"); } finally { setLoading(false); } @@ -61,8 +84,10 @@ const XpubPage: React.FC = ({ session }) => { } }; + const walletTypeLabel = WALLET_TYPE_LABELS[getBtcPurpose()] || "Unknown"; + const renderXpubBox = () => { - if (context.isMobile) { + if (isMobile) { return (