applesauce-wallet-connect
Version:
NIP-47 Nostr Wallet Connect implementation for both clients and services.
160 lines (159 loc) • 8.7 kB
TypeScript
import { EncryptionMethod } from "applesauce-core/helpers";
import { EventSigner } from "applesauce-core";
import { NostrEvent } from "applesauce-core/helpers";
import { BehaviorSubject, Observable, Subscription } from "rxjs";
import { NotificationType, WalletAuthURI, WalletConnectEncryptionMethod, WalletConnectURI, WalletNotification, WalletNotificationEvent, WalletResponseEvent, WalletSupport } from "./helpers/index.js";
import { CommonWalletMethods, GetBalanceMethod, GetInfoMethod, ListTransactionsMethod, LookupInvoiceMethod, MakeInvoiceMethod, MultiPayInvoiceMethod, MultiPayKeysendMethod, PayInvoiceMethod, PayKeysendMethod, Transaction, TWalletMethod } from "./helpers/methods.js";
import { NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "./interop.js";
export type SerializedWalletConnect = WalletConnectURI;
export type WalletConnectOptions = NostrConnectionMethodsOptions & {
/** 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;
/** Whether to accept the relay hint from the wallet service */
acceptRelayHint?: boolean;
};
export type WaitForPaidInvoice = MakeInvoiceMethod["response"]["result"] | LookupInvoiceMethod["request"]["params"];
export type WaitForPaidOptions = {
/** Polling interval in milliseconds when payment_received notifications are not supported */
pollInterval?: number;
};
export declare class WalletConnect<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 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 */
protected relays$: BehaviorSubject<string[]>;
get relays(): string[];
/** Whether to accept the relay hint from the wallet service */
acceptRelayHint: boolean;
/** The wallet service public key ( unset if waiting for service ) */
service$: BehaviorSubject<string | undefined>;
get service(): string | undefined;
/** The wallet connect URI for this connection, or undefined if the service pubkey is not yet known */
get connectURI(): 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<Method extends Methods>(event: WalletResponseEvent, encryption?: EncryptionMethod): Promise<Method["response"]>;
/** Handle notification events */
protected handleNotificationEvent(event: WalletNotificationEvent): Promise<WalletNotification>;
/** Generic call method with generic type */
genericCall<Method extends TWalletMethod>(method: Method["method"], params: Method["request"]["params"], options?: {
timeout?: number;
}): Observable<Method["response"] | Method["error"]>;
/** Request method with generic type */
genericRequest<Method extends TWalletMethod>(method: Method["method"], params: Method["request"]["params"], options?: {
timeout?: number;
}): Promise<Method["response"]["result"]>;
/** Call a method and return an observable of results */
call<K extends Methods["method"]>(method: K, params: Extract<Methods, {
method: K;
}>["request"]["params"], options?: {
timeout?: number;
}): Observable<Extract<Methods, {
method: K;
}>["response"] | Extract<Methods, {
method: K;
}>["error"]>;
/** Typed request method, returns the result or throws and error */
request<K extends Methods["method"]>(method: K, params: Extract<Methods, {
method: K;
}>["request"]["params"], options?: {
timeout?: number;
}): Promise<Extract<Methods, {
method: K;
}>["response"]["result"]>;
/**
* 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<Methods>, "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: string): 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<PayInvoiceMethod["response"]["result"]>;
/** Pay multiple lightning invoices */
payMultipleInvoices(invoices: Array<{
id?: string;
invoice: string;
amount?: number;
}>): Promise<MultiPayInvoiceMethod["response"]["result"][]>;
/** Send a keysend payment */
payKeysend(pubkey: string, amount: number, preimage?: string, tlv_records?: Array<{
type: number;
value: string;
}>): Promise<PayKeysendMethod["response"]["result"]>;
/** Send multiple keysend payments */
payMultipleKeysend(keysends: Array<{
id?: string;
pubkey: string;
amount: number;
preimage?: string;
tlv_records?: Array<{
type: number;
value: string;
}>;
}>): Promise<MultiPayKeysendMethod["response"]["result"][]>;
/** Create a new invoice */
makeInvoice(amount: number, options?: Omit<MakeInvoiceMethod["request"]["params"], "amount">): Promise<MakeInvoiceMethod["response"]["result"]>;
/** Wait for an invoice to be paid, or reject when it expires */
waitForPaid(invoice: WaitForPaidInvoice, options?: WaitForPaidOptions): Promise<Transaction>;
/** Look up an invoice by payment hash or invoice string */
lookupInvoice(payment_hash?: string, invoice?: string): Promise<LookupInvoiceMethod["response"]["result"]>;
/** List transactions */
listTransactions(params?: {
from?: number;
until?: number;
limit?: number;
offset?: number;
unpaid?: boolean;
type?: "incoming" | "outgoing";
}): Promise<ListTransactionsMethod["response"]["result"]>;
/** Get wallet balance */
getBalance(): Promise<GetBalanceMethod["response"]["result"]>;
/** Get wallet info */
getInfo(): Promise<GetInfoMethod["response"]["result"]>;
/** 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<Methods extends TWalletMethod = CommonWalletMethods>(connectionString: string, options?: Omit<WalletConnectOptions, "secret" | "relays" | "service">): WalletConnect<Methods>;
}