applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
127 lines (126 loc) • 6.81 kB
TypeScript
import { EventSigner } from "applesauce-factory";
import { NostrEvent } from "nostr-tools";
import { Observable, Subscription } from "rxjs";
import { WalletErrorCode } from "./helpers/error.js";
import { NotificationType, WalletNotification } from "./helpers/notification.js";
import { GetBalanceParams, GetInfoParams, ListTransactionsParams, LookupInvoiceParams, MakeInvoiceParams, MultiPayInvoiceParams, MultiPayKeysendParams, PayInvoiceParams, PayKeysendParams, WalletRequest } from "./helpers/request.js";
import { GetBalanceResult, GetInfoResult, ListTransactionsResult, LookupInvoiceResult, MakeInvoiceResult, MultiPayInvoiceResult, MultiPayKeysendResult, PayInvoiceResult, PayKeysendResult, WalletResponse } from "./helpers/response.js";
import { WalletSupport } from "./helpers/support.js";
import { NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "./types.js";
import { WalletAuthURI } from "./helpers/auth-uri.js";
/** Handler function for pay_invoice method */
export type PayInvoiceHandler = (params: PayInvoiceParams) => Promise<PayInvoiceResult>;
/** Handler function for multi_pay_invoice method */
export type MultiPayInvoiceHandler = (params: MultiPayInvoiceParams) => Promise<MultiPayInvoiceResult[]>;
/** Handler function for pay_keysend method */
export type PayKeysendHandler = (params: PayKeysendParams) => Promise<PayKeysendResult>;
/** Handler function for multi_pay_keysend method */
export type MultiPayKeysendHandler = (params: MultiPayKeysendParams) => Promise<MultiPayKeysendResult[]>;
/** Handler function for make_invoice method */
export type MakeInvoiceHandler = (params: MakeInvoiceParams) => Promise<MakeInvoiceResult>;
/** Handler function for lookup_invoice method */
export type LookupInvoiceHandler = (params: LookupInvoiceParams) => Promise<LookupInvoiceResult>;
/** Handler function for list_transactions method */
export type ListTransactionsHandler = (params: ListTransactionsParams) => Promise<ListTransactionsResult>;
/** Handler function for get_balance method */
export type GetBalanceHandler = (params: GetBalanceParams) => Promise<GetBalanceResult>;
/** Handler function for get_info method */
export type GetInfoHandler = (params: GetInfoParams) => Promise<GetInfoResult>;
/** Map of method handlers for the wallet service */
export interface WalletServiceHandlers {
pay_invoice?: PayInvoiceHandler;
multi_pay_invoice?: MultiPayInvoiceHandler;
pay_keysend?: PayKeysendHandler;
multi_pay_keysend?: MultiPayKeysendHandler;
make_invoice?: MakeInvoiceHandler;
lookup_invoice?: LookupInvoiceHandler;
list_transactions?: ListTransactionsHandler;
get_balance?: GetBalanceHandler;
get_info?: GetInfoHandler;
}
export type SerializedWalletService = {
/** The client's public key */
client: string;
/** The relays to use for the service */
relays: string[];
};
/** Options for creating a WalletService */
export interface WalletServiceOptions {
/** 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;
/** Map of method handlers */
handlers: WalletServiceHandlers;
/** An array of notifications this wallet supports */
notifications?: NotificationType[];
/** An optional method for subscribing to relays */
subscriptionMethod?: NostrSubscriptionMethod;
/** An optional method for publishing events */
publishMethod?: NostrPublishMethod;
/** An optional pool for connection methods */
pool?: NostrPool;
}
/** NIP-47 Wallet Service implementation */
export declare class WalletService {
/** 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;
/** 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;
/** Wallet support information */
protected readonly support: WalletSupport;
/** 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<NostrEvent> | null;
/** Subscription to the events observable */
protected subscription: Subscription | null;
/** Whether the service is currently running */
running: boolean;
constructor(options: WalletServiceOptions);
/** 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: NostrEvent): Promise<void>;
/** Process a decrypted wallet request */
protected processRequest(requestEvent: NostrEvent, request: WalletRequest): Promise<void>;
/** Send a success response */
protected sendSuccessResponse<T extends WalletResponse>(requestEvent: NostrEvent, method: T["result_type"], result: T["result"]): Promise<void>;
/** Send an error response */
protected sendErrorResponse(requestEvent: NostrEvent, errorType: WalletErrorCode, errorMessage: string): Promise<void>;
/** Send a response event */
protected sendResponse(requestEvent: NostrEvent, response: WalletResponse): Promise<void>;
/** Creates a service for a nostr+walletauth URI */
static fromAuthURI(uri: string | WalletAuthURI, options: Omit<WalletServiceOptions, "relays">): WalletService;
}