applesauce-accounts 3.1.0 → 4.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.
@@ -2,12 +2,12 @@ import { ExtensionAccount } from "./extension-account.js";
2
2
  import { NostrConnectAccount } from "./nostr-connect-account.js";
3
3
  import { PasswordAccount } from "./password-account.js";
4
4
  import { ReadonlyAccount } from "./readonly-account.js";
5
- import { SimpleAccount } from "./simple-account.js";
5
+ import { PrivateKeyAccount } from "./private-key-account.js";
6
6
  /** Registers the most common account types to a account manager */
7
7
  export function registerCommonAccountTypes(manager) {
8
8
  manager.registerType(ExtensionAccount);
9
9
  manager.registerType(PasswordAccount);
10
10
  manager.registerType(ReadonlyAccount);
11
- manager.registerType(SimpleAccount);
11
+ manager.registerType(PrivateKeyAccount);
12
12
  manager.registerType(NostrConnectAccount);
13
13
  }
@@ -3,6 +3,7 @@ export * from "./extension-account.js";
3
3
  export * from "./password-account.js";
4
4
  export * from "./readonly-account.js";
5
5
  export * from "./serial-port-account.js";
6
- export * from "./simple-account.js";
6
+ export * from "./private-key-account.js";
7
7
  export * from "./nostr-connect-account.js";
8
+ export * from "./simple-account.js";
8
9
  export * from "./common.js";
@@ -3,6 +3,7 @@ export * from "./extension-account.js";
3
3
  export * from "./password-account.js";
4
4
  export * from "./readonly-account.js";
5
5
  export * from "./serial-port-account.js";
6
- export * from "./simple-account.js";
6
+ export * from "./private-key-account.js";
7
7
  export * from "./nostr-connect-account.js";
8
+ export * from "./simple-account.js";
8
9
  export * from "./common.js";
@@ -1,4 +1,4 @@
1
- import { NostrConnectSigner, SimpleSigner } from "applesauce-signers";
1
+ import { NostrConnectSigner, PrivateKeySigner } from "applesauce-signers";
2
2
  import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
3
3
  import { BaseAccount } from "../account.js";
4
4
  /** An account type for NIP-46 signers */
@@ -20,7 +20,7 @@ export class NostrConnectAccount extends BaseAccount {
20
20
  relays: json.signer.relays,
21
21
  pubkey: json.pubkey,
22
22
  remote: json.signer.remote,
23
- signer: new SimpleSigner(hexToBytes(json.signer.clientKey)),
23
+ signer: new PrivateKeySigner(hexToBytes(json.signer.clientKey)),
24
24
  });
25
25
  const account = new NostrConnectAccount(json.pubkey, signer);
26
26
  return super.loadCommonFields(account, json);
@@ -0,0 +1,16 @@
1
+ import { PrivateKeySigner } from "applesauce-signers/signers/private-key-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ import { SerializedAccount } from "../types.js";
4
+ export type PrivateKeyAccountSignerData = {
5
+ key: string;
6
+ };
7
+ /** A simple account that hold the private key in memory */
8
+ export declare class PrivateKeyAccount<Metadata extends unknown> extends BaseAccount<PrivateKeySigner, PrivateKeyAccountSignerData, Metadata> {
9
+ static readonly type = "nsec";
10
+ toJSON(): SerializedAccount<PrivateKeyAccountSignerData, Metadata>;
11
+ static fromJSON<Metadata extends unknown>(json: SerializedAccount<PrivateKeyAccountSignerData, Metadata>): PrivateKeyAccount<Metadata>;
12
+ /** Creates a PrivateKeyAccount from a hex private key or NIP-19 nsec */
13
+ static fromKey<Metadata extends unknown>(privateKey: Uint8Array | string): PrivateKeyAccount<Metadata>;
14
+ /** Creates a new PrivateKeyAccount with a random private key */
15
+ static generateNew<Metadata extends unknown>(): PrivateKeyAccount<Metadata>;
16
+ }
@@ -0,0 +1,29 @@
1
+ import { generateSecretKey, getPublicKey } from "nostr-tools";
2
+ import { PrivateKeySigner } from "applesauce-signers/signers/private-key-signer";
3
+ import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
4
+ import { BaseAccount } from "../account.js";
5
+ /** A simple account that hold the private key in memory */
6
+ export class PrivateKeyAccount extends BaseAccount {
7
+ static type = "nsec";
8
+ toJSON() {
9
+ return super.saveCommonFields({
10
+ signer: { key: bytesToHex(this.signer.key) },
11
+ });
12
+ }
13
+ static fromJSON(json) {
14
+ const key = hexToBytes(json.signer.key);
15
+ const account = new PrivateKeyAccount(json.pubkey, new PrivateKeySigner(key));
16
+ return super.loadCommonFields(account, json);
17
+ }
18
+ /** Creates a PrivateKeyAccount from a hex private key or NIP-19 nsec */
19
+ static fromKey(privateKey) {
20
+ const signer = PrivateKeySigner.fromKey(privateKey);
21
+ const pubkey = getPublicKey(signer.key);
22
+ return new PrivateKeyAccount(pubkey, signer);
23
+ }
24
+ /** Creates a new PrivateKeyAccount with a random private key */
25
+ static generateNew() {
26
+ const key = generateSecretKey();
27
+ return PrivateKeyAccount.fromKey(key);
28
+ }
29
+ }
@@ -1,15 +1,4 @@
1
- import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
2
- import { BaseAccount } from "../account.js";
3
- import { SerializedAccount } from "../types.js";
4
- export type SimpleAccountSignerData = {
5
- key: string;
6
- };
7
- export declare class SimpleAccount<Metadata extends unknown> extends BaseAccount<SimpleSigner, SimpleAccountSignerData, Metadata> {
8
- static readonly type = "nsec";
9
- toJSON(): SerializedAccount<SimpleAccountSignerData, Metadata>;
10
- static fromJSON<Metadata extends unknown>(json: SerializedAccount<SimpleAccountSignerData, Metadata>): SimpleAccount<Metadata>;
11
- /** Creates a SimpleAccount from a hex private key or NIP-19 nsec */
12
- static fromKey<Metadata extends unknown>(privateKey: Uint8Array | string): SimpleAccount<Metadata>;
13
- /** Creates a new SimpleAccount with a random private key */
14
- static generateNew<Metadata extends unknown>(): SimpleAccount<Metadata>;
15
- }
1
+ import { PrivateKeyAccount } from "./private-key-account.js";
2
+ /** @deprecated Use PrivateKeyAccount instead */
3
+ declare const SimpleAccount: typeof PrivateKeyAccount;
4
+ export { SimpleAccount };
@@ -1,28 +1,4 @@
1
- import { generateSecretKey, getPublicKey } from "nostr-tools";
2
- import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
3
- import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
4
- import { BaseAccount } from "../account.js";
5
- export class SimpleAccount extends BaseAccount {
6
- static type = "nsec";
7
- toJSON() {
8
- return super.saveCommonFields({
9
- signer: { key: bytesToHex(this.signer.key) },
10
- });
11
- }
12
- static fromJSON(json) {
13
- const key = hexToBytes(json.signer.key);
14
- const account = new SimpleAccount(json.pubkey, new SimpleSigner(key));
15
- return super.loadCommonFields(account, json);
16
- }
17
- /** Creates a SimpleAccount from a hex private key or NIP-19 nsec */
18
- static fromKey(privateKey) {
19
- const signer = SimpleSigner.fromKey(privateKey);
20
- const pubkey = getPublicKey(signer.key);
21
- return new SimpleAccount(pubkey, signer);
22
- }
23
- /** Creates a new SimpleAccount with a random private key */
24
- static generateNew() {
25
- const key = generateSecretKey();
26
- return SimpleAccount.fromKey(key);
27
- }
28
- }
1
+ import { PrivateKeyAccount } from "./private-key-account.js";
2
+ /** @deprecated Use PrivateKeyAccount instead */
3
+ const SimpleAccount = PrivateKeyAccount;
4
+ export { SimpleAccount };
package/dist/manager.d.ts CHANGED
@@ -47,4 +47,6 @@ export declare class AccountManager<Metadata extends unknown = any> {
47
47
  * NOTE: this will clear all existing accounts
48
48
  */
49
49
  fromJSON(accounts: SerializedAccount<any, Metadata>[], quite?: boolean): void;
50
+ /** Create an account from a types array and a serialized account */
51
+ static deserialize<Signer extends ISigner = ISigner, SignerData extends unknown = any, Metadata extends unknown = any>(types: IAccountConstructor<any, any, any>[], json: SerializedAccount<SignerData, Metadata>): IAccount<Signer, SignerData, Metadata>;
50
52
  }
package/dist/manager.js CHANGED
@@ -155,4 +155,11 @@ export class AccountManager {
155
155
  }
156
156
  }
157
157
  }
158
+ /** Create an account from a types array and a serialized account */
159
+ static deserialize(types, json) {
160
+ const account = types.find((type) => type.type === json.type)?.fromJSON(json);
161
+ if (!account)
162
+ throw new Error(`Missing account type ${json.type}`);
163
+ return account;
164
+ }
158
165
  }
package/dist/types.d.ts CHANGED
@@ -7,7 +7,7 @@ export type EventTemplate = {
7
7
  created_at: number;
8
8
  };
9
9
  /** A type for serializing an account */
10
- export type SerializedAccount<SignerData, Metadata extends unknown> = {
10
+ export type SerializedAccount<SignerData extends unknown = any, Metadata extends unknown = any> = {
11
11
  /** Internal account ID */
12
12
  id: string;
13
13
  /** account type */
@@ -20,7 +20,7 @@ export type SerializedAccount<SignerData, Metadata extends unknown> = {
20
20
  metadata?: Metadata;
21
21
  };
22
22
  /** An interface for an account */
23
- export interface IAccount<Signer extends ISigner = ISigner, SignerData = any, Metadata extends unknown = any> extends ISigner {
23
+ export interface IAccount<Signer extends ISigner = ISigner, SignerData extends unknown = any, Metadata extends unknown = any> extends ISigner {
24
24
  id: string;
25
25
  name?: string;
26
26
  pubkey: string;
@@ -31,7 +31,7 @@ export interface IAccount<Signer extends ISigner = ISigner, SignerData = any, Me
31
31
  toJSON(): SerializedAccount<SignerData, Metadata>;
32
32
  }
33
33
  /** A constructor for an account */
34
- export interface IAccountConstructor<Signer extends ISigner, SignerData, Metadata extends unknown> {
34
+ export interface IAccountConstructor<Signer extends ISigner, SignerData extends unknown = any, Metadata extends unknown = any> {
35
35
  readonly type: string;
36
36
  new (pubkey: string, signer: Signer): IAccount<Signer, SignerData, Metadata>;
37
37
  fromJSON(json: SerializedAccount<SignerData, Metadata>): IAccount<Signer, SignerData, Metadata>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-accounts",
3
- "version": "3.1.0",
3
+ "version": "4.1.0",
4
4
  "description": "A simple nostr account management system",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -33,21 +33,23 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@noble/hashes": "^1.7.1",
36
- "applesauce-signers": "^3.1.0",
37
- "applesauce-core": "^3.1.0",
36
+ "applesauce-core": "^4.1.0",
37
+ "applesauce-signers": "^4.1.0",
38
38
  "nanoid": "^5.1.5",
39
- "nostr-tools": "~2.15",
39
+ "nostr-tools": "~2.17",
40
40
  "rxjs": "^7.8.1"
41
41
  },
42
42
  "devDependencies": {
43
+ "rimraf": "^6.0.1",
43
44
  "typescript": "^5.8.3",
44
- "vitest": "^3.2.3"
45
+ "vitest": "^3.2.4"
45
46
  },
46
47
  "funding": {
47
48
  "type": "lightning",
48
49
  "url": "lightning:nostrudel@geyser.fund"
49
50
  },
50
51
  "scripts": {
52
+ "prebuild": "rimraf dist",
51
53
  "build": "tsc",
52
54
  "watch:build": "tsc --watch > /dev/null",
53
55
  "test": "vitest run --passWithNoTests",