applesauce-accounts 0.10.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 hzrd149
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # applesauce-accounts
2
+
3
+ A simple nostr account management system
@@ -0,0 +1,31 @@
1
+ import { Nip07Interface } from "applesauce-signer";
2
+ import { EventTemplate } from "nostr-tools";
3
+ import { IAccount, SerializedAccount } from "./types.js";
4
+ export declare class SignerMismatchError extends Error {
5
+ }
6
+ export declare class AccountLockedError extends Error {
7
+ }
8
+ export declare class BaseAccount<T extends string, S> implements IAccount<T, S> {
9
+ pubkey: string;
10
+ signer: Nip07Interface;
11
+ name?: string;
12
+ locked: boolean;
13
+ nip04?: {
14
+ encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
15
+ decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
16
+ } | undefined;
17
+ nip44?: {
18
+ encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
19
+ decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
20
+ } | undefined;
21
+ constructor(pubkey: string, signer: Nip07Interface);
22
+ unlock(): Promise<boolean>;
23
+ lock(): void;
24
+ toJSON(): SerializedAccount<T, S>;
25
+ /** throws if account is locked */
26
+ protected checkLocked(): void;
27
+ /** Gets the pubkey from the signer */
28
+ getPublicKey(): Promise<string>;
29
+ /** sign the event and make sure its signed with the correct pubkey */
30
+ signEvent(template: EventTemplate): Promise<import("nostr-tools").Event>;
31
+ }
@@ -0,0 +1,77 @@
1
+ // errors
2
+ export class SignerMismatchError extends Error {
3
+ }
4
+ export class AccountLockedError extends Error {
5
+ }
6
+ export class BaseAccount {
7
+ pubkey;
8
+ signer;
9
+ name;
10
+ locked = true;
11
+ // encryption interfaces
12
+ nip04;
13
+ nip44;
14
+ constructor(pubkey, signer) {
15
+ this.pubkey = pubkey;
16
+ this.signer = signer;
17
+ // setup encryption interfaces to check if account is locked
18
+ if (this.signer.nip04) {
19
+ this.nip04 = {
20
+ encrypt: (pubkey, plaintext) => {
21
+ this.checkLocked();
22
+ return this.signer.nip04.encrypt(pubkey, plaintext);
23
+ },
24
+ decrypt: (pubkey, plaintext) => {
25
+ this.checkLocked();
26
+ return this.signer.nip04.decrypt(pubkey, plaintext);
27
+ },
28
+ };
29
+ }
30
+ if (this.signer.nip44) {
31
+ this.nip44 = {
32
+ encrypt: (pubkey, plaintext) => {
33
+ this.checkLocked();
34
+ return this.signer.nip44.encrypt(pubkey, plaintext);
35
+ },
36
+ decrypt: (pubkey, plaintext) => {
37
+ this.checkLocked();
38
+ return this.signer.nip44.decrypt(pubkey, plaintext);
39
+ },
40
+ };
41
+ }
42
+ }
43
+ async unlock() {
44
+ this.locked = false;
45
+ return true;
46
+ }
47
+ lock() {
48
+ this.locked = true;
49
+ }
50
+ // This should be overwritten by a sub class
51
+ toJSON() {
52
+ throw new Error("Not implemented");
53
+ }
54
+ /** throws if account is locked */
55
+ checkLocked() {
56
+ if (this.locked)
57
+ throw new AccountLockedError("Account is locked");
58
+ }
59
+ /** Gets the pubkey from the signer */
60
+ async getPublicKey() {
61
+ this.checkLocked();
62
+ const signerKey = await this.signer.getPublicKey();
63
+ if (this.pubkey !== signerKey)
64
+ throw new Error("Account signer mismatch");
65
+ return this.pubkey;
66
+ }
67
+ /** sign the event and make sure its signed with the correct pubkey */
68
+ async signEvent(template) {
69
+ this.checkLocked();
70
+ if (!Reflect.has(template, "pubkey"))
71
+ Reflect.set(template, "pubkey", this.pubkey);
72
+ const signed = await this.signer.signEvent(template);
73
+ if (signed.pubkey !== this.pubkey)
74
+ throw new SignerMismatchError("Signer signed with wrong pubkey");
75
+ return signed;
76
+ }
77
+ }
@@ -0,0 +1,10 @@
1
+ import { AmberClipboardSigner } from "applesauce-signer/signers/amber-clipboard-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ import { IAccount, SerializedAccount } from "../types.js";
4
+ /** An account for the amber clipboard api */
5
+ export declare class AmberClipboardAccount extends BaseAccount<"amber-clipboard", void> {
6
+ signer: AmberClipboardSigner;
7
+ constructor(pubkey: string, signer: AmberClipboardSigner);
8
+ toJSON(): SerializedAccount<"amber-clipboard", void>;
9
+ static fromJSON(json: SerializedAccount<"amber-clipboard", void>): IAccount<"amber-clipboard", void>;
10
+ }
@@ -0,0 +1,16 @@
1
+ import { AmberClipboardSigner } from "applesauce-signer/signers/amber-clipboard-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ /** An account for the amber clipboard api */
4
+ export class AmberClipboardAccount extends BaseAccount {
5
+ signer;
6
+ constructor(pubkey, signer) {
7
+ super(pubkey, signer);
8
+ this.signer = signer;
9
+ }
10
+ toJSON() {
11
+ return { type: "amber-clipboard", pubkey: this.pubkey, name: this.name, signer: void 0 };
12
+ }
13
+ static fromJSON(json) {
14
+ return new AmberClipboardAccount(json.pubkey, new AmberClipboardSigner());
15
+ }
16
+ }
@@ -0,0 +1,7 @@
1
+ import { BaseAccount } from "../account.js";
2
+ import { SerializedAccount } from "../types.js";
3
+ export default class ExtensionAccount extends BaseAccount<"extension", void> {
4
+ constructor(pubkey: string);
5
+ toJSON(): SerializedAccount<"extension", void>;
6
+ static fromJSON(json: SerializedAccount<"extension", void>): ExtensionAccount;
7
+ }
@@ -0,0 +1,13 @@
1
+ import { ExtensionSigner } from "applesauce-signer/signers/extension-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ export default class ExtensionAccount extends BaseAccount {
4
+ constructor(pubkey) {
5
+ super(pubkey, new ExtensionSigner());
6
+ }
7
+ toJSON() {
8
+ return { type: "extension", pubkey: this.pubkey, signer: undefined };
9
+ }
10
+ static fromJSON(json) {
11
+ return new ExtensionAccount(json.pubkey);
12
+ }
13
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./amber-clipboard-account.js";
2
+ export * from "./extension-account.js";
3
+ export * from "./password-account.js";
4
+ export * from "./readonly-account.js";
5
+ export * from "./serial-port-account.js";
6
+ export * from "./simple-account.js";
@@ -0,0 +1,6 @@
1
+ export * from "./amber-clipboard-account.js";
2
+ export * from "./extension-account.js";
3
+ export * from "./password-account.js";
4
+ export * from "./readonly-account.js";
5
+ export * from "./serial-port-account.js";
6
+ export * from "./simple-account.js";
@@ -0,0 +1,15 @@
1
+ import { PasswordSigner } from "applesauce-signer/signers/password-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ import { SerializedAccount } from "../types.js";
4
+ type SignerData = {
5
+ ncryptsec: string;
6
+ };
7
+ export default class PasswordAccount extends BaseAccount<"ncryptsec", SignerData> {
8
+ signer: PasswordSigner;
9
+ constructor(pubkey: string, signer: PasswordSigner);
10
+ unlock(): Promise<boolean>;
11
+ toJSON(): SerializedAccount<"ncryptsec", SignerData>;
12
+ static fromJSON(json: SerializedAccount<"ncryptsec", SignerData>): PasswordAccount;
13
+ static fromNcryptsec(pubkey: string, ncryptsec: string): PasswordAccount;
14
+ }
15
+ export {};
@@ -0,0 +1,36 @@
1
+ import { PasswordSigner } from "applesauce-signer/signers/password-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ export default class PasswordAccount extends BaseAccount {
4
+ signer;
5
+ constructor(pubkey, signer) {
6
+ super(pubkey, signer);
7
+ this.signer = signer;
8
+ }
9
+ async unlock() {
10
+ try {
11
+ const password = prompt("Unlock password");
12
+ if (password === null)
13
+ return false;
14
+ await this.signer.unlock(password);
15
+ return true;
16
+ }
17
+ catch (error) {
18
+ return false;
19
+ }
20
+ }
21
+ toJSON() {
22
+ if (!this.signer.ncryptsec)
23
+ throw new Error("Cant save account without ncryptsec");
24
+ return { type: "ncryptsec", pubkey: this.pubkey, signer: { ncryptsec: this.signer.ncryptsec } };
25
+ }
26
+ static fromJSON(json) {
27
+ const signer = new PasswordSigner();
28
+ signer.ncryptsec = json.signer.ncryptsec;
29
+ return new PasswordAccount(json.pubkey, signer);
30
+ }
31
+ static fromNcryptsec(pubkey, ncryptsec) {
32
+ const signer = new PasswordSigner();
33
+ signer.ncryptsec = ncryptsec;
34
+ return new PasswordAccount(pubkey, signer);
35
+ }
36
+ }
@@ -0,0 +1,9 @@
1
+ import { ReadonlySigner } from "applesauce-signer/signers/readonly-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ import { SerializedAccount } from "../types.js";
4
+ /** An account that cannot sign or encrypt anything */
5
+ export default class ReadonlyAccount extends BaseAccount<"readonly", void> {
6
+ constructor(pubkey: string, signer?: ReadonlySigner);
7
+ toJSON(): SerializedAccount<"readonly", void>;
8
+ static fromJSON(json: SerializedAccount<"readonly", void>): ReadonlyAccount;
9
+ }
@@ -0,0 +1,18 @@
1
+ import { ReadonlySigner } from "applesauce-signer/signers/readonly-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ /** An account that cannot sign or encrypt anything */
4
+ export default class ReadonlyAccount extends BaseAccount {
5
+ constructor(pubkey, signer) {
6
+ super(pubkey, signer || new ReadonlySigner(pubkey));
7
+ }
8
+ toJSON() {
9
+ return {
10
+ type: "readonly",
11
+ pubkey: this.pubkey,
12
+ signer: undefined,
13
+ };
14
+ }
15
+ static fromJSON(json) {
16
+ return new ReadonlyAccount(json.pubkey);
17
+ }
18
+ }
@@ -0,0 +1,10 @@
1
+ import { SerialPortSigner } from "applesauce-signer/signers/serial-port-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ import { SerializedAccount } from "../types.js";
4
+ /** An account for SerialPortSigner */
5
+ export default class SerialPortAccount extends BaseAccount<"serial-port", void> {
6
+ constructor(pubkey: string, signer?: SerialPortSigner);
7
+ unlock(): Promise<boolean>;
8
+ toJSON(): SerializedAccount<"serial-port", void>;
9
+ static fromJSON(json: SerializedAccount<"serial-port", void>): SerialPortAccount;
10
+ }
@@ -0,0 +1,25 @@
1
+ import { SerialPortSigner } from "applesauce-signer/signers/serial-port-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ /** An account for SerialPortSigner */
4
+ export default class SerialPortAccount extends BaseAccount {
5
+ constructor(pubkey, signer) {
6
+ super(pubkey, signer || new SerialPortSigner());
7
+ }
8
+ async unlock() {
9
+ try {
10
+ const pubkey = await this.signer.getPublicKey();
11
+ if (pubkey !== this.pubkey)
12
+ throw new Error("Signer returned incorrect pubkey");
13
+ return true;
14
+ }
15
+ catch (error) {
16
+ return false;
17
+ }
18
+ }
19
+ toJSON() {
20
+ return { type: "serial-port", pubkey: this.pubkey, signer: undefined };
21
+ }
22
+ static fromJSON(json) {
23
+ return new SerialPortAccount(json.pubkey);
24
+ }
25
+ }
@@ -0,0 +1,14 @@
1
+ import { SimpleSigner } from "applesauce-signer/signers/simple-signer";
2
+ import { BaseAccount } from "../account.js";
3
+ import { SerializedAccount } from "../types.js";
4
+ type SignerData = {
5
+ key: string;
6
+ };
7
+ export default class SimpleAccount extends BaseAccount<"nsec", SignerData> {
8
+ signer: SimpleSigner;
9
+ constructor(pubkey: string, signer: SimpleSigner);
10
+ static fromKey(key: Uint8Array | string): SimpleAccount;
11
+ toJSON(): SerializedAccount<"nsec", SignerData>;
12
+ static fromJSON(json: SerializedAccount<"nsec", SignerData>): SimpleAccount;
13
+ }
14
+ export {};
@@ -0,0 +1,24 @@
1
+ import { getPublicKey } from "nostr-tools";
2
+ import { SimpleSigner } from "applesauce-signer/signers/simple-signer";
3
+ import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
4
+ import { BaseAccount } from "../account.js";
5
+ export default class SimpleAccount extends BaseAccount {
6
+ signer;
7
+ constructor(pubkey, signer) {
8
+ super(pubkey, signer);
9
+ this.signer = signer;
10
+ }
11
+ static fromKey(key) {
12
+ if (typeof key === "string")
13
+ key = hexToBytes(key);
14
+ const pubkey = getPublicKey(key);
15
+ return new SimpleAccount(pubkey, new SimpleSigner(key));
16
+ }
17
+ toJSON() {
18
+ return { type: "nsec", pubkey: this.pubkey, signer: { key: bytesToHex(this.signer.key) } };
19
+ }
20
+ static fromJSON(json) {
21
+ const key = hexToBytes(json.signer.key);
22
+ return new SimpleAccount(json.pubkey, new SimpleSigner(key));
23
+ }
24
+ }
@@ -0,0 +1,4 @@
1
+ export * as Accounts from "./accounts/index.js";
2
+ export * from "./types.js";
3
+ export * from "./account.js";
4
+ export * from "./manager.js";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * as Accounts from "./accounts/index.js";
2
+ export * from "./types.js";
3
+ export * from "./account.js";
4
+ export * from "./manager.js";
@@ -0,0 +1,2 @@
1
+ export declare class AccountManager {
2
+ }
@@ -0,0 +1,2 @@
1
+ export class AccountManager {
2
+ }
@@ -0,0 +1,19 @@
1
+ import { Nip07Interface } from "applesauce-signer";
2
+ export type SerializedAccount<T extends string, S> = {
3
+ type: T;
4
+ name?: string;
5
+ pubkey: string;
6
+ signer: S;
7
+ };
8
+ export interface IAccount<T extends string, S> extends Nip07Interface {
9
+ name?: string;
10
+ pubkey: string;
11
+ locked: boolean;
12
+ unlock(): Promise<boolean>;
13
+ lock(): void;
14
+ toJSON(): SerializedAccount<T, S>;
15
+ }
16
+ export interface IAccountConstructor<T extends string, S> {
17
+ new (pubkey: string, signer: Nip07Interface): IAccount<T, S>;
18
+ fromJSON(json: SerializedAccount<T, S>): IAccount<T, S>;
19
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "applesauce-accounts",
3
+ "version": "0.10.0",
4
+ "description": "A simple nostr account management system",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "keywords": [
9
+ "nostr"
10
+ ],
11
+ "author": "hzrd149",
12
+ "license": "MIT",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.js",
19
+ "types": "./dist/index.d.ts"
20
+ },
21
+ "./accounts": {
22
+ "import": "./dist/accounts/index.js",
23
+ "types": "./dist/accounts/index.d.ts"
24
+ },
25
+ "./accounts/*": {
26
+ "import": "./dist/accounts/*.js",
27
+ "types": "./dist/accounts/*.d.ts"
28
+ }
29
+ },
30
+ "dependencies": {
31
+ "@noble/hashes": "^1.5.0",
32
+ "applesauce-signer": "^0.10.0",
33
+ "nanoid": "^5.0.9",
34
+ "nostr-tools": "^2.10.3",
35
+ "rxjs": "^7.8.1"
36
+ },
37
+ "devDependencies": {
38
+ "typescript": "^5.6.3",
39
+ "vitest": "^2.1.8"
40
+ },
41
+ "funding": {
42
+ "type": "lightning",
43
+ "url": "lightning:nostrudel@geyser.fund"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc",
47
+ "watch:build": "tsc --watch > /dev/null",
48
+ "test": "vitest run --passWithNoTests",
49
+ "watch:test": "vitest"
50
+ }
51
+ }