Skip to content

Commit

Permalink
inject webbtc providersArray and export utils
Browse files Browse the repository at this point in the history
  • Loading branch information
m-aboelenein committed Feb 7, 2024
1 parent 26b90e5 commit f6dd768
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 43 deletions.
25 changes: 0 additions & 25 deletions src/call/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/call/types.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/capabilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const extractOrValidateCapabilities = (
};

const capabilityMap: CapabilityMap = {
call: validateCapability('call'),
request: validateCapability('request'),
listen: validateCapability('listen'),
connect: validateCapability('connect'),
signMessage: validateCapability('signMessage'),
signTransaction: validateCapability('signTransaction'),
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './addresses';
export * from './call';
export * from './request';
export * from './capabilities';
export * from './inscriptions';
export * from './messages';
Expand Down
34 changes: 32 additions & 2 deletions src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import type { BitcoinProvider } from './types';
import { type BitcoinProvider, type WebbtcProvider } from './types';

export const XverseProvider = {
id: 'XverseProviders.BitcoinProvider',
name: 'Xverse Wallet',
icon: 'https://www.xverse.app/',
webUrl: 'https://www.xverse.app/',
chromeWebStoreUrl:
'https://chrome.google.com/webstore/detail/xverse-wallet/idnnbdplmphpflfnlkomgpfbpcgelopg?hl=en-GB&authuser=1',
googlePlayStoreUrl: 'https://play.google.com/store/apps/details?id=com.secretkeylabs.xverse',
iOSAppStoreUrl: 'https://apps.apple.com/app/xverse-bitcoin-web3-wallet/id1552272513',
};

if (!window.webbtc_providers) window.webbtc_providers = [];

window.webbtc_providers.unshift(XverseProvider);

export async function getProviderOrThrow(
getProvider?: () => Promise<BitcoinProvider | undefined>
): Promise<BitcoinProvider> {
const provider = (await getProvider?.()) || window.XverseProviders?.BitcoinProvider || window.BitcoinProvider;
const provider =
(await getProvider?.()) || window.XverseProviders?.BitcoinProvider || window.BitcoinProvider;

if (!provider) {
throw new Error('No Bitcoin wallet installed');
Expand All @@ -12,4 +28,18 @@ export async function getProviderOrThrow(
return provider;
}

export function getProviders(): WebbtcProvider[] {
if (!window.webbtc_providers) window.webbtc_providers = [XverseProvider];
return window.webbtc_providers;
}

export function getProviderById(providerId: string) {
if (Array.isArray(window.webbtc_providers)) {
return window.webbtc_providers.find((provider) => provider.id === providerId);
} else {
console.error('window.webbtc_providers is not defined or not an array');
return null;
}
}

export * from './types';
20 changes: 16 additions & 4 deletions src/provider/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GetAddressResponse } from '../addresses';
import type { CallWalletResponse } from '../call';
import type { GetCapabilitiesResponse } from '../capabilities';
import type { CreateInscriptionResponse, CreateRepeatInscriptionsResponse } from '../inscriptions';
import type { SignMessageResponse } from '../messages';
Expand All @@ -10,7 +9,8 @@ import type {
} from '../transactions';

interface BaseBitcoinProvider {
call: (request: string) => Promise<CallWalletResponse>;
request: (method: string, options: Record<string, any>) => Promise<Record<string, any>>;
listen: (method: string, callback: () => void) => void;
connect: (request: string) => Promise<GetAddressResponse>;
signMessage: (request: string) => Promise<SignMessageResponse>;
signTransaction: (request: string) => Promise<SignTransactionResponse>;
Expand All @@ -26,13 +26,25 @@ export interface BitcoinProvider extends BaseBitcoinProvider {
getCapabilities?: (request: string) => Promise<GetCapabilitiesResponse>;
}

export interface WebbtcProvider {
id: string;
name: string;
icon: string;
webUrl?: string;
chromeWebStoreUrl?: string;
mozillaAddOnsUrl?: string;
googlePlayStoreUrl?: string;
iOSAppStoreUrl?: string;
methods?: string[];
}

declare global {
interface XverseProviders {
BitcoinProvider?: BitcoinProvider;
}

interface Window {
BitcoinProvider?: BitcoinProvider;
XverseProviders?: XverseProviders
XverseProviders?: XverseProviders;
webbtc_providers: WebbtcProvider[];
}
}
15 changes: 15 additions & 0 deletions src/request/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getProviderOrThrow } from '../provider';

export const request = async (method: string, options: Record<string, any>) => {
const provider = await getProviderOrThrow(options.getProvider);

if (!method) {
throw new Error('A wallet method is required');
}

try {
return await provider.request(method, options);
} catch (error) {
console.error('[Connect] Error during call request', error);
}
};
Empty file added src/request/types.ts
Empty file.

0 comments on commit f6dd768

Please sign in to comment.