-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: fix default installed handling
- Loading branch information
Showing
9 changed files
with
109 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,31 +1,56 @@ | ||
// AUTO REGISTERED PROVIDERS | ||
|
||
export interface StxProvider { | ||
export interface WebBTCProvider { | ||
/** The global "path" of the provider (e.g. `"MyProvider"` if registered at `window.MyProvider`) */ | ||
id: string; | ||
/** The name of the provider, as displayed to the user */ | ||
name: string; | ||
/** The data URL of an image to show (e.g. `data:image/png;base64,iVBORw0...`) @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs */ | ||
icon?: string; | ||
/** Different web URLs of the provider */ | ||
urls?: { | ||
about?: string; | ||
chromeWebStore?: string; | ||
mozillaWebStore?: string; | ||
androidAppStore?: string; | ||
iOSAppStore?: string; | ||
}; | ||
/** Web URL of the provider */ | ||
webUrl?: string; | ||
|
||
chromeWebStoreUrl?: string; | ||
mozillaAddOnsUrl?: string; | ||
googlePlayStoreUrl?: string; | ||
iOSAppStoreUrl?: string; | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
webbtc_stx_providers?: any; // replace 'any' with the actual type if known | ||
/** | ||
* @experimental @beta | ||
* An experimental global registry of providers that can be used by the UI to display a list of providers to the user. | ||
* Wallets can add their provider to this list without needing to update `@stacks/connect` itself. | ||
* This is an experimental API and may change in the future. | ||
* This will be used as a test-run before switching to `webbtc` and WBIP004. | ||
* The provider objects are WBIP004 compliant. | ||
* It may happen that no wallet implements this feature before `@stacks/connect` switches to `webbtc`. | ||
*/ | ||
webbtc_stx_providers?: WebBTCProvider[]; | ||
} | ||
} | ||
|
||
export const getRegisteredProviders = () => { | ||
if (typeof window === 'undefined') return []; | ||
if (!window.webbtc_stx_providers) return []; | ||
|
||
return window.webbtc_stx_providers as StxProvider[]; | ||
return window.webbtc_stx_providers; | ||
}; | ||
|
||
export const getInstalledProviders = (defaultProviders: WebBTCProvider[] = []) => { | ||
if (typeof window === 'undefined') return []; | ||
|
||
const registeredProviders = getRegisteredProviders(); | ||
|
||
const additionalInstalledProviders = defaultProviders.filter(dp => { | ||
// already registered, don't add again | ||
if (registeredProviders.find(rp => rp.id === dp.id)) return false; | ||
|
||
// check if default provider is installed (even if not registered) | ||
const provider = dp.id.split('.').reduce((acc, part) => acc?.[part], window); | ||
return !!provider; | ||
}); | ||
|
||
return registeredProviders.concat(additionalInstalledProviders); | ||
}; |
This file contains 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 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 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
Oops, something went wrong.