applesauce-accounts 0.0.0-next-20250128164720 → 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.
@@ -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 {};
@@ -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 {};
@@ -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 {};
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-accounts",
3
- "version": "0.0.0-next-20250128164720",
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-20250128164720",
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"