applesauce-accounts 0.0.0-next-20250123205825 → 0.0.0-next-20250123215242
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 +31 -0
- package/dist/account.js +77 -0
- package/dist/accounts/amber-clipboard-account.d.ts +10 -0
- package/dist/accounts/amber-clipboard-account.js +16 -0
- package/dist/accounts/extension-account.d.ts +7 -0
- package/dist/accounts/extension-account.js +13 -0
- package/dist/accounts/index.d.ts +6 -0
- package/dist/accounts/index.js +6 -0
- package/dist/accounts/password-account.d.ts +15 -0
- package/dist/accounts/password-account.js +36 -0
- package/dist/accounts/readonly-account.d.ts +9 -0
- package/dist/accounts/readonly-account.js +18 -0
- package/dist/accounts/serial-port-account.d.ts +10 -0
- package/dist/accounts/serial-port-account.js +25 -0
- package/dist/accounts/simple-account.d.ts +14 -0
- package/dist/accounts/simple-account.js +24 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/manager.d.ts +2 -0
- package/dist/manager.js +2 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.js +1 -0
- package/package.json +2 -2
|
@@ -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
|
+
}
|
package/dist/account.js
ADDED
|
@@ -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,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
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/manager.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-accounts",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20250123215242",
|
|
4
4
|
"description": "A simple nostr account management system",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@noble/hashes": "^1.5.0",
|
|
35
|
-
"applesauce-signer": "0.0.0-next-
|
|
35
|
+
"applesauce-signer": "0.0.0-next-20250123215242",
|
|
36
36
|
"nanoid": "^5.0.9",
|
|
37
37
|
"nostr-tools": "^2.10.3",
|
|
38
38
|
"rxjs": "^7.8.1"
|