applesauce-accounts 0.11.0 → 0.12.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,14 +1,14 @@
1
- import { describe, it, expect, beforeEach, vi } from "vitest";
2
- import { BaseAccount } from "./account.js";
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
3
2
  import { SimpleSigner } from "applesauce-signers";
4
3
  import { finalizeEvent, generateSecretKey } from "nostr-tools";
5
- import { SimpleAccount } from "./accounts/simple-account.js";
4
+ import { BaseAccount } from "../account.js";
5
+ import { SimpleAccount } from "../accounts/simple-account.js";
6
6
  describe("BaseAccount", () => {
7
- let signer;
8
- beforeEach(() => {
9
- signer = new SimpleSigner();
10
- });
11
7
  describe("request queue", () => {
8
+ let signer;
9
+ beforeEach(() => {
10
+ signer = new SimpleSigner();
11
+ });
12
12
  it("should queue signing requests by default", async () => {
13
13
  const account = new BaseAccount(await signer.getPublicKey(), signer);
14
14
  let resolve = [];
@@ -63,4 +63,63 @@ describe("BaseAccount", () => {
63
63
  expect(account.type).toBe("nsec");
64
64
  });
65
65
  });
66
+ describe("nip04 and nip44", () => {
67
+ it("should return undefined when signer does not support nip04/nip44", () => {
68
+ const signer = {
69
+ getPublicKey: () => "test-pubkey",
70
+ signEvent: () => ({ id: "", pubkey: "test-pubkey", created_at: 0, kind: 1, tags: [], content: "", sig: "" }),
71
+ };
72
+ const account = new BaseAccount("test-pubkey", signer);
73
+ expect(account.nip04).toBeUndefined();
74
+ expect(account.nip44).toBeUndefined();
75
+ });
76
+ it("should return nip04/nip44 interface when signer supports them", async () => {
77
+ const signer = {
78
+ getPublicKey: () => "test-pubkey",
79
+ signEvent: () => ({ id: "", pubkey: "test-pubkey", created_at: 0, kind: 1, tags: [], content: "", sig: "" }),
80
+ nip04: {
81
+ encrypt: async () => "encrypted",
82
+ decrypt: async () => "decrypted",
83
+ },
84
+ nip44: {
85
+ encrypt: async () => "encrypted",
86
+ decrypt: async () => "decrypted",
87
+ },
88
+ };
89
+ const account = new BaseAccount("test-pubkey", signer);
90
+ expect(account.nip04).toBeDefined();
91
+ expect(account.nip44).toBeDefined();
92
+ const nip04Result = await account.nip04.encrypt("pubkey", "test");
93
+ expect(nip04Result).toBe("encrypted");
94
+ const nip44Result = await account.nip44.encrypt("pubkey", "test");
95
+ expect(nip44Result).toBe("encrypted");
96
+ });
97
+ it("should reflect changes in signer nip04/nip44 support", () => {
98
+ const signer = {
99
+ getPublicKey: () => "test-pubkey",
100
+ signEvent: () => ({ id: "", pubkey: "test-pubkey", created_at: 0, kind: 1, tags: [], content: "", sig: "" }),
101
+ };
102
+ const account = new BaseAccount("test-pubkey", signer);
103
+ expect(account.nip04).toBeUndefined();
104
+ expect(account.nip44).toBeUndefined();
105
+ // Add nip04 support
106
+ signer.nip04 = {
107
+ encrypt: async () => "encrypted",
108
+ decrypt: async () => "decrypted",
109
+ };
110
+ expect(account.nip04).toBeDefined();
111
+ expect(account.nip44).toBeUndefined();
112
+ // Add nip44 support
113
+ signer.nip44 = {
114
+ encrypt: async () => "encrypted",
115
+ decrypt: async () => "decrypted",
116
+ };
117
+ expect(account.nip04).toBeDefined();
118
+ expect(account.nip44).toBeDefined();
119
+ // Remove nip04 support
120
+ signer.nip04 = undefined;
121
+ expect(account.nip04).toBeUndefined();
122
+ expect(account.nip44).toBeDefined();
123
+ });
124
+ });
66
125
  });
package/dist/account.d.ts CHANGED
@@ -14,14 +14,8 @@ export declare class BaseAccount<Signer extends Nip07Interface, SignerData, Meta
14
14
  metadata$: BehaviorSubject<Metadata | undefined>;
15
15
  get metadata(): Metadata | undefined;
16
16
  set metadata(metadata: Metadata);
17
- nip04?: {
18
- encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
19
- decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
20
- } | undefined;
21
- nip44?: {
22
- encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
23
- decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
24
- } | undefined;
17
+ get nip04(): Nip07Interface["nip04"] | undefined;
18
+ get nip44(): Nip07Interface["nip44"] | undefined;
25
19
  constructor(pubkey: string, signer: Signer);
26
20
  toJSON(): SerializedAccount<SignerData, Metadata>;
27
21
  /** Adds the common fields to the serialized output of a toJSON method */
package/dist/account.js CHANGED
@@ -40,33 +40,33 @@ export class BaseAccount {
40
40
  set metadata(metadata) {
41
41
  this.metadata$.next(metadata);
42
42
  }
43
- // encryption interfaces
44
- nip04;
45
- nip44;
43
+ get nip04() {
44
+ if (!this.signer.nip04)
45
+ return undefined;
46
+ return {
47
+ encrypt: (pubkey, plaintext) => {
48
+ return this.waitForLock(() => this.signer.nip04.encrypt(pubkey, plaintext));
49
+ },
50
+ decrypt: (pubkey, plaintext) => {
51
+ return this.waitForLock(() => this.signer.nip04.decrypt(pubkey, plaintext));
52
+ },
53
+ };
54
+ }
55
+ get nip44() {
56
+ if (!this.signer.nip44)
57
+ return undefined;
58
+ return {
59
+ encrypt: (pubkey, plaintext) => {
60
+ return this.waitForLock(() => this.signer.nip44.encrypt(pubkey, plaintext));
61
+ },
62
+ decrypt: (pubkey, plaintext) => {
63
+ return this.waitForLock(() => this.signer.nip44.decrypt(pubkey, plaintext));
64
+ },
65
+ };
66
+ }
46
67
  constructor(pubkey, signer) {
47
68
  this.pubkey = pubkey;
48
69
  this.signer = signer;
49
- // setup encryption interfaces to check if account is locked
50
- if (this.signer.nip04) {
51
- this.nip04 = {
52
- encrypt: (pubkey, plaintext) => {
53
- return this.waitForLock(() => this.signer.nip04.encrypt(pubkey, plaintext));
54
- },
55
- decrypt: (pubkey, plaintext) => {
56
- return this.waitForLock(() => this.signer.nip04.decrypt(pubkey, plaintext));
57
- },
58
- };
59
- }
60
- if (this.signer.nip44) {
61
- this.nip44 = {
62
- encrypt: (pubkey, plaintext) => {
63
- return this.waitForLock(() => this.signer.nip44.encrypt(pubkey, plaintext));
64
- },
65
- decrypt: (pubkey, plaintext) => {
66
- return this.waitForLock(() => this.signer.nip44.decrypt(pubkey, plaintext));
67
- },
68
- };
69
- }
70
70
  }
71
71
  // This should be overwritten by a sub class
72
72
  toJSON() {
package/dist/manager.d.ts CHANGED
@@ -8,7 +8,7 @@ export declare class AccountManager<Metadata extends unknown = any> {
8
8
  accounts$: BehaviorSubject<IAccount<any, any, Metadata>[]>;
9
9
  get accounts(): IAccount<any, any, Metadata>[];
10
10
  /** Proxy signer for currently active account */
11
- signer: IAccount<any, any, Metadata>;
11
+ signer: Nip07Interface;
12
12
  /** Disable request queueing for any accounts added to this manager */
13
13
  disableQueue?: boolean;
14
14
  constructor();
package/dist/manager.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BehaviorSubject } from "rxjs";
2
+ import { ProxySigner } from "./proxy-signer.js";
2
3
  export class AccountManager {
3
4
  types = new Map();
4
5
  active$ = new BehaviorSubject(undefined);
@@ -14,18 +15,7 @@ export class AccountManager {
14
15
  /** Disable request queueing for any accounts added to this manager */
15
16
  disableQueue;
16
17
  constructor() {
17
- this.signer = new Proxy({}, {
18
- get: (_, p) => {
19
- if (!this.active)
20
- throw new Error("No active account");
21
- return Reflect.get(this.active, p);
22
- },
23
- has: (_, p) => {
24
- if (!this.active)
25
- throw new Error("No active account");
26
- return Reflect.has(this.active, p);
27
- },
28
- });
18
+ this.signer = new ProxySigner(this.active$, "No active account");
29
19
  }
30
20
  // Account type CRUD
31
21
  /** Add account type class */
@@ -0,0 +1,19 @@
1
+ import { Nip07Interface } from "applesauce-signers";
2
+ import { EventTemplate, NostrEvent } from "nostr-tools";
3
+ import { BehaviorSubject } from "rxjs";
4
+ export declare class ProxySigner<T extends Nip07Interface> implements Nip07Interface {
5
+ protected upstream: BehaviorSubject<T | undefined>;
6
+ protected error?: string | undefined;
7
+ nip04: {
8
+ encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
9
+ decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
10
+ };
11
+ nip44: {
12
+ encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
13
+ decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
14
+ };
15
+ constructor(upstream: BehaviorSubject<T | undefined>, error?: string | undefined);
16
+ protected get signer(): Nip07Interface;
17
+ signEvent(template: EventTemplate): Promise<NostrEvent> | NostrEvent;
18
+ getPublicKey(): Promise<string> | string;
19
+ }
@@ -0,0 +1,45 @@
1
+ export class ProxySigner {
2
+ upstream;
3
+ error;
4
+ nip04;
5
+ nip44;
6
+ constructor(upstream, error) {
7
+ this.upstream = upstream;
8
+ this.error = error;
9
+ this.nip04 = {
10
+ encrypt: (pubkey, plaintext) => {
11
+ if (!this.signer.nip04)
12
+ throw new Error("Signer does not support nip04");
13
+ return this.signer.nip04.encrypt(pubkey, plaintext);
14
+ },
15
+ decrypt: (pubkey, ciphertext) => {
16
+ if (!this.signer.nip04)
17
+ throw new Error("Signer does not support nip04");
18
+ return this.signer.nip04.decrypt(pubkey, ciphertext);
19
+ },
20
+ };
21
+ this.nip44 = {
22
+ encrypt: (pubkey, plaintext) => {
23
+ if (!this.signer.nip44)
24
+ throw new Error("Signer does not support nip44");
25
+ return this.signer.nip44.encrypt(pubkey, plaintext);
26
+ },
27
+ decrypt: (pubkey, ciphertext) => {
28
+ if (!this.signer.nip44)
29
+ throw new Error("Signer does not support nip44");
30
+ return this.signer.nip44.decrypt(pubkey, ciphertext);
31
+ },
32
+ };
33
+ }
34
+ get signer() {
35
+ if (!this.upstream.value)
36
+ throw new Error(this.error || "Missing signer");
37
+ return this.upstream.value;
38
+ }
39
+ signEvent(template) {
40
+ return this.signer.signEvent(template);
41
+ }
42
+ getPublicKey() {
43
+ return this.signer.getPublicKey();
44
+ }
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-accounts",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "A simple nostr account management system",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@noble/hashes": "^1.7.1",
36
- "applesauce-signers": "^0.11.0",
36
+ "applesauce-signers": "^0.12.0",
37
37
  "nanoid": "^5.0.9",
38
38
  "nostr-tools": "^2.10.4",
39
39
  "rxjs": "^7.8.1"