Skip to content

Commit

Permalink
Add event listener support
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Aug 8, 2024
1 parent 16297eb commit 7a223f6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
19 changes: 10 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@types/lodash.omit": "4.5.9",
"husky": "^8.0.3",
"lint-staged": "^13.2.3",
"prettier": "^2.8.4",
"prettier": "3.3.3",
"process": "^0.11.10",
"rimraf": "^3.0.2",
"stream-browserify": "^3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/adapters/BaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Params, Requests } from '../request';
import { SatsConnectAdapter } from './satsConnectAdapter';
import { request } from '../request';
import { RpcResult } from '../types';
import { AddListener } from 'src/provider';

class BaseAdapter extends SatsConnectAdapter {
id = '';
Expand All @@ -17,6 +18,10 @@ class BaseAdapter extends SatsConnectAdapter {
): Promise<RpcResult<Method>> => {
return request(method, params, this.id);
};

addListener: AddListener = (..._args) => {
throw new Error('Method not supported for `BaseAdapter`.');
};
}

export { BaseAdapter };
3 changes: 3 additions & 0 deletions src/adapters/satsConnectAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getRunesApiClient, RunesApi } from '../runes/api';
import { Params, Requests } from '../request';
import { RpcErrorCode, RpcResult } from '../types';
import { AddListener } from 'src/provider';

abstract class SatsConnectAdapter {
abstract readonly id: string;
Expand Down Expand Up @@ -358,6 +359,8 @@ abstract class SatsConnectAdapter {
}
}

abstract addListener: AddListener;

protected abstract requestInternal<Method extends keyof Requests>(
method: Method,
params: Params<Method>
Expand Down
13 changes: 12 additions & 1 deletion src/adapters/xverse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Params, Requests } from '../request';
import { addListener, Params, Requests } from '../request';
import { SatsConnectAdapter } from './satsConnectAdapter';
import { request } from '../request';
import { RpcResult } from '../types';
import { DefaultAdaptersInfo } from '.';
import { AddListener } from 'src/provider/types';

class XverseAdapter extends SatsConnectAdapter {
id = DefaultAdaptersInfo.xverse.id;
Expand All @@ -13,6 +14,16 @@ class XverseAdapter extends SatsConnectAdapter {
): Promise<RpcResult<Method>> => {
return request(method, params, this.id);
};

addListener: AddListener = (event, cb) => {
return addListener(
event,
// The types of the `addListener` function being called here are not
// entirely accurate.
cb as any,
this.id
);
};
}

export { XverseAdapter };
1 change: 1 addition & 0 deletions src/capabilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const extractOrValidateCapabilities = (
createInscription: validateCapability('createInscription'),
createRepeatInscriptions: validateCapability('createRepeatInscriptions'),
signMultipleTransactions: validateCapability('signMultipleTransactions'),
addListener: validateCapability('addListener'),
};

return Object.entries(capabilityMap).reduce((acc, [capability, value]) => {
Expand Down
24 changes: 24 additions & 0 deletions src/provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ import type {
SignTransactionResponse,
} from '../transactions';
import { RpcResponse } from '../types';
import * as v from 'valibot';

// accountChange
export const accountChangeEventName = 'accountChange';
export const accountChangeSchema = v.object({
type: v.literal(accountChangeEventName),
});
export type AccountChangeEvent = v.InferOutput<typeof accountChangeSchema>;

// networkChange
export const networkChangeEventName = 'networkChange';
export const networkChangeSchema = v.object({
type: v.literal(networkChangeEventName),
});
export type NetworkChangeEvent = v.InferOutput<typeof networkChangeSchema>;

export const walletEventSchema = v.variant('type', [accountChangeSchema, networkChangeSchema]);
export type WalletEvent = v.InferOutput<typeof walletEventSchema>;

export type AddListener = <const WalletEventName extends WalletEvent['type']>(
eventName: WalletEventName,
cb: (event: Extract<WalletEvent, { type: WalletEventName }>) => void
) => () => void;

interface BaseBitcoinProvider {
request: <Method extends keyof Requests>(
Expand All @@ -30,6 +53,7 @@ interface BaseBitcoinProvider {
createInscription: (request: string) => Promise<CreateInscriptionResponse>;
createRepeatInscriptions: (request: string) => Promise<CreateRepeatInscriptionsResponse>;
signMultipleTransactions: (request: string) => Promise<SignMultipleTransactionsResponse>;
addListener: AddListener;
}

export type Capability = keyof BaseBitcoinProvider;
Expand Down
18 changes: 17 additions & 1 deletion src/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProviderById } from '../provider';
import { AddListener, getProviderById } from '../provider';
import {
RpcErrorCode,
RpcResult,
Expand Down Expand Up @@ -50,4 +50,20 @@ export const request = async <Method extends keyof Requests>(
};
};

export const addListener = (
event: Parameters<AddListener>[0],
cb: Parameters<AddListener>[1],
providerId?: string
): ReturnType<AddListener> => {
let provider = window.XverseProviders?.BitcoinProvider || window.BitcoinProvider;
if (providerId) {
provider = getProviderById(providerId);
}
if (!provider) {
throw new Error('no wallet provider was found');
}

return provider.addListener(event, cb);
};

export * from './types';

0 comments on commit 7a223f6

Please sign in to comment.