|
| 1 | +import { |
| 2 | + CashtabConnect, |
| 3 | + CashtabExtensionUnavailableError, |
| 4 | + CashtabAddressDeniedError, |
| 5 | + CashtabTimeoutError |
| 6 | +} from 'cashtab-connect'; |
| 7 | + |
| 8 | +const cashtab = new CashtabConnect(); |
| 9 | + |
| 10 | +// Cache for extension status to avoid multiple checks |
| 11 | +let extensionStatusCache: boolean | null = null; |
| 12 | +let extensionStatusPromise: Promise<boolean> | null = null; |
| 13 | + |
| 14 | +/** |
| 15 | + * Check if the Cashtab extension is available (with caching) |
| 16 | + * This function caches the result to avoid multiple extension checks per page load |
| 17 | + * @returns Promise<boolean> - true if extension is available, false otherwise |
| 18 | + */ |
| 19 | +export const getCashtabProviderStatus = async (): Promise<boolean> => { |
| 20 | + // Return cached result if available |
| 21 | + if (extensionStatusCache !== null) { |
| 22 | + return extensionStatusCache; |
| 23 | + } |
| 24 | + |
| 25 | + // If a check is already in progress, wait for it |
| 26 | + if (extensionStatusPromise !== null) { |
| 27 | + return extensionStatusPromise; |
| 28 | + } |
| 29 | + |
| 30 | + extensionStatusPromise = (async () => { |
| 31 | + try { |
| 32 | + const isAvailable = await cashtab.isExtensionAvailable(); |
| 33 | + extensionStatusCache = isAvailable; |
| 34 | + return isAvailable; |
| 35 | + } catch (error) { |
| 36 | + extensionStatusCache = false; |
| 37 | + return false; |
| 38 | + } finally { |
| 39 | + // Clear the promise so future calls can make a fresh check if needed |
| 40 | + extensionStatusPromise = null; |
| 41 | + } |
| 42 | + })(); |
| 43 | + |
| 44 | + return extensionStatusPromise; |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Clear the cached extension status (useful for testing or if extension state changes) |
| 49 | + */ |
| 50 | +export const clearCashtabStatusCache = (): void => { |
| 51 | + extensionStatusCache = null; |
| 52 | + extensionStatusPromise = null; |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | +export const initializeCashtabStatus = async (): Promise<boolean> => { |
| 57 | + return getCashtabProviderStatus(); |
| 58 | +}; |
| 59 | + |
| 60 | +export const waitForCashtabExtension = async (timeout?: number): Promise<void> => { |
| 61 | + return cashtab.waitForExtension(timeout); |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * Request the user's eCash address from their Cashtab wallet |
| 66 | + * @returns Promise<string> - The user's address |
| 67 | + * @throws {CashtabExtensionUnavailableError} When the Cashtab extension is not available |
| 68 | + * @throws {CashtabAddressDeniedError} When the user denies the address request |
| 69 | + * @throws {CashtabTimeoutError} When the request times out |
| 70 | + */ |
| 71 | +export const requestCashtabAddress = async (): Promise<string> => { |
| 72 | + return cashtab.requestAddress(); |
| 73 | +}; |
| 74 | + |
| 75 | +export const sendXecWithCashtab = async (address: string, amount: string | number): Promise<any> => { |
| 76 | + return cashtab.sendXec(address, amount); |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Open Cashtab with a BIP21 payment URL |
| 81 | + * @param bip21Url - The BIP21 formatted payment URL |
| 82 | + * @param fallbackUrl - Optional fallback URL if extension is not available |
| 83 | + */ |
| 84 | +export const openCashtabPayment = async (bip21Url: string, fallbackUrl?: string): Promise<void> => { |
| 85 | + try { |
| 86 | + const isAvailable = await getCashtabProviderStatus(); |
| 87 | + |
| 88 | + if (isAvailable) { |
| 89 | + const url = new URL(bip21Url); |
| 90 | + const address = url.pathname; |
| 91 | + const amount = url.searchParams.get('amount'); |
| 92 | + |
| 93 | + if (amount) { |
| 94 | + await sendXecWithCashtab(address, amount); |
| 95 | + } else { |
| 96 | + const webUrl = fallbackUrl || `https://cashtab.com/#/send?bip21=${encodeURIComponent(bip21Url)}`; |
| 97 | + window.open(webUrl, '_blank'); |
| 98 | + } |
| 99 | + } else { |
| 100 | + const webUrl = fallbackUrl || `https://cashtab.com/#/send?bip21=${encodeURIComponent(bip21Url)}`; |
| 101 | + window.open(webUrl, '_blank'); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + if (error instanceof CashtabAddressDeniedError) { |
| 105 | + // User rejected the transaction - do nothing for now |
| 106 | + // This case is handled here in case we want to add specific behavior in the future |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const webUrl = fallbackUrl || `https://cashtab.com/#/send?bip21=${encodeURIComponent(bip21Url)}`; |
| 111 | + window.open(webUrl, '_blank'); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +export { |
| 116 | + CashtabExtensionUnavailableError, |
| 117 | + CashtabAddressDeniedError, |
| 118 | + CashtabTimeoutError |
| 119 | +}; |
0 commit comments