applesauce-wallet-connect 3.1.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 +13 -5
- package/dist/helpers/notification.js +18 -12
- package/dist/helpers/request.d.ts +13 -115
- package/dist/helpers/request.js +19 -14
- package/dist/helpers/response.d.ts +14 -126
- package/dist/helpers/response.js +20 -14
- package/dist/helpers/support.d.ts +7 -7
- package/dist/helpers/support.js +1 -14
- package/dist/wallet-connect.d.ts +48 -20
- package/dist/wallet-connect.js +71 -79
- package/dist/wallet-service.d.ts +34 -47
- package/dist/wallet-service.js +43 -49
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -41,10 +41,10 @@ Create a wallet service that handles NIP-47 requests:
|
|
|
41
41
|
|
|
42
42
|
```typescript
|
|
43
43
|
import { WalletService } from "applesauce-wallet-connect";
|
|
44
|
-
import {
|
|
44
|
+
import { PrivateKeySigner } from "applesauce-signers";
|
|
45
45
|
|
|
46
46
|
// Create a signer for the service
|
|
47
|
-
const signer = new
|
|
47
|
+
const signer = new PrivateKeySigner();
|
|
48
48
|
|
|
49
49
|
// Define method handlers
|
|
50
50
|
const handlers = {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { EventBlueprint } from "applesauce-factory";
|
|
2
|
-
import { WalletRequest } from "../helpers/request.js";
|
|
3
2
|
import { WalletConnectEncryptionMethod } from "../helpers/encryption.js";
|
|
3
|
+
import { TWalletMethod } from "../helpers/methods.js";
|
|
4
4
|
/**
|
|
5
5
|
* Creates a walelt request event
|
|
6
6
|
* @param service - The service pubkey
|
|
7
7
|
* @param request - The request to create an event for
|
|
8
8
|
*/
|
|
9
|
-
export declare function WalletRequestBlueprint<
|
|
9
|
+
export declare function WalletRequestBlueprint<Method extends TWalletMethod>(service: string, request: Method["request"], encryption?: WalletConnectEncryptionMethod): EventBlueprint;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventBlueprint } from "applesauce-factory";
|
|
2
2
|
import { NostrEvent } from "nostr-tools";
|
|
3
|
-
import {
|
|
3
|
+
import { TWalletMethod } from "../helpers/methods.js";
|
|
4
4
|
/** Creates a wallet response event */
|
|
5
|
-
export declare function WalletResponseBlueprint(request: NostrEvent, response:
|
|
5
|
+
export declare function WalletResponseBlueprint<Method extends TWalletMethod>(request: NostrEvent, response: Method["response"] | Method["error"]): EventBlueprint;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { EventBlueprint } from "applesauce-factory";
|
|
2
2
|
import { WalletSupport } from "../helpers/support.js";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { TWalletMethod } from "../helpers/methods.js";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a wallet info event
|
|
6
|
+
* @param info - The wallet support information
|
|
7
|
+
* @param client - The client pubkey
|
|
8
|
+
* @param overrideRelay - An optional relay to tell the client which relay to use (for nostr+walletauth URI connections)
|
|
9
|
+
*/
|
|
10
|
+
export declare function WalletSupportBlueprint<Methods extends TWalletMethod>(info: WalletSupport<Methods>, client?: string, overrideRelay?: string): EventBlueprint;
|
|
@@ -2,9 +2,14 @@ import { blueprint } from "applesauce-factory";
|
|
|
2
2
|
import { setContent } from "applesauce-factory/operations/content";
|
|
3
3
|
import { includeSingletonTag } from "applesauce-factory/operations";
|
|
4
4
|
import { WALLET_INFO_KIND } from "../helpers/support.js";
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Creates a wallet info event
|
|
7
|
+
* @param info - The wallet support information
|
|
8
|
+
* @param client - The client pubkey
|
|
9
|
+
* @param overrideRelay - An optional relay to tell the client which relay to use (for nostr+walletauth URI connections)
|
|
10
|
+
*/
|
|
11
|
+
export function WalletSupportBlueprint(info, client, overrideRelay) {
|
|
7
12
|
return blueprint(WALLET_INFO_KIND, setContent(info.methods.join(" ")), info.encryption ? includeSingletonTag(["encryption", info.encryption.join(" ")]) : undefined, info.notifications ? includeSingletonTag(["notifications", info.notifications.join(" ")]) : undefined,
|
|
8
13
|
// An optional client pubkey to notify the service is created (used for nostr+walletauth URI connections)
|
|
9
|
-
client ? includeSingletonTag(["p", client]) : undefined);
|
|
14
|
+
client ? includeSingletonTag(overrideRelay ? ["p", client, overrideRelay] : ["p", client]) : undefined);
|
|
10
15
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NotificationType } from "./notification.js";
|
|
2
|
-
import {
|
|
3
|
-
export interface WalletAuthURI {
|
|
2
|
+
import { CommonWalletMethods, TWalletMethod } from "./methods.js";
|
|
3
|
+
export interface WalletAuthURI<Methods extends TWalletMethod = CommonWalletMethods> {
|
|
4
4
|
/** The public key of the client requesting authorization */
|
|
5
5
|
client: string;
|
|
6
6
|
/** Required. URL of the relay where the client intends to communicate with the wallet service */
|
|
@@ -18,7 +18,7 @@ export interface WalletAuthURI {
|
|
|
18
18
|
/** The reset the budget at the end of the given budget renewal. Can be never (default), daily, weekly, monthly, yearly (optional) */
|
|
19
19
|
budgetRenewal?: "never" | "daily" | "weekly" | "monthly" | "yearly";
|
|
20
20
|
/** List of request types that you need permission for (optional) */
|
|
21
|
-
methods?:
|
|
21
|
+
methods?: Methods["method"][];
|
|
22
22
|
/** List of notification types that you need permission for (optional) */
|
|
23
23
|
notifications?: NotificationType[];
|
|
24
24
|
/** The makes an isolated app connection / sub-wallet with its own balance and only access to its own transaction list (optional) */
|
|
@@ -32,13 +32,11 @@ export interface WalletAuthURI {
|
|
|
32
32
|
* Parses a nostr+walletauth URI
|
|
33
33
|
* @throws {Error} if the authorization URI is invalid
|
|
34
34
|
*/
|
|
35
|
-
export declare function parseWalletAuthURI(authURI: string): WalletAuthURI
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
*/
|
|
39
|
-
export declare function createWalletAuthURI(parts: WalletAuthURI): string;
|
|
35
|
+
export declare function parseWalletAuthURI<Methods extends TWalletMethod = CommonWalletMethods>(authURI: string): WalletAuthURI<Methods>;
|
|
36
|
+
/** Creates a nostr+walletauth URI from a WalletAuthURI object */
|
|
37
|
+
export declare function createWalletAuthURI<Methods extends TWalletMethod = CommonWalletMethods>(parts: WalletAuthURI<Methods>): string;
|
|
40
38
|
/**
|
|
41
39
|
* Validates a WalletAuthURI object
|
|
42
40
|
* @returns true if valid, throws Error if invalid
|
|
43
41
|
*/
|
|
44
|
-
export declare function validateWalletAuthURI(parts: WalletAuthURI): boolean;
|
|
42
|
+
export declare function validateWalletAuthURI(parts: WalletAuthURI<TWalletMethod>): boolean;
|
package/dist/helpers/auth-uri.js
CHANGED
|
@@ -60,9 +60,7 @@ export function parseWalletAuthURI(authURI) {
|
|
|
60
60
|
walletName,
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Creates a nostr+walletauth URI from a WalletAuthURI object
|
|
65
|
-
*/
|
|
63
|
+
/** Creates a nostr+walletauth URI from a WalletAuthURI object */
|
|
66
64
|
export function createWalletAuthURI(parts) {
|
|
67
65
|
validateWalletAuthURI(parts);
|
|
68
66
|
// Determine the protocol based on whether wallet name is specified
|
package/dist/helpers/index.d.ts
CHANGED
package/dist/helpers/index.js
CHANGED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { WalletErrorCode } from "./error.js";
|
|
2
|
+
import { WalletSupport } from "./support.js";
|
|
3
|
+
/** Base request structure for all NIP-47 requests */
|
|
4
|
+
export interface TWalletRequest<Method extends string, Params> {
|
|
5
|
+
/** The method to call */
|
|
6
|
+
method: Method;
|
|
7
|
+
/** Parameters for the method */
|
|
8
|
+
params: Params;
|
|
9
|
+
}
|
|
10
|
+
/** Error object for wallet responses */
|
|
11
|
+
export interface TWalletResponseError {
|
|
12
|
+
type: WalletErrorCode;
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
/** Base response structure for all NIP-47 responses */
|
|
16
|
+
export type TWalletErrorResponse<Method extends string> = {
|
|
17
|
+
/** Indicates the structure of the result field */
|
|
18
|
+
result_type: Method;
|
|
19
|
+
/** Error object, non-null in case of error */
|
|
20
|
+
error: TWalletResponseError;
|
|
21
|
+
result: null;
|
|
22
|
+
};
|
|
23
|
+
export type TWalletSuccessResponse<Method extends string, Result> = {
|
|
24
|
+
/** Indicates the structure of the result field */
|
|
25
|
+
result_type: Method;
|
|
26
|
+
error: null;
|
|
27
|
+
/** Result object, null in case of error */
|
|
28
|
+
result: Result;
|
|
29
|
+
};
|
|
30
|
+
/** Merged wallet response and request types, this is designed to only be used with typescript */
|
|
31
|
+
export type TWalletMethod<Method extends string = string, Params extends any = any, Result extends any = any> = {
|
|
32
|
+
/** Method string */
|
|
33
|
+
method: Method;
|
|
34
|
+
/** Request type */
|
|
35
|
+
request: TWalletRequest<Method, Params>;
|
|
36
|
+
/** Response success type */
|
|
37
|
+
response: TWalletSuccessResponse<Method, Result>;
|
|
38
|
+
/** Response error type */
|
|
39
|
+
error: TWalletErrorResponse<Method>;
|
|
40
|
+
};
|
|
41
|
+
/** TLV record for keysend payments */
|
|
42
|
+
export interface TLVRecord {
|
|
43
|
+
/** TLV type */
|
|
44
|
+
type: number;
|
|
45
|
+
/** Hex encoded TLV value */
|
|
46
|
+
value: string;
|
|
47
|
+
}
|
|
48
|
+
/** Transaction object used in multiple response types */
|
|
49
|
+
export interface Transaction {
|
|
50
|
+
/** Type of transaction */
|
|
51
|
+
type: "incoming" | "outgoing";
|
|
52
|
+
/** State of the transaction */
|
|
53
|
+
state: "pending" | "settled" | "expired" | "failed";
|
|
54
|
+
/** Value in msats */
|
|
55
|
+
amount: number;
|
|
56
|
+
/** Value in msats */
|
|
57
|
+
fees_paid: number;
|
|
58
|
+
/** Invoice/payment creation unix timestamp */
|
|
59
|
+
created_at: number;
|
|
60
|
+
/** Encoded invoice, optional */
|
|
61
|
+
invoice?: string;
|
|
62
|
+
/** Invoice's description, optional */
|
|
63
|
+
description?: string;
|
|
64
|
+
/** Invoice's description hash, optional */
|
|
65
|
+
description_hash?: string;
|
|
66
|
+
/** Payment's preimage, optional if unpaid */
|
|
67
|
+
preimage?: string;
|
|
68
|
+
/** Payment hash for the payment, optional */
|
|
69
|
+
payment_hash?: string;
|
|
70
|
+
/** Invoice expiration unix timestamp, optional if not applicable */
|
|
71
|
+
expires_at?: number;
|
|
72
|
+
/** Invoice/payment settlement unix timestamp, optional if unpaid */
|
|
73
|
+
settled_at?: number;
|
|
74
|
+
/** Generic metadata that can be used to add things like zap/boostagram details */
|
|
75
|
+
metadata?: Record<string, any>;
|
|
76
|
+
}
|
|
77
|
+
export type PayInvoiceMethod = TWalletMethod<"pay_invoice", {
|
|
78
|
+
/** BOLT11 invoice */
|
|
79
|
+
invoice: string;
|
|
80
|
+
/** Invoice amount in msats, optional */
|
|
81
|
+
amount?: number;
|
|
82
|
+
}, {
|
|
83
|
+
/** Preimage of the payment */
|
|
84
|
+
preimage: string;
|
|
85
|
+
/** Value in msats, optional */
|
|
86
|
+
fees_paid?: number;
|
|
87
|
+
}>;
|
|
88
|
+
export type MultiPayInvoiceMethod = TWalletMethod<"multi_pay_invoice", {
|
|
89
|
+
/** Array of invoices to pay */
|
|
90
|
+
invoices: Array<{
|
|
91
|
+
/** ID to identify this invoice in the response */
|
|
92
|
+
id?: string;
|
|
93
|
+
/** BOLT11 invoice */
|
|
94
|
+
invoice: string;
|
|
95
|
+
/** Invoice amount in msats, optional */
|
|
96
|
+
amount?: number;
|
|
97
|
+
}>;
|
|
98
|
+
}, {
|
|
99
|
+
/** Preimage of the payment */
|
|
100
|
+
preimage: string;
|
|
101
|
+
/** Value in msats, optional */
|
|
102
|
+
fees_paid?: number;
|
|
103
|
+
}>;
|
|
104
|
+
export type PayKeysendMethod = TWalletMethod<"pay_keysend", {
|
|
105
|
+
/** Amount in msats, required */
|
|
106
|
+
amount: number;
|
|
107
|
+
/** Payee pubkey, required */
|
|
108
|
+
pubkey: string;
|
|
109
|
+
/** Preimage of the payment, optional */
|
|
110
|
+
preimage?: string;
|
|
111
|
+
/** TLV records, optional */
|
|
112
|
+
tlv_records?: TLVRecord[];
|
|
113
|
+
}, {
|
|
114
|
+
/** Preimage of the payment */
|
|
115
|
+
preimage: string;
|
|
116
|
+
/** Value in msats, optional */
|
|
117
|
+
fees_paid?: number;
|
|
118
|
+
}>;
|
|
119
|
+
export type MultiPayKeysendMethod = TWalletMethod<"multi_pay_keysend", {
|
|
120
|
+
/** Array of keysend payments */
|
|
121
|
+
keysends: Array<{
|
|
122
|
+
/** ID to identify this keysend in the response */
|
|
123
|
+
id?: string;
|
|
124
|
+
/** Payee pubkey, required */
|
|
125
|
+
pubkey: string;
|
|
126
|
+
/** Amount in msats, required */
|
|
127
|
+
amount: number;
|
|
128
|
+
/** Preimage of the payment, optional */
|
|
129
|
+
preimage?: string;
|
|
130
|
+
/** TLV records, optional */
|
|
131
|
+
tlv_records?: TLVRecord[];
|
|
132
|
+
}>;
|
|
133
|
+
}, {
|
|
134
|
+
/** Preimage of the payment */
|
|
135
|
+
preimage: string;
|
|
136
|
+
/** Value in msats, optional */
|
|
137
|
+
fees_paid?: number;
|
|
138
|
+
}>;
|
|
139
|
+
export type MakeInvoiceMethod = TWalletMethod<"make_invoice", {
|
|
140
|
+
/** Value in msats */
|
|
141
|
+
amount: number;
|
|
142
|
+
/** Invoice's description, optional */
|
|
143
|
+
description?: string;
|
|
144
|
+
/** Invoice's description hash, optional */
|
|
145
|
+
description_hash?: string;
|
|
146
|
+
/** Expiry in seconds from time invoice is created, optional */
|
|
147
|
+
expiry?: number;
|
|
148
|
+
}, Transaction>;
|
|
149
|
+
export type LookupInvoiceMethod = TWalletMethod<"lookup_invoice", {
|
|
150
|
+
/** Payment hash of the invoice, one of payment_hash or invoice is required */
|
|
151
|
+
payment_hash?: string;
|
|
152
|
+
/** Invoice to lookup */
|
|
153
|
+
invoice?: string;
|
|
154
|
+
}, Transaction>;
|
|
155
|
+
export type ListTransactionsMethod = TWalletMethod<"list_transactions", {
|
|
156
|
+
/** Starting timestamp in seconds since epoch (inclusive), optional */
|
|
157
|
+
from?: number;
|
|
158
|
+
/** Ending timestamp in seconds since epoch (inclusive), optional */
|
|
159
|
+
until?: number;
|
|
160
|
+
/** Maximum number of invoices to return, optional */
|
|
161
|
+
limit?: number;
|
|
162
|
+
/** Offset of the first invoice to return, optional */
|
|
163
|
+
offset?: number;
|
|
164
|
+
/** Include unpaid invoices, optional, default false */
|
|
165
|
+
unpaid?: boolean;
|
|
166
|
+
/** "incoming" for invoices, "outgoing" for payments, undefined for both */
|
|
167
|
+
type?: "incoming" | "outgoing";
|
|
168
|
+
}, {
|
|
169
|
+
/** Array of transactions */
|
|
170
|
+
transactions: Transaction[];
|
|
171
|
+
}>;
|
|
172
|
+
export type GetBalanceMethod = TWalletMethod<"get_balance", {}, {
|
|
173
|
+
/** User's balance in msats */
|
|
174
|
+
balance: number;
|
|
175
|
+
}>;
|
|
176
|
+
/** Type for wallet get_info */
|
|
177
|
+
export type WalletInfo = Omit<WalletSupport, "encryption"> & {
|
|
178
|
+
/** Node alias */
|
|
179
|
+
alias?: string;
|
|
180
|
+
/** Node color as hex string */
|
|
181
|
+
color?: string;
|
|
182
|
+
/** Node public key as hex string */
|
|
183
|
+
pubkey?: string;
|
|
184
|
+
/** Network type */
|
|
185
|
+
network?: "mainnet" | "testnet" | "signet" | "regtest";
|
|
186
|
+
/** Current block height */
|
|
187
|
+
block_height?: number;
|
|
188
|
+
/** Current block hash as hex string */
|
|
189
|
+
block_hash?: string;
|
|
190
|
+
};
|
|
191
|
+
export type GetInfoMethod = TWalletMethod<"get_info", {}, WalletInfo>;
|
|
192
|
+
/** Union type for all wallet method definitions */
|
|
193
|
+
export type CommonWalletMethods = PayInvoiceMethod | MultiPayInvoiceMethod | PayKeysendMethod | MultiPayKeysendMethod | MakeInvoiceMethod | LookupInvoiceMethod | ListTransactionsMethod | GetBalanceMethod | GetInfoMethod;
|
|
194
|
+
/** Withdraw a token from the wallet */
|
|
195
|
+
export type CashuWithdrawMethod = TWalletMethod<"cashu_withdraw", {
|
|
196
|
+
/** Token amount */
|
|
197
|
+
amount: number;
|
|
198
|
+
/** required token unit */
|
|
199
|
+
unit: string;
|
|
200
|
+
/** Optional array of mints, if provided token MUST be from one */
|
|
201
|
+
mints?: string[];
|
|
202
|
+
/** Size of proofs (optional) */
|
|
203
|
+
proofs?: number[];
|
|
204
|
+
/** Lock to pubkey (optional) */
|
|
205
|
+
p2pk?: string;
|
|
206
|
+
}, {
|
|
207
|
+
token: string;
|
|
208
|
+
}>;
|
|
209
|
+
/** Deposit a token into the wallet */
|
|
210
|
+
export type CashuDepositMethod = TWalletMethod<"cashu_deposit", {
|
|
211
|
+
/** Token to deposit */
|
|
212
|
+
token: string;
|
|
213
|
+
}, {
|
|
214
|
+
success: true;
|
|
215
|
+
}>;
|
|
216
|
+
export type CashuPayRequestMethod = TWalletMethod<"cashu_pay_request", {
|
|
217
|
+
/** Cashu payment request. if request originally does not contain an amount they client may add one and re-encode */
|
|
218
|
+
request: string;
|
|
219
|
+
}, {
|
|
220
|
+
success: true;
|
|
221
|
+
}>;
|
|
222
|
+
/** Info for a single mint used in the wallet service */
|
|
223
|
+
export type CashuMintInfo = {
|
|
224
|
+
/** Mint URL */
|
|
225
|
+
url: string;
|
|
226
|
+
/** Units and balances */
|
|
227
|
+
balances: Record<string, number>;
|
|
228
|
+
};
|
|
229
|
+
/** Get the list of mints that are used in the wallet and their balances */
|
|
230
|
+
export type CashuListMintsMethod = TWalletMethod<"cashu_list_mints", {}, {
|
|
231
|
+
mints: CashuMintInfo[];
|
|
232
|
+
}>;
|
|
233
|
+
/** Union of all cashu methods */
|
|
234
|
+
export type CashuWalletMethods = CashuWithdrawMethod | CashuDepositMethod | CashuPayRequestMethod | CashuListMintsMethod;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { HiddenContentSigner } from "applesauce-core/helpers";
|
|
1
|
+
import { HiddenContentSigner, KnownEvent, UnlockedHiddenContent } from "applesauce-core/helpers";
|
|
2
2
|
import { NostrEvent } from "nostr-tools";
|
|
3
|
-
import { Transaction } from "./
|
|
3
|
+
import { Transaction } from "./methods.js";
|
|
4
4
|
export declare const WALLET_NOTIFICATION_KIND = 23197;
|
|
5
5
|
export declare const WALLET_LEGACY_NOTIFICATION_KIND = 23196;
|
|
6
|
+
/** A type for validated wallet notification events */
|
|
7
|
+
export type WalletNotificationEvent = KnownEvent<typeof WALLET_NOTIFICATION_KIND> | KnownEvent<typeof WALLET_LEGACY_NOTIFICATION_KIND>;
|
|
6
8
|
/** A symbol used to cache the wallet notification on the event */
|
|
7
9
|
export declare const WalletNotificationSymbol: unique symbol;
|
|
10
|
+
/** A type for unlocked notifications events */
|
|
11
|
+
export type UnlockedWalletNotification = UnlockedHiddenContent & {
|
|
12
|
+
[WalletNotificationSymbol]: WalletNotification;
|
|
13
|
+
};
|
|
8
14
|
/** Supported notification types */
|
|
9
15
|
export type NotificationType = "payment_received" | "payment_sent";
|
|
10
16
|
/** Base notification structure */
|
|
@@ -21,8 +27,10 @@ export type PaymentSentNotification = BaseNotification<"payment_sent", Transacti
|
|
|
21
27
|
/** Union type for all NIP-47 notification types */
|
|
22
28
|
export type WalletNotification = PaymentReceivedNotification | PaymentSentNotification;
|
|
23
29
|
/** Checks if a kind 23196 or 23197 event is locked */
|
|
24
|
-
export declare function
|
|
30
|
+
export declare function isWalletNotificationUnlocked(notification: any): notification is UnlockedWalletNotification;
|
|
25
31
|
/** Unlocks a kind 23196 or 23197 event */
|
|
26
|
-
export declare function unlockWalletNotification(notification: NostrEvent, signer: HiddenContentSigner): Promise<WalletNotification | undefined
|
|
32
|
+
export declare function unlockWalletNotification(notification: NostrEvent, signer: HiddenContentSigner): Promise<WalletNotification | undefined>;
|
|
27
33
|
/** Gets the wallet notification from a kind 23196 or 23197 event */
|
|
28
|
-
export declare function getWalletNotification(notification: NostrEvent): WalletNotification | undefined
|
|
34
|
+
export declare function getWalletNotification(notification: NostrEvent): WalletNotification | undefined;
|
|
35
|
+
/** Checks if an event is a valid wallet notification event */
|
|
36
|
+
export declare function isValidWalletNotification(notification: NostrEvent): notification is WalletNotificationEvent;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isHiddenContentUnlocked, notifyEventUpdate, setHiddenContentEncryptionMethod, unlockHiddenContent, } from "applesauce-core/helpers";
|
|
2
2
|
export const WALLET_NOTIFICATION_KIND = 23197;
|
|
3
3
|
export const WALLET_LEGACY_NOTIFICATION_KIND = 23196;
|
|
4
4
|
/** A symbol used to cache the wallet notification on the event */
|
|
@@ -7,22 +7,28 @@ export const WalletNotificationSymbol = Symbol("wallet-notification");
|
|
|
7
7
|
setHiddenContentEncryptionMethod(WALLET_NOTIFICATION_KIND, "nip44");
|
|
8
8
|
setHiddenContentEncryptionMethod(WALLET_LEGACY_NOTIFICATION_KIND, "nip04");
|
|
9
9
|
/** Checks if a kind 23196 or 23197 event is locked */
|
|
10
|
-
export function
|
|
11
|
-
return
|
|
10
|
+
export function isWalletNotificationUnlocked(notification) {
|
|
11
|
+
return isHiddenContentUnlocked(notification) && Reflect.has(notification, WalletNotificationSymbol) === true;
|
|
12
12
|
}
|
|
13
13
|
/** Unlocks a kind 23196 or 23197 event */
|
|
14
14
|
export async function unlockWalletNotification(notification, signer) {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
if (isWalletNotificationUnlocked(notification))
|
|
16
|
+
return notification[WalletNotificationSymbol];
|
|
17
|
+
const content = await unlockHiddenContent(notification, signer);
|
|
18
|
+
const parsed = JSON.parse(content);
|
|
19
|
+
// Save the parsed content
|
|
20
|
+
Reflect.set(notification, WalletNotificationSymbol, parsed);
|
|
21
|
+
notifyEventUpdate(notification);
|
|
22
|
+
return parsed;
|
|
17
23
|
}
|
|
18
24
|
/** Gets the wallet notification from a kind 23196 or 23197 event */
|
|
19
25
|
export function getWalletNotification(notification) {
|
|
20
|
-
if (
|
|
26
|
+
if (isWalletNotificationUnlocked(notification))
|
|
27
|
+
return notification[WalletNotificationSymbol];
|
|
28
|
+
else
|
|
21
29
|
return undefined;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return JSON.parse(content);
|
|
27
|
-
});
|
|
30
|
+
}
|
|
31
|
+
/** Checks if an event is a valid wallet notification event */
|
|
32
|
+
export function isValidWalletNotification(notification) {
|
|
33
|
+
return notification.kind === WALLET_NOTIFICATION_KIND || notification.kind === WALLET_LEGACY_NOTIFICATION_KIND;
|
|
28
34
|
}
|
|
@@ -1,127 +1,23 @@
|
|
|
1
|
-
import { HiddenContentSigner } from "applesauce-core/helpers";
|
|
1
|
+
import { HiddenContentSigner, KnownEvent, UnlockedHiddenContent } from "applesauce-core/helpers";
|
|
2
2
|
import { NostrEvent } from "nostr-tools";
|
|
3
3
|
import { WalletConnectEncryptionMethod } from "./encryption.js";
|
|
4
|
-
import {
|
|
4
|
+
import { TWalletMethod } from "./methods.js";
|
|
5
5
|
export declare const WALLET_REQUEST_KIND = 23194;
|
|
6
|
+
export type WalletRequestEvent = KnownEvent<typeof WALLET_REQUEST_KIND>;
|
|
6
7
|
/** A symbol used to cache the wallet request on the event */
|
|
7
8
|
export declare const WalletRequestSymbol: unique symbol;
|
|
8
|
-
/**
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/** Hex encoded TLV value */
|
|
13
|
-
value: string;
|
|
14
|
-
}
|
|
15
|
-
/** Base request structure for all NIP-47 requests */
|
|
16
|
-
export interface BaseWalletRequest<TMethod extends WalletMethod, TParams> {
|
|
17
|
-
/** The method to call */
|
|
18
|
-
method: TMethod;
|
|
19
|
-
/** Parameters for the method */
|
|
20
|
-
params: TParams;
|
|
21
|
-
}
|
|
22
|
-
/** Parameters for pay_invoice method */
|
|
23
|
-
export interface PayInvoiceParams {
|
|
24
|
-
/** BOLT11 invoice */
|
|
25
|
-
invoice: string;
|
|
26
|
-
/** Invoice amount in msats, optional */
|
|
27
|
-
amount?: number;
|
|
28
|
-
}
|
|
29
|
-
export type PayInvoiceRequest = BaseWalletRequest<"pay_invoice", PayInvoiceParams>;
|
|
30
|
-
/** Parameters for multi_pay_invoice method */
|
|
31
|
-
export interface MultiPayInvoiceParams {
|
|
32
|
-
/** Array of invoices to pay */
|
|
33
|
-
invoices: Array<{
|
|
34
|
-
/** ID to identify this invoice in the response */
|
|
35
|
-
id?: string;
|
|
36
|
-
/** BOLT11 invoice */
|
|
37
|
-
invoice: string;
|
|
38
|
-
/** Invoice amount in msats, optional */
|
|
39
|
-
amount?: number;
|
|
40
|
-
}>;
|
|
41
|
-
}
|
|
42
|
-
export type MultiPayInvoiceRequest = BaseWalletRequest<"multi_pay_invoice", MultiPayInvoiceParams>;
|
|
43
|
-
/** Parameters for pay_keysend method */
|
|
44
|
-
export interface PayKeysendParams {
|
|
45
|
-
/** Amount in msats, required */
|
|
46
|
-
amount: number;
|
|
47
|
-
/** Payee pubkey, required */
|
|
48
|
-
pubkey: string;
|
|
49
|
-
/** Preimage of the payment, optional */
|
|
50
|
-
preimage?: string;
|
|
51
|
-
/** TLV records, optional */
|
|
52
|
-
tlv_records?: TLVRecord[];
|
|
53
|
-
}
|
|
54
|
-
export type PayKeysendRequest = BaseWalletRequest<"pay_keysend", PayKeysendParams>;
|
|
55
|
-
/** Parameters for multi_pay_keysend method */
|
|
56
|
-
export interface MultiPayKeysendParams {
|
|
57
|
-
/** Array of keysend payments */
|
|
58
|
-
keysends: Array<{
|
|
59
|
-
/** ID to identify this keysend in the response */
|
|
60
|
-
id?: string;
|
|
61
|
-
/** Payee pubkey, required */
|
|
62
|
-
pubkey: string;
|
|
63
|
-
/** Amount in msats, required */
|
|
64
|
-
amount: number;
|
|
65
|
-
/** Preimage of the payment, optional */
|
|
66
|
-
preimage?: string;
|
|
67
|
-
/** TLV records, optional */
|
|
68
|
-
tlv_records?: TLVRecord[];
|
|
69
|
-
}>;
|
|
70
|
-
}
|
|
71
|
-
export type MultiPayKeysendRequest = BaseWalletRequest<"multi_pay_keysend", MultiPayKeysendParams>;
|
|
72
|
-
/** Parameters for make_invoice method */
|
|
73
|
-
export interface MakeInvoiceParams {
|
|
74
|
-
/** Value in msats */
|
|
75
|
-
amount: number;
|
|
76
|
-
/** Invoice's description, optional */
|
|
77
|
-
description?: string;
|
|
78
|
-
/** Invoice's description hash, optional */
|
|
79
|
-
description_hash?: string;
|
|
80
|
-
/** Expiry in seconds from time invoice is created, optional */
|
|
81
|
-
expiry?: number;
|
|
82
|
-
}
|
|
83
|
-
export type MakeInvoiceRequest = BaseWalletRequest<"make_invoice", MakeInvoiceParams>;
|
|
84
|
-
/** Parameters for lookup_invoice method */
|
|
85
|
-
export interface LookupInvoiceParams {
|
|
86
|
-
/** Payment hash of the invoice, one of payment_hash or invoice is required */
|
|
87
|
-
payment_hash?: string;
|
|
88
|
-
/** Invoice to lookup */
|
|
89
|
-
invoice?: string;
|
|
90
|
-
}
|
|
91
|
-
export type LookupInvoiceRequest = BaseWalletRequest<"lookup_invoice", LookupInvoiceParams>;
|
|
92
|
-
/** Parameters for list_transactions method */
|
|
93
|
-
export interface ListTransactionsParams {
|
|
94
|
-
/** Starting timestamp in seconds since epoch (inclusive), optional */
|
|
95
|
-
from?: number;
|
|
96
|
-
/** Ending timestamp in seconds since epoch (inclusive), optional */
|
|
97
|
-
until?: number;
|
|
98
|
-
/** Maximum number of invoices to return, optional */
|
|
99
|
-
limit?: number;
|
|
100
|
-
/** Offset of the first invoice to return, optional */
|
|
101
|
-
offset?: number;
|
|
102
|
-
/** Include unpaid invoices, optional, default false */
|
|
103
|
-
unpaid?: boolean;
|
|
104
|
-
/** "incoming" for invoices, "outgoing" for payments, undefined for both */
|
|
105
|
-
type?: "incoming" | "outgoing";
|
|
106
|
-
}
|
|
107
|
-
export type ListTransactionsRequest = BaseWalletRequest<"list_transactions", ListTransactionsParams>;
|
|
108
|
-
/** Parameters for get_balance method */
|
|
109
|
-
export interface GetBalanceParams {
|
|
110
|
-
}
|
|
111
|
-
export type GetBalanceRequest = BaseWalletRequest<"get_balance", GetBalanceParams>;
|
|
112
|
-
/** Parameters for get_info method */
|
|
113
|
-
export interface GetInfoParams {
|
|
114
|
-
}
|
|
115
|
-
export type GetInfoRequest = BaseWalletRequest<"get_info", GetInfoParams>;
|
|
116
|
-
/** Union type for all NIP-47 request types */
|
|
117
|
-
export type WalletRequest = PayInvoiceRequest | MultiPayInvoiceRequest | PayKeysendRequest | MultiPayKeysendRequest | MakeInvoiceRequest | LookupInvoiceRequest | ListTransactionsRequest | GetBalanceRequest | GetInfoRequest;
|
|
9
|
+
/** Type for events with unlocked hidden content */
|
|
10
|
+
export type UnlockedWalletRequest<Method extends TWalletMethod = TWalletMethod> = UnlockedHiddenContent & {
|
|
11
|
+
[WalletRequestSymbol]: Method["request"];
|
|
12
|
+
};
|
|
118
13
|
/** Checks if a kind 23194 event is locked */
|
|
119
|
-
export declare function
|
|
14
|
+
export declare function isWalletRequestUnlocked<Method extends TWalletMethod = TWalletMethod>(request: any): request is UnlockedWalletRequest<Method>;
|
|
120
15
|
/** Unlocks a kind 23194 event */
|
|
121
|
-
export declare function unlockWalletRequest(request: NostrEvent, signer: HiddenContentSigner): Promise<
|
|
16
|
+
export declare function unlockWalletRequest<Method extends TWalletMethod = TWalletMethod>(request: NostrEvent, signer: HiddenContentSigner): Promise<Method["request"] | undefined>;
|
|
122
17
|
/** Gets the wallet request from a kind 23194 event */
|
|
123
|
-
export declare function getWalletRequest(request: NostrEvent):
|
|
18
|
+
export declare function getWalletRequest<Method extends TWalletMethod = TWalletMethod>(request: NostrEvent): Method["request"] | undefined;
|
|
124
19
|
/** Returns the wallet service pubkey from a request */
|
|
20
|
+
export declare function getWalletRequestServicePubkey(request: WalletRequestEvent): string;
|
|
125
21
|
export declare function getWalletRequestServicePubkey(request: NostrEvent): string | undefined;
|
|
126
22
|
/** Returns the expiration timestamp from a request */
|
|
127
23
|
export declare function getWalletRequestExpiration(request: NostrEvent): number | undefined;
|
|
@@ -129,3 +25,5 @@ export declare function getWalletRequestExpiration(request: NostrEvent): number
|
|
|
129
25
|
export declare function isWalletRequestExpired(request: NostrEvent): boolean;
|
|
130
26
|
/** Gets the encryption method used for a request */
|
|
131
27
|
export declare function getWalletRequestEncryption(request: NostrEvent): WalletConnectEncryptionMethod;
|
|
28
|
+
/** Checks if an event is a valid wallet request event */
|
|
29
|
+
export declare function isValidWalletRequest(request: NostrEvent): request is WalletRequestEvent;
|
package/dist/helpers/request.js
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getTagValue, isHiddenContentUnlocked, isNIP04Encrypted, notifyEventUpdate, setHiddenContentEncryptionMethod, unixNow, unlockHiddenContent, } from "applesauce-core/helpers";
|
|
2
2
|
export const WALLET_REQUEST_KIND = 23194;
|
|
3
3
|
// Set the encryption method to use for request kind
|
|
4
4
|
setHiddenContentEncryptionMethod(WALLET_REQUEST_KIND, "nip44");
|
|
5
5
|
/** A symbol used to cache the wallet request on the event */
|
|
6
6
|
export const WalletRequestSymbol = Symbol("wallet-request");
|
|
7
7
|
/** Checks if a kind 23194 event is locked */
|
|
8
|
-
export function
|
|
9
|
-
return
|
|
8
|
+
export function isWalletRequestUnlocked(request) {
|
|
9
|
+
return isHiddenContentUnlocked(request) && Reflect.has(request, WalletRequestSymbol) === true;
|
|
10
10
|
}
|
|
11
11
|
/** Unlocks a kind 23194 event */
|
|
12
12
|
export async function unlockWalletRequest(request, signer) {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
if (isWalletRequestUnlocked(request))
|
|
14
|
+
return request[WalletRequestSymbol];
|
|
15
|
+
const content = await unlockHiddenContent(request, signer);
|
|
16
|
+
const parsed = JSON.parse(content);
|
|
17
|
+
// Save the parsed content
|
|
18
|
+
Reflect.set(request, WalletRequestSymbol, parsed);
|
|
19
|
+
notifyEventUpdate(request);
|
|
20
|
+
return parsed;
|
|
15
21
|
}
|
|
16
22
|
/** Gets the wallet request from a kind 23194 event */
|
|
17
23
|
export function getWalletRequest(request) {
|
|
18
|
-
if (
|
|
24
|
+
if (isWalletRequestUnlocked(request))
|
|
25
|
+
return request[WalletRequestSymbol];
|
|
26
|
+
else
|
|
19
27
|
return undefined;
|
|
20
|
-
|
|
21
|
-
const content = getHiddenContent(request);
|
|
22
|
-
if (!content)
|
|
23
|
-
return null;
|
|
24
|
-
return JSON.parse(content);
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
/** Returns the wallet service pubkey from a request */
|
|
28
|
+
}
|
|
28
29
|
export function getWalletRequestServicePubkey(request) {
|
|
29
30
|
return getTagValue(request, "p");
|
|
30
31
|
}
|
|
@@ -49,3 +50,7 @@ export function getWalletRequestEncryption(request) {
|
|
|
49
50
|
? "nip04"
|
|
50
51
|
: "nip44_v2";
|
|
51
52
|
}
|
|
53
|
+
/** Checks if an event is a valid wallet request event */
|
|
54
|
+
export function isValidWalletRequest(request) {
|
|
55
|
+
return request.kind === WALLET_REQUEST_KIND && getWalletRequestServicePubkey(request) !== undefined;
|
|
56
|
+
}
|