applesauce-signers 0.0.0-next-20250522030625 → 0.0.0-next-20250606170247

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.
@@ -82,8 +82,10 @@ interface Observer<T> {
82
82
  type Subscribable<T extends unknown> = {
83
83
  subscribe: (observer: Partial<Observer<T>>) => Unsubscribable;
84
84
  };
85
- export type NostrSubscriptionMethod = (relays: string[], filters: Filter[]) => Subscribable<NostrEvent>;
86
- export type NostrPublishMethod = (relays: string[], event: NostrEvent) => any | Promise<any>;
85
+ /** A method used to subscribe to events on a set of relays */
86
+ export type NostrSubscriptionMethod = (relays: string[], filters: Filter[]) => Subscribable<NostrEvent | string>;
87
+ /** A method used for publishing an event, can return a Promise that completes when published or an Observable that completes when published*/
88
+ export type NostrPublishMethod = (relays: string[], event: NostrEvent) => Promise<any> | Subscribable<any>;
87
89
  export type NostrConnectAppMetadata = {
88
90
  name?: string;
89
91
  image?: string;
@@ -104,7 +104,7 @@ export class NostrConnectSigner {
104
104
  "#p": [pubkey],
105
105
  },
106
106
  ]).subscribe({
107
- next: (event) => this.handleEvent(event),
107
+ next: (event) => typeof event !== "string" && this.handleEvent(event),
108
108
  });
109
109
  this.log("Opened", this.relays);
110
110
  }
@@ -195,10 +195,16 @@ export class NostrConnectSigner {
195
195
  const request = { id, method, params };
196
196
  const encrypted = await this.signer.nip44.encrypt(this.remote, JSON.stringify(request));
197
197
  const event = await this.createRequestEvent(encrypted, this.remote, kind);
198
- this.log(`Sending request ${id} (${method}) ${JSON.stringify(params)}`);
198
+ this.log(`Sending ${id} (${method}) ${JSON.stringify(params)}`);
199
199
  const p = createDefer();
200
200
  this.requests.set(id, p);
201
- await this.publishMethod?.(this.relays, event);
201
+ const result = this.publishMethod?.(this.relays, event);
202
+ // Handle returned Promise or Observable
203
+ if (result instanceof Promise)
204
+ await result;
205
+ else if ("subscribe" in result)
206
+ await new Promise((res) => result.subscribe({ complete: res }));
207
+ this.log(`Sent ${id} (${method})`);
202
208
  return p;
203
209
  }
204
210
  /** Connect to remote signer */
@@ -17,9 +17,14 @@ export declare class PasswordSigner implements Nip07Interface {
17
17
  constructor();
18
18
  unlockPromise?: Deferred<void>;
19
19
  protected requestUnlock(): Deferred<void> | undefined;
20
+ /** Sets the ncryptsec from the key and password */
20
21
  setPassword(password: string): Promise<void>;
22
+ /** Tests if the provided password is correct by decrypting the ncryptsec */
21
23
  testPassword(password: string): Promise<void>;
24
+ /** Unlocks the signer by decrypting the ncryptsec using the provided password */
22
25
  unlock(password: string): Promise<void>;
26
+ /** Locks the signer by removing the unencrypted key from memory */
27
+ lock(): void;
23
28
  getPublicKey(): Promise<string>;
24
29
  signEvent(event: EventTemplate): Promise<import("nostr-tools").VerifiedEvent>;
25
30
  nip04Encrypt(pubkey: string, plaintext: string): Promise<string>;
@@ -30,11 +30,13 @@ export class PasswordSigner {
30
30
  this.unlockPromise = p;
31
31
  return p;
32
32
  }
33
+ /** Sets the ncryptsec from the key and password */
33
34
  async setPassword(password) {
34
35
  if (!this.key)
35
36
  throw new Error("Cant set password until unlocked");
36
37
  this.ncryptsec = encrypt(this.key, password);
37
38
  }
39
+ /** Tests if the provided password is correct by decrypting the ncryptsec */
38
40
  async testPassword(password) {
39
41
  if (this.ncryptsec) {
40
42
  const key = decrypt(this.ncryptsec, password);
@@ -44,6 +46,7 @@ export class PasswordSigner {
44
46
  else
45
47
  throw new Error("Missing ncryptsec");
46
48
  }
49
+ /** Unlocks the signer by decrypting the ncryptsec using the provided password */
47
50
  async unlock(password) {
48
51
  if (this.key)
49
52
  return;
@@ -55,6 +58,10 @@ export class PasswordSigner {
55
58
  else
56
59
  throw new Error("Missing ncryptsec");
57
60
  }
61
+ /** Locks the signer by removing the unencrypted key from memory */
62
+ lock() {
63
+ this.key = null;
64
+ }
58
65
  // public methods
59
66
  async getPublicKey() {
60
67
  await this.requestUnlock();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-signers",
3
- "version": "0.0.0-next-20250522030625",
3
+ "version": "0.0.0-next-20250606170247",
4
4
  "description": "Signer classes for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -36,7 +36,7 @@
36
36
  "@noble/hashes": "^1.7.1",
37
37
  "@noble/secp256k1": "^1.7.1",
38
38
  "@scure/base": "^1.2.4",
39
- "applesauce-core": "0.0.0-next-20250522030625",
39
+ "applesauce-core": "0.0.0-next-20250606170247",
40
40
  "debug": "^4.4.0",
41
41
  "nanoid": "^5.0.9",
42
42
  "nostr-tools": "^2.13"
package/dist/logger.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import debug from "debug";
2
- export declare const logger: debug.Debugger;
package/dist/logger.js DELETED
@@ -1,2 +0,0 @@
1
- import debug from "debug";
2
- export const logger = debug("applesauce:signer");
@@ -1 +0,0 @@
1
- export {};
@@ -1,23 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { NostrConnectSigner } from "./nostr-connect-signer.js";
3
- import { SimpleSigner } from "./simple-signer.js";
4
- describe("NostrConnectSigner", () => {
5
- describe("connection", () => {
6
- it("should call subscription method with filters", async () => {
7
- const relays = ["wss://relay.signer.com"];
8
- const subscription = vi.fn().mockReturnValue({ subscribe: vi.fn() });
9
- const publish = vi.fn(async () => { });
10
- const client = new SimpleSigner();
11
- const remote = new SimpleSigner();
12
- const signer = new NostrConnectSigner({
13
- relays,
14
- remote: await remote.getPublicKey(),
15
- signer: client,
16
- subscriptionMethod: subscription,
17
- publishMethod: publish,
18
- });
19
- signer.connect();
20
- expect(subscription).toHaveBeenCalledWith(relays, [{ "#p": [await client.getPublicKey()], kinds: [24133] }]);
21
- });
22
- });
23
- });