-
Notifications
You must be signed in to change notification settings - Fork 19
Switched to the new cashtab-connect lib #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3894e4c
Switch to the new cashtab-connect library (simpler, more capable, not…
Klakurka b1e0e12
Do nothing if user rejects broadcasting a transaction.
Klakurka a68e6be
Updated Cashtab extension check to happen only once and at page load.
Klakurka bd904d2
Cleaned up code comments.
Klakurka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { | ||
| CashtabConnect, | ||
| CashtabExtensionUnavailableError, | ||
| CashtabAddressDeniedError, | ||
| CashtabTimeoutError | ||
| } from 'cashtab-connect'; | ||
|
|
||
| const cashtab = new CashtabConnect(); | ||
|
|
||
| // Cache for extension status to avoid multiple checks | ||
| let extensionStatusCache: boolean | null = null; | ||
| let extensionStatusPromise: Promise<boolean> | null = null; | ||
|
|
||
| /** | ||
| * Check if the Cashtab extension is available (with caching) | ||
| * This function caches the result to avoid multiple extension checks per page load | ||
| * @returns Promise<boolean> - true if extension is available, false otherwise | ||
| */ | ||
| export const getCashtabProviderStatus = async (): Promise<boolean> => { | ||
| // Return cached result if available | ||
| if (extensionStatusCache !== null) { | ||
| return extensionStatusCache; | ||
| } | ||
|
|
||
| // If a check is already in progress, wait for it | ||
| if (extensionStatusPromise !== null) { | ||
| return extensionStatusPromise; | ||
| } | ||
|
|
||
| extensionStatusPromise = (async () => { | ||
| try { | ||
| const isAvailable = await cashtab.isExtensionAvailable(); | ||
| extensionStatusCache = isAvailable; | ||
| return isAvailable; | ||
| } catch (error) { | ||
| extensionStatusCache = false; | ||
| return false; | ||
| } finally { | ||
| // Clear the promise so future calls can make a fresh check if needed | ||
| extensionStatusPromise = null; | ||
| } | ||
| })(); | ||
|
|
||
| return extensionStatusPromise; | ||
| }; | ||
|
|
||
| /** | ||
| * Clear the cached extension status (useful for testing or if extension state changes) | ||
| */ | ||
| export const clearCashtabStatusCache = (): void => { | ||
| extensionStatusCache = null; | ||
| extensionStatusPromise = null; | ||
| }; | ||
|
|
||
|
|
||
| export const initializeCashtabStatus = async (): Promise<boolean> => { | ||
| return getCashtabProviderStatus(); | ||
| }; | ||
|
|
||
| export const waitForCashtabExtension = async (timeout?: number): Promise<void> => { | ||
| return cashtab.waitForExtension(timeout); | ||
| }; | ||
|
|
||
| /** | ||
| * Request the user's eCash address from their Cashtab wallet | ||
| * @returns Promise<string> - The user's address | ||
| * @throws {CashtabExtensionUnavailableError} When the Cashtab extension is not available | ||
| * @throws {CashtabAddressDeniedError} When the user denies the address request | ||
| * @throws {CashtabTimeoutError} When the request times out | ||
| */ | ||
| export const requestCashtabAddress = async (): Promise<string> => { | ||
| return cashtab.requestAddress(); | ||
| }; | ||
|
|
||
| export const sendXecWithCashtab = async (address: string, amount: string | number): Promise<any> => { | ||
| return cashtab.sendXec(address, amount); | ||
| }; | ||
|
|
||
| /** | ||
| * Open Cashtab with a BIP21 payment URL | ||
| * @param bip21Url - The BIP21 formatted payment URL | ||
| * @param fallbackUrl - Optional fallback URL if extension is not available | ||
| */ | ||
| export const openCashtabPayment = async (bip21Url: string, fallbackUrl?: string): Promise<void> => { | ||
| try { | ||
| const isAvailable = await getCashtabProviderStatus(); | ||
|
|
||
| if (isAvailable) { | ||
| const url = new URL(bip21Url); | ||
| const address = url.pathname; | ||
| const amount = url.searchParams.get('amount'); | ||
|
|
||
| if (amount) { | ||
| await sendXecWithCashtab(address, amount); | ||
| } else { | ||
| const webUrl = fallbackUrl || `https://cashtab.com/#/send?bip21=${encodeURIComponent(bip21Url)}`; | ||
| window.open(webUrl, '_blank'); | ||
| } | ||
| } else { | ||
| const webUrl = fallbackUrl || `https://cashtab.com/#/send?bip21=${encodeURIComponent(bip21Url)}`; | ||
| window.open(webUrl, '_blank'); | ||
| } | ||
| } catch (error) { | ||
| if (error instanceof CashtabAddressDeniedError) { | ||
| // User rejected the transaction - do nothing for now | ||
| // This case is handled here in case we want to add specific behavior in the future | ||
| return; | ||
| } | ||
|
|
||
| const webUrl = fallbackUrl || `https://cashtab.com/#/send?bip21=${encodeURIComponent(bip21Url)}`; | ||
| window.open(webUrl, '_blank'); | ||
| } | ||
| }; | ||
|
|
||
| export { | ||
| CashtabExtensionUnavailableError, | ||
| CashtabAddressDeniedError, | ||
| CashtabTimeoutError | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this approach -- checking status on button click -- is probably effective in most current contexts. However it would be wasteful for any application where multiple clicks are expected.
The cashtab provider status is not expected to change during a user's visit (and, even if it did --- e.g. if the user installs the cashtab extension --- a refresh is required for this to be detected).
Would be more robust and payments would be faster if this check were performed on page load. This would also allow you to conditionally render the button, i.e. show the user that it will open Cashtab or the mobile app or Electrum before a click.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea - now we can make this nice little text change on widget load: