Skip to content

Commit

Permalink
update adapters interface
Browse files Browse the repository at this point in the history
  • Loading branch information
m-aboelenein committed Mar 26, 2024
1 parent 2ec36ba commit 8ca6048
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 60 deletions.
15 changes: 2 additions & 13 deletions src/adapters/BaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@ import { Provider, getProviderById } from '../provider';

class BaseAdapter extends SatsConnectAdapter {
id = '';
name = '';
url = '';

supportedMethods: (keyof StxRequests | keyof BtcRequests)[] = [];

constructor(providerInfo: Provider) {
constructor(providerId: string) {
super();
this.id = providerInfo.id;
this.name = providerInfo.name;
this.url = providerInfo.webUrl || '';
this.supportedMethods = providerInfo.methods || [];
this.id = providerId;
}

request = async <Method extends keyof Requests>(
method: Method,
params: Params<Method>
): Promise<RpcResult<Method> | undefined> => {
if (!this.supportedMethods.includes(method)) {
console.error('Method not supported by the selected wallet');
}

return request(method, params, this.id);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const defaultAdapters: Record<string, new () => SatsConnectAdapter> = {
[DefaultAdaptersInfo.unisat.id]: UnisatAdapter,
};

export * from './persistence';
export * from './satsConnectAdapter';
export * from './baseAdapter';
11 changes: 0 additions & 11 deletions src/adapters/persistence.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/adapters/satsConnectAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { RpcResult } from '../types';

abstract class SatsConnectAdapter {
abstract readonly id: string;
abstract readonly name: string;
abstract url: string;

supportedMethods: (StxRequestMethod | BtcRequestMethod)[] = [];

abstract request<Method extends keyof Requests>(
method: Method,
Expand Down
18 changes: 0 additions & 18 deletions src/adapters/unisat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,12 @@ declare global {

class UnisatAdapter extends SatsConnectAdapter {
id = 'unisat';
name = 'Unisat';
url = 'https://unisat.io/';

supportedMethods: (keyof StxRequests | keyof BtcRequests)[] = [
'getAccounts',
'sendTransfer',
'signMessage',
'signPsbt',
];

private async getAccounts(): Promise<Return<'getAccounts'>> {
const [accounts, publickKey] = await Promise.all([
window.unisat.requestAccounts(),
window.unisat.getPublicKey(),
]);
// to-do: create a generic purpose type for the response
const response: Return<'getAccounts'> = accounts.map((address) => ({
address,
publicKey: publickKey,
Expand Down Expand Up @@ -108,11 +98,6 @@ class UnisatAdapter extends SatsConnectAdapter {
address,
index: indexes[0],
sighashTypes: allowedSignHash ? [allowedSignHash] : undefined,
/**
* to-do: get the public key from the address
*/
// disableTweakSigner: true,
// publicKey: '',
})),
});
if (broadcast) {
Expand All @@ -131,9 +116,6 @@ class UnisatAdapter extends SatsConnectAdapter {
method: Method,
params: Params<Method>
): Promise<RpcResult<Method> | undefined> => {
if (!this.supportedMethods.includes(method)) {
console.error('Method not supported by the selected wallet');
}
try {
switch (method) {
case 'getAccounts': {
Expand Down
13 changes: 0 additions & 13 deletions src/adapters/xverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@ import { RpcResult } from '../types';

class XverseAdapter extends SatsConnectAdapter {
id = 'XverseProviders.BitcoinProvider';
name = 'Xverse';
url = 'xverse.app';

supportedMethods: (keyof StxRequests | keyof BtcRequests)[] = [
'getAccounts',
'sendTransfer',
'signMessage',
'signPsbt',
];

request = async <Method extends keyof Requests>(
method: Method,
params: Params<Method>
): Promise<RpcResult<Method> | undefined> => {
if (!this.supportedMethods.includes(method)) {
console.error('Method not supported by the selected wallet');
}

return request(method, params, this.id);
};
}
Expand Down
12 changes: 12 additions & 0 deletions src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export function isProviderInstalled(providerId: string) {
return !!getProviderById(providerId);
}

export function setDefaultProvider(providerId: string) {
localStorage.setItem('sats-connect_defaultProvider', providerId);
}

export function getDefaultProvider() {
return localStorage.getItem('sats-connect_defaultProvider');
}

export function removeDefaultProvider() {
localStorage.removeItem('sats-connect_defaultProvider');
}

export function getSupportedWallets(): SupportedWallet[] {
const btc_providers = getProviders();
for (let key in DefaultAdaptersInfo) {
Expand Down

0 comments on commit 8ca6048

Please sign in to comment.