applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
129 lines (128 loc) • 6.53 kB
TypeScript
import { EncryptionMethod } from "applesauce-core/helpers";
import { EventSigner } from "applesauce-factory";
import { NostrEvent } from "nostr-tools";
import { BehaviorSubject, Observable, Subscription } from "rxjs";
import { GetBalanceResult, GetInfoResult, ListTransactionsResult, LookupInvoiceResult, MakeInvoiceParams, MakeInvoiceResult, NotificationType, PayInvoiceResult, PayKeysendResult, WalletAuthURI, WalletConnectEncryptionMethod, WalletConnectURI, WalletMethod, WalletNotification, WalletRequest, WalletResponse, WalletSupport } from "./helpers/index.js";
import { NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "./types.js";
export type SerializedWalletConnect = WalletConnectURI;
export type WalletConnectOptions = {
/** The secret to use for the connection */
secret: Uint8Array;
/** The relays to use for the connection */
relays: string[];
/** The service pubkey to use for the connection (optional) */
service?: string;
/** Default timeout for RPC requests in milliseconds */
timeout?: number;
/** A method for subscribing to relays */
subscriptionMethod?: NostrSubscriptionMethod;
/** A method for publishing events */
publishMethod?: NostrPublishMethod;
/** An optional pool for connection methods */
pool?: NostrPool;
};
export declare class WalletConnect {
/** 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 that is called when an event needs to be published */
protected publishMethod: NostrPublishMethod;
/** The active nostr subscription method */
protected subscriptionMethod: NostrSubscriptionMethod;
/** The local client signer */
readonly secret: Uint8Array;
protected readonly signer: EventSigner;
/** The relays to use for the connection */
readonly relays: string[];
/** The wallet service public key ( unset if waiting for service ) */
service$: BehaviorSubject<string | undefined>;
get service(): string | undefined;
/** Default timeout for requests */
defaultTimeout: number;
/** Observable for wallet info updates */
support$: Observable<WalletSupport | null>;
/** The preferred encryption method for the wallet */
encryption$: Observable<WalletConnectEncryptionMethod>;
/** Shared observable for all wallet response events and notifications */
protected events$: Observable<NostrEvent>;
/** Shared observable for all wallet notifications */
notifications$: Observable<WalletNotification>;
/** An internal observable for listening for the wallet service to connect */
protected waitForService$: Observable<string>;
constructor(options: WalletConnectOptions);
/** Process response events and return WalletResponse or throw error */
protected handleResponseEvent(event: NostrEvent, encryption?: EncryptionMethod): Promise<WalletResponse>;
/** Handle notification events */
protected handleNotificationEvent(event: NostrEvent): Promise<WalletNotification>;
/** Core RPC method that makes a request and returns the response */
request(request: WalletRequest, options?: {
timeout?: number;
}): Observable<WalletResponse>;
/**
* Listen for a type of notification
* @returns a method to unsubscribe the listener
*/
notification<T extends WalletNotification>(type: T["notification_type"], listener: (notification: T["notification"]) => any): Subscription;
/** Gets the nostr+walletauth URI for the connection */
getAuthURI(parts?: Omit<WalletAuthURI, "client" | "relays">): string;
/** Wait for the wallet service to connect */
waitForService(abortSignal?: AbortSignal): Promise<string>;
/** Get the wallet support info */
getSupport(): Promise<WalletSupport | null>;
/** Check if the wallet supports a method */
supportsMethod(method: WalletMethod): Promise<boolean>;
/** Check if the wallet supports notifications */
supportsNotifications(): Promise<boolean>;
/** Check if the wallet supports a notification type */
supportsNotificationType(type: NotificationType): Promise<boolean>;
/** Pay a lightning invoice */
payInvoice(invoice: string, amount?: number): Promise<PayInvoiceResult>;
/** Pay multiple lightning invoices */
payMultipleInvoices(invoices: Array<{
id?: string;
invoice: string;
amount?: number;
}>): Promise<PayInvoiceResult[]>;
/** Send a keysend payment */
payKeysend(pubkey: string, amount: number, preimage?: string, tlv_records?: Array<{
type: number;
value: string;
}>): Promise<PayKeysendResult>;
/** Send multiple keysend payments */
payMultipleKeysend(keysends: Array<{
id?: string;
pubkey: string;
amount: number;
preimage?: string;
tlv_records?: Array<{
type: number;
value: string;
}>;
}>): Promise<PayKeysendResult[]>;
/** Create a new invoice */
makeInvoice(amount: number, options?: Omit<MakeInvoiceParams, "amount">): Promise<MakeInvoiceResult>;
/** Look up an invoice by payment hash or invoice string */
lookupInvoice(payment_hash?: string, invoice?: string): Promise<LookupInvoiceResult>;
/** List transactions */
listTransactions(params?: {
from?: number;
until?: number;
limit?: number;
offset?: number;
unpaid?: boolean;
type?: "incoming" | "outgoing";
}): Promise<ListTransactionsResult>;
/** Get wallet balance */
getBalance(): Promise<GetBalanceResult>;
/** Get wallet info */
getInfo(): Promise<GetInfoResult>;
/** Serialize the WalletConnect instance */
toJSON(): SerializedWalletConnect;
/** Create a new WalletConnect instance from a serialized object */
static fromJSON(json: SerializedWalletConnect, options?: Omit<WalletConnectOptions, "secret" | "relays" | "service">): WalletConnect;
/** Create a new WalletConnect instance from a connection string */
static fromConnectURI(connectionString: string, options?: Omit<WalletConnectOptions, "secret" | "relays" | "service">): WalletConnect;
}