applesauce-accounts 0.0.0-next-20250128154531 → 0.0.0-next-20250128165956

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/dist/account.d.ts CHANGED
@@ -23,6 +23,8 @@ export declare class BaseAccount<Signer extends Nip07Interface, SignerData, Meta
23
23
  } | undefined;
24
24
  constructor(pubkey: string, signer: Signer);
25
25
  toJSON(): SerializedAccount<SignerData, Metadata>;
26
+ /** Sets an accounts id and metadata. NOTE: This should only be used in fromJSON methods */
27
+ static loadCommonFields<T extends IAccount<any, any, any>>(account: T, json: SerializedAccount<any, any>): T;
26
28
  /** Gets the pubkey from the signer */
27
29
  getPublicKey(): string | Promise<string>;
28
30
  /** sign the event and make sure its signed with the correct pubkey */
package/dist/account.js CHANGED
@@ -68,6 +68,14 @@ export class BaseAccount {
68
68
  toJSON() {
69
69
  throw new Error("Not implemented");
70
70
  }
71
+ /** Sets an accounts id and metadata. NOTE: This should only be used in fromJSON methods */
72
+ static loadCommonFields(account, json) {
73
+ if (json.id)
74
+ account.id = json.id;
75
+ if (json.metadata)
76
+ account.metadata = json.metadata;
77
+ return account;
78
+ }
71
79
  /** Gets the pubkey from the signer */
72
80
  getPublicKey() {
73
81
  const result = this.signer.getPublicKey();
@@ -5,5 +5,5 @@ import { SerializedAccount } from "../types.js";
5
5
  export declare class AmberClipboardAccount<Metadata extends unknown> extends BaseAccount<AmberClipboardSigner, void, Metadata> {
6
6
  static type: string;
7
7
  toJSON(): SerializedAccount<void, Metadata>;
8
- static fromJSON<MD extends unknown>(json: SerializedAccount<void, MD>): AmberClipboardAccount<MD>;
8
+ static fromJSON<Metadata extends unknown>(json: SerializedAccount<void, Metadata>): AmberClipboardAccount<Metadata>;
9
9
  }
@@ -13,6 +13,7 @@ export class AmberClipboardAccount extends BaseAccount {
13
13
  };
14
14
  }
15
15
  static fromJSON(json) {
16
- return new AmberClipboardAccount(json.pubkey, new AmberClipboardSigner());
16
+ const account = new AmberClipboardAccount(json.pubkey, new AmberClipboardSigner());
17
+ return super.loadCommonFields(account, json);
17
18
  }
18
19
  }
@@ -12,5 +12,5 @@ export declare class ExtensionAccount<Metadata extends unknown> extends BaseAcco
12
12
  metadata: Metadata | undefined;
13
13
  signer: undefined;
14
14
  };
15
- static fromJSON<MD extends unknown>(json: SerializedAccount<void, MD>): ExtensionAccount<unknown>;
15
+ static fromJSON<Metadata extends unknown>(json: SerializedAccount<void, Metadata>): ExtensionAccount<Metadata>;
16
16
  }
@@ -17,6 +17,7 @@ export class ExtensionAccount extends BaseAccount {
17
17
  };
18
18
  }
19
19
  static fromJSON(json) {
20
- return new ExtensionAccount(json.pubkey, new ExtensionSigner());
20
+ const account = new ExtensionAccount(json.pubkey, new ExtensionSigner());
21
+ return super.loadCommonFields(account, json);
21
22
  }
22
23
  }
@@ -1,17 +1,16 @@
1
1
  import { NostrConnectConnectionMethods, NostrConnectSigner } from "applesauce-signers";
2
2
  import { BaseAccount } from "../account.js";
3
3
  import { SerializedAccount } from "../types.js";
4
- type SignerData = {
4
+ export type NostrConnectAccountSignerData = {
5
5
  clientKey: string;
6
6
  remote: string;
7
7
  relays: string[];
8
8
  };
9
9
  /** An account type for NIP-46 signers */
10
- export declare class NostrConnectAccount<Metadata extends unknown> extends BaseAccount<NostrConnectSigner, SignerData, Metadata> {
10
+ export declare class NostrConnectAccount<Metadata extends unknown> extends BaseAccount<NostrConnectSigner, NostrConnectAccountSignerData, Metadata> {
11
11
  static type: string;
12
- toJSON(): SerializedAccount<SignerData, Metadata>;
12
+ toJSON(): SerializedAccount<NostrConnectAccountSignerData, Metadata>;
13
13
  /** This is called when NostrConnectAccount.fromJSON needs new connection methods for NostrConnectSigner */
14
14
  static createConnectionMethods(): NostrConnectConnectionMethods;
15
- static fromJSON<Metadata extends unknown>(json: SerializedAccount<SignerData, Metadata>, connection?: NostrConnectConnectionMethods): NostrConnectAccount<Metadata>;
15
+ static fromJSON<Metadata extends unknown>(json: SerializedAccount<NostrConnectAccountSignerData, Metadata>, connection?: NostrConnectConnectionMethods): NostrConnectAccount<Metadata>;
16
16
  }
17
- export {};
@@ -27,6 +27,7 @@ export class NostrConnectAccount extends BaseAccount {
27
27
  remote: json.signer.remote,
28
28
  signer: new SimpleSigner(hexToBytes(json.signer.clientKey)),
29
29
  });
30
- return new NostrConnectAccount(json.pubkey, signer);
30
+ const account = new NostrConnectAccount(json.pubkey, signer);
31
+ return super.loadCommonFields(account, json);
31
32
  }
32
33
  }
@@ -1,10 +1,10 @@
1
1
  import { PasswordSigner } from "applesauce-signers/signers/password-signer";
2
2
  import { BaseAccount } from "../account.js";
3
3
  import { SerializedAccount } from "../types.js";
4
- type SignerData = {
4
+ export type PasswordAccountSignerData = {
5
5
  ncryptsec: string;
6
6
  };
7
- export declare class PasswordAccount<Metadata extends unknown> extends BaseAccount<PasswordSigner, SignerData, Metadata> {
7
+ export declare class PasswordAccount<Metadata extends unknown> extends BaseAccount<PasswordSigner, PasswordAccountSignerData, Metadata> {
8
8
  static type: string;
9
9
  get unlocked(): boolean;
10
10
  /** called when PasswordAccount.unlock is called without a password */
@@ -14,9 +14,8 @@ export declare class PasswordAccount<Metadata extends unknown> extends BaseAccou
14
14
  * @throws
15
15
  */
16
16
  unlock(password?: string): Promise<void>;
17
- toJSON(): SerializedAccount<SignerData, Metadata>;
18
- static fromJSON<Metadata extends unknown>(json: SerializedAccount<SignerData, Metadata>): PasswordAccount<Metadata>;
17
+ toJSON(): SerializedAccount<PasswordAccountSignerData, Metadata>;
18
+ static fromJSON<Metadata extends unknown>(json: SerializedAccount<PasswordAccountSignerData, Metadata>): PasswordAccount<Metadata>;
19
19
  /** Creates a new PasswordAccount from a ncryptsec string */
20
20
  static fromNcryptsec<Metadata extends unknown>(pubkey: string, ncryptsec: string): PasswordAccount<Metadata>;
21
21
  }
22
- export {};
@@ -31,7 +31,8 @@ export class PasswordAccount extends BaseAccount {
31
31
  static fromJSON(json) {
32
32
  const signer = new PasswordSigner();
33
33
  signer.ncryptsec = json.signer.ncryptsec;
34
- return new PasswordAccount(json.pubkey, signer);
34
+ const account = new PasswordAccount(json.pubkey, signer);
35
+ return super.loadCommonFields(account, json);
35
36
  }
36
37
  /** Creates a new PasswordAccount from a ncryptsec string */
37
38
  static fromNcryptsec(pubkey, ncryptsec) {
@@ -13,6 +13,7 @@ export class ReadonlyAccount extends BaseAccount {
13
13
  };
14
14
  }
15
15
  static fromJSON(json) {
16
- return new ReadonlyAccount(json.pubkey, new ReadonlySigner(json.pubkey));
16
+ const account = new ReadonlyAccount(json.pubkey, new ReadonlySigner(json.pubkey));
17
+ return super.loadCommonFields(account, json);
17
18
  }
18
19
  }
@@ -24,6 +24,8 @@ export class SerialPortAccount extends BaseAccount {
24
24
  };
25
25
  }
26
26
  static fromJSON(json) {
27
- return new SerialPortAccount(json.pubkey, new SerialPortSigner());
27
+ const signer = new SerialPortSigner();
28
+ const account = new SerialPortAccount(json.pubkey, signer);
29
+ return super.loadCommonFields(account, json);
28
30
  }
29
31
  }
@@ -1,13 +1,12 @@
1
1
  import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
2
2
  import { BaseAccount } from "../account.js";
3
3
  import { SerializedAccount } from "../types.js";
4
- type SignerData = {
4
+ export type SimpleAccountSignerData = {
5
5
  key: string;
6
6
  };
7
- export declare class SimpleAccount<Metadata extends unknown> extends BaseAccount<SimpleSigner, SignerData, Metadata> {
7
+ export declare class SimpleAccount<Metadata extends unknown> extends BaseAccount<SimpleSigner, SimpleAccountSignerData, Metadata> {
8
8
  static type: string;
9
- toJSON(): SerializedAccount<SignerData, Metadata>;
10
- static fromJSON<Metadata extends unknown>(json: SerializedAccount<SignerData, Metadata>): SimpleAccount<Metadata>;
9
+ toJSON(): SerializedAccount<SimpleAccountSignerData, Metadata>;
10
+ static fromJSON<Metadata extends unknown>(json: SerializedAccount<SimpleAccountSignerData, Metadata>): SimpleAccount<Metadata>;
11
11
  static fromKey<Metadata extends unknown>(key: Uint8Array | string): SimpleAccount<Metadata>;
12
12
  }
13
- export {};
@@ -15,7 +15,8 @@ export class SimpleAccount extends BaseAccount {
15
15
  }
16
16
  static fromJSON(json) {
17
17
  const key = hexToBytes(json.signer.key);
18
- return new SimpleAccount(json.pubkey, new SimpleSigner(key));
18
+ const account = new SimpleAccount(json.pubkey, new SimpleSigner(key));
19
+ return super.loadCommonFields(account, json);
19
20
  }
20
21
  static fromKey(key) {
21
22
  if (typeof key === "string")
package/dist/manager.d.ts CHANGED
@@ -33,6 +33,8 @@ export declare class AccountManager<Metadata extends unknown = any> {
33
33
  clearActive(): void;
34
34
  /** sets the metadata on an account */
35
35
  setAccountMetadata(id: string | IAccount<any, any, Metadata>, metadata: Metadata): void;
36
+ /** sets the metadata on an account */
37
+ getAccountMetadata(id: string | IAccount<any, any, Metadata>): Metadata | undefined;
36
38
  /** Removes all metadata on the account */
37
39
  clearAccountMetadata(id: string | IAccount<any, any, Metadata>): void;
38
40
  /** Returns an array of serialized accounts */
package/dist/manager.js CHANGED
@@ -50,10 +50,7 @@ export class AccountManager {
50
50
  if (this.disableQueue !== undefined && account.disableQueue !== undefined) {
51
51
  account.disableQueue = this.disableQueue;
52
52
  }
53
- this.accounts$.next({
54
- ...this.accounts$.value,
55
- [account.id]: account,
56
- });
53
+ this.accounts$.next([...this.accounts$.value, account]);
57
54
  }
58
55
  /** Removes an account from the manager */
59
56
  removeAccount(account) {
@@ -95,6 +92,13 @@ export class AccountManager {
95
92
  throw new Error("Cant find account with that ID");
96
93
  account.metadata = metadata;
97
94
  }
95
+ /** sets the metadata on an account */
96
+ getAccountMetadata(id) {
97
+ const account = this.getAccount(id);
98
+ if (!account)
99
+ throw new Error("Cant find account with that ID");
100
+ return account.metadata;
101
+ }
98
102
  /** Removes all metadata on the account */
99
103
  clearAccountMetadata(id) {
100
104
  const account = this.getAccount(id);
@@ -105,7 +109,7 @@ export class AccountManager {
105
109
  // Serialize / Deserialize
106
110
  /** Returns an array of serialized accounts */
107
111
  toJSON() {
108
- return Array.from(Object.values(this.accounts$)).map((account) => account.toJSON());
112
+ return Array.from(this.accounts$.value).map((account) => account.toJSON());
109
113
  }
110
114
  /**
111
115
  * Restores all accounts from an array of serialized accounts
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,45 @@
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
+ import { AccountManager } from "./manager.js";
3
+ import { SimpleAccount } from "./accounts/simple-account.js";
4
+ import { generateSecretKey, getPublicKey } from "nostr-tools";
5
+ import { bytesToHex } from "@noble/hashes/utils";
6
+ describe("AccountManager", () => {
7
+ let manager;
8
+ beforeEach(() => {
9
+ manager = new AccountManager();
10
+ });
11
+ describe("toJSON", () => {
12
+ it("should return an array of serialized accounts", () => {
13
+ manager.addAccount(SimpleAccount.fromKey(generateSecretKey()));
14
+ manager.setAccountMetadata(manager.accounts[0], { name: "testing" });
15
+ expect(manager.toJSON()).toEqual([
16
+ {
17
+ id: expect.any(String),
18
+ type: "nsec",
19
+ pubkey: expect.any(String),
20
+ metadata: { name: "testing" },
21
+ signer: { key: expect.any(String) },
22
+ },
23
+ ]);
24
+ });
25
+ });
26
+ describe("fromJSON", () => {
27
+ it("should recreate accounts", () => {
28
+ const key = generateSecretKey();
29
+ const json = [
30
+ {
31
+ id: "custom-id",
32
+ type: "nsec",
33
+ pubkey: getPublicKey(key),
34
+ metadata: { name: "testing" },
35
+ signer: { key: bytesToHex(key) },
36
+ },
37
+ ];
38
+ manager.registerType(SimpleAccount);
39
+ manager.fromJSON(json);
40
+ expect(manager.getAccount("custom-id")).toBeInstanceOf(SimpleAccount);
41
+ expect(manager.getAccountForPubkey(getPublicKey(key))).toBeInstanceOf(SimpleAccount);
42
+ expect(manager.getAccountMetadata("custom-id")).toEqual({ name: "testing" });
43
+ });
44
+ });
45
+ });
package/dist/types.d.ts CHANGED
@@ -10,8 +10,6 @@ export type SerializedAccount<SignerData, Metadata extends unknown> = {
10
10
  id: string;
11
11
  /** account type */
12
12
  type: string;
13
- /** local name of the account */
14
- name?: string;
15
13
  /** pubkey of the account */
16
14
  pubkey: string;
17
15
  /** Signer data */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-accounts",
3
- "version": "0.0.0-next-20250128154531",
3
+ "version": "0.0.0-next-20250128165956",
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.5.0",
36
- "applesauce-signers": "0.0.0-next-20250128154531",
36
+ "applesauce-signers": "0.0.0-next-20250128165956",
37
37
  "nanoid": "^5.0.9",
38
38
  "nostr-tools": "^2.10.3",
39
39
  "rxjs": "^7.8.1"