applesauce-signers 7.0.1 → 7.2.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.
@@ -1,5 +1,5 @@
1
1
  import { type Debugger } from "applesauce-core";
2
- import { EventTemplate, NostrEvent } from "applesauce-core/helpers/event";
2
+ import { EventTemplate, NostrEvent, verifyEvent } from "applesauce-core/helpers/event";
3
3
  import { Subscription } from "rxjs";
4
4
  import { ConnectRequestParams, ConnectResponseResults, NostrConnectMethod, NostrConnectResponse, NostrConnectURI } from "../helpers/nostr-connect.js";
5
5
  import { ISigner, NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod, NostrSubscriptionMethod } from "../interop.js";
@@ -23,6 +23,8 @@ export interface ProviderAuthorization {
23
23
  onLogout?: (client: string) => void | Promise<void>;
24
24
  }
25
25
  export type NostrConnectProviderOptions = ProviderAuthorization & NostrConnectionMethodsOptions & {
26
+ /** A synchronous method that verifies event hashes and signatures */
27
+ verifyEvent?: typeof verifyEvent;
26
28
  /** The relays to communicate over */
27
29
  relays: string[];
28
30
  /** The signer to use for signing events and encryption */
@@ -39,6 +41,10 @@ export type NostrConnectProviderOptions = ProviderAuthorization & NostrConnectio
39
41
  onClientDisconnect?: (client: string) => void;
40
42
  };
41
43
  export declare class NostrConnectProvider implements ProviderAuthorization {
44
+ /** The default event verifier for new instances */
45
+ static verifyEvent: typeof verifyEvent;
46
+ /** A synchronous method that verifies incoming request events */
47
+ verifyEvent: typeof verifyEvent;
42
48
  /** A fallback method to use for subscriptionMethod if none is passed in when creating the provider */
43
49
  static subscriptionMethod: NostrSubscriptionMethod | undefined;
44
50
  /** A fallback method to use for publishMethod if none is passed in when creating the provider */
@@ -9,6 +9,10 @@ import { createBunkerURI, createNbunksec, NostrConnectMethod, parseNostrConnectU
9
9
  import { getConnectionMethods, } from "../interop.js";
10
10
  import { PrivateKeySigner } from "./private-key-signer.js";
11
11
  export class NostrConnectProvider {
12
+ /** The default event verifier for new instances */
13
+ static verifyEvent = verifyEvent;
14
+ /** A synchronous method that verifies incoming request events */
15
+ verifyEvent;
12
16
  /** A fallback method to use for subscriptionMethod if none is passed in when creating the provider */
13
17
  static subscriptionMethod = undefined;
14
18
  /** A fallback method to use for publishMethod if none is passed in when creating the provider */
@@ -68,6 +72,7 @@ export class NostrConnectProvider {
68
72
  /** A method called when a client requests to logout (end the session) */
69
73
  onLogout;
70
74
  constructor(options) {
75
+ this.verifyEvent = options.verifyEvent ?? NostrConnectProvider.verifyEvent;
71
76
  this.relays = options.relays;
72
77
  this.upstream = options.upstream;
73
78
  this.signer = options.signer ?? new PrivateKeySigner();
@@ -189,7 +194,7 @@ export class NostrConnectProvider {
189
194
  }
190
195
  /** Call this method with incoming events */
191
196
  async handleEvent(event) {
192
- if (!verifyEvent(event))
197
+ if (!this.verifyEvent(event))
193
198
  return;
194
199
  // Do nothing if this request has already been seen
195
200
  if (this.seen.has(event.id) || event.created_at <= this.seenUntil)
@@ -5,6 +5,8 @@ import { ISigner, NostrConnectionMethodsOptions, NostrPool, NostrPublishMethod,
5
5
  import { Subscription } from "rxjs";
6
6
  import { BunkerURI, NostrConnectAppMetadata, Nbunksec } from "../helpers/nostr-connect.js";
7
7
  export type NostrConnectSignerOptions = NostrConnectionMethodsOptions & {
8
+ /** A synchronous method that verifies event hashes and signatures */
9
+ verifyEvent?: typeof verifyEvent;
8
10
  /** The relays to communicate over */
9
11
  relays: string[];
10
12
  /** A {@link PrivateKeySigner} for this client */
@@ -34,6 +36,8 @@ export type NostrConnectLogoutOptions = {
34
36
  timeout?: number;
35
37
  };
36
38
  export declare class NostrConnectSigner implements ISigner {
39
+ /** The default event verifier for new instances */
40
+ static verifyEvent: typeof verifyEvent;
37
41
  /** A fallback method to use for subscriptionMethod if none is pass in when creating the signer */
38
42
  static subscriptionMethod: NostrSubscriptionMethod | undefined;
39
43
  /** A fallback method to use for publishMethod if none is pass in when creating the signer */
@@ -67,6 +71,7 @@ export declare class NostrConnectSigner implements ISigner {
67
71
  autoSwitchRelays: boolean;
68
72
  /** Client metadata sent with `connect` requests */
69
73
  metadata?: NostrConnectAppMetadata;
74
+ /** A synchronous method that verifies response events and returned signed events */
70
75
  verifyEvent: typeof verifyEvent;
71
76
  /** A secret used when initiating a connection from the client side (the `secret` in a nostrconnect:// URI) */
72
77
  connectSecret: string;
@@ -93,6 +98,8 @@ export declare class NostrConnectSigner implements ISigner {
93
98
  /** Close the connection */
94
99
  close(): Promise<void>;
95
100
  protected requests: Map<string, Deferred<any>>;
101
+ /** Kept separately so the protected `requests` map keeps its published type (patch-semver) */
102
+ private requestMethods;
96
103
  protected auths: Set<string>;
97
104
  /** Timeout timers for pending requests */
98
105
  protected timers: Map<string, number>;
@@ -11,7 +11,34 @@ import { createConnectMetadata, createNbunksec, NostrConnectMethod, buildSigning
11
11
  async function defaultHandleAuth(url) {
12
12
  window.open(url, "auth", "width=400,height=600,resizable=no,status=no,location=no,toolbar=no,menubar=no");
13
13
  }
14
+ /**
15
+ * Lifecycle methods whose params and results are safe to log verbatim (public keys, relay
16
+ * lists, status acks). Everything else fails closed: params and results log as "[redacted]",
17
+ * so newly added NIP-46 methods can never leak secrets into an enabled debug namespace.
18
+ */
19
+ // undefined (a response with no matching request) is not in the set: redacted, fail-closed
20
+ const SAFE_LOG_METHODS = new Set([
21
+ NostrConnectMethod.GetPublicKey,
22
+ NostrConnectMethod.Ping,
23
+ NostrConnectMethod.SwitchRelays,
24
+ NostrConnectMethod.Logout,
25
+ ]);
26
+ /**
27
+ * Strips the query and fragment from an auth URL before logging — bunker auth challenges carry
28
+ * bearer capabilities (approval tokens, OAuth state) in exactly those parts.
29
+ */
30
+ function redactAuthUrl(raw) {
31
+ try {
32
+ const url = new URL(raw);
33
+ return url.origin + url.pathname + (url.search ? "?[redacted]" : "") + (url.hash ? "#[redacted]" : "");
34
+ }
35
+ catch {
36
+ return "[invalid-auth-url]";
37
+ }
38
+ }
14
39
  export class NostrConnectSigner {
40
+ /** The default event verifier for new instances */
41
+ static verifyEvent = verifyEvent;
15
42
  /** A fallback method to use for subscriptionMethod if none is pass in when creating the signer */
16
43
  static subscriptionMethod = undefined;
17
44
  /** A fallback method to use for publishMethod if none is pass in when creating the signer */
@@ -47,7 +74,8 @@ export class NostrConnectSigner {
47
74
  autoSwitchRelays;
48
75
  /** Client metadata sent with `connect` requests */
49
76
  metadata;
50
- verifyEvent = verifyEvent;
77
+ /** A synchronous method that verifies response events and returned signed events */
78
+ verifyEvent;
51
79
  /** A secret used when initiating a connection from the client side (the `secret` in a nostrconnect:// URI) */
52
80
  connectSecret;
53
81
  /** A secret used when connecting to a remote bunker (the `secret` in a bunker:// URI) */
@@ -62,6 +90,7 @@ export class NostrConnectSigner {
62
90
  nip04;
63
91
  nip44;
64
92
  constructor(options) {
93
+ this.verifyEvent = options.verifyEvent ?? NostrConnectSigner.verifyEvent;
65
94
  this.relays = options.relays;
66
95
  this.pubkey = options.pubkey;
67
96
  this.remote = options.remote;
@@ -141,6 +170,8 @@ export class NostrConnectSigner {
141
170
  this.log("Closed");
142
171
  }
143
172
  requests = new Map();
173
+ /** Kept separately so the protected `requests` map keeps its published type (patch-semver) */
174
+ requestMethods = new Map();
144
175
  auths = new Set();
145
176
  /** Timeout timers for pending requests */
146
177
  timers = new Map();
@@ -182,12 +213,15 @@ export class NostrConnectSigner {
182
213
  return;
183
214
  }
184
215
  if (response.id) {
185
- const p = this.requests.get(response.id);
186
- if (!p)
216
+ const defer = this.requests.get(response.id);
217
+ if (!defer)
187
218
  return;
219
+ const method = this.requestMethods.get(response.id);
188
220
  if (response.error) {
189
- this.log("Got Error", response.id, response.result, response.error);
190
221
  if (response.result === "auth_url") {
222
+ // the error field carries the auth URL; its query and fragment hold bearer
223
+ // capabilities, so log the URL with those parts stripped
224
+ this.log("Got auth_url", response.id, redactAuthUrl(response.error));
191
225
  if (!this.auths.has(response.id)) {
192
226
  this.auths.add(response.id);
193
227
  // Give the user time to authenticate before timing out
@@ -197,17 +231,20 @@ export class NostrConnectSigner {
197
231
  await this.onAuth(response.error);
198
232
  }
199
233
  catch (e) {
200
- p.reject(e);
234
+ defer.reject(e);
201
235
  }
202
236
  }
203
237
  }
204
238
  }
205
- else
206
- p.reject(new Error(response.error));
239
+ else {
240
+ // error strings from sensitive methods can echo rejected secrets or drafts
241
+ this.log("Got Error", response.id, SAFE_LOG_METHODS.has(method) ? response.error : `[${method}] [error redacted]`);
242
+ defer.reject(new Error(response.error));
243
+ }
207
244
  }
208
245
  else if (response.result !== undefined) {
209
- this.log("Got Response", response.id, response.result);
210
- p.resolve(response.result);
246
+ this.log("Got Response", response.id, SAFE_LOG_METHODS.has(method) ? response.result : "[redacted]");
247
+ defer.resolve(response.result);
211
248
  }
212
249
  }
213
250
  }
@@ -237,12 +274,14 @@ export class NostrConnectSigner {
237
274
  const request = { id, method, params };
238
275
  const encrypted = await this.signer.nip44.encrypt(this.remote, JSON.stringify(request));
239
276
  const event = await this.createRequestEvent(encrypted, this.remote, kind);
240
- this.log(`Sending ${id} (${method}) ${JSON.stringify(params)}`);
277
+ this.log(`Sending ${id} (${method})`, SAFE_LOG_METHODS.has(method) ? JSON.stringify(params) : "[redacted]");
241
278
  const p = createDefer();
242
279
  this.requests.set(id, p);
280
+ this.requestMethods.set(id, method);
243
281
  // Cleanup when the request is resolved, rejected, or timed out
244
282
  const cleanup = () => {
245
283
  this.requests.delete(id);
284
+ this.requestMethods.delete(id);
246
285
  this.auths.delete(id);
247
286
  clearTimeout(this.timers.get(id));
248
287
  this.timers.delete(id);
@@ -53,5 +53,10 @@ export declare class SerialPortSigner implements ISigner {
53
53
  static METHOD_PUBLIC_KEY: string;
54
54
  static METHOD_RESTORE: string;
55
55
  static PUBLIC_METHODS: string[];
56
+ /** Methods whose params and responses are safe to log verbatim. Everything else ([restore]
57
+ * carries the raw private key, [shared-secret] returns ECDH conversation keys, [sign-message]
58
+ * returns draft signatures) logs as "[redacted]" so an enabled debug namespace cannot capture
59
+ * key material. */
60
+ static SAFE_LOG_METHODS: string[];
56
61
  }
57
62
  export {};
@@ -77,7 +77,9 @@ export class SerialPortSigner {
77
77
  // ignore /ping, /log responses
78
78
  continue;
79
79
  }
80
- this.log("Received: ", method, data);
80
+ // /shared-secret responses are raw ECDH conversation keys and /sign-message
81
+ // responses are signatures over drafts — only the key and ping methods log verbatim
82
+ this.log("Received: ", method, SerialPortSigner.SAFE_LOG_METHODS.includes(method) ? data : "[redacted]");
81
83
  if (this.lastCommand) {
82
84
  this.lastCommand.resolve(data);
83
85
  this.lastCommand = null;
@@ -130,7 +132,8 @@ export class SerialPortSigner {
130
132
  async sendCommand(method, params = []) {
131
133
  if (!this.writer)
132
134
  return;
133
- this.log("Send command", method, params);
135
+ // /restore params carry the raw private key in hex — only the key and ping methods log verbatim
136
+ this.log("Send command", method, SerialPortSigner.SAFE_LOG_METHODS.includes(method) ? params : "[redacted]");
134
137
  const message = [method].concat(params).join(" ");
135
138
  await this.writer.write(message + "\n");
136
139
  }
@@ -236,4 +239,9 @@ export class SerialPortSigner {
236
239
  SerialPortSigner.METHOD_SHARED_SECRET,
237
240
  SerialPortSigner.METHOD_RESTORE,
238
241
  ];
242
+ /** Methods whose params and responses are safe to log verbatim. Everything else ([restore]
243
+ * carries the raw private key, [shared-secret] returns ECDH conversation keys, [sign-message]
244
+ * returns draft signatures) logs as "[redacted]" so an enabled debug namespace cannot capture
245
+ * key material. */
246
+ static SAFE_LOG_METHODS = [SerialPortSigner.METHOD_PING, SerialPortSigner.METHOD_PUBLIC_KEY];
239
247
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-signers",
3
- "version": "7.0.1",
3
+ "version": "7.2.0",
4
4
  "description": "Signer classes for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -44,20 +44,20 @@
44
44
  }
45
45
  },
46
46
  "dependencies": {
47
- "@noble/secp256k1": "^3.1.0",
48
- "@scure/base": "^2.2.0",
47
+ "@noble/secp256k1": "^3.2.0",
48
+ "@scure/base": "^2.4.0",
49
49
  "@scure/bip32": "^2.4.0",
50
- "@scure/bip39": "^2.0.1",
51
- "applesauce-core": "^7.0.0",
52
- "nanoid": "^5.0.9",
50
+ "@scure/bip39": "^2.4.0",
51
+ "applesauce-core": "^7.1.0",
52
+ "nanoid": "^6.0.2",
53
53
  "rxjs": "^7.8.2"
54
54
  },
55
55
  "devDependencies": {
56
- "@types/dom-serial": "^1.0.6",
57
- "rimraf": "^6.1.3",
56
+ "@types/dom-serial": "^1.0.7",
57
+ "rimraf": "^6.1.4",
58
58
  "typescript": "^7.0.2",
59
- "vitest": "^4.1.11",
60
- "vitest-websocket-mock": "^0.5.0"
59
+ "vitest": "^5.1.0",
60
+ "vitest-websocket-mock": "^0.8.0"
61
61
  },
62
62
  "funding": {
63
63
  "type": "lightning",
@@ -70,7 +70,7 @@
70
70
  "prebuild": "rimraf dist",
71
71
  "build": "tsc",
72
72
  "watch:build": "tsc --watch > /dev/null",
73
- "test": "vitest run --passWithNoTests",
74
- "watch:test": "vitest"
73
+ "test": "vitest run --config ../../vitest.config.mts --dir . --passWithNoTests",
74
+ "watch:test": "vitest --config ../../vitest.config.mts --dir ."
75
75
  }
76
76
  }