applesauce-accounts 0.11.0 → 1.0.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/dist/{account.test.js → __tests__/account.test.js} +66 -7
- package/dist/__tests__/manager.test.js +65 -0
- package/dist/account.d.ts +2 -8
- package/dist/account.js +24 -24
- package/dist/accounts/nostr-connect-account.d.ts +2 -4
- package/dist/accounts/nostr-connect-account.js +1 -7
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/manager.d.ts +1 -1
- package/dist/manager.js +5 -12
- package/dist/proxy-signer.d.ts +20 -0
- package/dist/proxy-signer.js +31 -0
- package/package.json +5 -5
- package/dist/manager.test.js +0 -58
- /package/dist/{account.test.d.ts → __tests__/account.test.d.ts} +0 -0
- /package/dist/{manager.test.d.ts → __tests__/manager.test.d.ts} +0 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { describe, it, expect,
|
|
2
|
-
import { BaseAccount } from "./account.js";
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
3
2
|
import { SimpleSigner } from "applesauce-signers";
|
|
4
3
|
import { finalizeEvent, generateSecretKey } from "nostr-tools";
|
|
5
|
-
import {
|
|
4
|
+
import { BaseAccount } from "../account.js";
|
|
5
|
+
import { SimpleAccount } from "../accounts/simple-account.js";
|
|
6
6
|
describe("BaseAccount", () => {
|
|
7
|
-
let signer;
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
signer = new SimpleSigner();
|
|
10
|
-
});
|
|
11
7
|
describe("request queue", () => {
|
|
8
|
+
let signer;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
signer = new SimpleSigner();
|
|
11
|
+
});
|
|
12
12
|
it("should queue signing requests by default", async () => {
|
|
13
13
|
const account = new BaseAccount(await signer.getPublicKey(), signer);
|
|
14
14
|
let resolve = [];
|
|
@@ -63,4 +63,63 @@ describe("BaseAccount", () => {
|
|
|
63
63
|
expect(account.type).toBe("nsec");
|
|
64
64
|
});
|
|
65
65
|
});
|
|
66
|
+
describe("nip04 and nip44", () => {
|
|
67
|
+
it("should return undefined when signer does not support nip04/nip44", () => {
|
|
68
|
+
const signer = {
|
|
69
|
+
getPublicKey: () => "test-pubkey",
|
|
70
|
+
signEvent: () => ({ id: "", pubkey: "test-pubkey", created_at: 0, kind: 1, tags: [], content: "", sig: "" }),
|
|
71
|
+
};
|
|
72
|
+
const account = new BaseAccount("test-pubkey", signer);
|
|
73
|
+
expect(account.nip04).toBeUndefined();
|
|
74
|
+
expect(account.nip44).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
it("should return nip04/nip44 interface when signer supports them", async () => {
|
|
77
|
+
const signer = {
|
|
78
|
+
getPublicKey: () => "test-pubkey",
|
|
79
|
+
signEvent: () => ({ id: "", pubkey: "test-pubkey", created_at: 0, kind: 1, tags: [], content: "", sig: "" }),
|
|
80
|
+
nip04: {
|
|
81
|
+
encrypt: async () => "encrypted",
|
|
82
|
+
decrypt: async () => "decrypted",
|
|
83
|
+
},
|
|
84
|
+
nip44: {
|
|
85
|
+
encrypt: async () => "encrypted",
|
|
86
|
+
decrypt: async () => "decrypted",
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
const account = new BaseAccount("test-pubkey", signer);
|
|
90
|
+
expect(account.nip04).toBeDefined();
|
|
91
|
+
expect(account.nip44).toBeDefined();
|
|
92
|
+
const nip04Result = await account.nip04.encrypt("pubkey", "test");
|
|
93
|
+
expect(nip04Result).toBe("encrypted");
|
|
94
|
+
const nip44Result = await account.nip44.encrypt("pubkey", "test");
|
|
95
|
+
expect(nip44Result).toBe("encrypted");
|
|
96
|
+
});
|
|
97
|
+
it("should reflect changes in signer nip04/nip44 support", () => {
|
|
98
|
+
const signer = {
|
|
99
|
+
getPublicKey: () => "test-pubkey",
|
|
100
|
+
signEvent: () => ({ id: "", pubkey: "test-pubkey", created_at: 0, kind: 1, tags: [], content: "", sig: "" }),
|
|
101
|
+
};
|
|
102
|
+
const account = new BaseAccount("test-pubkey", signer);
|
|
103
|
+
expect(account.nip04).toBeUndefined();
|
|
104
|
+
expect(account.nip44).toBeUndefined();
|
|
105
|
+
// Add nip04 support
|
|
106
|
+
signer.nip04 = {
|
|
107
|
+
encrypt: async () => "encrypted",
|
|
108
|
+
decrypt: async () => "decrypted",
|
|
109
|
+
};
|
|
110
|
+
expect(account.nip04).toBeDefined();
|
|
111
|
+
expect(account.nip44).toBeUndefined();
|
|
112
|
+
// Add nip44 support
|
|
113
|
+
signer.nip44 = {
|
|
114
|
+
encrypt: async () => "encrypted",
|
|
115
|
+
decrypt: async () => "decrypted",
|
|
116
|
+
};
|
|
117
|
+
expect(account.nip04).toBeDefined();
|
|
118
|
+
expect(account.nip44).toBeDefined();
|
|
119
|
+
// Remove nip04 support
|
|
120
|
+
signer.nip04 = undefined;
|
|
121
|
+
expect(account.nip04).toBeUndefined();
|
|
122
|
+
expect(account.nip44).toBeDefined();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
66
125
|
});
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
let manager;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
manager = new AccountManager();
|
|
9
|
+
});
|
|
10
|
+
describe("toJSON", () => {
|
|
11
|
+
it("should return an array of serialized accounts", () => {
|
|
12
|
+
manager.addAccount(SimpleAccount.fromKey(generateSecretKey()));
|
|
13
|
+
manager.setAccountMetadata(manager.accounts[0], { name: "testing" });
|
|
14
|
+
expect(manager.toJSON()).toEqual([
|
|
15
|
+
{
|
|
16
|
+
id: expect.any(String),
|
|
17
|
+
type: "nsec",
|
|
18
|
+
pubkey: expect.any(String),
|
|
19
|
+
metadata: { name: "testing" },
|
|
20
|
+
signer: { key: expect.any(String) },
|
|
21
|
+
},
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe("fromJSON", () => {
|
|
26
|
+
it("should recreate accounts", () => {
|
|
27
|
+
const key = generateSecretKey();
|
|
28
|
+
const json = [
|
|
29
|
+
{
|
|
30
|
+
id: "custom-id",
|
|
31
|
+
type: "nsec",
|
|
32
|
+
pubkey: getPublicKey(key),
|
|
33
|
+
metadata: { name: "testing" },
|
|
34
|
+
signer: { key: bytesToHex(key) },
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
manager.registerType(SimpleAccount);
|
|
38
|
+
manager.fromJSON(json);
|
|
39
|
+
expect(manager.getAccount("custom-id")).toBeInstanceOf(SimpleAccount);
|
|
40
|
+
expect(manager.getAccountForPubkey(getPublicKey(key))).toBeInstanceOf(SimpleAccount);
|
|
41
|
+
expect(manager.getAccountMetadata("custom-id")).toEqual({ name: "testing" });
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe("signer", () => {
|
|
45
|
+
it("should proxy active account", async () => {
|
|
46
|
+
const account = SimpleAccount.generateNew();
|
|
47
|
+
manager.addAccount(account);
|
|
48
|
+
manager.setActive(account);
|
|
49
|
+
expect(await manager.signer.getPublicKey()).toBe(getPublicKey(account.signer.key));
|
|
50
|
+
});
|
|
51
|
+
it("should throw if there is no active account", () => {
|
|
52
|
+
expect(() => {
|
|
53
|
+
manager.signer.getPublicKey();
|
|
54
|
+
}).toThrow("No active account");
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe("removeAccount", () => {
|
|
58
|
+
it("should clear active account if removed account was active", () => {
|
|
59
|
+
const account = SimpleAccount.generateNew();
|
|
60
|
+
manager.addAccount(account);
|
|
61
|
+
manager.setActive(account);
|
|
62
|
+
manager.removeAccount(account);
|
|
63
|
+
expect(manager.active).toBeUndefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
package/dist/account.d.ts
CHANGED
|
@@ -14,14 +14,8 @@ export declare class BaseAccount<Signer extends Nip07Interface, SignerData, Meta
|
|
|
14
14
|
metadata$: BehaviorSubject<Metadata | undefined>;
|
|
15
15
|
get metadata(): Metadata | undefined;
|
|
16
16
|
set metadata(metadata: Metadata);
|
|
17
|
-
nip04
|
|
18
|
-
|
|
19
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
20
|
-
} | undefined;
|
|
21
|
-
nip44?: {
|
|
22
|
-
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
23
|
-
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
24
|
-
} | undefined;
|
|
17
|
+
get nip04(): Nip07Interface["nip04"] | undefined;
|
|
18
|
+
get nip44(): Nip07Interface["nip44"] | undefined;
|
|
25
19
|
constructor(pubkey: string, signer: Signer);
|
|
26
20
|
toJSON(): SerializedAccount<SignerData, Metadata>;
|
|
27
21
|
/** Adds the common fields to the serialized output of a toJSON method */
|
package/dist/account.js
CHANGED
|
@@ -40,33 +40,33 @@ export class BaseAccount {
|
|
|
40
40
|
set metadata(metadata) {
|
|
41
41
|
this.metadata$.next(metadata);
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
get nip04() {
|
|
44
|
+
if (!this.signer.nip04)
|
|
45
|
+
return undefined;
|
|
46
|
+
return {
|
|
47
|
+
encrypt: (pubkey, plaintext) => {
|
|
48
|
+
return this.waitForLock(() => this.signer.nip04.encrypt(pubkey, plaintext));
|
|
49
|
+
},
|
|
50
|
+
decrypt: (pubkey, plaintext) => {
|
|
51
|
+
return this.waitForLock(() => this.signer.nip04.decrypt(pubkey, plaintext));
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
get nip44() {
|
|
56
|
+
if (!this.signer.nip44)
|
|
57
|
+
return undefined;
|
|
58
|
+
return {
|
|
59
|
+
encrypt: (pubkey, plaintext) => {
|
|
60
|
+
return this.waitForLock(() => this.signer.nip44.encrypt(pubkey, plaintext));
|
|
61
|
+
},
|
|
62
|
+
decrypt: (pubkey, plaintext) => {
|
|
63
|
+
return this.waitForLock(() => this.signer.nip44.decrypt(pubkey, plaintext));
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
46
67
|
constructor(pubkey, signer) {
|
|
47
68
|
this.pubkey = pubkey;
|
|
48
69
|
this.signer = signer;
|
|
49
|
-
// setup encryption interfaces to check if account is locked
|
|
50
|
-
if (this.signer.nip04) {
|
|
51
|
-
this.nip04 = {
|
|
52
|
-
encrypt: (pubkey, plaintext) => {
|
|
53
|
-
return this.waitForLock(() => this.signer.nip04.encrypt(pubkey, plaintext));
|
|
54
|
-
},
|
|
55
|
-
decrypt: (pubkey, plaintext) => {
|
|
56
|
-
return this.waitForLock(() => this.signer.nip04.decrypt(pubkey, plaintext));
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
if (this.signer.nip44) {
|
|
61
|
-
this.nip44 = {
|
|
62
|
-
encrypt: (pubkey, plaintext) => {
|
|
63
|
-
return this.waitForLock(() => this.signer.nip44.encrypt(pubkey, plaintext));
|
|
64
|
-
},
|
|
65
|
-
decrypt: (pubkey, plaintext) => {
|
|
66
|
-
return this.waitForLock(() => this.signer.nip44.decrypt(pubkey, plaintext));
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
70
|
}
|
|
71
71
|
// This should be overwritten by a sub class
|
|
72
72
|
toJSON() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NostrConnectSigner } from "applesauce-signers";
|
|
2
2
|
import { BaseAccount } from "../account.js";
|
|
3
3
|
import { SerializedAccount } from "../types.js";
|
|
4
4
|
export type NostrConnectAccountSignerData = {
|
|
@@ -10,7 +10,5 @@ export type NostrConnectAccountSignerData = {
|
|
|
10
10
|
export declare class NostrConnectAccount<Metadata extends unknown> extends BaseAccount<NostrConnectSigner, NostrConnectAccountSignerData, Metadata> {
|
|
11
11
|
static readonly type = "nostr-connect";
|
|
12
12
|
toJSON(): SerializedAccount<NostrConnectAccountSignerData, Metadata>;
|
|
13
|
-
|
|
14
|
-
static createConnectionMethods(): NostrConnectConnectionMethods;
|
|
15
|
-
static fromJSON<Metadata extends unknown>(json: SerializedAccount<NostrConnectAccountSignerData, Metadata>, connection?: NostrConnectConnectionMethods): NostrConnectAccount<Metadata>;
|
|
13
|
+
static fromJSON<Metadata extends unknown>(json: SerializedAccount<NostrConnectAccountSignerData, Metadata>): NostrConnectAccount<Metadata>;
|
|
16
14
|
}
|
|
@@ -15,14 +15,8 @@ export class NostrConnectAccount extends BaseAccount {
|
|
|
15
15
|
},
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
static createConnectionMethods() {
|
|
20
|
-
throw new Error("Cant create NostrConnectAccount without either passing in connection methods or setting NostrConnectAccount.createConnectionMethods");
|
|
21
|
-
}
|
|
22
|
-
static fromJSON(json, connection) {
|
|
23
|
-
connection = connection || NostrConnectAccount.createConnectionMethods();
|
|
18
|
+
static fromJSON(json) {
|
|
24
19
|
const signer = new NostrConnectSigner({
|
|
25
|
-
...connection,
|
|
26
20
|
relays: json.signer.relays,
|
|
27
21
|
pubkey: json.pubkey,
|
|
28
22
|
remote: json.signer.remote,
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/manager.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare class AccountManager<Metadata extends unknown = any> {
|
|
|
8
8
|
accounts$: BehaviorSubject<IAccount<any, any, Metadata>[]>;
|
|
9
9
|
get accounts(): IAccount<any, any, Metadata>[];
|
|
10
10
|
/** Proxy signer for currently active account */
|
|
11
|
-
signer:
|
|
11
|
+
signer: Nip07Interface;
|
|
12
12
|
/** Disable request queueing for any accounts added to this manager */
|
|
13
13
|
disableQueue?: boolean;
|
|
14
14
|
constructor();
|
package/dist/manager.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BehaviorSubject } from "rxjs";
|
|
2
|
+
import { ProxySigner } from "./proxy-signer.js";
|
|
2
3
|
export class AccountManager {
|
|
3
4
|
types = new Map();
|
|
4
5
|
active$ = new BehaviorSubject(undefined);
|
|
@@ -14,18 +15,7 @@ export class AccountManager {
|
|
|
14
15
|
/** Disable request queueing for any accounts added to this manager */
|
|
15
16
|
disableQueue;
|
|
16
17
|
constructor() {
|
|
17
|
-
this.signer = new
|
|
18
|
-
get: (_, p) => {
|
|
19
|
-
if (!this.active)
|
|
20
|
-
throw new Error("No active account");
|
|
21
|
-
return Reflect.get(this.active, p);
|
|
22
|
-
},
|
|
23
|
-
has: (_, p) => {
|
|
24
|
-
if (!this.active)
|
|
25
|
-
throw new Error("No active account");
|
|
26
|
-
return Reflect.has(this.active, p);
|
|
27
|
-
},
|
|
28
|
-
});
|
|
18
|
+
this.signer = new ProxySigner(this.active$, "No active account");
|
|
29
19
|
}
|
|
30
20
|
// Account type CRUD
|
|
31
21
|
/** Add account type class */
|
|
@@ -72,6 +62,9 @@ export class AccountManager {
|
|
|
72
62
|
removeAccount(account) {
|
|
73
63
|
const id = typeof account === "string" ? account : account.id;
|
|
74
64
|
this.accounts$.next(this.accounts$.value.filter((a) => a.id !== id));
|
|
65
|
+
// if the removed account was active, clear the active account
|
|
66
|
+
if (this.active$.value?.id === id)
|
|
67
|
+
this.active$.next(undefined);
|
|
75
68
|
}
|
|
76
69
|
/** Replaces an account with another */
|
|
77
70
|
replaceAccount(old, account) {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Nip07Interface } from "applesauce-signers";
|
|
2
|
+
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
3
|
+
import { Observable } from "rxjs";
|
|
4
|
+
export declare class ProxySigner<T extends Nip07Interface> implements Nip07Interface {
|
|
5
|
+
protected upstream: Observable<T | undefined>;
|
|
6
|
+
protected error?: string | undefined;
|
|
7
|
+
private _signer;
|
|
8
|
+
protected get signer(): T;
|
|
9
|
+
get nip04(): {
|
|
10
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
11
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
12
|
+
};
|
|
13
|
+
get nip44(): {
|
|
14
|
+
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
15
|
+
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
16
|
+
};
|
|
17
|
+
constructor(upstream: Observable<T | undefined>, error?: string | undefined);
|
|
18
|
+
signEvent(template: EventTemplate): Promise<NostrEvent> | NostrEvent;
|
|
19
|
+
getPublicKey(): Promise<string> | string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class ProxySigner {
|
|
2
|
+
upstream;
|
|
3
|
+
error;
|
|
4
|
+
_signer;
|
|
5
|
+
get signer() {
|
|
6
|
+
if (!this._signer)
|
|
7
|
+
throw new Error(this.error || "Missing signer");
|
|
8
|
+
return this._signer;
|
|
9
|
+
}
|
|
10
|
+
get nip04() {
|
|
11
|
+
if (!this.signer.nip04)
|
|
12
|
+
throw new Error("Signer does not support nip04");
|
|
13
|
+
return this.signer.nip04;
|
|
14
|
+
}
|
|
15
|
+
get nip44() {
|
|
16
|
+
if (!this.signer.nip44)
|
|
17
|
+
throw new Error("Signer does not support nip44");
|
|
18
|
+
return this.signer.nip44;
|
|
19
|
+
}
|
|
20
|
+
constructor(upstream, error) {
|
|
21
|
+
this.upstream = upstream;
|
|
22
|
+
this.error = error;
|
|
23
|
+
this.upstream.subscribe((signer) => (this._signer = signer));
|
|
24
|
+
}
|
|
25
|
+
signEvent(template) {
|
|
26
|
+
return this.signer.signEvent(template);
|
|
27
|
+
}
|
|
28
|
+
getPublicKey() {
|
|
29
|
+
return this.signer.getPublicKey();
|
|
30
|
+
}
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-accounts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A simple nostr account management system",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@noble/hashes": "^1.7.1",
|
|
36
|
-
"applesauce-signers": "^0.
|
|
37
|
-
"nanoid": "^5.
|
|
36
|
+
"applesauce-signers": "^1.0.0",
|
|
37
|
+
"nanoid": "^5.1.5",
|
|
38
38
|
"nostr-tools": "^2.10.4",
|
|
39
39
|
"rxjs": "^7.8.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"typescript": "^5.
|
|
43
|
-
"vitest": "^3.
|
|
42
|
+
"typescript": "^5.8.3",
|
|
43
|
+
"vitest": "^3.1.1"
|
|
44
44
|
},
|
|
45
45
|
"funding": {
|
|
46
46
|
"type": "lightning",
|
package/dist/manager.test.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
});
|
|
File without changes
|
|
File without changes
|