applesauce-accounts 0.10.0 → 0.12.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/README.md +3 -1
- package/dist/__tests__/account.test.d.ts +1 -0
- package/dist/__tests__/account.test.js +125 -0
- package/dist/account.d.ts +31 -25
- package/dist/account.js +147 -56
- package/dist/accounts/amber-clipboard-account.d.ts +6 -7
- package/dist/accounts/amber-clipboard-account.js +7 -8
- package/dist/accounts/common.d.ts +3 -0
- package/dist/accounts/common.js +13 -0
- package/dist/accounts/extension-account.d.ts +7 -4
- package/dist/accounts/extension-account.js +12 -6
- package/dist/accounts/index.d.ts +2 -0
- package/dist/accounts/index.js +2 -0
- package/dist/accounts/nostr-connect-account.d.ts +16 -0
- package/dist/accounts/nostr-connect-account.js +34 -0
- package/dist/accounts/password-account.d.ts +16 -10
- package/dist/accounts/password-account.js +22 -19
- package/dist/accounts/readonly-account.d.ts +6 -5
- package/dist/accounts/readonly-account.js +10 -10
- package/dist/accounts/serial-port-account.d.ts +5 -5
- package/dist/accounts/serial-port-account.js +7 -7
- package/dist/accounts/simple-account.d.ts +8 -9
- package/dist/accounts/simple-account.js +16 -13
- package/dist/manager.d.ts +49 -1
- package/dist/manager.js +153 -0
- package/dist/manager.test.d.ts +1 -0
- package/dist/manager.test.js +58 -0
- package/dist/proxy-signer.d.ts +19 -0
- package/dist/proxy-signer.js +45 -0
- package/dist/types.d.ts +28 -13
- package/package.json +11 -7
|
@@ -1,33 +1,36 @@
|
|
|
1
|
-
import { PasswordSigner } from "applesauce-
|
|
1
|
+
import { PasswordSigner } from "applesauce-signers/signers/password-signer";
|
|
2
2
|
import { BaseAccount } from "../account.js";
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.signer = signer;
|
|
3
|
+
export class PasswordAccount extends BaseAccount {
|
|
4
|
+
static type = "ncryptsec";
|
|
5
|
+
get unlocked() {
|
|
6
|
+
return this.signer.unlocked;
|
|
8
7
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
8
|
+
/** called when PasswordAccount.unlock is called without a password */
|
|
9
|
+
static async requestUnlockPassword(_account) {
|
|
10
|
+
throw new Error("Cant unlock PasswordAccount without a password. either pass one in or set PasswordAccount.requestUnlockPassword");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Attempt to unlock the signer with a password
|
|
14
|
+
* @throws
|
|
15
|
+
*/
|
|
16
|
+
async unlock(password) {
|
|
17
|
+
password = password || (await PasswordAccount.requestUnlockPassword(this));
|
|
18
|
+
await this.signer.unlock(password);
|
|
20
19
|
}
|
|
21
20
|
toJSON() {
|
|
22
21
|
if (!this.signer.ncryptsec)
|
|
23
22
|
throw new Error("Cant save account without ncryptsec");
|
|
24
|
-
return
|
|
23
|
+
return super.saveCommonFields({
|
|
24
|
+
signer: { ncryptsec: this.signer.ncryptsec },
|
|
25
|
+
});
|
|
25
26
|
}
|
|
26
27
|
static fromJSON(json) {
|
|
27
28
|
const signer = new PasswordSigner();
|
|
28
29
|
signer.ncryptsec = json.signer.ncryptsec;
|
|
29
|
-
|
|
30
|
+
const account = new PasswordAccount(json.pubkey, signer);
|
|
31
|
+
return super.loadCommonFields(account, json);
|
|
30
32
|
}
|
|
33
|
+
/** Creates a new PasswordAccount from a ncryptsec string */
|
|
31
34
|
static fromNcryptsec(pubkey, ncryptsec) {
|
|
32
35
|
const signer = new PasswordSigner();
|
|
33
36
|
signer.ncryptsec = ncryptsec;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { ReadonlySigner } from "applesauce-
|
|
1
|
+
import { ReadonlySigner } from "applesauce-signers/signers/readonly-signer";
|
|
2
2
|
import { BaseAccount } from "../account.js";
|
|
3
3
|
import { SerializedAccount } from "../types.js";
|
|
4
4
|
/** An account that cannot sign or encrypt anything */
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
toJSON(): SerializedAccount<
|
|
8
|
-
static fromJSON(json: SerializedAccount<
|
|
5
|
+
export declare class ReadonlyAccount<Metadata extends unknown> extends BaseAccount<ReadonlySigner, void, Metadata> {
|
|
6
|
+
static readonly type = "readonly";
|
|
7
|
+
toJSON(): SerializedAccount<void, Metadata>;
|
|
8
|
+
static fromJSON<Metadata extends unknown>(json: SerializedAccount<void, Metadata>): ReadonlyAccount<Metadata>;
|
|
9
|
+
static fromPubkey(pubkey: string): ReadonlyAccount<unknown>;
|
|
9
10
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { ReadonlySigner } from "applesauce-
|
|
1
|
+
import { ReadonlySigner } from "applesauce-signers/signers/readonly-signer";
|
|
2
2
|
import { BaseAccount } from "../account.js";
|
|
3
3
|
/** An account that cannot sign or encrypt anything */
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
super(pubkey, signer || new ReadonlySigner(pubkey));
|
|
7
|
-
}
|
|
4
|
+
export class ReadonlyAccount extends BaseAccount {
|
|
5
|
+
static type = "readonly";
|
|
8
6
|
toJSON() {
|
|
9
|
-
return {
|
|
10
|
-
type: "readonly",
|
|
11
|
-
pubkey: this.pubkey,
|
|
7
|
+
return super.saveCommonFields({
|
|
12
8
|
signer: undefined,
|
|
13
|
-
};
|
|
9
|
+
});
|
|
14
10
|
}
|
|
15
11
|
static fromJSON(json) {
|
|
16
|
-
|
|
12
|
+
const account = new ReadonlyAccount(json.pubkey, new ReadonlySigner(json.pubkey));
|
|
13
|
+
return super.loadCommonFields(account, json);
|
|
14
|
+
}
|
|
15
|
+
static fromPubkey(pubkey) {
|
|
16
|
+
return new ReadonlyAccount(pubkey, new ReadonlySigner(pubkey));
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { SerialPortSigner } from "applesauce-
|
|
1
|
+
import { SerialPortSigner } from "applesauce-signers/signers/serial-port-signer";
|
|
2
2
|
import { BaseAccount } from "../account.js";
|
|
3
3
|
import { SerializedAccount } from "../types.js";
|
|
4
4
|
/** An account for SerialPortSigner */
|
|
5
|
-
export
|
|
6
|
-
|
|
5
|
+
export declare class SerialPortAccount<Metadata extends unknown> extends BaseAccount<SerialPortSigner, void, Metadata> {
|
|
6
|
+
static readonly type = "serial-port";
|
|
7
7
|
unlock(): Promise<boolean>;
|
|
8
|
-
toJSON(): SerializedAccount<
|
|
9
|
-
static fromJSON(json: SerializedAccount<
|
|
8
|
+
toJSON(): SerializedAccount<void, Metadata>;
|
|
9
|
+
static fromJSON<Metadata extends unknown>(json: SerializedAccount<void, Metadata>): SerialPortAccount<Metadata>;
|
|
10
10
|
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { SerialPortSigner } from "applesauce-
|
|
1
|
+
import { SerialPortSigner } from "applesauce-signers/signers/serial-port-signer";
|
|
2
2
|
import { BaseAccount } from "../account.js";
|
|
3
3
|
/** An account for SerialPortSigner */
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
super(pubkey, signer || new SerialPortSigner());
|
|
7
|
-
}
|
|
4
|
+
export class SerialPortAccount extends BaseAccount {
|
|
5
|
+
static type = "serial-port";
|
|
8
6
|
async unlock() {
|
|
9
7
|
try {
|
|
10
8
|
const pubkey = await this.signer.getPublicKey();
|
|
@@ -17,9 +15,11 @@ export default class SerialPortAccount extends BaseAccount {
|
|
|
17
15
|
}
|
|
18
16
|
}
|
|
19
17
|
toJSON() {
|
|
20
|
-
return {
|
|
18
|
+
return super.saveCommonFields({ signer: undefined });
|
|
21
19
|
}
|
|
22
20
|
static fromJSON(json) {
|
|
23
|
-
|
|
21
|
+
const signer = new SerialPortSigner();
|
|
22
|
+
const account = new SerialPortAccount(json.pubkey, signer);
|
|
23
|
+
return super.loadCommonFields(account, json);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { SimpleSigner } from "applesauce-
|
|
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
|
|
4
|
+
export type SimpleAccountSignerData = {
|
|
5
5
|
key: string;
|
|
6
6
|
};
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
static
|
|
11
|
-
|
|
12
|
-
static
|
|
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
|
+
static fromKey<Metadata extends unknown>(key: Uint8Array | string): SimpleAccount<Metadata>;
|
|
12
|
+
static generateNew<Metadata extends unknown>(): SimpleAccount<Metadata>;
|
|
13
13
|
}
|
|
14
|
-
export {};
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { getPublicKey } from "nostr-tools";
|
|
2
|
-
import { SimpleSigner } from "applesauce-
|
|
1
|
+
import { generateSecretKey, getPublicKey } from "nostr-tools";
|
|
2
|
+
import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
|
|
3
3
|
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
|
|
4
4
|
import { BaseAccount } from "../account.js";
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
super(
|
|
9
|
-
|
|
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);
|
|
10
16
|
}
|
|
11
17
|
static fromKey(key) {
|
|
12
18
|
if (typeof key === "string")
|
|
@@ -14,11 +20,8 @@ export default class SimpleAccount extends BaseAccount {
|
|
|
14
20
|
const pubkey = getPublicKey(key);
|
|
15
21
|
return new SimpleAccount(pubkey, new SimpleSigner(key));
|
|
16
22
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
static fromJSON(json) {
|
|
21
|
-
const key = hexToBytes(json.signer.key);
|
|
22
|
-
return new SimpleAccount(json.pubkey, new SimpleSigner(key));
|
|
23
|
+
static generateNew() {
|
|
24
|
+
const key = generateSecretKey();
|
|
25
|
+
return SimpleAccount.fromKey(key);
|
|
23
26
|
}
|
|
24
27
|
}
|
package/dist/manager.d.ts
CHANGED
|
@@ -1,2 +1,50 @@
|
|
|
1
|
-
|
|
1
|
+
import { Nip07Interface } from "applesauce-signers";
|
|
2
|
+
import { BehaviorSubject } from "rxjs";
|
|
3
|
+
import { IAccount, IAccountConstructor, SerializedAccount } from "./types.js";
|
|
4
|
+
export declare class AccountManager<Metadata extends unknown = any> {
|
|
5
|
+
types: Map<string, IAccountConstructor<any, any, Metadata>>;
|
|
6
|
+
active$: BehaviorSubject<IAccount<any, any, Metadata> | undefined>;
|
|
7
|
+
get active(): IAccount<any, any, Metadata> | undefined;
|
|
8
|
+
accounts$: BehaviorSubject<IAccount<any, any, Metadata>[]>;
|
|
9
|
+
get accounts(): IAccount<any, any, Metadata>[];
|
|
10
|
+
/** Proxy signer for currently active account */
|
|
11
|
+
signer: Nip07Interface;
|
|
12
|
+
/** Disable request queueing for any accounts added to this manager */
|
|
13
|
+
disableQueue?: boolean;
|
|
14
|
+
constructor();
|
|
15
|
+
/** Add account type class */
|
|
16
|
+
registerType<S extends Nip07Interface>(accountType: IAccountConstructor<S, any, Metadata>): void;
|
|
17
|
+
/** Remove account type */
|
|
18
|
+
unregisterType(type: string): void;
|
|
19
|
+
/** gets an account in the manager */
|
|
20
|
+
getAccount<Signer extends Nip07Interface>(id: string | IAccount<Signer, any, Metadata>): IAccount<Signer, any, Metadata> | undefined;
|
|
21
|
+
/** Return the first account for a pubkey */
|
|
22
|
+
getAccountForPubkey(pubkey: string): IAccount<any, any, Metadata> | undefined;
|
|
23
|
+
/** Returns all accounts for a pubkey */
|
|
24
|
+
getAccountsForPubkey(pubkey: string): IAccount<any, any, Metadata>[];
|
|
25
|
+
/** adds an account to the manager */
|
|
26
|
+
addAccount(account: IAccount<any, any, Metadata>): void;
|
|
27
|
+
/** Removes an account from the manager */
|
|
28
|
+
removeAccount(account: string | IAccount<any, any, Metadata>): void;
|
|
29
|
+
/** Replaces an account with another */
|
|
30
|
+
replaceAccount(old: string | IAccount<any, any, Metadata>, account: IAccount<any, any, Metadata>): void;
|
|
31
|
+
/** Returns the currently active account */
|
|
32
|
+
getActive(): IAccount<any, any, Metadata> | undefined;
|
|
33
|
+
/** Sets the currently active account */
|
|
34
|
+
setActive(id: string | IAccount<any, any, Metadata>): void;
|
|
35
|
+
/** Clears the currently active account */
|
|
36
|
+
clearActive(): void;
|
|
37
|
+
/** sets the metadata on an account */
|
|
38
|
+
setAccountMetadata(id: string | IAccount<any, any, Metadata>, metadata: Metadata): void;
|
|
39
|
+
/** sets the metadata on an account */
|
|
40
|
+
getAccountMetadata(id: string | IAccount<any, any, Metadata>): Metadata | undefined;
|
|
41
|
+
/** Removes all metadata on the account */
|
|
42
|
+
clearAccountMetadata(id: string | IAccount<any, any, Metadata>): void;
|
|
43
|
+
/** Returns an array of serialized accounts */
|
|
44
|
+
toJSON(quite?: boolean): SerializedAccount<any, Metadata>[];
|
|
45
|
+
/**
|
|
46
|
+
* Restores all accounts from an array of serialized accounts
|
|
47
|
+
* NOTE: this will clear all existing accounts
|
|
48
|
+
*/
|
|
49
|
+
fromJSON(accounts: SerializedAccount<any, Metadata>[], quite?: boolean): void;
|
|
2
50
|
}
|
package/dist/manager.js
CHANGED
|
@@ -1,2 +1,155 @@
|
|
|
1
|
+
import { BehaviorSubject } from "rxjs";
|
|
2
|
+
import { ProxySigner } from "./proxy-signer.js";
|
|
1
3
|
export class AccountManager {
|
|
4
|
+
types = new Map();
|
|
5
|
+
active$ = new BehaviorSubject(undefined);
|
|
6
|
+
get active() {
|
|
7
|
+
return this.active$.value;
|
|
8
|
+
}
|
|
9
|
+
accounts$ = new BehaviorSubject([]);
|
|
10
|
+
get accounts() {
|
|
11
|
+
return this.accounts$.value;
|
|
12
|
+
}
|
|
13
|
+
/** Proxy signer for currently active account */
|
|
14
|
+
signer;
|
|
15
|
+
/** Disable request queueing for any accounts added to this manager */
|
|
16
|
+
disableQueue;
|
|
17
|
+
constructor() {
|
|
18
|
+
this.signer = new ProxySigner(this.active$, "No active account");
|
|
19
|
+
}
|
|
20
|
+
// Account type CRUD
|
|
21
|
+
/** Add account type class */
|
|
22
|
+
registerType(accountType) {
|
|
23
|
+
if (!accountType.type)
|
|
24
|
+
throw new Error(`Account class missing static "type" field`);
|
|
25
|
+
if (this.types.has(accountType.type))
|
|
26
|
+
throw new Error(`An account type of ${accountType.type} already exists`);
|
|
27
|
+
this.types.set(accountType.type, accountType);
|
|
28
|
+
}
|
|
29
|
+
/** Remove account type */
|
|
30
|
+
unregisterType(type) {
|
|
31
|
+
this.types.delete(type);
|
|
32
|
+
}
|
|
33
|
+
// Accounts CRUD
|
|
34
|
+
/** gets an account in the manager */
|
|
35
|
+
getAccount(id) {
|
|
36
|
+
if (typeof id === "string")
|
|
37
|
+
return this.accounts$.value.find((a) => a.id === id);
|
|
38
|
+
else if (this.accounts$.value.includes(id))
|
|
39
|
+
return id;
|
|
40
|
+
else
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Return the first account for a pubkey */
|
|
44
|
+
getAccountForPubkey(pubkey) {
|
|
45
|
+
return Object.values(this.accounts$.value).find((account) => account.pubkey === pubkey);
|
|
46
|
+
}
|
|
47
|
+
/** Returns all accounts for a pubkey */
|
|
48
|
+
getAccountsForPubkey(pubkey) {
|
|
49
|
+
return Object.values(this.accounts$.value).filter((account) => account.pubkey === pubkey);
|
|
50
|
+
}
|
|
51
|
+
/** adds an account to the manager */
|
|
52
|
+
addAccount(account) {
|
|
53
|
+
if (this.getAccount(account.id))
|
|
54
|
+
return;
|
|
55
|
+
// copy the disableQueue flag only if its set
|
|
56
|
+
if (this.disableQueue !== undefined && account.disableQueue !== undefined) {
|
|
57
|
+
account.disableQueue = this.disableQueue;
|
|
58
|
+
}
|
|
59
|
+
this.accounts$.next([...this.accounts$.value, account]);
|
|
60
|
+
}
|
|
61
|
+
/** Removes an account from the manager */
|
|
62
|
+
removeAccount(account) {
|
|
63
|
+
const id = typeof account === "string" ? account : account.id;
|
|
64
|
+
this.accounts$.next(this.accounts$.value.filter((a) => a.id !== id));
|
|
65
|
+
}
|
|
66
|
+
/** Replaces an account with another */
|
|
67
|
+
replaceAccount(old, account) {
|
|
68
|
+
this.addAccount(account);
|
|
69
|
+
// if the old account was active, switch to the new one
|
|
70
|
+
const id = typeof account === "string" ? account : account.id;
|
|
71
|
+
if (this.active$.value?.id === id)
|
|
72
|
+
this.setActive(account);
|
|
73
|
+
this.removeAccount(old);
|
|
74
|
+
}
|
|
75
|
+
// Active account methods
|
|
76
|
+
/** Returns the currently active account */
|
|
77
|
+
getActive() {
|
|
78
|
+
return this.active$.value;
|
|
79
|
+
}
|
|
80
|
+
/** Sets the currently active account */
|
|
81
|
+
setActive(id) {
|
|
82
|
+
const account = this.getAccount(id);
|
|
83
|
+
if (!account)
|
|
84
|
+
throw new Error("Cant find account with that ID");
|
|
85
|
+
if (this.active$.value?.id !== account.id) {
|
|
86
|
+
this.active$.next(account);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Clears the currently active account */
|
|
90
|
+
clearActive() {
|
|
91
|
+
this.active$.next(undefined);
|
|
92
|
+
}
|
|
93
|
+
// Metadata CRUD
|
|
94
|
+
/** sets the metadata on an account */
|
|
95
|
+
setAccountMetadata(id, metadata) {
|
|
96
|
+
const account = this.getAccount(id);
|
|
97
|
+
if (!account)
|
|
98
|
+
throw new Error("Cant find account with that ID");
|
|
99
|
+
account.metadata = metadata;
|
|
100
|
+
}
|
|
101
|
+
/** sets the metadata on an account */
|
|
102
|
+
getAccountMetadata(id) {
|
|
103
|
+
const account = this.getAccount(id);
|
|
104
|
+
if (!account)
|
|
105
|
+
throw new Error("Cant find account with that ID");
|
|
106
|
+
return account.metadata;
|
|
107
|
+
}
|
|
108
|
+
/** Removes all metadata on the account */
|
|
109
|
+
clearAccountMetadata(id) {
|
|
110
|
+
const account = this.getAccount(id);
|
|
111
|
+
if (!account)
|
|
112
|
+
throw new Error("Cant find account with that ID");
|
|
113
|
+
account.metadata = undefined;
|
|
114
|
+
}
|
|
115
|
+
// Serialize / Deserialize
|
|
116
|
+
/** Returns an array of serialized accounts */
|
|
117
|
+
toJSON(quite = false) {
|
|
118
|
+
const accounts = [];
|
|
119
|
+
for (const account of this.accounts) {
|
|
120
|
+
try {
|
|
121
|
+
accounts.push(account.toJSON());
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (!quite)
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return accounts;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Restores all accounts from an array of serialized accounts
|
|
132
|
+
* NOTE: this will clear all existing accounts
|
|
133
|
+
*/
|
|
134
|
+
fromJSON(accounts, quite = false) {
|
|
135
|
+
for (const json of accounts) {
|
|
136
|
+
try {
|
|
137
|
+
const AccountType = this.types.get(json.type);
|
|
138
|
+
if (!AccountType) {
|
|
139
|
+
if (!quite)
|
|
140
|
+
throw new Error(`Missing account type ${json.type}`);
|
|
141
|
+
else
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const account = AccountType.fromJSON(json);
|
|
145
|
+
this.addAccount(account);
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
if (!quite)
|
|
149
|
+
throw error;
|
|
150
|
+
else
|
|
151
|
+
console.log(`Failed to load account`, error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
2
155
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
describe("signer", () => {
|
|
46
|
+
it("should proxy active account", async () => {
|
|
47
|
+
const account = SimpleAccount.generateNew();
|
|
48
|
+
manager.addAccount(account);
|
|
49
|
+
manager.setActive(account);
|
|
50
|
+
expect(await manager.signer.getPublicKey()).toBe(getPublicKey(account.signer.key));
|
|
51
|
+
});
|
|
52
|
+
it("should throw if there is no active account", () => {
|
|
53
|
+
expect(() => {
|
|
54
|
+
manager.signer.getPublicKey();
|
|
55
|
+
}).toThrow("No active account");
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Nip07Interface } from "applesauce-signers";
|
|
2
|
+
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
3
|
+
import { BehaviorSubject } from "rxjs";
|
|
4
|
+
export declare class ProxySigner<T extends Nip07Interface> implements Nip07Interface {
|
|
5
|
+
protected upstream: BehaviorSubject<T | undefined>;
|
|
6
|
+
protected error?: string | undefined;
|
|
7
|
+
nip04: {
|
|
8
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
9
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
10
|
+
};
|
|
11
|
+
nip44: {
|
|
12
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
13
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
14
|
+
};
|
|
15
|
+
constructor(upstream: BehaviorSubject<T | undefined>, error?: string | undefined);
|
|
16
|
+
protected get signer(): Nip07Interface;
|
|
17
|
+
signEvent(template: EventTemplate): Promise<NostrEvent> | NostrEvent;
|
|
18
|
+
getPublicKey(): Promise<string> | string;
|
|
19
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export class ProxySigner {
|
|
2
|
+
upstream;
|
|
3
|
+
error;
|
|
4
|
+
nip04;
|
|
5
|
+
nip44;
|
|
6
|
+
constructor(upstream, error) {
|
|
7
|
+
this.upstream = upstream;
|
|
8
|
+
this.error = error;
|
|
9
|
+
this.nip04 = {
|
|
10
|
+
encrypt: (pubkey, plaintext) => {
|
|
11
|
+
if (!this.signer.nip04)
|
|
12
|
+
throw new Error("Signer does not support nip04");
|
|
13
|
+
return this.signer.nip04.encrypt(pubkey, plaintext);
|
|
14
|
+
},
|
|
15
|
+
decrypt: (pubkey, ciphertext) => {
|
|
16
|
+
if (!this.signer.nip04)
|
|
17
|
+
throw new Error("Signer does not support nip04");
|
|
18
|
+
return this.signer.nip04.decrypt(pubkey, ciphertext);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
this.nip44 = {
|
|
22
|
+
encrypt: (pubkey, plaintext) => {
|
|
23
|
+
if (!this.signer.nip44)
|
|
24
|
+
throw new Error("Signer does not support nip44");
|
|
25
|
+
return this.signer.nip44.encrypt(pubkey, plaintext);
|
|
26
|
+
},
|
|
27
|
+
decrypt: (pubkey, ciphertext) => {
|
|
28
|
+
if (!this.signer.nip44)
|
|
29
|
+
throw new Error("Signer does not support nip44");
|
|
30
|
+
return this.signer.nip44.decrypt(pubkey, ciphertext);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
get signer() {
|
|
35
|
+
if (!this.upstream.value)
|
|
36
|
+
throw new Error(this.error || "Missing signer");
|
|
37
|
+
return this.upstream.value;
|
|
38
|
+
}
|
|
39
|
+
signEvent(template) {
|
|
40
|
+
return this.signer.signEvent(template);
|
|
41
|
+
}
|
|
42
|
+
getPublicKey() {
|
|
43
|
+
return this.signer.getPublicKey();
|
|
44
|
+
}
|
|
45
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
import { Nip07Interface } from "applesauce-
|
|
2
|
-
export type
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Nip07Interface } from "applesauce-signers";
|
|
2
|
+
export type EventTemplate = {
|
|
3
|
+
kind: number;
|
|
4
|
+
content: string;
|
|
5
|
+
tags: string[][];
|
|
6
|
+
created_at: number;
|
|
7
|
+
};
|
|
8
|
+
export type SerializedAccount<SignerData, Metadata extends unknown> = {
|
|
9
|
+
/** Internal account ID */
|
|
10
|
+
id: string;
|
|
11
|
+
/** account type */
|
|
12
|
+
type: string;
|
|
13
|
+
/** pubkey of the account */
|
|
5
14
|
pubkey: string;
|
|
6
|
-
|
|
15
|
+
/** Signer data */
|
|
16
|
+
signer: SignerData;
|
|
17
|
+
/** Extra application specific account metadata */
|
|
18
|
+
metadata?: Metadata;
|
|
7
19
|
};
|
|
8
|
-
export interface IAccount<
|
|
20
|
+
export interface IAccount<Signer extends Nip07Interface = Nip07Interface, SignerData = any, Metadata extends unknown = any> extends Nip07Interface {
|
|
21
|
+
id: string;
|
|
9
22
|
name?: string;
|
|
10
23
|
pubkey: string;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
metadata?: Metadata;
|
|
25
|
+
signer: Signer;
|
|
26
|
+
type: string;
|
|
27
|
+
disableQueue?: boolean;
|
|
28
|
+
toJSON(): SerializedAccount<SignerData, Metadata>;
|
|
15
29
|
}
|
|
16
|
-
export interface IAccountConstructor<
|
|
17
|
-
|
|
18
|
-
|
|
30
|
+
export interface IAccountConstructor<Signer extends Nip07Interface, SignerData, Metadata extends unknown> {
|
|
31
|
+
readonly type: string;
|
|
32
|
+
new (pubkey: string, signer: Signer): IAccount<Signer, SignerData, Metadata>;
|
|
33
|
+
fromJSON(json: SerializedAccount<SignerData, Metadata>): IAccount<Signer, SignerData, Metadata>;
|
|
19
34
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-accounts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "A simple nostr account management system",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"keywords": [
|
|
9
|
-
"nostr"
|
|
9
|
+
"nostr",
|
|
10
|
+
"applesauce"
|
|
10
11
|
],
|
|
11
12
|
"author": "hzrd149",
|
|
12
13
|
"license": "MIT",
|
|
@@ -16,27 +17,30 @@
|
|
|
16
17
|
"exports": {
|
|
17
18
|
".": {
|
|
18
19
|
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.js",
|
|
19
21
|
"types": "./dist/index.d.ts"
|
|
20
22
|
},
|
|
21
23
|
"./accounts": {
|
|
22
24
|
"import": "./dist/accounts/index.js",
|
|
25
|
+
"require": "./dist/accounts/index.js",
|
|
23
26
|
"types": "./dist/accounts/index.d.ts"
|
|
24
27
|
},
|
|
25
28
|
"./accounts/*": {
|
|
26
29
|
"import": "./dist/accounts/*.js",
|
|
30
|
+
"require": "./dist/accounts/*.js",
|
|
27
31
|
"types": "./dist/accounts/*.d.ts"
|
|
28
32
|
}
|
|
29
33
|
},
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"@noble/hashes": "^1.
|
|
32
|
-
"applesauce-
|
|
35
|
+
"@noble/hashes": "^1.7.1",
|
|
36
|
+
"applesauce-signers": "^0.12.0",
|
|
33
37
|
"nanoid": "^5.0.9",
|
|
34
|
-
"nostr-tools": "^2.10.
|
|
38
|
+
"nostr-tools": "^2.10.4",
|
|
35
39
|
"rxjs": "^7.8.1"
|
|
36
40
|
},
|
|
37
41
|
"devDependencies": {
|
|
38
|
-
"typescript": "^5.
|
|
39
|
-
"vitest": "^
|
|
42
|
+
"typescript": "^5.7.3",
|
|
43
|
+
"vitest": "^3.0.5"
|
|
40
44
|
},
|
|
41
45
|
"funding": {
|
|
42
46
|
"type": "lightning",
|