applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
108 lines (107 loc) • 5.98 kB
TypeScript
import { EventSigner } from "applesauce-core";
import { Observable, Subscription } from "rxjs";
import { WalletAuthURI } from "./helpers/auth-uri.js";
import { WalletErrorCode } from "./helpers/error.js";
import { CommonWalletMethods, TWalletMethod, WalletInfo } from "./helpers/methods.js";
import { NotificationType, WalletNotification } from "./helpers/notification.js";
import { WalletRequestEvent } from "./helpers/request.js";
import { WalletSupport } from "./helpers/support.js";
import { NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "./interop.js";
/** Generic type for wallet method handlers */
export type WalletMethodHandler<Method extends TWalletMethod> = (params: Method["request"]["params"]) => Promise<Method["response"]["result"]>;
/** Map of method handlers for the wallet service for a specific set of methods */
export type WalletServiceHandlers<Methods extends TWalletMethod = TWalletMethod> = {
[]]?: WalletMethodHandler<Extract<Methods, {
method: K;
}>>;
};
/** Serialized wallet service */
export type SerializedWalletService = {
/** The client's public key */
client: string;
/** The relays to use for the service */
relays: string[];
};
/** Only the necessary info for the getInfo method on wallet service */
export type WalletServiceInfo = Partial<Omit<WalletInfo, "methods" | "notifications">>;
/** Options for creating a WalletService */
export interface WalletServiceOptions<Methods extends TWalletMethod = CommonWalletMethods> extends NostrConnectionMethodsOptions {
/** The relays to use for the service */
relays: string[];
/** The signer to use for creating and unlocking events */
signer: EventSigner;
/** The client's secret key */
secret?: Uint8Array;
/** The client's public key (used for restoring the service) */
client?: string;
/** A method for getting the general wallet information (Can be overwritten if get_info is set in handlers) */
getInfo?: () => Promise<WalletServiceInfo>;
/** Map of method handlers */
handlers: WalletServiceHandlers<Methods>;
/** An array of notifications this wallet supports */
notifications?: NotificationType[];
}
/** NIP-47 Wallet Service implementation */
export declare class WalletService<Methods extends TWalletMethod = CommonWalletMethods> {
/** A fallback method to use for subscriptionMethod if none is passed in when creating the client */
static subscriptionMethod: NostrSubscriptionMethod | undefined;
/** A fallback method to use for publishMethod if none is passed in when creating the client */
static publishMethod: NostrPublishMethod | undefined;
/** A fallback pool to use if none is pass in when creating the signer */
static pool: NostrPool | undefined;
/** A method for subscribing to relays */
protected readonly subscriptionMethod: NostrSubscriptionMethod;
/** A method for publishing events */
protected readonly publishMethod: NostrPublishMethod;
protected log: import("debug").Debugger;
/** A special method for getting the generic wallet information */
getInfo?: () => Promise<WalletServiceInfo>;
/** The relays to use for the service */
readonly relays: string[];
/** The signer used for creating and unlocking events */
protected readonly signer: EventSigner;
/** Map of method handlers */
protected readonly handlers: WalletServiceHandlers<Methods>;
/** Wallet support information */
protected readonly support: WalletSupport<Methods>;
/** The service's public key */
pubkey: string | null;
/** The client's secret key */
protected secret?: Uint8Array;
/** The client's public key */
client: string;
/** Shared observable for all wallet request events */
protected events$: Observable<WalletRequestEvent> | null;
/** Subscription to the events observable */
protected subscription: Subscription | null;
/** Whether the service is currently running */
running: boolean;
constructor(options: WalletServiceOptions<Methods>);
/** Start the wallet service */
start(): Promise<void>;
/** Stop the wallet service */
stop(): void;
/** Check if the service is running */
isRunning(): boolean;
/** Get the connection URI for the service */
getConnectURI(): string;
/** Send a notification to the client */
notify<T extends WalletNotification>(type: T["notification_type"], notification: T["notification"], legacy?: boolean): Promise<void>;
/** Publish the wallet support event */
protected publishSupportEvent(): Promise<void>;
/** Handle a wallet request event */
protected handleRequestEvent(requestEvent: WalletRequestEvent): Promise<void>;
/** Process a decrypted wallet request */
protected processRequest<Method extends Methods>(requestEvent: WalletRequestEvent, request: Method["request"]): Promise<void>;
/** Send a success response */
protected sendSuccessResponse<Method extends Methods>(requestEvent: WalletRequestEvent, method: Method["response"]["result_type"], result: Method["response"]["result"]): Promise<void>;
/** Send an error response */
protected sendErrorResponse<Method extends Methods>(requestEvent: WalletRequestEvent, errorType: WalletErrorCode, errorMessage: string): Promise<void>;
/** Send a response event */
protected sendResponse<Method extends Methods>(requestEvent: WalletRequestEvent, response: Method["response"] | Method["error"]): Promise<void>;
/** Creates a service for a nostr+walletauth URI */
static fromAuthURI<Methods extends TWalletMethod = CommonWalletMethods>(uri: string | WalletAuthURI, options: Omit<WalletServiceOptions<Methods>, "relays"> & {
/** A relay or method to select a single relay for the client and service to communicate over */
overrideRelay?: string | ((relays: string[]) => string);
}): WalletService<Methods>;
}