applesauce-signers 7.0.1 → 7.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.
@@ -93,6 +93,8 @@ export declare class NostrConnectSigner implements ISigner {
93
93
  /** Close the connection */
94
94
  close(): Promise<void>;
95
95
  protected requests: Map<string, Deferred<any>>;
96
+ /** Kept separately so the protected `requests` map keeps its published type (patch-semver) */
97
+ private requestMethods;
96
98
  protected auths: Set<string>;
97
99
  /** Timeout timers for pending requests */
98
100
  protected timers: Map<string, number>;
@@ -11,6 +11,31 @@ 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 {
15
40
  /** A fallback method to use for subscriptionMethod if none is pass in when creating the signer */
16
41
  static subscriptionMethod = undefined;
@@ -141,6 +166,8 @@ export class NostrConnectSigner {
141
166
  this.log("Closed");
142
167
  }
143
168
  requests = new Map();
169
+ /** Kept separately so the protected `requests` map keeps its published type (patch-semver) */
170
+ requestMethods = new Map();
144
171
  auths = new Set();
145
172
  /** Timeout timers for pending requests */
146
173
  timers = new Map();
@@ -182,12 +209,15 @@ export class NostrConnectSigner {
182
209
  return;
183
210
  }
184
211
  if (response.id) {
185
- const p = this.requests.get(response.id);
186
- if (!p)
212
+ const defer = this.requests.get(response.id);
213
+ if (!defer)
187
214
  return;
215
+ const method = this.requestMethods.get(response.id);
188
216
  if (response.error) {
189
- this.log("Got Error", response.id, response.result, response.error);
190
217
  if (response.result === "auth_url") {
218
+ // the error field carries the auth URL; its query and fragment hold bearer
219
+ // capabilities, so log the URL with those parts stripped
220
+ this.log("Got auth_url", response.id, redactAuthUrl(response.error));
191
221
  if (!this.auths.has(response.id)) {
192
222
  this.auths.add(response.id);
193
223
  // Give the user time to authenticate before timing out
@@ -197,17 +227,20 @@ export class NostrConnectSigner {
197
227
  await this.onAuth(response.error);
198
228
  }
199
229
  catch (e) {
200
- p.reject(e);
230
+ defer.reject(e);
201
231
  }
202
232
  }
203
233
  }
204
234
  }
205
- else
206
- p.reject(new Error(response.error));
235
+ else {
236
+ // error strings from sensitive methods can echo rejected secrets or drafts
237
+ this.log("Got Error", response.id, SAFE_LOG_METHODS.has(method) ? response.error : `[${method}] [error redacted]`);
238
+ defer.reject(new Error(response.error));
239
+ }
207
240
  }
208
241
  else if (response.result !== undefined) {
209
- this.log("Got Response", response.id, response.result);
210
- p.resolve(response.result);
242
+ this.log("Got Response", response.id, SAFE_LOG_METHODS.has(method) ? response.result : "[redacted]");
243
+ defer.resolve(response.result);
211
244
  }
212
245
  }
213
246
  }
@@ -237,12 +270,14 @@ export class NostrConnectSigner {
237
270
  const request = { id, method, params };
238
271
  const encrypted = await this.signer.nip44.encrypt(this.remote, JSON.stringify(request));
239
272
  const event = await this.createRequestEvent(encrypted, this.remote, kind);
240
- this.log(`Sending ${id} (${method}) ${JSON.stringify(params)}`);
273
+ this.log(`Sending ${id} (${method})`, SAFE_LOG_METHODS.has(method) ? JSON.stringify(params) : "[redacted]");
241
274
  const p = createDefer();
242
275
  this.requests.set(id, p);
276
+ this.requestMethods.set(id, method);
243
277
  // Cleanup when the request is resolved, rejected, or timed out
244
278
  const cleanup = () => {
245
279
  this.requests.delete(id);
280
+ this.requestMethods.delete(id);
246
281
  this.auths.delete(id);
247
282
  clearTimeout(this.timers.get(id));
248
283
  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.1.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
  }