applesauce-wallet-connect 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/blueprints/request.d.ts +2 -2
- package/dist/blueprints/response.d.ts +2 -2
- package/dist/blueprints/support.d.ts +8 -2
- package/dist/blueprints/support.js +8 -3
- package/dist/helpers/auth-uri.d.ts +7 -9
- package/dist/helpers/auth-uri.js +1 -3
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/methods.d.ts +234 -0
- package/dist/helpers/methods.js +1 -0
- package/dist/helpers/notification.d.ts +1 -1
- package/dist/helpers/request.d.ts +6 -116
- package/dist/helpers/response.d.ts +6 -128
- package/dist/helpers/support.d.ts +7 -7
- package/dist/helpers/support.js +1 -14
- package/dist/wallet-connect.d.ts +47 -19
- package/dist/wallet-connect.js +49 -54
- package/dist/wallet-service.d.ts +31 -43
- package/dist/wallet-service.js +30 -40
- package/package.json +2 -2
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { NostrEvent } from "nostr-tools";
|
|
2
2
|
import { WalletConnectEncryptionMethod } from "./encryption.js";
|
|
3
|
-
import {
|
|
3
|
+
import { TWalletMethod } from "./methods.js";
|
|
4
|
+
import { NotificationType } from "./notification.js";
|
|
4
5
|
export declare const WALLET_INFO_KIND = 13194;
|
|
5
6
|
/** A symbol used to cache the wallet info on the event */
|
|
6
7
|
export declare const WalletInfoSymbol: unique symbol;
|
|
7
|
-
export type WalletMethod = "pay_invoice" | "multi_pay_invoice" | "pay_keysend" | "multi_pay_keysend" | "make_invoice" | "lookup_invoice" | "list_transactions" | "get_balance" | "get_info";
|
|
8
8
|
/** Wallet service capabilities and information */
|
|
9
|
-
export interface WalletSupport {
|
|
9
|
+
export interface WalletSupport<Methods extends TWalletMethod = TWalletMethod> {
|
|
10
10
|
/** List of supported methods for this wallet service */
|
|
11
|
-
methods:
|
|
11
|
+
methods: Array<Methods["method"]>;
|
|
12
12
|
/** List of supported encryption methods */
|
|
13
13
|
encryption: WalletConnectEncryptionMethod[];
|
|
14
14
|
/** List of supported notifications, optional */
|
|
15
15
|
notifications?: NotificationType[];
|
|
16
16
|
}
|
|
17
17
|
/** Gets the wallet info from a kind 13194 event */
|
|
18
|
-
export declare function getWalletSupport(info: NostrEvent): WalletSupport | null;
|
|
18
|
+
export declare function getWalletSupport<Methods extends TWalletMethod = TWalletMethod>(info: NostrEvent): WalletSupport<Methods> | null;
|
|
19
19
|
/** Gets the encryption methods from a wallet info event */
|
|
20
20
|
export declare function getEncryptionMethods(info: NostrEvent | WalletSupport): WalletConnectEncryptionMethod[];
|
|
21
21
|
/** Checks if the wallet service supports a specific encryption method */
|
|
@@ -23,12 +23,12 @@ export declare function supportsEncryption(info: NostrEvent | WalletSupport, enc
|
|
|
23
23
|
/** Gets the preferred encryption method (nip44_v2 preferred over nip04) */
|
|
24
24
|
export declare function getPreferredEncryption(info: NostrEvent | WalletSupport): WalletConnectEncryptionMethod;
|
|
25
25
|
/** Checks if the wallet service supports a specific method */
|
|
26
|
-
export declare function supportsMethod(info: NostrEvent | WalletSupport, method:
|
|
26
|
+
export declare function supportsMethod(info: NostrEvent | WalletSupport, method: string): boolean;
|
|
27
27
|
/** Checks if the wallet service supports notifications */
|
|
28
28
|
export declare function supportsNotifications(info: NostrEvent | WalletSupport): boolean;
|
|
29
29
|
/** Checks if the wallet service supports a specific notification type */
|
|
30
30
|
export declare function supportsNotificationType(info: NostrEvent | WalletSupport, notificationType: NotificationType): boolean;
|
|
31
31
|
/** Gets all supported methods from the wallet info */
|
|
32
|
-
export declare function getSupportedMethods(info: NostrEvent | WalletSupport):
|
|
32
|
+
export declare function getSupportedMethods(info: NostrEvent | WalletSupport): string[];
|
|
33
33
|
/** Gets all supported notifications from the wallet info */
|
|
34
34
|
export declare function getSupportedNotifications(info: NostrEvent | WalletSupport): NotificationType[];
|
package/dist/helpers/support.js
CHANGED
|
@@ -12,20 +12,7 @@ export function getWalletSupport(info) {
|
|
|
12
12
|
return null;
|
|
13
13
|
// Parse methods from content (space-separated)
|
|
14
14
|
const contentParts = content.split(/\s+/);
|
|
15
|
-
const methods = contentParts
|
|
16
|
-
.filter((part) => [
|
|
17
|
-
"pay_invoice",
|
|
18
|
-
"multi_pay_invoice",
|
|
19
|
-
"pay_keysend",
|
|
20
|
-
"multi_pay_keysend",
|
|
21
|
-
"make_invoice",
|
|
22
|
-
"lookup_invoice",
|
|
23
|
-
"list_transactions",
|
|
24
|
-
"get_balance",
|
|
25
|
-
"get_info",
|
|
26
|
-
"notifications",
|
|
27
|
-
].includes(part))
|
|
28
|
-
.filter((part) => part !== "notifications");
|
|
15
|
+
const methods = contentParts.filter((part) => part !== "notifications");
|
|
29
16
|
// Parse encryption methods from encryption tag
|
|
30
17
|
const encryptionTag = getTagValue(info, "encryption");
|
|
31
18
|
const encryption = encryptionTag
|
package/dist/wallet-connect.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { EncryptionMethod } from "applesauce-core/helpers";
|
|
|
2
2
|
import { EventSigner } from "applesauce-factory";
|
|
3
3
|
import { NostrEvent } from "nostr-tools";
|
|
4
4
|
import { BehaviorSubject, Observable, Subscription } from "rxjs";
|
|
5
|
-
import {
|
|
5
|
+
import { NotificationType, WalletAuthURI, WalletConnectEncryptionMethod, WalletConnectURI, WalletNotification, WalletNotificationEvent, WalletResponseEvent, WalletSupport } from "./helpers/index.js";
|
|
6
|
+
import { CommonWalletMethods, GetBalanceMethod, GetInfoMethod, ListTransactionsMethod, LookupInvoiceMethod, MakeInvoiceMethod, MultiPayInvoiceMethod, MultiPayKeysendMethod, PayInvoiceMethod, PayKeysendMethod, TWalletMethod } from "./helpers/methods.js";
|
|
6
7
|
import { NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "./interop.js";
|
|
7
8
|
export type SerializedWalletConnect = WalletConnectURI;
|
|
8
9
|
export type WalletConnectOptions = NostrConnectionMethodsOptions & {
|
|
@@ -14,8 +15,10 @@ export type WalletConnectOptions = NostrConnectionMethodsOptions & {
|
|
|
14
15
|
service?: string;
|
|
15
16
|
/** Default timeout for RPC requests in milliseconds */
|
|
16
17
|
timeout?: number;
|
|
18
|
+
/** Whether to accept the relay hint from the wallet service */
|
|
19
|
+
acceptRelayHint?: boolean;
|
|
17
20
|
};
|
|
18
|
-
export declare class WalletConnect {
|
|
21
|
+
export declare class WalletConnect<Methods extends TWalletMethod = CommonWalletMethods> {
|
|
19
22
|
/** A fallback method to use for subscriptionMethod if none is passed in when creating the client */
|
|
20
23
|
static subscriptionMethod: NostrSubscriptionMethod | undefined;
|
|
21
24
|
/** A fallback method to use for publishMethod if none is passed in when creating the client */
|
|
@@ -30,7 +33,10 @@ export declare class WalletConnect {
|
|
|
30
33
|
readonly secret: Uint8Array;
|
|
31
34
|
protected readonly signer: EventSigner;
|
|
32
35
|
/** The relays to use for the connection */
|
|
33
|
-
|
|
36
|
+
protected relays$: BehaviorSubject<string[]>;
|
|
37
|
+
get relays(): string[];
|
|
38
|
+
/** Whether to accept the relay hint from the wallet service */
|
|
39
|
+
acceptRelayHint: boolean;
|
|
34
40
|
/** The wallet service public key ( unset if waiting for service ) */
|
|
35
41
|
service$: BehaviorSubject<string | undefined>;
|
|
36
42
|
get service(): string | undefined;
|
|
@@ -48,43 +54,65 @@ export declare class WalletConnect {
|
|
|
48
54
|
protected waitForService$: Observable<string>;
|
|
49
55
|
constructor(options: WalletConnectOptions);
|
|
50
56
|
/** Process response events and return WalletResponse or throw error */
|
|
51
|
-
protected handleResponseEvent(event: WalletResponseEvent, encryption?: EncryptionMethod): Promise<
|
|
57
|
+
protected handleResponseEvent<Method extends Methods>(event: WalletResponseEvent, encryption?: EncryptionMethod): Promise<Method["response"]>;
|
|
52
58
|
/** Handle notification events */
|
|
53
59
|
protected handleNotificationEvent(event: WalletNotificationEvent): Promise<WalletNotification>;
|
|
54
|
-
/**
|
|
55
|
-
|
|
60
|
+
/** Generic call method with generic type */
|
|
61
|
+
genericCall<Method extends TWalletMethod>(method: Method["method"], params: Method["request"]["params"], options?: {
|
|
56
62
|
timeout?: number;
|
|
57
|
-
}): Observable<
|
|
63
|
+
}): Observable<Method["response"] | Method["error"]>;
|
|
64
|
+
/** Request method with generic type */
|
|
65
|
+
genericRequest<Method extends TWalletMethod>(method: Method["method"], params: Method["request"]["params"], options?: {
|
|
66
|
+
timeout?: number;
|
|
67
|
+
}): Promise<Method["response"]["result"]>;
|
|
68
|
+
/** Call a method and return an observable of results */
|
|
69
|
+
call<K extends Methods["method"]>(method: K, params: Extract<Methods, {
|
|
70
|
+
method: K;
|
|
71
|
+
}>["request"]["params"], options?: {
|
|
72
|
+
timeout?: number;
|
|
73
|
+
}): Observable<Extract<Methods, {
|
|
74
|
+
method: K;
|
|
75
|
+
}>["response"] | Extract<Methods, {
|
|
76
|
+
method: K;
|
|
77
|
+
}>["error"]>;
|
|
78
|
+
/** Typed request method, returns the result or throws and error */
|
|
79
|
+
request<K extends Methods["method"]>(method: K, params: Extract<Methods, {
|
|
80
|
+
method: K;
|
|
81
|
+
}>["request"]["params"], options?: {
|
|
82
|
+
timeout?: number;
|
|
83
|
+
}): Promise<Extract<Methods, {
|
|
84
|
+
method: K;
|
|
85
|
+
}>["response"]["result"]>;
|
|
58
86
|
/**
|
|
59
87
|
* Listen for a type of notification
|
|
60
88
|
* @returns a method to unsubscribe the listener
|
|
61
89
|
*/
|
|
62
90
|
notification<T extends WalletNotification>(type: T["notification_type"], listener: (notification: T["notification"]) => any): Subscription;
|
|
63
91
|
/** Gets the nostr+walletauth URI for the connection */
|
|
64
|
-
getAuthURI(parts?: Omit<WalletAuthURI
|
|
92
|
+
getAuthURI(parts?: Omit<WalletAuthURI<Methods>, "client" | "relays">): string;
|
|
65
93
|
/** Wait for the wallet service to connect */
|
|
66
94
|
waitForService(abortSignal?: AbortSignal): Promise<string>;
|
|
67
95
|
/** Get the wallet support info */
|
|
68
96
|
getSupport(): Promise<WalletSupport | null>;
|
|
69
97
|
/** Check if the wallet supports a method */
|
|
70
|
-
supportsMethod(method:
|
|
98
|
+
supportsMethod(method: string): Promise<boolean>;
|
|
71
99
|
/** Check if the wallet supports notifications */
|
|
72
100
|
supportsNotifications(): Promise<boolean>;
|
|
73
101
|
/** Check if the wallet supports a notification type */
|
|
74
102
|
supportsNotificationType(type: NotificationType): Promise<boolean>;
|
|
75
103
|
/** Pay a lightning invoice */
|
|
76
|
-
payInvoice(invoice: string, amount?: number): Promise<
|
|
104
|
+
payInvoice(invoice: string, amount?: number): Promise<PayInvoiceMethod["response"]["result"]>;
|
|
77
105
|
/** Pay multiple lightning invoices */
|
|
78
106
|
payMultipleInvoices(invoices: Array<{
|
|
79
107
|
id?: string;
|
|
80
108
|
invoice: string;
|
|
81
109
|
amount?: number;
|
|
82
|
-
}>): Promise<
|
|
110
|
+
}>): Promise<MultiPayInvoiceMethod["response"]["result"][]>;
|
|
83
111
|
/** Send a keysend payment */
|
|
84
112
|
payKeysend(pubkey: string, amount: number, preimage?: string, tlv_records?: Array<{
|
|
85
113
|
type: number;
|
|
86
114
|
value: string;
|
|
87
|
-
}>): Promise<
|
|
115
|
+
}>): Promise<PayKeysendMethod["response"]["result"]>;
|
|
88
116
|
/** Send multiple keysend payments */
|
|
89
117
|
payMultipleKeysend(keysends: Array<{
|
|
90
118
|
id?: string;
|
|
@@ -95,11 +123,11 @@ export declare class WalletConnect {
|
|
|
95
123
|
type: number;
|
|
96
124
|
value: string;
|
|
97
125
|
}>;
|
|
98
|
-
}>): Promise<
|
|
126
|
+
}>): Promise<MultiPayKeysendMethod["response"]["result"][]>;
|
|
99
127
|
/** Create a new invoice */
|
|
100
|
-
makeInvoice(amount: number, options?: Omit<
|
|
128
|
+
makeInvoice(amount: number, options?: Omit<MakeInvoiceMethod["request"]["params"], "amount">): Promise<MakeInvoiceMethod["response"]["result"]>;
|
|
101
129
|
/** Look up an invoice by payment hash or invoice string */
|
|
102
|
-
lookupInvoice(payment_hash?: string, invoice?: string): Promise<
|
|
130
|
+
lookupInvoice(payment_hash?: string, invoice?: string): Promise<LookupInvoiceMethod["response"]["result"]>;
|
|
103
131
|
/** List transactions */
|
|
104
132
|
listTransactions(params?: {
|
|
105
133
|
from?: number;
|
|
@@ -108,15 +136,15 @@ export declare class WalletConnect {
|
|
|
108
136
|
offset?: number;
|
|
109
137
|
unpaid?: boolean;
|
|
110
138
|
type?: "incoming" | "outgoing";
|
|
111
|
-
}): Promise<
|
|
139
|
+
}): Promise<ListTransactionsMethod["response"]["result"]>;
|
|
112
140
|
/** Get wallet balance */
|
|
113
|
-
getBalance(): Promise<
|
|
141
|
+
getBalance(): Promise<GetBalanceMethod["response"]["result"]>;
|
|
114
142
|
/** Get wallet info */
|
|
115
|
-
getInfo(): Promise<
|
|
143
|
+
getInfo(): Promise<GetInfoMethod["response"]["result"]>;
|
|
116
144
|
/** Serialize the WalletConnect instance */
|
|
117
145
|
toJSON(): SerializedWalletConnect;
|
|
118
146
|
/** Create a new WalletConnect instance from a serialized object */
|
|
119
147
|
static fromJSON(json: SerializedWalletConnect, options?: Omit<WalletConnectOptions, "secret" | "relays" | "service">): WalletConnect;
|
|
120
148
|
/** Create a new WalletConnect instance from a connection string */
|
|
121
|
-
static fromConnectURI(connectionString: string, options?: Omit<WalletConnectOptions, "secret" | "relays" | "service">): WalletConnect
|
|
149
|
+
static fromConnectURI<Methods extends TWalletMethod = CommonWalletMethods>(connectionString: string, options?: Omit<WalletConnectOptions, "secret" | "relays" | "service">): WalletConnect<Methods>;
|
|
122
150
|
}
|
package/dist/wallet-connect.js
CHANGED
|
@@ -2,7 +2,7 @@ import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
|
|
|
2
2
|
import { simpleTimeout } from "applesauce-core";
|
|
3
3
|
import { create } from "applesauce-factory";
|
|
4
4
|
import { finalizeEvent, getPublicKey, nip04, nip44, verifyEvent } from "nostr-tools";
|
|
5
|
-
import { BehaviorSubject, defer, filter, firstValueFrom, from, fromEvent, identity, ignoreElements, lastValueFrom, map, merge, mergeMap, of, repeat, ReplaySubject, retry, share, switchMap, take, takeUntil, tap, timer, toArray, } from "rxjs";
|
|
5
|
+
import { BehaviorSubject, combineLatest, defer, filter, firstValueFrom, from, fromEvent, identity, ignoreElements, lastValueFrom, map, merge, mergeMap, of, repeat, ReplaySubject, retry, share, switchMap, take, takeUntil, tap, timer, toArray, } from "rxjs";
|
|
6
6
|
import { WalletRequestBlueprint } from "./blueprints/index.js";
|
|
7
7
|
import { createWalletError } from "./helpers/error.js";
|
|
8
8
|
import { createWalletAuthURI, getPreferredEncryption, getWalletRequestEncryption, getWalletResponseRequestId, getWalletSupport, isValidWalletNotification, isValidWalletResponse, parseWalletConnectURI, supportsMethod, supportsNotifications, supportsNotificationType, unlockWalletNotification, unlockWalletResponse, WALLET_INFO_KIND, WALLET_LEGACY_NOTIFICATION_KIND, WALLET_NOTIFICATION_KIND, WALLET_RESPONSE_KIND, } from "./helpers/index.js";
|
|
@@ -22,7 +22,12 @@ export class WalletConnect {
|
|
|
22
22
|
secret;
|
|
23
23
|
signer;
|
|
24
24
|
/** The relays to use for the connection */
|
|
25
|
-
relays;
|
|
25
|
+
relays$ = new BehaviorSubject([]);
|
|
26
|
+
get relays() {
|
|
27
|
+
return this.relays$.value;
|
|
28
|
+
}
|
|
29
|
+
/** Whether to accept the relay hint from the wallet service */
|
|
30
|
+
acceptRelayHint;
|
|
26
31
|
/** The wallet service public key ( unset if waiting for service ) */
|
|
27
32
|
service$ = new BehaviorSubject(undefined);
|
|
28
33
|
get service() {
|
|
@@ -42,7 +47,8 @@ export class WalletConnect {
|
|
|
42
47
|
waitForService$;
|
|
43
48
|
constructor(options) {
|
|
44
49
|
this.secret = options.secret;
|
|
45
|
-
this.relays
|
|
50
|
+
this.relays$.next(options.relays);
|
|
51
|
+
this.acceptRelayHint = options.acceptRelayHint ?? true;
|
|
46
52
|
this.service$.next(options.service);
|
|
47
53
|
this.defaultTimeout = options.timeout || 30000; // 30 second default timeout
|
|
48
54
|
// Create a signer for the factory
|
|
@@ -64,18 +70,18 @@ export class WalletConnect {
|
|
|
64
70
|
this.subscriptionMethod = (relays, filters) => subscriptionMethod(relays, filters);
|
|
65
71
|
this.publishMethod = (relays, event) => publishMethod(relays, event);
|
|
66
72
|
// Create shared observable for all wallet events
|
|
67
|
-
this.events$ = this.service
|
|
73
|
+
this.events$ = combineLatest([this.service$, this.relays$]).pipe(switchMap(([service, relays]) => {
|
|
68
74
|
const client = getPublicKey(this.secret);
|
|
69
75
|
// If the service is not known yet, subscribe to a wallet info event tagging the client
|
|
70
76
|
if (!service)
|
|
71
|
-
return from(this.subscriptionMethod(
|
|
77
|
+
return from(this.subscriptionMethod(relays, [{ kinds: [WALLET_INFO_KIND], "#p": [client] }])).pipe(
|
|
72
78
|
// Keep the connection open indefinitely
|
|
73
79
|
repeat(),
|
|
74
80
|
// Retry on connection failure
|
|
75
81
|
retry(),
|
|
76
82
|
// Ignore strings (support for applesauce-relay)
|
|
77
83
|
filter((event) => typeof event !== "string"));
|
|
78
|
-
return from(this.subscriptionMethod(
|
|
84
|
+
return from(this.subscriptionMethod(relays, [
|
|
79
85
|
// Subscribe to response events
|
|
80
86
|
{
|
|
81
87
|
kinds: [WALLET_RESPONSE_KIND, WALLET_NOTIFICATION_KIND, WALLET_LEGACY_NOTIFICATION_KIND],
|
|
@@ -116,6 +122,12 @@ export class WalletConnect {
|
|
|
116
122
|
tap((event) => {
|
|
117
123
|
// Set the service to the pubkey of the wallet info event
|
|
118
124
|
this.service$.next(event.pubkey);
|
|
125
|
+
// Switch to the relay from the service if its set
|
|
126
|
+
if (this.acceptRelayHint) {
|
|
127
|
+
const relay = event.tags.find((t) => t[0] === "p" && t[2])?.[2];
|
|
128
|
+
if (relay)
|
|
129
|
+
this.relays$.next([relay]);
|
|
130
|
+
}
|
|
119
131
|
}),
|
|
120
132
|
// Get the service pubkey from the event
|
|
121
133
|
map((event) => event.pubkey),
|
|
@@ -145,16 +157,16 @@ export class WalletConnect {
|
|
|
145
157
|
throw new Error("Failed to decrypt or parse notification");
|
|
146
158
|
return notification;
|
|
147
159
|
}
|
|
148
|
-
/**
|
|
149
|
-
|
|
160
|
+
/** Generic call method with generic type */
|
|
161
|
+
genericCall(method, params, options = {}) {
|
|
150
162
|
if (!this.service)
|
|
151
163
|
throw new Error("WalletConnect is not connected to a service");
|
|
152
|
-
// Create the request
|
|
164
|
+
// Create the request event
|
|
153
165
|
return defer(async () => {
|
|
154
166
|
// Get the preferred encryption method for the wallet
|
|
155
167
|
const encryption = await firstValueFrom(this.encryption$);
|
|
156
168
|
// Create the request event
|
|
157
|
-
const draft = await create({ signer: this.signer }, WalletRequestBlueprint, this.service,
|
|
169
|
+
const draft = await create({ signer: this.signer }, WalletRequestBlueprint, this.service, { method, params }, encryption);
|
|
158
170
|
// Sign the request event
|
|
159
171
|
return await this.signer.signEvent(draft);
|
|
160
172
|
}).pipe(
|
|
@@ -170,6 +182,23 @@ export class WalletConnect {
|
|
|
170
182
|
return merge(request$, responses$);
|
|
171
183
|
}));
|
|
172
184
|
}
|
|
185
|
+
/** Request method with generic type */
|
|
186
|
+
async genericRequest(method, params, options = {}) {
|
|
187
|
+
const result = await firstValueFrom(this.genericCall(method, params, options));
|
|
188
|
+
if (result.result_type !== method)
|
|
189
|
+
throw new Error(`Unexpected response type: ${result.result_type}`);
|
|
190
|
+
if (result.error)
|
|
191
|
+
throw createWalletError(result.error.type, result.error.message);
|
|
192
|
+
return result.result;
|
|
193
|
+
}
|
|
194
|
+
/** Call a method and return an observable of results */
|
|
195
|
+
call(method, params, options = {}) {
|
|
196
|
+
return this.genericCall(method, params, options);
|
|
197
|
+
}
|
|
198
|
+
/** Typed request method, returns the result or throws and error */
|
|
199
|
+
async request(method, params, options = {}) {
|
|
200
|
+
return this.genericRequest(method, params, options);
|
|
201
|
+
}
|
|
173
202
|
/**
|
|
174
203
|
* Listen for a type of notification
|
|
175
204
|
* @returns a method to unsubscribe the listener
|
|
@@ -212,18 +241,14 @@ export class WalletConnect {
|
|
|
212
241
|
const support = await this.getSupport();
|
|
213
242
|
return support ? supportsNotificationType(support, type) : false;
|
|
214
243
|
}
|
|
244
|
+
// Methods for common types
|
|
215
245
|
/** Pay a lightning invoice */
|
|
216
246
|
async payInvoice(invoice, amount) {
|
|
217
|
-
|
|
218
|
-
if (response.result_type !== "pay_invoice")
|
|
219
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
220
|
-
if (response.error)
|
|
221
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
222
|
-
return response.result;
|
|
247
|
+
return await this.genericRequest("pay_invoice", { invoice, amount });
|
|
223
248
|
}
|
|
224
249
|
/** Pay multiple lightning invoices */
|
|
225
250
|
async payMultipleInvoices(invoices) {
|
|
226
|
-
return await lastValueFrom(this.
|
|
251
|
+
return await lastValueFrom(this.genericCall("multi_pay_invoice", { invoices })
|
|
227
252
|
.pipe(map((response) => {
|
|
228
253
|
if (response.result_type !== "multi_pay_invoice")
|
|
229
254
|
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
@@ -235,16 +260,11 @@ export class WalletConnect {
|
|
|
235
260
|
}
|
|
236
261
|
/** Send a keysend payment */
|
|
237
262
|
async payKeysend(pubkey, amount, preimage, tlv_records) {
|
|
238
|
-
|
|
239
|
-
if (response.result_type !== "pay_keysend")
|
|
240
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
241
|
-
if (response.error)
|
|
242
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
243
|
-
return response.result;
|
|
263
|
+
return await this.genericRequest("pay_keysend", { pubkey, amount, preimage, tlv_records });
|
|
244
264
|
}
|
|
245
265
|
/** Send multiple keysend payments */
|
|
246
266
|
async payMultipleKeysend(keysends) {
|
|
247
|
-
return lastValueFrom(this.
|
|
267
|
+
return lastValueFrom(this.genericCall("multi_pay_keysend", { keysends }).pipe(map((response) => {
|
|
248
268
|
if (response.result_type !== "multi_pay_keysend")
|
|
249
269
|
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
250
270
|
if (response.error)
|
|
@@ -254,48 +274,23 @@ export class WalletConnect {
|
|
|
254
274
|
}
|
|
255
275
|
/** Create a new invoice */
|
|
256
276
|
async makeInvoice(amount, options) {
|
|
257
|
-
|
|
258
|
-
if (response.result_type !== "make_invoice")
|
|
259
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
260
|
-
if (response.error)
|
|
261
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
262
|
-
return response.result;
|
|
277
|
+
return await this.genericRequest("make_invoice", { amount, ...options });
|
|
263
278
|
}
|
|
264
279
|
/** Look up an invoice by payment hash or invoice string */
|
|
265
280
|
async lookupInvoice(payment_hash, invoice) {
|
|
266
|
-
|
|
267
|
-
if (response.result_type !== "lookup_invoice")
|
|
268
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
269
|
-
if (response.error)
|
|
270
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
271
|
-
return response.result;
|
|
281
|
+
return await this.genericRequest("lookup_invoice", { payment_hash, invoice });
|
|
272
282
|
}
|
|
273
283
|
/** List transactions */
|
|
274
284
|
async listTransactions(params) {
|
|
275
|
-
|
|
276
|
-
if (response.result_type !== "list_transactions")
|
|
277
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
278
|
-
if (response.error)
|
|
279
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
280
|
-
return response.result;
|
|
285
|
+
return await this.genericRequest("list_transactions", params || {});
|
|
281
286
|
}
|
|
282
287
|
/** Get wallet balance */
|
|
283
288
|
async getBalance() {
|
|
284
|
-
|
|
285
|
-
if (response.result_type !== "get_balance")
|
|
286
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
287
|
-
if (response.error)
|
|
288
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
289
|
-
return response.result;
|
|
289
|
+
return await this.genericRequest("get_balance", {});
|
|
290
290
|
}
|
|
291
291
|
/** Get wallet info */
|
|
292
292
|
async getInfo() {
|
|
293
|
-
|
|
294
|
-
if (response.result_type !== "get_info")
|
|
295
|
-
throw new Error(`Unexpected response type: ${response.result_type}`);
|
|
296
|
-
if (response.error)
|
|
297
|
-
throw createWalletError(response.error.type, response.error.message);
|
|
298
|
-
return response.result;
|
|
293
|
+
return await this.genericRequest("get_info", {});
|
|
299
294
|
}
|
|
300
295
|
/** Serialize the WalletConnect instance */
|
|
301
296
|
toJSON() {
|
package/dist/wallet-service.d.ts
CHANGED
|
@@ -2,49 +2,30 @@ import { EventSigner } from "applesauce-factory";
|
|
|
2
2
|
import { Observable, Subscription } from "rxjs";
|
|
3
3
|
import { WalletAuthURI } from "./helpers/auth-uri.js";
|
|
4
4
|
import { WalletErrorCode } from "./helpers/error.js";
|
|
5
|
+
import { CommonWalletMethods, TWalletMethod, WalletInfo } from "./helpers/methods.js";
|
|
5
6
|
import { NotificationType, WalletNotification } from "./helpers/notification.js";
|
|
6
|
-
import {
|
|
7
|
-
import { GetBalanceResult, GetInfoResult, ListTransactionsResult, LookupInvoiceResult, MakeInvoiceResult, MultiPayInvoiceResult, MultiPayKeysendResult, PayInvoiceResult, PayKeysendResult, WalletResponse } from "./helpers/response.js";
|
|
7
|
+
import { WalletRequestEvent } from "./helpers/request.js";
|
|
8
8
|
import { WalletSupport } from "./helpers/support.js";
|
|
9
9
|
import { NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "./interop.js";
|
|
10
|
-
/**
|
|
11
|
-
export type
|
|
12
|
-
/**
|
|
13
|
-
export type
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
export type MakeInvoiceHandler = (params: MakeInvoiceParams) => Promise<MakeInvoiceResult>;
|
|
20
|
-
/** Handler function for lookup_invoice method */
|
|
21
|
-
export type LookupInvoiceHandler = (params: LookupInvoiceParams) => Promise<LookupInvoiceResult>;
|
|
22
|
-
/** Handler function for list_transactions method */
|
|
23
|
-
export type ListTransactionsHandler = (params: ListTransactionsParams) => Promise<ListTransactionsResult>;
|
|
24
|
-
/** Handler function for get_balance method */
|
|
25
|
-
export type GetBalanceHandler = (params: GetBalanceParams) => Promise<GetBalanceResult>;
|
|
26
|
-
/** Handler function for get_info method */
|
|
27
|
-
export type GetInfoHandler = (params: GetInfoParams) => Promise<GetInfoResult>;
|
|
28
|
-
/** Map of method handlers for the wallet service */
|
|
29
|
-
export interface WalletServiceHandlers {
|
|
30
|
-
pay_invoice?: PayInvoiceHandler;
|
|
31
|
-
multi_pay_invoice?: MultiPayInvoiceHandler;
|
|
32
|
-
pay_keysend?: PayKeysendHandler;
|
|
33
|
-
multi_pay_keysend?: MultiPayKeysendHandler;
|
|
34
|
-
make_invoice?: MakeInvoiceHandler;
|
|
35
|
-
lookup_invoice?: LookupInvoiceHandler;
|
|
36
|
-
list_transactions?: ListTransactionsHandler;
|
|
37
|
-
get_balance?: GetBalanceHandler;
|
|
38
|
-
get_info?: GetInfoHandler;
|
|
39
|
-
}
|
|
10
|
+
/** Generic type for wallet method handlers */
|
|
11
|
+
export type WalletMethodHandler<Method extends TWalletMethod> = (params: Method["request"]["params"]) => Promise<Method["response"]["result"]>;
|
|
12
|
+
/** Map of method handlers for the wallet service for a specific set of methods */
|
|
13
|
+
export type WalletServiceHandlers<Methods extends TWalletMethod = TWalletMethod> = {
|
|
14
|
+
[K in Methods["method"]]?: WalletMethodHandler<Extract<Methods, {
|
|
15
|
+
method: K;
|
|
16
|
+
}>>;
|
|
17
|
+
};
|
|
18
|
+
/** Serialized wallet service */
|
|
40
19
|
export type SerializedWalletService = {
|
|
41
20
|
/** The client's public key */
|
|
42
21
|
client: string;
|
|
43
22
|
/** The relays to use for the service */
|
|
44
23
|
relays: string[];
|
|
45
24
|
};
|
|
25
|
+
/** Only the necessary info for the getInfo method on wallet service */
|
|
26
|
+
export type WalletServiceInfo = Partial<Omit<WalletInfo, "methods" | "notifications">>;
|
|
46
27
|
/** Options for creating a WalletService */
|
|
47
|
-
export interface WalletServiceOptions extends NostrConnectionMethodsOptions {
|
|
28
|
+
export interface WalletServiceOptions<Methods extends TWalletMethod = CommonWalletMethods> extends NostrConnectionMethodsOptions {
|
|
48
29
|
/** The relays to use for the service */
|
|
49
30
|
relays: string[];
|
|
50
31
|
/** The signer to use for creating and unlocking events */
|
|
@@ -53,13 +34,15 @@ export interface WalletServiceOptions extends NostrConnectionMethodsOptions {
|
|
|
53
34
|
secret?: Uint8Array;
|
|
54
35
|
/** The client's public key (used for restoring the service) */
|
|
55
36
|
client?: string;
|
|
37
|
+
/** A method for getting the general wallet information (Can be overwritten if get_info is set in handlers) */
|
|
38
|
+
getInfo?: () => Promise<WalletServiceInfo>;
|
|
56
39
|
/** Map of method handlers */
|
|
57
|
-
handlers: WalletServiceHandlers
|
|
40
|
+
handlers: WalletServiceHandlers<Methods>;
|
|
58
41
|
/** An array of notifications this wallet supports */
|
|
59
42
|
notifications?: NotificationType[];
|
|
60
43
|
}
|
|
61
44
|
/** NIP-47 Wallet Service implementation */
|
|
62
|
-
export declare class WalletService {
|
|
45
|
+
export declare class WalletService<Methods extends TWalletMethod = CommonWalletMethods> {
|
|
63
46
|
/** A fallback method to use for subscriptionMethod if none is passed in when creating the client */
|
|
64
47
|
static subscriptionMethod: NostrSubscriptionMethod | undefined;
|
|
65
48
|
/** A fallback method to use for publishMethod if none is passed in when creating the client */
|
|
@@ -71,14 +54,16 @@ export declare class WalletService {
|
|
|
71
54
|
/** A method for publishing events */
|
|
72
55
|
protected readonly publishMethod: NostrPublishMethod;
|
|
73
56
|
protected log: import("debug").Debugger;
|
|
57
|
+
/** A special method for getting the generic wallet information */
|
|
58
|
+
getInfo?: () => Promise<WalletServiceInfo>;
|
|
74
59
|
/** The relays to use for the service */
|
|
75
60
|
readonly relays: string[];
|
|
76
61
|
/** The signer used for creating and unlocking events */
|
|
77
62
|
protected readonly signer: EventSigner;
|
|
78
63
|
/** Map of method handlers */
|
|
79
|
-
protected readonly handlers: WalletServiceHandlers
|
|
64
|
+
protected readonly handlers: WalletServiceHandlers<Methods>;
|
|
80
65
|
/** Wallet support information */
|
|
81
|
-
protected readonly support: WalletSupport
|
|
66
|
+
protected readonly support: WalletSupport<Methods>;
|
|
82
67
|
/** The service's public key */
|
|
83
68
|
pubkey: string | null;
|
|
84
69
|
/** The client's secret key */
|
|
@@ -91,7 +76,7 @@ export declare class WalletService {
|
|
|
91
76
|
protected subscription: Subscription | null;
|
|
92
77
|
/** Whether the service is currently running */
|
|
93
78
|
running: boolean;
|
|
94
|
-
constructor(options: WalletServiceOptions);
|
|
79
|
+
constructor(options: WalletServiceOptions<Methods>);
|
|
95
80
|
/** Start the wallet service */
|
|
96
81
|
start(): Promise<void>;
|
|
97
82
|
/** Stop the wallet service */
|
|
@@ -107,13 +92,16 @@ export declare class WalletService {
|
|
|
107
92
|
/** Handle a wallet request event */
|
|
108
93
|
protected handleRequestEvent(requestEvent: WalletRequestEvent): Promise<void>;
|
|
109
94
|
/** Process a decrypted wallet request */
|
|
110
|
-
protected processRequest(requestEvent: WalletRequestEvent, request:
|
|
95
|
+
protected processRequest<Method extends Methods>(requestEvent: WalletRequestEvent, request: Method["request"]): Promise<void>;
|
|
111
96
|
/** Send a success response */
|
|
112
|
-
protected sendSuccessResponse<
|
|
97
|
+
protected sendSuccessResponse<Method extends Methods>(requestEvent: WalletRequestEvent, method: Method["response"]["result_type"], result: Method["response"]["result"]): Promise<void>;
|
|
113
98
|
/** Send an error response */
|
|
114
|
-
protected sendErrorResponse(requestEvent: WalletRequestEvent, errorType: WalletErrorCode, errorMessage: string): Promise<void>;
|
|
99
|
+
protected sendErrorResponse<Method extends Methods>(requestEvent: WalletRequestEvent, errorType: WalletErrorCode, errorMessage: string): Promise<void>;
|
|
115
100
|
/** Send a response event */
|
|
116
|
-
protected sendResponse(requestEvent: WalletRequestEvent, response:
|
|
101
|
+
protected sendResponse<Method extends Methods>(requestEvent: WalletRequestEvent, response: Method["response"] | Method["error"]): Promise<void>;
|
|
117
102
|
/** Creates a service for a nostr+walletauth URI */
|
|
118
|
-
static fromAuthURI(uri: string | WalletAuthURI, options: Omit<WalletServiceOptions
|
|
103
|
+
static fromAuthURI<Methods extends TWalletMethod = CommonWalletMethods>(uri: string | WalletAuthURI, options: Omit<WalletServiceOptions<Methods>, "relays"> & {
|
|
104
|
+
/** A relay or method to select a single relay for the client and service to communicate over */
|
|
105
|
+
overrideRelay?: string | ((relays: string[]) => string);
|
|
106
|
+
}): WalletService<Methods>;
|
|
119
107
|
}
|